Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7188ac27 KM |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
b702c21d | 5 | * %sccs.include.redist.c% |
7188ac27 | 6 | * |
d8ba9f9d | 7 | * @(#)ufs_vnops.c 7.77 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
6459ebe0 | 9 | |
031fd966 KB |
10 | #include <sys/param.h> |
11 | #include <sys/systm.h> | |
12 | #include <sys/namei.h> | |
13 | #include <sys/resourcevar.h> | |
14 | #include <sys/kernel.h> | |
15 | #include <sys/file.h> | |
16 | #include <sys/stat.h> | |
17 | #include <sys/buf.h> | |
18 | #include <sys/proc.h> | |
19 | #include <sys/conf.h> | |
20 | #include <sys/mount.h> | |
21 | #include <sys/vnode.h> | |
22 | #include <sys/specdev.h> | |
23 | #include <sys/fifo.h> | |
24 | #include <sys/malloc.h> | |
25 | ||
26 | #include <ufs/ufs/lockf.h> | |
27 | #include <ufs/ufs/quota.h> | |
28 | #include <ufs/ufs/inode.h> | |
29 | #include <ufs/ufs/dir.h> | |
30 | #include <ufs/ufs/ufsmount.h> | |
31 | #include <ufs/ufs/ufs_extern.h> | |
32 | ||
1a3cb193 KM |
33 | int ufs_chmod __P((struct vnode *, int, struct proc *)); |
34 | int ufs_chown __P((struct vnode *, u_int, u_int, struct proc *)); | |
031fd966 | 35 | |
1a3cb193 KM |
36 | #ifdef _NOQUAD |
37 | #define SETHIGH(q, h) (q).val[_QUAD_HIGHWORD] = (h) | |
38 | #define SETLOW(q, l) (q).val[_QUAD_LOWWORD] = (l) | |
39 | #else /* QUAD */ | |
40 | union _qcvt { | |
41 | quad_t qcvt; | |
42 | long val[2]; | |
43 | }; | |
44 | #define SETHIGH(q, h) { \ | |
45 | union _qcvt tmp; \ | |
46 | tmp.qcvt = (q); \ | |
47 | tmp.val[_QUAD_HIGHWORD] = (h); \ | |
48 | (q) = tmp.qcvt; \ | |
49 | } | |
50 | #define SETLOW(q, l) { \ | |
51 | union _qcvt tmp; \ | |
52 | tmp.qcvt = (q); \ | |
53 | tmp.val[_QUAD_LOWWORD] = (l); \ | |
54 | (q) = tmp.qcvt; \ | |
55 | } | |
56 | #endif /* QUAD */ | |
57 | ||
4f083fd7 | 58 | /* |
7188ac27 | 59 | * Create a regular file |
4f083fd7 | 60 | */ |
031fd966 | 61 | int |
6e57e9ff | 62 | ufs_create(dvp, vpp, cnp, vap) |
cfef4373 JH |
63 | struct vnode *dvp; |
64 | struct vnode **vpp; | |
65 | struct componentname *cnp; | |
7188ac27 | 66 | struct vattr *vap; |
3e78e260 | 67 | { |
7188ac27 | 68 | int error; |
3e78e260 | 69 | |
031fd966 | 70 | if (error = |
cfef4373 | 71 | ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp)) |
7188ac27 | 72 | return (error); |
7188ac27 | 73 | return (0); |
3e78e260 BJ |
74 | } |
75 | ||
4f083fd7 | 76 | /* |
7188ac27 | 77 | * Mknod vnode call |
4f083fd7 | 78 | */ |
7188ac27 | 79 | /* ARGSUSED */ |
031fd966 | 80 | int |
6e57e9ff | 81 | ufs_mknod(dvp, vpp, cnp, vap) |
cfef4373 JH |
82 | struct vnode *dvp; |
83 | struct vnode **vpp; | |
84 | struct componentname *cnp; | |
7188ac27 | 85 | struct vattr *vap; |
3e78e260 | 86 | { |
c59af027 | 87 | register struct inode *ip; |
7188ac27 | 88 | int error; |
3e78e260 | 89 | |
031fd966 | 90 | if (error = |
cfef4373 | 91 | ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), dvp, vpp, cnp)) |
7188ac27 | 92 | return (error); |
cfef4373 | 93 | ip = VTOI(*vpp); |
d1c43d7f KM |
94 | ip->i_flag |= IACC|IUPD|ICHG; |
95 | if (vap->va_rdev != VNOVAL) { | |
7188ac27 KM |
96 | /* |
97 | * Want to be able to use this to make badblock | |
98 | * inodes, so don't truncate the dev number. | |
99 | */ | |
b373e060 | 100 | ip->i_rdev = vap->va_rdev; |
7188ac27 | 101 | } |
7188ac27 KM |
102 | /* |
103 | * Remove inode so that it will be reloaded by iget and | |
104 | * checked to see if it is an alias of an existing entry | |
105 | * in the inode cache. | |
106 | */ | |
cfef4373 JH |
107 | vput(*vpp); |
108 | (*vpp)->v_type = VNON; | |
109 | vgone(*vpp); | |
110 | *vpp = 0; | |
7188ac27 | 111 | return (0); |
3e78e260 BJ |
112 | } |
113 | ||
114 | /* | |
7188ac27 KM |
115 | * Open called. |
116 | * | |
117 | * Nothing to do. | |
3e78e260 | 118 | */ |
7188ac27 | 119 | /* ARGSUSED */ |
031fd966 | 120 | int |
5b169cb7 | 121 | ufs_open(vp, mode, cred, p) |
7188ac27 KM |
122 | struct vnode *vp; |
123 | int mode; | |
124 | struct ucred *cred; | |
5b169cb7 | 125 | struct proc *p; |
3e78e260 | 126 | { |
3e78e260 | 127 | |
7188ac27 | 128 | return (0); |
3e78e260 BJ |
129 | } |
130 | ||
131 | /* | |
7188ac27 KM |
132 | * Close called |
133 | * | |
134 | * Update the times on the inode. | |
3e78e260 | 135 | */ |
7188ac27 | 136 | /* ARGSUSED */ |
031fd966 | 137 | int |
5b169cb7 | 138 | ufs_close(vp, fflag, cred, p) |
7188ac27 KM |
139 | struct vnode *vp; |
140 | int fflag; | |
141 | struct ucred *cred; | |
5b169cb7 | 142 | struct proc *p; |
3e78e260 | 143 | { |
031fd966 | 144 | register struct inode *ip; |
3e78e260 | 145 | |
031fd966 | 146 | ip = VTOI(vp); |
de67eefc | 147 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) |
7188ac27 KM |
148 | ITIMES(ip, &time, &time); |
149 | return (0); | |
3e78e260 BJ |
150 | } |
151 | ||
4b61628b KM |
152 | /* |
153 | * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. | |
154 | * The mode is shifted to select the owner/group/other fields. The | |
155 | * super user is granted all permissions. | |
156 | */ | |
031fd966 | 157 | int |
5b169cb7 | 158 | ufs_access(vp, mode, cred, p) |
7188ac27 | 159 | struct vnode *vp; |
4b61628b | 160 | register int mode; |
7188ac27 | 161 | struct ucred *cred; |
5b169cb7 | 162 | struct proc *p; |
3e78e260 | 163 | { |
4b61628b KM |
164 | register struct inode *ip = VTOI(vp); |
165 | register gid_t *gp; | |
166 | int i, error; | |
3e78e260 | 167 | |
4b61628b KM |
168 | #ifdef DIAGNOSTIC |
169 | if (!VOP_ISLOCKED(vp)) { | |
170 | vprint("ufs_access: not locked", vp); | |
171 | panic("ufs_access: not locked"); | |
172 | } | |
173 | #endif | |
174 | #ifdef QUOTA | |
175 | if (mode & VWRITE) { | |
176 | switch (vp->v_type) { | |
177 | case VREG: case VDIR: case VLNK: | |
178 | if (error = getinoquota(ip)) | |
179 | return (error); | |
180 | } | |
181 | } | |
182 | #endif /* QUOTA */ | |
183 | /* | |
184 | * If you're the super-user, you always get access. | |
185 | */ | |
186 | if (cred->cr_uid == 0) | |
187 | return (0); | |
188 | /* | |
189 | * Access check is based on only one of owner, group, public. | |
190 | * If not owner, then check group. If not a member of the | |
191 | * group, then check public access. | |
192 | */ | |
193 | if (cred->cr_uid != ip->i_uid) { | |
194 | mode >>= 3; | |
195 | gp = cred->cr_groups; | |
196 | for (i = 0; i < cred->cr_ngroups; i++, gp++) | |
197 | if (ip->i_gid == *gp) | |
198 | goto found; | |
199 | mode >>= 3; | |
200 | found: | |
201 | ; | |
202 | } | |
203 | if ((ip->i_mode & mode) != 0) | |
204 | return (0); | |
205 | return (EACCES); | |
3e78e260 BJ |
206 | } |
207 | ||
7188ac27 | 208 | /* ARGSUSED */ |
031fd966 | 209 | int |
5b169cb7 | 210 | ufs_getattr(vp, vap, cred, p) |
7188ac27 KM |
211 | struct vnode *vp; |
212 | register struct vattr *vap; | |
213 | struct ucred *cred; | |
5b169cb7 | 214 | struct proc *p; |
3e78e260 | 215 | { |
031fd966 | 216 | register struct inode *ip; |
3e78e260 | 217 | |
031fd966 | 218 | ip = VTOI(vp); |
7188ac27 | 219 | ITIMES(ip, &time, &time); |
3e78e260 | 220 | /* |
7188ac27 | 221 | * Copy from inode table |
3e78e260 | 222 | */ |
7188ac27 KM |
223 | vap->va_fsid = ip->i_dev; |
224 | vap->va_fileid = ip->i_number; | |
225 | vap->va_mode = ip->i_mode & ~IFMT; | |
226 | vap->va_nlink = ip->i_nlink; | |
227 | vap->va_uid = ip->i_uid; | |
228 | vap->va_gid = ip->i_gid; | |
229 | vap->va_rdev = (dev_t)ip->i_rdev; | |
4b61628b KM |
230 | #ifdef tahoe |
231 | vap->va_size = ip->i_size; | |
232 | vap->va_size_rsv = 0; | |
233 | #else | |
8fae943a | 234 | vap->va_qsize = ip->i_din.di_qsize; |
4b61628b | 235 | #endif |
7188ac27 | 236 | vap->va_atime.tv_sec = ip->i_atime; |
6ef53d70 | 237 | vap->va_atime.tv_usec = 0; |
7188ac27 | 238 | vap->va_mtime.tv_sec = ip->i_mtime; |
6ef53d70 | 239 | vap->va_mtime.tv_usec = 0; |
7188ac27 | 240 | vap->va_ctime.tv_sec = ip->i_ctime; |
6ef53d70 | 241 | vap->va_ctime.tv_usec = 0; |
d07fc92a KM |
242 | vap->va_flags = ip->i_flags; |
243 | vap->va_gen = ip->i_gen; | |
7188ac27 KM |
244 | /* this doesn't belong here */ |
245 | if (vp->v_type == VBLK) | |
246 | vap->va_blocksize = BLKDEV_IOSIZE; | |
247 | else if (vp->v_type == VCHR) | |
248 | vap->va_blocksize = MAXBSIZE; | |
8eee8525 | 249 | else |
cc173077 | 250 | vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; |
a61a68d6 | 251 | vap->va_bytes = dbtob(ip->i_blocks); |
1a3cb193 | 252 | #ifdef _NOQUAD |
8fae943a | 253 | vap->va_bytes_rsv = 0; |
1a3cb193 | 254 | #endif |
7188ac27 | 255 | vap->va_type = vp->v_type; |
1a3cb193 | 256 | vap->va_filerev = ip->i_modrev; |
7188ac27 | 257 | return (0); |
3e78e260 BJ |
258 | } |
259 | ||
260 | /* | |
7188ac27 | 261 | * Set attribute vnode op. called from several syscalls |
3e78e260 | 262 | */ |
031fd966 | 263 | int |
5b169cb7 | 264 | ufs_setattr(vp, vap, cred, p) |
7188ac27 KM |
265 | register struct vnode *vp; |
266 | register struct vattr *vap; | |
267 | register struct ucred *cred; | |
5b169cb7 | 268 | struct proc *p; |
3e78e260 | 269 | { |
031fd966 | 270 | register struct inode *ip; |
031fd966 | 271 | int error; |
b4d1aee9 | 272 | |
7188ac27 | 273 | /* |
031fd966 | 274 | * Check for unsettable attributes. |
7188ac27 KM |
275 | */ |
276 | if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || | |
277 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || | |
278 | (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || | |
d07fc92a | 279 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
7188ac27 | 280 | return (EINVAL); |
b4d1aee9 | 281 | } |
7188ac27 KM |
282 | /* |
283 | * Go through the fields and update iff not VNOVAL. | |
284 | */ | |
285 | if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) | |
031fd966 | 286 | if (error = ufs_chown(vp, vap->va_uid, vap->va_gid, p)) |
7188ac27 KM |
287 | return (error); |
288 | if (vap->va_size != VNOVAL) { | |
289 | if (vp->v_type == VDIR) | |
290 | return (EISDIR); | |
c59af027 | 291 | if (error = VOP_TRUNCATE(vp, vap->va_size, 0)) /* IO_SYNC? */ |
7188ac27 | 292 | return (error); |
3e78e260 | 293 | } |
c59af027 | 294 | ip = VTOI(vp); |
7188ac27 | 295 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { |
de412887 | 296 | if (cred->cr_uid != ip->i_uid && |
c6f5111d | 297 | (error = suser(cred, &p->p_acflag))) |
de412887 | 298 | return (error); |
7188ac27 KM |
299 | if (vap->va_atime.tv_sec != VNOVAL) |
300 | ip->i_flag |= IACC; | |
301 | if (vap->va_mtime.tv_sec != VNOVAL) | |
302 | ip->i_flag |= IUPD; | |
303 | ip->i_flag |= ICHG; | |
c59af027 | 304 | if (error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1)) |
7188ac27 | 305 | return (error); |
5485e062 | 306 | } |
031fd966 | 307 | error = 0; |
7188ac27 | 308 | if (vap->va_mode != (u_short)VNOVAL) |
031fd966 | 309 | error = ufs_chmod(vp, (int)vap->va_mode, p); |
d07fc92a KM |
310 | if (vap->va_flags != VNOVAL) { |
311 | if (cred->cr_uid != ip->i_uid && | |
c6f5111d | 312 | (error = suser(cred, &p->p_acflag))) |
d07fc92a KM |
313 | return (error); |
314 | if (cred->cr_uid == 0) { | |
315 | ip->i_flags = vap->va_flags; | |
316 | } else { | |
317 | ip->i_flags &= 0xffff0000; | |
318 | ip->i_flags |= (vap->va_flags & 0xffff); | |
319 | } | |
320 | ip->i_flag |= ICHG; | |
321 | } | |
7188ac27 | 322 | return (error); |
528f664c SL |
323 | } |
324 | ||
4f083fd7 SL |
325 | /* |
326 | * Change the mode on a file. | |
327 | * Inode must be locked before calling. | |
328 | */ | |
ca3e8b68 | 329 | static int |
031fd966 | 330 | ufs_chmod(vp, mode, p) |
7188ac27 | 331 | register struct vnode *vp; |
528f664c | 332 | register int mode; |
c6f5111d | 333 | struct proc *p; |
528f664c | 334 | { |
c6f5111d | 335 | register struct ucred *cred = p->p_ucred; |
7188ac27 | 336 | register struct inode *ip = VTOI(vp); |
de412887 | 337 | int error; |
197da11b | 338 | |
de412887 | 339 | if (cred->cr_uid != ip->i_uid && |
c6f5111d | 340 | (error = suser(cred, &p->p_acflag))) |
de412887 | 341 | return (error); |
7188ac27 | 342 | if (cred->cr_uid) { |
e57b6138 | 343 | if (vp->v_type != VDIR && (mode & ISVTX)) |
39b5be4c | 344 | return (EFTYPE); |
e57b6138 | 345 | if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) |
39b5be4c | 346 | return (EPERM); |
f94ceb3b | 347 | } |
39b5be4c | 348 | ip->i_mode &= ~07777; |
7188ac27 | 349 | ip->i_mode |= mode & 07777; |
3e78e260 | 350 | ip->i_flag |= ICHG; |
7188ac27 | 351 | if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) |
8986c97c | 352 | (void) vnode_pager_uncache(vp); |
47af7174 | 353 | return (0); |
5485e062 BJ |
354 | } |
355 | ||
528f664c SL |
356 | /* |
357 | * Perform chown operation on inode ip; | |
358 | * inode must be locked prior to call. | |
359 | */ | |
ca3e8b68 | 360 | static int |
031fd966 | 361 | ufs_chown(vp, uid, gid, p) |
7188ac27 | 362 | register struct vnode *vp; |
031fd966 KB |
363 | u_int uid; |
364 | u_int gid; | |
c6f5111d | 365 | struct proc *p; |
528f664c | 366 | { |
7188ac27 | 367 | register struct inode *ip = VTOI(vp); |
c6f5111d | 368 | register struct ucred *cred = p->p_ucred; |
4b61628b KM |
369 | uid_t ouid; |
370 | gid_t ogid; | |
371 | int error = 0; | |
528f664c | 372 | #ifdef QUOTA |
4b61628b KM |
373 | register int i; |
374 | long change; | |
bb1b75f4 | 375 | #endif |
528f664c | 376 | |
7188ac27 | 377 | if (uid == (u_short)VNOVAL) |
bb1b75f4 | 378 | uid = ip->i_uid; |
7188ac27 | 379 | if (gid == (u_short)VNOVAL) |
bb1b75f4 | 380 | gid = ip->i_gid; |
18b0bce6 KB |
381 | /* |
382 | * If we don't own the file, are trying to change the owner | |
383 | * of the file, or are not a member of the target group, | |
384 | * the caller must be superuser or the call fails. | |
385 | */ | |
7188ac27 KM |
386 | if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || |
387 | !groupmember((gid_t)gid, cred)) && | |
c6f5111d | 388 | (error = suser(cred, &p->p_acflag))) |
7188ac27 | 389 | return (error); |
4b61628b KM |
390 | ouid = ip->i_uid; |
391 | ogid = ip->i_gid; | |
bb1b75f4 | 392 | #ifdef QUOTA |
4b61628b KM |
393 | if (error = getinoquota(ip)) |
394 | return (error); | |
395 | if (ouid == uid) { | |
396 | dqrele(vp, ip->i_dquot[USRQUOTA]); | |
397 | ip->i_dquot[USRQUOTA] = NODQUOT; | |
398 | } | |
399 | if (ogid == gid) { | |
400 | dqrele(vp, ip->i_dquot[GRPQUOTA]); | |
401 | ip->i_dquot[GRPQUOTA] = NODQUOT; | |
402 | } | |
403 | change = ip->i_blocks; | |
404 | (void) chkdq(ip, -change, cred, CHOWN); | |
405 | (void) chkiq(ip, -1, cred, CHOWN); | |
406 | for (i = 0; i < MAXQUOTAS; i++) { | |
407 | dqrele(vp, ip->i_dquot[i]); | |
408 | ip->i_dquot[i] = NODQUOT; | |
409 | } | |
f94ceb3b | 410 | #endif |
bb1b75f4 SL |
411 | ip->i_uid = uid; |
412 | ip->i_gid = gid; | |
528f664c | 413 | #ifdef QUOTA |
4b61628b KM |
414 | if ((error = getinoquota(ip)) == 0) { |
415 | if (ouid == uid) { | |
416 | dqrele(vp, ip->i_dquot[USRQUOTA]); | |
417 | ip->i_dquot[USRQUOTA] = NODQUOT; | |
418 | } | |
419 | if (ogid == gid) { | |
420 | dqrele(vp, ip->i_dquot[GRPQUOTA]); | |
421 | ip->i_dquot[GRPQUOTA] = NODQUOT; | |
422 | } | |
423 | if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { | |
424 | if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) | |
722a9b31 | 425 | goto good; |
4b61628b KM |
426 | else |
427 | (void) chkdq(ip, -change, cred, CHOWN|FORCE); | |
428 | } | |
429 | for (i = 0; i < MAXQUOTAS; i++) { | |
430 | dqrele(vp, ip->i_dquot[i]); | |
431 | ip->i_dquot[i] = NODQUOT; | |
432 | } | |
433 | } | |
434 | ip->i_uid = ouid; | |
435 | ip->i_gid = ogid; | |
436 | if (getinoquota(ip) == 0) { | |
437 | if (ouid == uid) { | |
438 | dqrele(vp, ip->i_dquot[USRQUOTA]); | |
439 | ip->i_dquot[USRQUOTA] = NODQUOT; | |
440 | } | |
441 | if (ogid == gid) { | |
442 | dqrele(vp, ip->i_dquot[GRPQUOTA]); | |
443 | ip->i_dquot[GRPQUOTA] = NODQUOT; | |
444 | } | |
722a9b31 KM |
445 | (void) chkdq(ip, change, cred, FORCE|CHOWN); |
446 | (void) chkiq(ip, 1, cred, FORCE|CHOWN); | |
6cb69bbe | 447 | (void) getinoquota(ip); |
4b61628b | 448 | } |
6cb69bbe | 449 | return (error); |
722a9b31 | 450 | good: |
6cb69bbe KM |
451 | if (getinoquota(ip)) |
452 | panic("chown: lost quota"); | |
453 | #endif /* QUOTA */ | |
4b61628b KM |
454 | if (ouid != uid || ogid != gid) |
455 | ip->i_flag |= ICHG; | |
456 | if (ouid != uid && cred->cr_uid != 0) | |
457 | ip->i_mode &= ~ISUID; | |
458 | if (ogid != gid && cred->cr_uid != 0) | |
459 | ip->i_mode &= ~ISGID; | |
460 | return (0); | |
d67a03eb BJ |
461 | } |
462 | ||
7188ac27 | 463 | /* ARGSUSED */ |
031fd966 | 464 | int |
5b169cb7 | 465 | ufs_ioctl(vp, com, data, fflag, cred, p) |
7188ac27 KM |
466 | struct vnode *vp; |
467 | int com; | |
468 | caddr_t data; | |
469 | int fflag; | |
470 | struct ucred *cred; | |
5b169cb7 | 471 | struct proc *p; |
bb1b75f4 | 472 | { |
bb1b75f4 | 473 | |
7188ac27 KM |
474 | return (ENOTTY); |
475 | } | |
476 | ||
477 | /* ARGSUSED */ | |
031fd966 | 478 | int |
5b169cb7 | 479 | ufs_select(vp, which, fflags, cred, p) |
7188ac27 | 480 | struct vnode *vp; |
d1c43d7f | 481 | int which, fflags; |
7188ac27 | 482 | struct ucred *cred; |
5b169cb7 | 483 | struct proc *p; |
7188ac27 KM |
484 | { |
485 | ||
5b169cb7 KM |
486 | /* |
487 | * We should really check to see if I/O is possible. | |
488 | */ | |
489 | return (1); | |
bb1b75f4 | 490 | } |
d67a03eb | 491 | |
4f083fd7 | 492 | /* |
7188ac27 KM |
493 | * Mmap a file |
494 | * | |
495 | * NB Currently unsupported. | |
4f083fd7 | 496 | */ |
7188ac27 | 497 | /* ARGSUSED */ |
031fd966 | 498 | int |
5b169cb7 | 499 | ufs_mmap(vp, fflags, cred, p) |
7188ac27 KM |
500 | struct vnode *vp; |
501 | int fflags; | |
502 | struct ucred *cred; | |
5b169cb7 | 503 | struct proc *p; |
d67a03eb | 504 | { |
d67a03eb | 505 | |
7188ac27 | 506 | return (EINVAL); |
d67a03eb | 507 | } |
64d3a787 | 508 | |
4f083fd7 | 509 | /* |
7188ac27 KM |
510 | * Seek on a file |
511 | * | |
512 | * Nothing to do, so just return. | |
4f083fd7 | 513 | */ |
7188ac27 | 514 | /* ARGSUSED */ |
031fd966 | 515 | int |
7188ac27 KM |
516 | ufs_seek(vp, oldoff, newoff, cred) |
517 | struct vnode *vp; | |
518 | off_t oldoff, newoff; | |
519 | struct ucred *cred; | |
528f664c | 520 | { |
7188ac27 KM |
521 | |
522 | return (0); | |
523 | } | |
524 | ||
525 | /* | |
526 | * ufs remove | |
527 | * Hard to avoid races here, especially | |
528 | * in unlinking directories. | |
529 | */ | |
031fd966 | 530 | int |
6e57e9ff | 531 | ufs_remove(dvp, vp, cnp) |
cfef4373 JH |
532 | struct vnode *dvp, *vp; |
533 | struct componentname *cnp; | |
7188ac27 KM |
534 | { |
535 | register struct inode *ip, *dp; | |
536 | int error; | |
537 | ||
cfef4373 JH |
538 | ip = VTOI(vp); |
539 | dp = VTOI(dvp); | |
540 | error = ufs_dirremove(dvp, cnp); | |
7188ac27 KM |
541 | if (!error) { |
542 | ip->i_nlink--; | |
543 | ip->i_flag |= ICHG; | |
528f664c | 544 | } |
7188ac27 KM |
545 | if (dp == ip) |
546 | vrele(ITOV(ip)); | |
547 | else | |
031fd966 KB |
548 | ufs_iput(ip); |
549 | ufs_iput(dp); | |
7188ac27 | 550 | return (error); |
4f083fd7 SL |
551 | } |
552 | ||
553 | /* | |
7188ac27 | 554 | * link vnode call |
4f083fd7 | 555 | */ |
031fd966 | 556 | int |
6e57e9ff | 557 | ufs_link(vp, tdvp, cnp) |
cfef4373 JH |
558 | register struct vnode *vp; /* source vnode */ |
559 | struct vnode *tdvp; | |
560 | struct componentname *cnp; | |
4f083fd7 | 561 | { |
031fd966 | 562 | register struct inode *ip; |
7188ac27 | 563 | int error; |
4f083fd7 | 564 | |
655da945 | 565 | #ifdef DIANOSTIC |
cfef4373 | 566 | if ((cnp->cn_flags & HASBUF) == 0) |
655da945 KM |
567 | panic("ufs_link: no name"); |
568 | #endif | |
031fd966 | 569 | ip = VTOI(vp); |
655da945 | 570 | if ((unsigned short)ip->i_nlink >= LINK_MAX) { |
cfef4373 | 571 | free(cnp->cn_pnbuf, M_NAMEI); |
986aedaa | 572 | return (EMLINK); |
655da945 | 573 | } |
cfef4373 | 574 | if (tdvp != vp) |
7188ac27 | 575 | ILOCK(ip); |
7188ac27 KM |
576 | ip->i_nlink++; |
577 | ip->i_flag |= ICHG; | |
c59af027 | 578 | error = VOP_UPDATE(vp, &time, &time, 1); |
7188ac27 | 579 | if (!error) |
cfef4373 JH |
580 | error = ufs_direnter(ip, tdvp, cnp); |
581 | if (tdvp != vp) | |
7188ac27 | 582 | IUNLOCK(ip); |
cfef4373 JH |
583 | FREE(cnp->cn_pnbuf, M_NAMEI); |
584 | vput(tdvp); | |
7188ac27 KM |
585 | if (error) { |
586 | ip->i_nlink--; | |
82252d2b | 587 | ip->i_flag |= ICHG; |
7188ac27 KM |
588 | } |
589 | return (error); | |
528f664c SL |
590 | } |
591 | ||
cfef4373 JH |
592 | |
593 | ||
594 | /* | |
595 | * relookup - lookup a path name component | |
596 | * Used by lookup to re-aquire things. | |
597 | */ | |
598 | int | |
6e57e9ff | 599 | relookup(dvp, vpp, cnp) |
cfef4373 JH |
600 | struct vnode *dvp, **vpp; |
601 | struct componentname *cnp; | |
602 | { | |
cfef4373 JH |
603 | register struct vnode *dp = 0; /* the directory we are searching */ |
604 | struct vnode *tdp; /* saved dp */ | |
605 | struct mount *mp; /* mount table entry */ | |
606 | int docache; /* == 0 do not cache last component */ | |
607 | int wantparent; /* 1 => wantparent or lockparent flag */ | |
608 | int rdonly; /* lookup read-only flag bit */ | |
d36023e3 KM |
609 | char *cp; /* DEBUG: check name ptr/len */ |
610 | int newhash; /* DEBUG: check name hash */ | |
cfef4373 | 611 | int error = 0; |
cfef4373 JH |
612 | |
613 | /* | |
614 | * Setup: break out flag bits into variables. | |
615 | */ | |
616 | wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); | |
617 | docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; | |
d7961b1d JH |
618 | if (cnp->cn_nameiop == DELETE || |
619 | (wantparent && cnp->cn_nameiop != CREATE)) | |
cfef4373 JH |
620 | docache = 0; |
621 | rdonly = cnp->cn_flags & RDONLY; | |
622 | cnp->cn_flags &= ~ISSYMLINK; | |
623 | dp = dvp; | |
624 | VOP_LOCK(dp); | |
625 | ||
626 | /* dirloop: */ | |
627 | /* | |
628 | * Search a new directory. | |
629 | * | |
630 | * The cn_hash value is for use by vfs_cache. | |
631 | * The last component of the filename is left accessible via | |
632 | * cnp->cn_nameptr for callers that need the name. Callers needing | |
633 | * the name set the SAVENAME flag. When done, they assume | |
634 | * responsibility for freeing the pathname buffer. | |
635 | */ | |
6e57e9ff | 636 | #ifdef NAMEI_DIAGNOSTIC |
d36023e3 | 637 | for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) |
cfef4373 JH |
638 | newhash += (unsigned char)*cp; |
639 | if (newhash != cnp->cn_hash) | |
640 | panic("relookup: bad hash"); | |
641 | if (cnp->cn_namelen != cp - cnp->cn_nameptr) | |
642 | panic ("relookup: bad len"); | |
d36023e3 KM |
643 | if (*cp != 0) |
644 | panic("relookup: not last component"); | |
cfef4373 | 645 | printf("{%s}: ", cnp->cn_nameptr); |
cfef4373 | 646 | #endif |
cfef4373 JH |
647 | |
648 | /* | |
649 | * Check for degenerate name (e.g. / or "") | |
650 | * which is a way of talking about a directory, | |
651 | * e.g. like "/." or ".". | |
652 | */ | |
653 | if (cnp->cn_nameptr[0] == '\0') { | |
654 | if (cnp->cn_nameiop != LOOKUP || wantparent) { | |
655 | error = EISDIR; | |
656 | goto bad; | |
657 | } | |
658 | if (dp->v_type != VDIR) { | |
659 | error = ENOTDIR; | |
660 | goto bad; | |
661 | } | |
662 | if (!(cnp->cn_flags & LOCKLEAF)) | |
663 | VOP_UNLOCK(dp); | |
664 | *vpp = dp; | |
665 | if (cnp->cn_flags & SAVESTART) | |
666 | panic("lookup: SAVESTART"); | |
667 | return (0); | |
668 | } | |
669 | ||
cfef4373 JH |
670 | if (cnp->cn_flags & ISDOTDOT) |
671 | panic ("relookup: lookup on dot-dot"); | |
672 | ||
673 | /* | |
674 | * We now have a segment name to search for, and a directory to search. | |
675 | */ | |
676 | if (error = VOP_LOOKUP(dp, vpp, cnp)) { | |
677 | #ifdef DIAGNOSTIC | |
678 | if (*vpp != NULL) | |
679 | panic("leaf should be empty"); | |
680 | #endif | |
681 | #ifdef NAMEI_DIAGNOSTIC | |
682 | printf("not found\n"); | |
683 | #endif | |
684 | if (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == DELETE || | |
d36023e3 | 685 | error != ENOENT) |
cfef4373 JH |
686 | goto bad; |
687 | /* | |
688 | * If creating and at end of pathname, then can consider | |
689 | * allowing file to be created. | |
690 | */ | |
691 | if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) { | |
692 | error = EROFS; | |
693 | goto bad; | |
694 | } | |
d36023e3 KM |
695 | /* ASSERT(dvp == ndp->ni_startdir) */ |
696 | if (cnp->cn_flags & SAVESTART) | |
697 | VREF(dvp); | |
cfef4373 JH |
698 | /* |
699 | * We return with ni_vp NULL to indicate that the entry | |
700 | * doesn't currently exist, leaving a pointer to the | |
701 | * (possibly locked) directory inode in ndp->ni_dvp. | |
702 | */ | |
cfef4373 JH |
703 | return (0); |
704 | } | |
705 | #ifdef NAMEI_DIAGNOSTIC | |
706 | printf("found\n"); | |
707 | #endif | |
708 | ||
709 | dp = *vpp; | |
6e57e9ff | 710 | #ifdef DIAGNOSTIC |
cfef4373 JH |
711 | /* |
712 | * Check for symbolic link | |
713 | */ | |
cfef4373 JH |
714 | if (dp->v_type == VLNK) { |
715 | panic ("relookup: symlink found.\n"); | |
716 | }; | |
717 | ||
718 | /* | |
719 | * Check to see if the vnode has been mounted on; | |
720 | * if so find the root of the mounted file system. | |
721 | */ | |
cfef4373 | 722 | #endif |
cfef4373 JH |
723 | |
724 | ||
725 | nextname: | |
cfef4373 JH |
726 | /* |
727 | * Check for read-only file systems. | |
728 | */ | |
729 | if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) { | |
730 | /* | |
731 | * Disallow directory write attempts on read-only | |
732 | * file systems. | |
733 | */ | |
734 | if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) || | |
735 | (wantparent && | |
736 | (dvp->v_mount->mnt_flag & MNT_RDONLY))) { | |
737 | error = EROFS; | |
738 | goto bad2; | |
739 | } | |
740 | } | |
d36023e3 KM |
741 | /* ASSERT(dvp == ndp->ni_startdir) */ |
742 | if (cnp->cn_flags & SAVESTART) | |
cfef4373 | 743 | VREF(dvp); |
cfef4373 JH |
744 | |
745 | if (!wantparent) | |
746 | vrele(dvp); | |
747 | if ((cnp->cn_flags & LOCKLEAF) == 0) | |
748 | VOP_UNLOCK(dp); | |
749 | return (0); | |
750 | ||
751 | bad2: | |
752 | if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) | |
753 | VOP_UNLOCK(dvp); | |
754 | vrele(dvp); | |
755 | bad: | |
756 | vput(dp); | |
757 | *vpp = NULL; | |
758 | return (error); | |
759 | } | |
760 | ||
761 | ||
4f083fd7 SL |
762 | /* |
763 | * Rename system call. | |
764 | * rename("foo", "bar"); | |
765 | * is essentially | |
766 | * unlink("bar"); | |
767 | * link("foo", "bar"); | |
768 | * unlink("foo"); | |
769 | * but ``atomically''. Can't do full commit without saving state in the | |
770 | * inode on disk which isn't feasible at this time. Best we can do is | |
771 | * always guarantee the target exists. | |
772 | * | |
773 | * Basic algorithm is: | |
774 | * | |
775 | * 1) Bump link count on source while we're linking it to the | |
7188ac27 | 776 | * target. This also ensure the inode won't be deleted out |
68f21562 KM |
777 | * from underneath us while we work (it may be truncated by |
778 | * a concurrent `trunc' or `open' for creation). | |
4f083fd7 SL |
779 | * 2) Link source to destination. If destination already exists, |
780 | * delete it first. | |
68f21562 KM |
781 | * 3) Unlink source reference to inode if still around. If a |
782 | * directory was moved and the parent of the destination | |
4f083fd7 SL |
783 | * is different from the source, patch the ".." entry in the |
784 | * directory. | |
4f083fd7 | 785 | */ |
031fd966 | 786 | int |
cfef4373 | 787 | ufs_rename(fdvp, fvp, fcnp, |
6e57e9ff | 788 | tdvp, tvp, tcnp) |
cfef4373 JH |
789 | struct vnode *fdvp, *fvp; |
790 | struct componentname *fcnp; | |
791 | struct vnode *tdvp, *tvp; | |
792 | struct componentname *tcnp; | |
528f664c | 793 | { |
4f083fd7 | 794 | register struct inode *ip, *xp, *dp; |
68f21562 KM |
795 | struct dirtemplate dirbuf; |
796 | int doingdirectory = 0, oldparent = 0, newparent = 0; | |
a5390dce | 797 | int error = 0; |
cfef4373 | 798 | int fdvpneedsrele = 1, tdvpneedsrele = 1; |
4f083fd7 | 799 | |
655da945 | 800 | #ifdef DIANOSTIC |
cfef4373 JH |
801 | if ((tcnp->cn_flags & HASBUF) == 0 || |
802 | (fcnp->cn_flags & HASBUF) == 0) | |
655da945 KM |
803 | panic("ufs_rename: no name"); |
804 | #endif | |
cfef4373 JH |
805 | dp = VTOI(fdvp); |
806 | ip = VTOI(fvp); | |
655da945 KM |
807 | /* |
808 | * Check if just deleting a link name. | |
809 | */ | |
cfef4373 JH |
810 | if (fvp == tvp) { |
811 | VOP_ABORTOP(tdvp, tcnp); | |
812 | vput(tdvp); | |
813 | vput(tvp); | |
814 | vrele(fdvp); | |
655da945 | 815 | if ((ip->i_mode&IFMT) == IFDIR) { |
cfef4373 JH |
816 | VOP_ABORTOP(fdvp, fcnp); |
817 | vrele(fvp); | |
655da945 KM |
818 | return (EINVAL); |
819 | } | |
820 | doingdirectory = 0; | |
821 | goto unlinkit; | |
822 | } | |
7188ac27 | 823 | ILOCK(ip); |
4f083fd7 | 824 | if ((ip->i_mode&IFMT) == IFDIR) { |
4f083fd7 | 825 | /* |
046f18d1 | 826 | * Avoid ".", "..", and aliases of "." for obvious reasons. |
4f083fd7 | 827 | */ |
cfef4373 JH |
828 | if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || |
829 | dp == ip || (fcnp->cn_flags&ISDOTDOT) || (ip->i_flag & IRENAME)) { | |
830 | VOP_ABORTOP(tdvp, tcnp); | |
831 | vput(tdvp); | |
832 | if (tvp) | |
833 | vput(tvp); | |
834 | VOP_ABORTOP(fdvp, fcnp); | |
835 | vrele(fdvp); | |
836 | vput(fvp); | |
7188ac27 | 837 | return (EINVAL); |
4f083fd7 | 838 | } |
68f21562 | 839 | ip->i_flag |= IRENAME; |
4f083fd7 SL |
840 | oldparent = dp->i_number; |
841 | doingdirectory++; | |
842 | } | |
cfef4373 | 843 | vrele(fdvp); |
4f083fd7 SL |
844 | |
845 | /* | |
846 | * 1) Bump link count while we're moving stuff | |
847 | * around. If we crash somewhere before | |
848 | * completing our work, the link count | |
849 | * may be wrong, but correctable. | |
850 | */ | |
851 | ip->i_nlink++; | |
852 | ip->i_flag |= ICHG; | |
cfef4373 | 853 | error = VOP_UPDATE(fvp, &time, &time, 1); |
a388503d | 854 | IUNLOCK(ip); |
4f083fd7 SL |
855 | |
856 | /* | |
857 | * When the target exists, both the directory | |
7188ac27 | 858 | * and target vnodes are returned locked. |
4f083fd7 | 859 | */ |
cfef4373 | 860 | dp = VTOI(tdvp); |
7188ac27 | 861 | xp = NULL; |
cfef4373 JH |
862 | if (tvp) |
863 | xp = VTOI(tvp); | |
046f18d1 SL |
864 | /* |
865 | * If ".." must be changed (ie the directory gets a new | |
81552f0f KM |
866 | * parent) then the source directory must not be in the |
867 | * directory heirarchy above the target, as this would | |
868 | * orphan everything below the source directory. Also | |
869 | * the user must have write permission in the source so | |
870 | * as to be able to change "..". We must repeat the call | |
871 | * to namei, as the parent directory is unlocked by the | |
872 | * call to checkpath(). | |
046f18d1 | 873 | */ |
68f21562 KM |
874 | if (oldparent != dp->i_number) |
875 | newparent = dp->i_number; | |
876 | if (doingdirectory && newparent) { | |
cfef4373 JH |
877 | VOP_LOCK(fvp); |
878 | error = ufs_access(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); | |
879 | VOP_UNLOCK(fvp); | |
48c3b6e8 | 880 | if (error) |
81552f0f | 881 | goto bad; |
655da945 | 882 | if (xp != NULL) |
031fd966 | 883 | ufs_iput(xp); |
cfef4373 | 884 | if (error = ufs_checkpath(ip, dp, tcnp->cn_cred)) |
655da945 | 885 | goto out; |
cfef4373 | 886 | if ((tcnp->cn_flags & SAVESTART) == 0) |
655da945 | 887 | panic("ufs_rename: lost to startdir"); |
5e03b55d | 888 | p->p_spare[1]--; |
cfef4373 | 889 | if (error = relookup(tdvp, &tvp, tcnp)) |
655da945 | 890 | goto out; |
cfef4373 | 891 | dp = VTOI(tdvp); |
655da945 | 892 | xp = NULL; |
cfef4373 JH |
893 | if (tvp) |
894 | xp = VTOI(tvp); | |
81552f0f | 895 | } |
4f083fd7 SL |
896 | /* |
897 | * 2) If target doesn't exist, link the target | |
898 | * to the source and unlink the source. | |
899 | * Otherwise, rewrite the target directory | |
900 | * entry to reference the source inode and | |
901 | * expunge the original entry's existence. | |
902 | */ | |
4f083fd7 | 903 | if (xp == NULL) { |
7188ac27 KM |
904 | if (dp->i_dev != ip->i_dev) |
905 | panic("rename: EXDEV"); | |
4f083fd7 | 906 | /* |
68f21562 KM |
907 | * Account for ".." in new directory. |
908 | * When source and destination have the same | |
909 | * parent we don't fool with the link count. | |
4f083fd7 | 910 | */ |
68f21562 | 911 | if (doingdirectory && newparent) { |
986aedaa KM |
912 | if ((unsigned short)dp->i_nlink >= LINK_MAX) { |
913 | error = EMLINK; | |
914 | goto bad; | |
915 | } | |
4f083fd7 SL |
916 | dp->i_nlink++; |
917 | dp->i_flag |= ICHG; | |
c59af027 | 918 | if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1)) |
986aedaa | 919 | goto bad; |
4f083fd7 | 920 | } |
cfef4373 | 921 | if (error = ufs_direnter(ip, tdvp, tcnp)) { |
394d67a8 KM |
922 | if (doingdirectory && newparent) { |
923 | dp->i_nlink--; | |
924 | dp->i_flag |= ICHG; | |
c59af027 | 925 | (void)VOP_UPDATE(ITOV(dp), &time, &time, 1); |
394d67a8 KM |
926 | } |
927 | goto bad; | |
928 | } | |
031fd966 | 929 | ufs_iput(dp); |
4f083fd7 | 930 | } else { |
7188ac27 KM |
931 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) |
932 | panic("rename: EXDEV"); | |
e69c3c9c SL |
933 | /* |
934 | * Short circuit rename(foo, foo). | |
935 | */ | |
936 | if (xp->i_number == ip->i_number) | |
7188ac27 | 937 | panic("rename: same file"); |
80cee150 JB |
938 | /* |
939 | * If the parent directory is "sticky", then the user must | |
940 | * own the parent directory, or the destination of the rename, | |
941 | * otherwise the destination may not be changed (except by | |
942 | * root). This implements append-only directories. | |
943 | */ | |
cfef4373 JH |
944 | if ((dp->i_mode & ISVTX) && tcnp->cn_cred->cr_uid != 0 && |
945 | tcnp->cn_cred->cr_uid != dp->i_uid && | |
946 | xp->i_uid != tcnp->cn_cred->cr_uid) { | |
80cee150 JB |
947 | error = EPERM; |
948 | goto bad; | |
949 | } | |
4f083fd7 | 950 | /* |
655da945 KM |
951 | * Target must be empty if a directory and have no links |
952 | * to it. Also, ensure source and target are compatible | |
953 | * (both directories, or both not directories). | |
4f083fd7 SL |
954 | */ |
955 | if ((xp->i_mode&IFMT) == IFDIR) { | |
cfef4373 | 956 | if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) || |
7188ac27 | 957 | xp->i_nlink > 2) { |
a5390dce | 958 | error = ENOTEMPTY; |
4f083fd7 SL |
959 | goto bad; |
960 | } | |
961 | if (!doingdirectory) { | |
a5390dce | 962 | error = ENOTDIR; |
4f083fd7 SL |
963 | goto bad; |
964 | } | |
7188ac27 | 965 | cache_purge(ITOV(dp)); |
4f083fd7 | 966 | } else if (doingdirectory) { |
a5390dce | 967 | error = EISDIR; |
4f083fd7 SL |
968 | goto bad; |
969 | } | |
cfef4373 | 970 | if (error = ufs_dirrewrite(dp, ip, tcnp)) |
7188ac27 | 971 | goto bad; |
a62786b9 KM |
972 | /* |
973 | * If the target directory is in the same | |
974 | * directory as the source directory, | |
975 | * decrement the link count on the parent | |
976 | * of the target directory. | |
977 | */ | |
978 | if (doingdirectory && !newparent) { | |
979 | dp->i_nlink--; | |
980 | dp->i_flag |= ICHG; | |
981 | } | |
c59af027 | 982 | ufs_iput(dp); |
4f083fd7 | 983 | /* |
a5390dce SL |
984 | * Adjust the link count of the target to |
985 | * reflect the dirrewrite above. If this is | |
986 | * a directory it is empty and there are | |
987 | * no links to it, so we can squash the inode and | |
988 | * any space associated with it. We disallowed | |
989 | * renaming over top of a directory with links to | |
68f21562 KM |
990 | * it above, as the remaining link would point to |
991 | * a directory without "." or ".." entries. | |
4f083fd7 | 992 | */ |
a5390dce | 993 | xp->i_nlink--; |
4f083fd7 | 994 | if (doingdirectory) { |
a5390dce SL |
995 | if (--xp->i_nlink != 0) |
996 | panic("rename: linked directory"); | |
c59af027 | 997 | error = VOP_TRUNCATE(ITOV(xp), (u_long)0, IO_SYNC); |
a5390dce | 998 | } |
4f083fd7 | 999 | xp->i_flag |= ICHG; |
031fd966 | 1000 | ufs_iput(xp); |
31db12cb | 1001 | xp = NULL; |
4f083fd7 SL |
1002 | } |
1003 | ||
1004 | /* | |
1005 | * 3) Unlink the source. | |
1006 | */ | |
655da945 | 1007 | unlinkit: |
cfef4373 JH |
1008 | fcnp->cn_flags &= ~MODMASK; |
1009 | fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; | |
1010 | if ((fcnp->cn_flags & SAVESTART) == 0) | |
655da945 | 1011 | panic("ufs_rename: lost from startdir"); |
5e03b55d | 1012 | p->p_spare[1]--; |
d36023e3 | 1013 | (void) relookup(fdvp, &fvp, fcnp); |
cfef4373 JH |
1014 | if (fvp != NULL) { |
1015 | xp = VTOI(fvp); | |
1016 | dp = VTOI(fdvp); | |
7188ac27 | 1017 | } else { |
d9d75b8f KM |
1018 | /* |
1019 | * From name has disappeared. | |
1020 | */ | |
1021 | if (doingdirectory) | |
1022 | panic("rename: lost dir entry"); | |
1023 | vrele(ITOV(ip)); | |
1024 | return (0); | |
7188ac27 | 1025 | } |
4f083fd7 | 1026 | /* |
7188ac27 | 1027 | * Ensure that the directory entry still exists and has not |
68f21562 KM |
1028 | * changed while the new name has been entered. If the source is |
1029 | * a file then the entry may have been unlinked or renamed. In | |
1030 | * either case there is no further work to be done. If the source | |
1031 | * is a directory then it cannot have been rmdir'ed; its link | |
1032 | * count of three would cause a rmdir to fail with ENOTEMPTY. | |
7188ac27 | 1033 | * The IRENAME flag ensures that it cannot be moved by another |
68f21562 | 1034 | * rename. |
4f083fd7 | 1035 | */ |
4f1a9037 | 1036 | if (xp != ip) { |
68f21562 | 1037 | if (doingdirectory) |
4f1a9037 | 1038 | panic("rename: lost dir entry"); |
68f21562 | 1039 | } else { |
4f083fd7 | 1040 | /* |
68f21562 KM |
1041 | * If the source is a directory with a |
1042 | * new parent, the link count of the old | |
1043 | * parent directory must be decremented | |
1044 | * and ".." set to point to the new parent. | |
4f083fd7 | 1045 | */ |
68f21562 | 1046 | if (doingdirectory && newparent) { |
4f083fd7 SL |
1047 | dp->i_nlink--; |
1048 | dp->i_flag |= ICHG; | |
86cdabf6 | 1049 | error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, |
7188ac27 | 1050 | sizeof (struct dirtemplate), (off_t)0, |
86cdabf6 | 1051 | UIO_SYSSPACE, IO_NODELOCKED, |
cfef4373 | 1052 | tcnp->cn_cred, (int *)0, (struct proc *)0); |
68f21562 KM |
1053 | if (error == 0) { |
1054 | if (dirbuf.dotdot_namlen != 2 || | |
1055 | dirbuf.dotdot_name[0] != '.' || | |
1056 | dirbuf.dotdot_name[1] != '.') { | |
031fd966 KB |
1057 | ufs_dirbad(xp, 12, |
1058 | "rename: mangled dir"); | |
68f21562 KM |
1059 | } else { |
1060 | dirbuf.dotdot_ino = newparent; | |
86cdabf6 | 1061 | (void) vn_rdwr(UIO_WRITE, ITOV(xp), |
68f21562 KM |
1062 | (caddr_t)&dirbuf, |
1063 | sizeof (struct dirtemplate), | |
93e273b9 | 1064 | (off_t)0, UIO_SYSSPACE, |
86cdabf6 | 1065 | IO_NODELOCKED|IO_SYNC, |
cfef4373 | 1066 | tcnp->cn_cred, (int *)0, |
5b169cb7 | 1067 | (struct proc *)0); |
7188ac27 | 1068 | cache_purge(ITOV(dp)); |
68f21562 KM |
1069 | } |
1070 | } | |
4f083fd7 | 1071 | } |
cfef4373 | 1072 | error = ufs_dirremove(fdvp, fcnp); |
7188ac27 | 1073 | if (!error) { |
68f21562 KM |
1074 | xp->i_nlink--; |
1075 | xp->i_flag |= ICHG; | |
4f083fd7 | 1076 | } |
68f21562 | 1077 | xp->i_flag &= ~IRENAME; |
4f083fd7 | 1078 | } |
4f083fd7 | 1079 | if (dp) |
7188ac27 | 1080 | vput(ITOV(dp)); |
68f21562 | 1081 | if (xp) |
7188ac27 KM |
1082 | vput(ITOV(xp)); |
1083 | vrele(ITOV(ip)); | |
1084 | return (error); | |
a5390dce | 1085 | |
4f083fd7 | 1086 | bad: |
4f083fd7 | 1087 | if (xp) |
7188ac27 KM |
1088 | vput(ITOV(xp)); |
1089 | vput(ITOV(dp)); | |
4f083fd7 SL |
1090 | out: |
1091 | ip->i_nlink--; | |
1092 | ip->i_flag |= ICHG; | |
7188ac27 KM |
1093 | vrele(ITOV(ip)); |
1094 | return (error); | |
64d3a787 | 1095 | } |
88a7a62a SL |
1096 | |
1097 | /* | |
1098 | * A virgin directory (no blushing please). | |
1099 | */ | |
031fd966 | 1100 | static struct dirtemplate mastertemplate = { |
88a7a62a SL |
1101 | 0, 12, 1, ".", |
1102 | 0, DIRBLKSIZ - 12, 2, ".." | |
1103 | }; | |
1104 | ||
1105 | /* | |
1106 | * Mkdir system call | |
1107 | */ | |
031fd966 | 1108 | int |
6e57e9ff | 1109 | ufs_mkdir(dvp, vpp, cnp, vap) |
cfef4373 JH |
1110 | struct vnode *dvp; |
1111 | struct vnode **vpp; | |
1112 | struct componentname *cnp; | |
7188ac27 | 1113 | struct vattr *vap; |
88a7a62a | 1114 | { |
88a7a62a | 1115 | register struct inode *ip, *dp; |
c59af027 | 1116 | struct vnode *tvp; |
88a7a62a | 1117 | struct dirtemplate dirtemplate; |
7188ac27 KM |
1118 | int error; |
1119 | int dmode; | |
1120 | ||
655da945 | 1121 | #ifdef DIANOSTIC |
cfef4373 | 1122 | if ((cnp->cn_flags & HASBUF) == 0) |
655da945 KM |
1123 | panic("ufs_mkdir: no name"); |
1124 | #endif | |
7188ac27 | 1125 | dp = VTOI(dvp); |
986aedaa | 1126 | if ((unsigned short)dp->i_nlink >= LINK_MAX) { |
cfef4373 | 1127 | free(cnp->cn_pnbuf, M_NAMEI); |
031fd966 | 1128 | ufs_iput(dp); |
986aedaa KM |
1129 | return (EMLINK); |
1130 | } | |
7188ac27 KM |
1131 | dmode = vap->va_mode&0777; |
1132 | dmode |= IFDIR; | |
88a7a62a | 1133 | /* |
655da945 KM |
1134 | * Must simulate part of maknode here to acquire the inode, but |
1135 | * not have it entered in the parent directory. The entry is made | |
1136 | * later after writing "." and ".." entries. | |
88a7a62a | 1137 | */ |
cfef4373 JH |
1138 | if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) { |
1139 | free(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1140 | ufs_iput(dp); |
7188ac27 | 1141 | return (error); |
88a7a62a | 1142 | } |
c59af027 | 1143 | ip = VTOI(tvp); |
cfef4373 | 1144 | ip->i_uid = cnp->cn_cred->cr_uid; |
4b61628b | 1145 | ip->i_gid = dp->i_gid; |
88a7a62a | 1146 | #ifdef QUOTA |
4b61628b | 1147 | if ((error = getinoquota(ip)) || |
cfef4373 JH |
1148 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
1149 | free(cnp->cn_pnbuf, M_NAMEI); | |
c59af027 | 1150 | VOP_VFREE(tvp, ip->i_number, dmode); |
031fd966 KB |
1151 | ufs_iput(ip); |
1152 | ufs_iput(dp); | |
4b61628b KM |
1153 | return (error); |
1154 | } | |
88a7a62a SL |
1155 | #endif |
1156 | ip->i_flag |= IACC|IUPD|ICHG; | |
7188ac27 KM |
1157 | ip->i_mode = dmode; |
1158 | ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ | |
88a7a62a | 1159 | ip->i_nlink = 2; |
c59af027 | 1160 | error = VOP_UPDATE(ITOV(ip), &time, &time, 1); |
88a7a62a SL |
1161 | |
1162 | /* | |
1163 | * Bump link count in parent directory | |
1164 | * to reflect work done below. Should | |
1165 | * be done before reference is created | |
1166 | * so reparation is possible if we crash. | |
1167 | */ | |
1168 | dp->i_nlink++; | |
1169 | dp->i_flag |= ICHG; | |
c59af027 | 1170 | if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1)) |
394d67a8 | 1171 | goto bad; |
88a7a62a | 1172 | |
031fd966 | 1173 | /* Initialize directory with "." and ".." from static template. */ |
88a7a62a SL |
1174 | dirtemplate = mastertemplate; |
1175 | dirtemplate.dot_ino = ip->i_number; | |
1176 | dirtemplate.dotdot_ino = dp->i_number; | |
86cdabf6 | 1177 | error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, |
5b169cb7 | 1178 | sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, |
cfef4373 | 1179 | IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0); |
7188ac27 | 1180 | if (error) { |
88a7a62a SL |
1181 | dp->i_nlink--; |
1182 | dp->i_flag |= ICHG; | |
1183 | goto bad; | |
1184 | } | |
cc173077 | 1185 | if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) |
031fd966 KB |
1186 | panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */ |
1187 | else { | |
23de9f20 | 1188 | ip->i_size = DIRBLKSIZ; |
15365e07 KM |
1189 | ip->i_flag |= ICHG; |
1190 | } | |
031fd966 KB |
1191 | |
1192 | /* Directory set up, now install it's entry in the parent directory. */ | |
cfef4373 | 1193 | if (error = ufs_direnter(ip, dvp, cnp)) { |
2a5d2f56 KM |
1194 | dp->i_nlink--; |
1195 | dp->i_flag |= ICHG; | |
88a7a62a SL |
1196 | } |
1197 | bad: | |
1198 | /* | |
c59af027 KM |
1199 | * No need to do an explicit VOP_TRUNCATE here, vrele will do this |
1200 | * for us because we set the link count to 0. | |
88a7a62a | 1201 | */ |
7188ac27 | 1202 | if (error) { |
88a7a62a SL |
1203 | ip->i_nlink = 0; |
1204 | ip->i_flag |= ICHG; | |
031fd966 | 1205 | ufs_iput(ip); |
cbcdacd6 | 1206 | } else |
cfef4373 JH |
1207 | *vpp = ITOV(ip); |
1208 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1209 | ufs_iput(dp); |
7188ac27 | 1210 | return (error); |
88a7a62a SL |
1211 | } |
1212 | ||
1213 | /* | |
1214 | * Rmdir system call. | |
1215 | */ | |
031fd966 | 1216 | int |
6e57e9ff JH |
1217 | ufs_rmdir(dvp, vp, cnp) |
1218 | struct vnode *dvp; | |
1219 | struct vnode *vp; | |
cfef4373 | 1220 | struct componentname *cnp; |
88a7a62a | 1221 | { |
88a7a62a | 1222 | register struct inode *ip, *dp; |
031fd966 | 1223 | int error; |
7188ac27 | 1224 | |
cfef4373 JH |
1225 | ip = VTOI(vp); |
1226 | dp = VTOI(dvp); | |
88a7a62a SL |
1227 | /* |
1228 | * No rmdir "." please. | |
1229 | */ | |
1230 | if (dp == ip) { | |
cfef4373 | 1231 | vrele(dvp); |
031fd966 | 1232 | ufs_iput(ip); |
7188ac27 | 1233 | return (EINVAL); |
88a7a62a SL |
1234 | } |
1235 | /* | |
1236 | * Verify the directory is empty (and valid). | |
1237 | * (Rmdir ".." won't be valid since | |
1238 | * ".." will contain a reference to | |
1239 | * the current directory and thus be | |
1240 | * non-empty.) | |
1241 | */ | |
031fd966 KB |
1242 | error = 0; |
1243 | if (ip->i_nlink != 2 || | |
cfef4373 | 1244 | !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { |
7188ac27 | 1245 | error = ENOTEMPTY; |
88a7a62a SL |
1246 | goto out; |
1247 | } | |
1248 | /* | |
1249 | * Delete reference to directory before purging | |
1250 | * inode. If we crash in between, the directory | |
1251 | * will be reattached to lost+found, | |
1252 | */ | |
cfef4373 | 1253 | if (error = ufs_dirremove(dvp, cnp)) |
88a7a62a SL |
1254 | goto out; |
1255 | dp->i_nlink--; | |
1256 | dp->i_flag |= ICHG; | |
cfef4373 | 1257 | cache_purge(dvp); |
031fd966 | 1258 | ufs_iput(dp); |
cfef4373 | 1259 | dvp = NULL; |
88a7a62a SL |
1260 | /* |
1261 | * Truncate inode. The only stuff left | |
1262 | * in the directory is "." and "..". The | |
1263 | * "." reference is inconsequential since | |
1264 | * we're quashing it. The ".." reference | |
1265 | * has already been adjusted above. We've | |
1266 | * removed the "." reference and the reference | |
1267 | * in the parent directory, but there may be | |
1268 | * other hard links so decrement by 2 and | |
1269 | * worry about them later. | |
1270 | */ | |
1271 | ip->i_nlink -= 2; | |
cfef4373 | 1272 | error = VOP_TRUNCATE(vp, (u_long)0, IO_SYNC); |
7188ac27 | 1273 | cache_purge(ITOV(ip)); |
88a7a62a | 1274 | out: |
cfef4373 | 1275 | if (dvp) |
031fd966 KB |
1276 | ufs_iput(dp); |
1277 | ufs_iput(ip); | |
7188ac27 | 1278 | return (error); |
88a7a62a SL |
1279 | } |
1280 | ||
7188ac27 KM |
1281 | /* |
1282 | * symlink -- make a symbolic link | |
1283 | */ | |
031fd966 | 1284 | int |
6e57e9ff | 1285 | ufs_symlink(dvp, vpp, cnp, vap, target) |
cfef4373 JH |
1286 | struct vnode *dvp; |
1287 | struct vnode **vpp; | |
1288 | struct componentname *cnp; | |
7188ac27 KM |
1289 | struct vattr *vap; |
1290 | char *target; | |
1291 | { | |
7188ac27 KM |
1292 | int error; |
1293 | ||
cfef4373 | 1294 | if (error = ufs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp)) |
7188ac27 | 1295 | return (error); |
cfef4373 JH |
1296 | error = vn_rdwr(UIO_WRITE, *vpp, target, strlen(target), (off_t)0, |
1297 | UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred, (int *)0, | |
5b169cb7 | 1298 | (struct proc *)0); |
cfef4373 | 1299 | vput(*vpp); |
7188ac27 KM |
1300 | return (error); |
1301 | } | |
1302 | ||
1303 | /* | |
d8ba9f9d KM |
1304 | * Vnode op for reading directories. |
1305 | * | |
1306 | * The routine below assumes that the on-disk format of a directory | |
1307 | * is the same as that defined by <sys/dirent.h>. If the on-disk | |
1308 | * format changes, then it will be necessary to do a conversion | |
1309 | * from the on-disk format that read returns to the format defined | |
1310 | * by <sys/dirent.h>. | |
7188ac27 | 1311 | */ |
031fd966 | 1312 | int |
930730fd | 1313 | ufs_readdir(vp, uio, cred, eofflagp) |
7188ac27 KM |
1314 | struct vnode *vp; |
1315 | register struct uio *uio; | |
7188ac27 | 1316 | struct ucred *cred; |
930730fd | 1317 | int *eofflagp; |
88a7a62a | 1318 | { |
86cdabf6 | 1319 | int count, lost, error; |
88a7a62a | 1320 | |
7188ac27 KM |
1321 | count = uio->uio_resid; |
1322 | count &= ~(DIRBLKSIZ - 1); | |
86cdabf6 KM |
1323 | lost = uio->uio_resid - count; |
1324 | if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) | |
7188ac27 | 1325 | return (EINVAL); |
7188ac27 KM |
1326 | uio->uio_resid = count; |
1327 | uio->uio_iov->iov_len = count; | |
031fd966 | 1328 | error = VOP_READ(vp, uio, 0, cred); |
86cdabf6 | 1329 | uio->uio_resid += lost; |
930730fd KM |
1330 | if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) |
1331 | *eofflagp = 1; | |
1332 | else | |
1333 | *eofflagp = 0; | |
7188ac27 KM |
1334 | return (error); |
1335 | } | |
1336 | ||
1337 | /* | |
1338 | * Return target name of a symbolic link | |
1339 | */ | |
031fd966 | 1340 | int |
7188ac27 KM |
1341 | ufs_readlink(vp, uiop, cred) |
1342 | struct vnode *vp; | |
1343 | struct uio *uiop; | |
1344 | struct ucred *cred; | |
1345 | { | |
1346 | ||
031fd966 | 1347 | return (VOP_READ(vp, uiop, 0, cred)); |
7188ac27 KM |
1348 | } |
1349 | ||
1350 | /* | |
1351 | * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually | |
655da945 | 1352 | * done. If a buffer has been saved in anticipation of a CREATE, delete it. |
7188ac27 | 1353 | */ |
66955caf | 1354 | /* ARGSUSED */ |
031fd966 | 1355 | int |
6e57e9ff | 1356 | ufs_abortop(dvp, cnp) |
cfef4373 JH |
1357 | struct vnode *dvp; |
1358 | struct componentname *cnp; | |
7188ac27 | 1359 | { |
cfef4373 JH |
1360 | if ((cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) |
1361 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
66955caf | 1362 | return (0); |
7188ac27 KM |
1363 | } |
1364 | ||
1c3ebc10 KM |
1365 | /* |
1366 | * Lock an inode. | |
1367 | */ | |
031fd966 | 1368 | int |
7188ac27 KM |
1369 | ufs_lock(vp) |
1370 | struct vnode *vp; | |
1371 | { | |
1372 | register struct inode *ip = VTOI(vp); | |
1373 | ||
1374 | ILOCK(ip); | |
1375 | return (0); | |
1376 | } | |
1377 | ||
1c3ebc10 KM |
1378 | /* |
1379 | * Unlock an inode. | |
1380 | */ | |
031fd966 | 1381 | int |
7188ac27 KM |
1382 | ufs_unlock(vp) |
1383 | struct vnode *vp; | |
1384 | { | |
1385 | register struct inode *ip = VTOI(vp); | |
1386 | ||
1387 | if (!(ip->i_flag & ILOCKED)) | |
1388 | panic("ufs_unlock NOT LOCKED"); | |
1389 | IUNLOCK(ip); | |
1390 | return (0); | |
1391 | } | |
1392 | ||
1c3ebc10 KM |
1393 | /* |
1394 | * Check for a locked inode. | |
1395 | */ | |
031fd966 | 1396 | int |
1c3ebc10 KM |
1397 | ufs_islocked(vp) |
1398 | struct vnode *vp; | |
1399 | { | |
1400 | ||
1401 | if (VTOI(vp)->i_flag & ILOCKED) | |
1402 | return (1); | |
1403 | return (0); | |
1404 | } | |
1405 | ||
88a7a62a | 1406 | /* |
4f1ff475 KM |
1407 | * Calculate the logical to physical mapping if not done already, |
1408 | * then call the device strategy routine. | |
88a7a62a | 1409 | */ |
4f1ff475 | 1410 | int checkoverlap = 0; |
e16fa59e | 1411 | |
031fd966 | 1412 | int |
7188ac27 KM |
1413 | ufs_strategy(bp) |
1414 | register struct buf *bp; | |
88a7a62a | 1415 | { |
031fd966 | 1416 | register struct inode *ip; |
e16fa59e | 1417 | struct vnode *vp; |
e16fa59e KM |
1418 | int error; |
1419 | ||
031fd966 | 1420 | ip = VTOI(bp->b_vp); |
e16fa59e KM |
1421 | if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) |
1422 | panic("ufs_strategy: spec"); | |
1423 | if (bp->b_blkno == bp->b_lblkno) { | |
c59af027 KM |
1424 | if (error = |
1425 | VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno)) | |
e16fa59e | 1426 | return (error); |
20aa076b | 1427 | if ((long)bp->b_blkno == -1) |
e16fa59e | 1428 | clrbuf(bp); |
e16fa59e | 1429 | } |
20aa076b KM |
1430 | if ((long)bp->b_blkno == -1) { |
1431 | biodone(bp); | |
e16fa59e | 1432 | return (0); |
20aa076b | 1433 | } |
4f1ff475 | 1434 | #ifdef DIAGNOSTIC |
031fd966 KB |
1435 | if (checkoverlap && bp->b_vp->v_mount->mnt_stat.f_type == MOUNT_UFS) |
1436 | ffs_checkoverlap(bp, ip); | |
1437 | #endif | |
1438 | ||
e16fa59e KM |
1439 | vp = ip->i_devvp; |
1440 | bp->b_dev = vp->v_rdev; | |
031fd966 | 1441 | (vp->v_op->vop_strategy)(bp); |
7188ac27 KM |
1442 | return (0); |
1443 | } | |
88a7a62a | 1444 | |
e16fa59e KM |
1445 | /* |
1446 | * Print out the contents of an inode. | |
1447 | */ | |
031fd966 | 1448 | int |
e16fa59e KM |
1449 | ufs_print(vp) |
1450 | struct vnode *vp; | |
1451 | { | |
1452 | register struct inode *ip = VTOI(vp); | |
1453 | ||
a8f829ed KM |
1454 | printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, |
1455 | major(ip->i_dev), minor(ip->i_dev)); | |
1456 | #ifdef FIFO | |
1457 | if (vp->v_type == VFIFO) | |
1458 | fifo_printinfo(vp); | |
1459 | #endif /* FIFO */ | |
1460 | printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); | |
4a8fb1bb | 1461 | if (ip->i_lockholder == 0) |
031fd966 | 1462 | return (0); |
4a8fb1bb KM |
1463 | printf("\towner pid %d", ip->i_lockholder); |
1464 | if (ip->i_lockwaiter) | |
1465 | printf(" waiting pid %d", ip->i_lockwaiter); | |
96bae3e0 | 1466 | printf("\n"); |
031fd966 | 1467 | return (0); |
e16fa59e KM |
1468 | } |
1469 | ||
24a31b70 KM |
1470 | /* |
1471 | * Read wrapper for special devices. | |
1472 | */ | |
031fd966 | 1473 | int |
24a31b70 KM |
1474 | ufsspec_read(vp, uio, ioflag, cred) |
1475 | struct vnode *vp; | |
1476 | struct uio *uio; | |
1477 | int ioflag; | |
1478 | struct ucred *cred; | |
1479 | { | |
1480 | ||
1481 | /* | |
1482 | * Set access flag. | |
1483 | */ | |
1484 | VTOI(vp)->i_flag |= IACC; | |
1485 | return (spec_read(vp, uio, ioflag, cred)); | |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Write wrapper for special devices. | |
1490 | */ | |
031fd966 | 1491 | int |
24a31b70 KM |
1492 | ufsspec_write(vp, uio, ioflag, cred) |
1493 | struct vnode *vp; | |
1494 | struct uio *uio; | |
1495 | int ioflag; | |
1496 | struct ucred *cred; | |
1497 | { | |
1498 | ||
1499 | /* | |
1500 | * Set update and change flags. | |
1501 | */ | |
1502 | VTOI(vp)->i_flag |= IUPD|ICHG; | |
1503 | return (spec_write(vp, uio, ioflag, cred)); | |
1504 | } | |
1505 | ||
1506 | /* | |
1507 | * Close wrapper for special devices. | |
1508 | * | |
1509 | * Update the times on the inode then do device close. | |
1510 | */ | |
031fd966 | 1511 | int |
5b169cb7 | 1512 | ufsspec_close(vp, fflag, cred, p) |
24a31b70 KM |
1513 | struct vnode *vp; |
1514 | int fflag; | |
1515 | struct ucred *cred; | |
5b169cb7 | 1516 | struct proc *p; |
24a31b70 KM |
1517 | { |
1518 | register struct inode *ip = VTOI(vp); | |
1519 | ||
de67eefc | 1520 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) |
24a31b70 | 1521 | ITIMES(ip, &time, &time); |
5b169cb7 | 1522 | return (spec_close(vp, fflag, cred, p)); |
24a31b70 KM |
1523 | } |
1524 | ||
d1c43d7f KM |
1525 | #ifdef FIFO |
1526 | /* | |
1527 | * Read wrapper for fifo's | |
1528 | */ | |
031fd966 | 1529 | int |
d1c43d7f KM |
1530 | ufsfifo_read(vp, uio, ioflag, cred) |
1531 | struct vnode *vp; | |
1532 | struct uio *uio; | |
1533 | int ioflag; | |
1534 | struct ucred *cred; | |
1535 | { | |
1536 | ||
1537 | /* | |
1538 | * Set access flag. | |
1539 | */ | |
1540 | VTOI(vp)->i_flag |= IACC; | |
1541 | return (fifo_read(vp, uio, ioflag, cred)); | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * Write wrapper for fifo's. | |
1546 | */ | |
031fd966 | 1547 | int |
d1c43d7f KM |
1548 | ufsfifo_write(vp, uio, ioflag, cred) |
1549 | struct vnode *vp; | |
1550 | struct uio *uio; | |
1551 | int ioflag; | |
1552 | struct ucred *cred; | |
1553 | { | |
1554 | ||
1555 | /* | |
1556 | * Set update and change flags. | |
1557 | */ | |
1558 | VTOI(vp)->i_flag |= IUPD|ICHG; | |
1559 | return (fifo_write(vp, uio, ioflag, cred)); | |
1560 | } | |
1561 | ||
1562 | /* | |
1563 | * Close wrapper for fifo's. | |
1564 | * | |
1565 | * Update the times on the inode then do device close. | |
1566 | */ | |
5b169cb7 | 1567 | ufsfifo_close(vp, fflag, cred, p) |
d1c43d7f KM |
1568 | struct vnode *vp; |
1569 | int fflag; | |
1570 | struct ucred *cred; | |
5b169cb7 | 1571 | struct proc *p; |
d1c43d7f KM |
1572 | { |
1573 | register struct inode *ip = VTOI(vp); | |
1574 | ||
1575 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) | |
1576 | ITIMES(ip, &time, &time); | |
5b169cb7 | 1577 | return (fifo_close(vp, fflag, cred, p)); |
d1c43d7f KM |
1578 | } |
1579 | #endif /* FIFO */ | |
1580 | ||
0b0bf936 KM |
1581 | /* |
1582 | * Advisory record locking support | |
1583 | */ | |
031fd966 | 1584 | int |
0b0bf936 KM |
1585 | ufs_advlock(vp, id, op, fl, flags) |
1586 | struct vnode *vp; | |
1587 | caddr_t id; | |
1588 | int op; | |
1589 | register struct flock *fl; | |
1590 | int flags; | |
1591 | { | |
1592 | register struct inode *ip = VTOI(vp); | |
1593 | register struct lockf *lock; | |
1594 | off_t start, end; | |
1595 | int error; | |
1596 | ||
1597 | /* | |
1598 | * Avoid the common case of unlocking when inode has no locks. | |
1599 | */ | |
1600 | if (ip->i_lockf == (struct lockf *)0) { | |
1601 | if (op != F_SETLK) { | |
1602 | fl->l_type = F_UNLCK; | |
1603 | return (0); | |
1604 | } | |
1605 | } | |
1606 | /* | |
1607 | * Convert the flock structure into a start and end. | |
1608 | */ | |
1609 | switch (fl->l_whence) { | |
1610 | ||
1611 | case SEEK_SET: | |
1612 | case SEEK_CUR: | |
1613 | /* | |
1614 | * Caller is responsible for adding any necessary offset | |
1615 | * when SEEK_CUR is used. | |
1616 | */ | |
1617 | start = fl->l_start; | |
1618 | break; | |
1619 | ||
1620 | case SEEK_END: | |
1621 | start = ip->i_size + fl->l_start; | |
1622 | break; | |
1623 | ||
1624 | default: | |
1625 | return (EINVAL); | |
1626 | } | |
1627 | if (start < 0) | |
1628 | return (EINVAL); | |
1629 | if (fl->l_len == 0) | |
1630 | end = -1; | |
1631 | else | |
c412cb68 | 1632 | end = start + fl->l_len - 1; |
0b0bf936 KM |
1633 | /* |
1634 | * Create the lockf structure | |
1635 | */ | |
1636 | MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); | |
1637 | lock->lf_start = start; | |
1638 | lock->lf_end = end; | |
1639 | lock->lf_id = id; | |
1640 | lock->lf_inode = ip; | |
1641 | lock->lf_type = fl->l_type; | |
1642 | lock->lf_next = (struct lockf *)0; | |
1643 | lock->lf_block = (struct lockf *)0; | |
1644 | lock->lf_flags = flags; | |
1645 | /* | |
1646 | * Do the requested operation. | |
1647 | */ | |
1648 | switch(op) { | |
1649 | case F_SETLK: | |
b9fc1077 | 1650 | return (lf_setlock(lock)); |
0b0bf936 KM |
1651 | |
1652 | case F_UNLCK: | |
b9fc1077 KM |
1653 | error = lf_clearlock(lock); |
1654 | FREE(lock, M_LOCKF); | |
1655 | return (error); | |
0b0bf936 KM |
1656 | |
1657 | case F_GETLK: | |
b9fc1077 KM |
1658 | error = lf_getlock(lock, fl); |
1659 | FREE(lock, M_LOCKF); | |
1660 | return (error); | |
0b0bf936 KM |
1661 | |
1662 | default: | |
1663 | free(lock, M_LOCKF); | |
1664 | return (EINVAL); | |
1665 | } | |
1666 | /* NOTREACHED */ | |
1667 | } | |
5b169cb7 KM |
1668 | |
1669 | /* | |
031fd966 KB |
1670 | * Initialize the vnode associated with a new inode, handle aliased |
1671 | * vnodes. | |
5b169cb7 | 1672 | */ |
031fd966 | 1673 | int |
c59af027 | 1674 | ufs_vinit(mntp, specops, fifoops, vpp) |
031fd966 | 1675 | struct mount *mntp; |
c59af027 | 1676 | struct vnodeops *specops, *fifoops; |
031fd966 KB |
1677 | struct vnode **vpp; |
1678 | { | |
1679 | struct inode *ip, *nip; | |
1680 | struct vnode *vp, *nvp; | |
4a8fb1bb | 1681 | extern struct vnodeops spec_vnodeops; |
031fd966 KB |
1682 | |
1683 | vp = *vpp; | |
1684 | ip = VTOI(vp); | |
1685 | switch(vp->v_type = IFTOVT(ip->i_mode)) { | |
1686 | case VCHR: | |
1687 | case VBLK: | |
c59af027 | 1688 | vp->v_op = specops; |
031fd966 | 1689 | if (nvp = checkalias(vp, ip->i_rdev, mntp)) { |
c59af027 | 1690 | /* |
4a8fb1bb | 1691 | * Discard unneeded vnode, but save its inode. |
c59af027 | 1692 | */ |
4a8fb1bb KM |
1693 | remque(ip); |
1694 | IUNLOCK(ip); | |
1695 | nvp->v_data = vp->v_data; | |
1696 | vp->v_data = NULL; | |
1697 | vp->v_op = &spec_vnodeops; | |
1698 | vrele(vp); | |
1699 | vgone(vp); | |
c59af027 | 1700 | /* |
4a8fb1bb | 1701 | * Reinitialize aliased inode. |
c59af027 | 1702 | */ |
4a8fb1bb KM |
1703 | vp = nvp; |
1704 | ip->i_vnode = vp; | |
1705 | ufs_ihashins(ip); | |
031fd966 KB |
1706 | } |
1707 | break; | |
1708 | case VFIFO: | |
1709 | #ifdef FIFO | |
c59af027 | 1710 | vp->v_op = fifoops; |
031fd966 KB |
1711 | break; |
1712 | #else | |
1713 | return (EOPNOTSUPP); | |
1714 | #endif | |
1715 | } | |
031fd966 KB |
1716 | if (ip->i_number == ROOTINO) |
1717 | vp->v_flag |= VROOT; | |
1a3cb193 KM |
1718 | /* |
1719 | * Initialize modrev times | |
1720 | */ | |
1721 | SETHIGH(ip->i_modrev, mono_time.tv_sec); | |
1722 | SETLOW(ip->i_modrev, mono_time.tv_usec * 4294); | |
031fd966 KB |
1723 | *vpp = vp; |
1724 | return (0); | |
1725 | } | |
5b169cb7 | 1726 | |
031fd966 KB |
1727 | /* |
1728 | * Allocate a new inode. | |
1729 | */ | |
1730 | int | |
6e57e9ff | 1731 | ufs_makeinode(mode, dvp, vpp, cnp) |
031fd966 | 1732 | int mode; |
cfef4373 | 1733 | struct vnode *dvp; |
c59af027 | 1734 | struct vnode **vpp; |
cfef4373 | 1735 | struct componentname *cnp; |
031fd966 KB |
1736 | { |
1737 | register struct inode *ip, *pdir; | |
c59af027 | 1738 | struct vnode *tvp; |
031fd966 KB |
1739 | int error; |
1740 | ||
cfef4373 | 1741 | pdir = VTOI(dvp); |
031fd966 | 1742 | #ifdef DIANOSTIC |
cfef4373 | 1743 | if ((cnp->cn_flags & HASBUF) == 0) |
031fd966 KB |
1744 | panic("ufs_makeinode: no name"); |
1745 | #endif | |
c59af027 | 1746 | *vpp = NULL; |
031fd966 KB |
1747 | if ((mode & IFMT) == 0) |
1748 | mode |= IFREG; | |
1749 | ||
cfef4373 JH |
1750 | if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) { |
1751 | free(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 KB |
1752 | ufs_iput(pdir); |
1753 | return (error); | |
1754 | } | |
c59af027 | 1755 | ip = VTOI(tvp); |
cfef4373 | 1756 | ip->i_uid = cnp->cn_cred->cr_uid; |
031fd966 KB |
1757 | ip->i_gid = pdir->i_gid; |
1758 | #ifdef QUOTA | |
1759 | if ((error = getinoquota(ip)) || | |
cfef4373 JH |
1760 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
1761 | free(cnp->cn_pnbuf, M_NAMEI); | |
c59af027 | 1762 | VOP_VFREE(tvp, ip->i_number, mode); |
031fd966 KB |
1763 | ufs_iput(ip); |
1764 | ufs_iput(pdir); | |
1765 | return (error); | |
1766 | } | |
1767 | #endif | |
1768 | ip->i_flag |= IACC|IUPD|ICHG; | |
1769 | ip->i_mode = mode; | |
c59af027 | 1770 | tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */ |
031fd966 | 1771 | ip->i_nlink = 1; |
cfef4373 JH |
1772 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && |
1773 | suser(cnp->cn_cred, NULL)) | |
031fd966 KB |
1774 | ip->i_mode &= ~ISGID; |
1775 | ||
1776 | /* | |
1777 | * Make sure inode goes to disk before directory entry. | |
1778 | */ | |
c59af027 | 1779 | if (error = VOP_UPDATE(tvp, &time, &time, 1)) |
031fd966 | 1780 | goto bad; |
cfef4373 | 1781 | if (error = ufs_direnter(ip, dvp, cnp)) |
031fd966 | 1782 | goto bad; |
cfef4373 JH |
1783 | if ((cnp->cn_flags & SAVESTART) == 0) |
1784 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1785 | ufs_iput(pdir); |
c59af027 | 1786 | *vpp = tvp; |
031fd966 KB |
1787 | return (0); |
1788 | ||
1789 | bad: | |
1790 | /* | |
1791 | * Write error occurred trying to update the inode | |
1792 | * or the directory so must deallocate the inode. | |
1793 | */ | |
cfef4373 | 1794 | free(cnp->cn_pnbuf, M_NAMEI); |
031fd966 KB |
1795 | ufs_iput(pdir); |
1796 | ip->i_nlink = 0; | |
1797 | ip->i_flag |= ICHG; | |
1798 | ufs_iput(ip); | |
1799 | return (error); | |
1800 | } | |
cfef4373 JH |
1801 | |
1802 | ||
1803 | #if defined(JOHNH) && 0 | |
1804 | /* | |
1805 | * A hack to get the kernel to compile. | |
1806 | */ | |
1807 | int | |
1808 | hang_addrlist() | |
1809 | { | |
1810 | return 0; | |
1811 | } | |
1812 | int | |
1813 | free_addrlist() | |
1814 | { | |
1815 | return 0; | |
1816 | } | |
1817 | #endif | |
1818 |