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 | * |
39a892ba | 7 | * @(#)ufs_vnops.c 7.81 (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"); | |
cfef4373 | 680 | #endif |
39a892ba | 681 | if (error != EJUSTRETURN) |
cfef4373 JH |
682 | goto bad; |
683 | /* | |
684 | * If creating and at end of pathname, then can consider | |
685 | * allowing file to be created. | |
686 | */ | |
687 | if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) { | |
688 | error = EROFS; | |
689 | goto bad; | |
690 | } | |
d36023e3 KM |
691 | /* ASSERT(dvp == ndp->ni_startdir) */ |
692 | if (cnp->cn_flags & SAVESTART) | |
693 | VREF(dvp); | |
cfef4373 JH |
694 | /* |
695 | * We return with ni_vp NULL to indicate that the entry | |
696 | * doesn't currently exist, leaving a pointer to the | |
697 | * (possibly locked) directory inode in ndp->ni_dvp. | |
698 | */ | |
cfef4373 JH |
699 | return (0); |
700 | } | |
cfef4373 | 701 | dp = *vpp; |
2021967e | 702 | |
6e57e9ff | 703 | #ifdef DIAGNOSTIC |
cfef4373 JH |
704 | /* |
705 | * Check for symbolic link | |
706 | */ | |
2021967e | 707 | if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) |
cfef4373 | 708 | panic ("relookup: symlink found.\n"); |
cfef4373 | 709 | #endif |
cfef4373 | 710 | |
cfef4373 | 711 | nextname: |
cfef4373 JH |
712 | /* |
713 | * Check for read-only file systems. | |
714 | */ | |
715 | if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) { | |
716 | /* | |
717 | * Disallow directory write attempts on read-only | |
718 | * file systems. | |
719 | */ | |
720 | if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) || | |
721 | (wantparent && | |
722 | (dvp->v_mount->mnt_flag & MNT_RDONLY))) { | |
723 | error = EROFS; | |
724 | goto bad2; | |
725 | } | |
726 | } | |
d36023e3 KM |
727 | /* ASSERT(dvp == ndp->ni_startdir) */ |
728 | if (cnp->cn_flags & SAVESTART) | |
cfef4373 | 729 | VREF(dvp); |
cfef4373 JH |
730 | |
731 | if (!wantparent) | |
732 | vrele(dvp); | |
733 | if ((cnp->cn_flags & LOCKLEAF) == 0) | |
734 | VOP_UNLOCK(dp); | |
735 | return (0); | |
736 | ||
737 | bad2: | |
738 | if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN)) | |
739 | VOP_UNLOCK(dvp); | |
740 | vrele(dvp); | |
741 | bad: | |
742 | vput(dp); | |
743 | *vpp = NULL; | |
744 | return (error); | |
745 | } | |
746 | ||
747 | ||
4f083fd7 SL |
748 | /* |
749 | * Rename system call. | |
750 | * rename("foo", "bar"); | |
751 | * is essentially | |
752 | * unlink("bar"); | |
753 | * link("foo", "bar"); | |
754 | * unlink("foo"); | |
755 | * but ``atomically''. Can't do full commit without saving state in the | |
756 | * inode on disk which isn't feasible at this time. Best we can do is | |
757 | * always guarantee the target exists. | |
758 | * | |
759 | * Basic algorithm is: | |
760 | * | |
761 | * 1) Bump link count on source while we're linking it to the | |
7188ac27 | 762 | * target. This also ensure the inode won't be deleted out |
68f21562 KM |
763 | * from underneath us while we work (it may be truncated by |
764 | * a concurrent `trunc' or `open' for creation). | |
4f083fd7 SL |
765 | * 2) Link source to destination. If destination already exists, |
766 | * delete it first. | |
68f21562 KM |
767 | * 3) Unlink source reference to inode if still around. If a |
768 | * directory was moved and the parent of the destination | |
4f083fd7 SL |
769 | * is different from the source, patch the ".." entry in the |
770 | * directory. | |
4f083fd7 | 771 | */ |
031fd966 | 772 | int |
cfef4373 | 773 | ufs_rename(fdvp, fvp, fcnp, |
6e57e9ff | 774 | tdvp, tvp, tcnp) |
cfef4373 JH |
775 | struct vnode *fdvp, *fvp; |
776 | struct componentname *fcnp; | |
777 | struct vnode *tdvp, *tvp; | |
778 | struct componentname *tcnp; | |
528f664c | 779 | { |
4f083fd7 | 780 | register struct inode *ip, *xp, *dp; |
68f21562 KM |
781 | struct dirtemplate dirbuf; |
782 | int doingdirectory = 0, oldparent = 0, newparent = 0; | |
a5390dce | 783 | int error = 0; |
cfef4373 | 784 | int fdvpneedsrele = 1, tdvpneedsrele = 1; |
4f083fd7 | 785 | |
655da945 | 786 | #ifdef DIANOSTIC |
cfef4373 JH |
787 | if ((tcnp->cn_flags & HASBUF) == 0 || |
788 | (fcnp->cn_flags & HASBUF) == 0) | |
655da945 KM |
789 | panic("ufs_rename: no name"); |
790 | #endif | |
cfef4373 JH |
791 | dp = VTOI(fdvp); |
792 | ip = VTOI(fvp); | |
655da945 KM |
793 | /* |
794 | * Check if just deleting a link name. | |
795 | */ | |
cfef4373 JH |
796 | if (fvp == tvp) { |
797 | VOP_ABORTOP(tdvp, tcnp); | |
798 | vput(tdvp); | |
799 | vput(tvp); | |
800 | vrele(fdvp); | |
655da945 | 801 | if ((ip->i_mode&IFMT) == IFDIR) { |
cfef4373 JH |
802 | VOP_ABORTOP(fdvp, fcnp); |
803 | vrele(fvp); | |
655da945 KM |
804 | return (EINVAL); |
805 | } | |
806 | doingdirectory = 0; | |
807 | goto unlinkit; | |
808 | } | |
7188ac27 | 809 | ILOCK(ip); |
4f083fd7 | 810 | if ((ip->i_mode&IFMT) == IFDIR) { |
4f083fd7 | 811 | /* |
046f18d1 | 812 | * Avoid ".", "..", and aliases of "." for obvious reasons. |
4f083fd7 | 813 | */ |
cfef4373 JH |
814 | if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || |
815 | dp == ip || (fcnp->cn_flags&ISDOTDOT) || (ip->i_flag & IRENAME)) { | |
816 | VOP_ABORTOP(tdvp, tcnp); | |
817 | vput(tdvp); | |
818 | if (tvp) | |
819 | vput(tvp); | |
820 | VOP_ABORTOP(fdvp, fcnp); | |
821 | vrele(fdvp); | |
822 | vput(fvp); | |
7188ac27 | 823 | return (EINVAL); |
4f083fd7 | 824 | } |
68f21562 | 825 | ip->i_flag |= IRENAME; |
4f083fd7 SL |
826 | oldparent = dp->i_number; |
827 | doingdirectory++; | |
828 | } | |
cfef4373 | 829 | vrele(fdvp); |
4f083fd7 SL |
830 | |
831 | /* | |
832 | * 1) Bump link count while we're moving stuff | |
833 | * around. If we crash somewhere before | |
834 | * completing our work, the link count | |
835 | * may be wrong, but correctable. | |
836 | */ | |
837 | ip->i_nlink++; | |
838 | ip->i_flag |= ICHG; | |
cfef4373 | 839 | error = VOP_UPDATE(fvp, &time, &time, 1); |
a388503d | 840 | IUNLOCK(ip); |
4f083fd7 SL |
841 | |
842 | /* | |
843 | * When the target exists, both the directory | |
7188ac27 | 844 | * and target vnodes are returned locked. |
4f083fd7 | 845 | */ |
cfef4373 | 846 | dp = VTOI(tdvp); |
7188ac27 | 847 | xp = NULL; |
cfef4373 JH |
848 | if (tvp) |
849 | xp = VTOI(tvp); | |
046f18d1 SL |
850 | /* |
851 | * If ".." must be changed (ie the directory gets a new | |
81552f0f KM |
852 | * parent) then the source directory must not be in the |
853 | * directory heirarchy above the target, as this would | |
854 | * orphan everything below the source directory. Also | |
855 | * the user must have write permission in the source so | |
856 | * as to be able to change "..". We must repeat the call | |
857 | * to namei, as the parent directory is unlocked by the | |
858 | * call to checkpath(). | |
046f18d1 | 859 | */ |
68f21562 KM |
860 | if (oldparent != dp->i_number) |
861 | newparent = dp->i_number; | |
862 | if (doingdirectory && newparent) { | |
cfef4373 JH |
863 | VOP_LOCK(fvp); |
864 | error = ufs_access(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); | |
865 | VOP_UNLOCK(fvp); | |
48c3b6e8 | 866 | if (error) |
81552f0f | 867 | goto bad; |
655da945 | 868 | if (xp != NULL) |
031fd966 | 869 | ufs_iput(xp); |
cfef4373 | 870 | if (error = ufs_checkpath(ip, dp, tcnp->cn_cred)) |
655da945 | 871 | goto out; |
cfef4373 | 872 | if ((tcnp->cn_flags & SAVESTART) == 0) |
655da945 | 873 | panic("ufs_rename: lost to startdir"); |
5e03b55d | 874 | p->p_spare[1]--; |
cfef4373 | 875 | if (error = relookup(tdvp, &tvp, tcnp)) |
655da945 | 876 | goto out; |
cfef4373 | 877 | dp = VTOI(tdvp); |
655da945 | 878 | xp = NULL; |
cfef4373 JH |
879 | if (tvp) |
880 | xp = VTOI(tvp); | |
81552f0f | 881 | } |
4f083fd7 SL |
882 | /* |
883 | * 2) If target doesn't exist, link the target | |
884 | * to the source and unlink the source. | |
885 | * Otherwise, rewrite the target directory | |
886 | * entry to reference the source inode and | |
887 | * expunge the original entry's existence. | |
888 | */ | |
4f083fd7 | 889 | if (xp == NULL) { |
7188ac27 KM |
890 | if (dp->i_dev != ip->i_dev) |
891 | panic("rename: EXDEV"); | |
4f083fd7 | 892 | /* |
68f21562 KM |
893 | * Account for ".." in new directory. |
894 | * When source and destination have the same | |
895 | * parent we don't fool with the link count. | |
4f083fd7 | 896 | */ |
68f21562 | 897 | if (doingdirectory && newparent) { |
986aedaa KM |
898 | if ((unsigned short)dp->i_nlink >= LINK_MAX) { |
899 | error = EMLINK; | |
900 | goto bad; | |
901 | } | |
4f083fd7 SL |
902 | dp->i_nlink++; |
903 | dp->i_flag |= ICHG; | |
c59af027 | 904 | if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1)) |
986aedaa | 905 | goto bad; |
4f083fd7 | 906 | } |
cfef4373 | 907 | if (error = ufs_direnter(ip, tdvp, tcnp)) { |
394d67a8 KM |
908 | if (doingdirectory && newparent) { |
909 | dp->i_nlink--; | |
910 | dp->i_flag |= ICHG; | |
c59af027 | 911 | (void)VOP_UPDATE(ITOV(dp), &time, &time, 1); |
394d67a8 KM |
912 | } |
913 | goto bad; | |
914 | } | |
031fd966 | 915 | ufs_iput(dp); |
4f083fd7 | 916 | } else { |
7188ac27 KM |
917 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) |
918 | panic("rename: EXDEV"); | |
e69c3c9c SL |
919 | /* |
920 | * Short circuit rename(foo, foo). | |
921 | */ | |
922 | if (xp->i_number == ip->i_number) | |
7188ac27 | 923 | panic("rename: same file"); |
80cee150 JB |
924 | /* |
925 | * If the parent directory is "sticky", then the user must | |
926 | * own the parent directory, or the destination of the rename, | |
927 | * otherwise the destination may not be changed (except by | |
928 | * root). This implements append-only directories. | |
929 | */ | |
cfef4373 JH |
930 | if ((dp->i_mode & ISVTX) && tcnp->cn_cred->cr_uid != 0 && |
931 | tcnp->cn_cred->cr_uid != dp->i_uid && | |
932 | xp->i_uid != tcnp->cn_cred->cr_uid) { | |
80cee150 JB |
933 | error = EPERM; |
934 | goto bad; | |
935 | } | |
4f083fd7 | 936 | /* |
655da945 KM |
937 | * Target must be empty if a directory and have no links |
938 | * to it. Also, ensure source and target are compatible | |
939 | * (both directories, or both not directories). | |
4f083fd7 SL |
940 | */ |
941 | if ((xp->i_mode&IFMT) == IFDIR) { | |
cfef4373 | 942 | if (!ufs_dirempty(xp, dp->i_number, tcnp->cn_cred) || |
7188ac27 | 943 | xp->i_nlink > 2) { |
a5390dce | 944 | error = ENOTEMPTY; |
4f083fd7 SL |
945 | goto bad; |
946 | } | |
947 | if (!doingdirectory) { | |
a5390dce | 948 | error = ENOTDIR; |
4f083fd7 SL |
949 | goto bad; |
950 | } | |
7188ac27 | 951 | cache_purge(ITOV(dp)); |
4f083fd7 | 952 | } else if (doingdirectory) { |
a5390dce | 953 | error = EISDIR; |
4f083fd7 SL |
954 | goto bad; |
955 | } | |
cfef4373 | 956 | if (error = ufs_dirrewrite(dp, ip, tcnp)) |
7188ac27 | 957 | goto bad; |
a62786b9 KM |
958 | /* |
959 | * If the target directory is in the same | |
960 | * directory as the source directory, | |
961 | * decrement the link count on the parent | |
962 | * of the target directory. | |
963 | */ | |
964 | if (doingdirectory && !newparent) { | |
965 | dp->i_nlink--; | |
966 | dp->i_flag |= ICHG; | |
967 | } | |
c59af027 | 968 | ufs_iput(dp); |
4f083fd7 | 969 | /* |
a5390dce SL |
970 | * Adjust the link count of the target to |
971 | * reflect the dirrewrite above. If this is | |
972 | * a directory it is empty and there are | |
973 | * no links to it, so we can squash the inode and | |
974 | * any space associated with it. We disallowed | |
975 | * renaming over top of a directory with links to | |
68f21562 KM |
976 | * it above, as the remaining link would point to |
977 | * a directory without "." or ".." entries. | |
4f083fd7 | 978 | */ |
a5390dce | 979 | xp->i_nlink--; |
4f083fd7 | 980 | if (doingdirectory) { |
a5390dce SL |
981 | if (--xp->i_nlink != 0) |
982 | panic("rename: linked directory"); | |
c59af027 | 983 | error = VOP_TRUNCATE(ITOV(xp), (u_long)0, IO_SYNC); |
a5390dce | 984 | } |
4f083fd7 | 985 | xp->i_flag |= ICHG; |
031fd966 | 986 | ufs_iput(xp); |
31db12cb | 987 | xp = NULL; |
4f083fd7 SL |
988 | } |
989 | ||
990 | /* | |
991 | * 3) Unlink the source. | |
992 | */ | |
655da945 | 993 | unlinkit: |
cfef4373 JH |
994 | fcnp->cn_flags &= ~MODMASK; |
995 | fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; | |
996 | if ((fcnp->cn_flags & SAVESTART) == 0) | |
655da945 | 997 | panic("ufs_rename: lost from startdir"); |
5e03b55d | 998 | p->p_spare[1]--; |
d36023e3 | 999 | (void) relookup(fdvp, &fvp, fcnp); |
cfef4373 JH |
1000 | if (fvp != NULL) { |
1001 | xp = VTOI(fvp); | |
1002 | dp = VTOI(fdvp); | |
7188ac27 | 1003 | } else { |
d9d75b8f KM |
1004 | /* |
1005 | * From name has disappeared. | |
1006 | */ | |
1007 | if (doingdirectory) | |
1008 | panic("rename: lost dir entry"); | |
1009 | vrele(ITOV(ip)); | |
1010 | return (0); | |
7188ac27 | 1011 | } |
4f083fd7 | 1012 | /* |
7188ac27 | 1013 | * Ensure that the directory entry still exists and has not |
68f21562 KM |
1014 | * changed while the new name has been entered. If the source is |
1015 | * a file then the entry may have been unlinked or renamed. In | |
1016 | * either case there is no further work to be done. If the source | |
1017 | * is a directory then it cannot have been rmdir'ed; its link | |
1018 | * count of three would cause a rmdir to fail with ENOTEMPTY. | |
7188ac27 | 1019 | * The IRENAME flag ensures that it cannot be moved by another |
68f21562 | 1020 | * rename. |
4f083fd7 | 1021 | */ |
4f1a9037 | 1022 | if (xp != ip) { |
68f21562 | 1023 | if (doingdirectory) |
4f1a9037 | 1024 | panic("rename: lost dir entry"); |
68f21562 | 1025 | } else { |
4f083fd7 | 1026 | /* |
68f21562 KM |
1027 | * If the source is a directory with a |
1028 | * new parent, the link count of the old | |
1029 | * parent directory must be decremented | |
1030 | * and ".." set to point to the new parent. | |
4f083fd7 | 1031 | */ |
68f21562 | 1032 | if (doingdirectory && newparent) { |
4f083fd7 SL |
1033 | dp->i_nlink--; |
1034 | dp->i_flag |= ICHG; | |
86cdabf6 | 1035 | error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, |
7188ac27 | 1036 | sizeof (struct dirtemplate), (off_t)0, |
86cdabf6 | 1037 | UIO_SYSSPACE, IO_NODELOCKED, |
cfef4373 | 1038 | tcnp->cn_cred, (int *)0, (struct proc *)0); |
68f21562 KM |
1039 | if (error == 0) { |
1040 | if (dirbuf.dotdot_namlen != 2 || | |
1041 | dirbuf.dotdot_name[0] != '.' || | |
1042 | dirbuf.dotdot_name[1] != '.') { | |
031fd966 KB |
1043 | ufs_dirbad(xp, 12, |
1044 | "rename: mangled dir"); | |
68f21562 KM |
1045 | } else { |
1046 | dirbuf.dotdot_ino = newparent; | |
86cdabf6 | 1047 | (void) vn_rdwr(UIO_WRITE, ITOV(xp), |
68f21562 KM |
1048 | (caddr_t)&dirbuf, |
1049 | sizeof (struct dirtemplate), | |
93e273b9 | 1050 | (off_t)0, UIO_SYSSPACE, |
86cdabf6 | 1051 | IO_NODELOCKED|IO_SYNC, |
cfef4373 | 1052 | tcnp->cn_cred, (int *)0, |
5b169cb7 | 1053 | (struct proc *)0); |
7188ac27 | 1054 | cache_purge(ITOV(dp)); |
68f21562 KM |
1055 | } |
1056 | } | |
4f083fd7 | 1057 | } |
cfef4373 | 1058 | error = ufs_dirremove(fdvp, fcnp); |
7188ac27 | 1059 | if (!error) { |
68f21562 KM |
1060 | xp->i_nlink--; |
1061 | xp->i_flag |= ICHG; | |
4f083fd7 | 1062 | } |
68f21562 | 1063 | xp->i_flag &= ~IRENAME; |
4f083fd7 | 1064 | } |
4f083fd7 | 1065 | if (dp) |
7188ac27 | 1066 | vput(ITOV(dp)); |
68f21562 | 1067 | if (xp) |
7188ac27 KM |
1068 | vput(ITOV(xp)); |
1069 | vrele(ITOV(ip)); | |
1070 | return (error); | |
a5390dce | 1071 | |
4f083fd7 | 1072 | bad: |
4f083fd7 | 1073 | if (xp) |
7188ac27 KM |
1074 | vput(ITOV(xp)); |
1075 | vput(ITOV(dp)); | |
4f083fd7 SL |
1076 | out: |
1077 | ip->i_nlink--; | |
1078 | ip->i_flag |= ICHG; | |
7188ac27 KM |
1079 | vrele(ITOV(ip)); |
1080 | return (error); | |
64d3a787 | 1081 | } |
88a7a62a SL |
1082 | |
1083 | /* | |
1084 | * A virgin directory (no blushing please). | |
1085 | */ | |
031fd966 | 1086 | static struct dirtemplate mastertemplate = { |
88a7a62a SL |
1087 | 0, 12, 1, ".", |
1088 | 0, DIRBLKSIZ - 12, 2, ".." | |
1089 | }; | |
1090 | ||
1091 | /* | |
1092 | * Mkdir system call | |
1093 | */ | |
031fd966 | 1094 | int |
6e57e9ff | 1095 | ufs_mkdir(dvp, vpp, cnp, vap) |
cfef4373 JH |
1096 | struct vnode *dvp; |
1097 | struct vnode **vpp; | |
1098 | struct componentname *cnp; | |
7188ac27 | 1099 | struct vattr *vap; |
88a7a62a | 1100 | { |
88a7a62a | 1101 | register struct inode *ip, *dp; |
c59af027 | 1102 | struct vnode *tvp; |
88a7a62a | 1103 | struct dirtemplate dirtemplate; |
7188ac27 KM |
1104 | int error; |
1105 | int dmode; | |
1106 | ||
655da945 | 1107 | #ifdef DIANOSTIC |
cfef4373 | 1108 | if ((cnp->cn_flags & HASBUF) == 0) |
655da945 KM |
1109 | panic("ufs_mkdir: no name"); |
1110 | #endif | |
7188ac27 | 1111 | dp = VTOI(dvp); |
986aedaa | 1112 | if ((unsigned short)dp->i_nlink >= LINK_MAX) { |
cfef4373 | 1113 | free(cnp->cn_pnbuf, M_NAMEI); |
031fd966 | 1114 | ufs_iput(dp); |
986aedaa KM |
1115 | return (EMLINK); |
1116 | } | |
7188ac27 KM |
1117 | dmode = vap->va_mode&0777; |
1118 | dmode |= IFDIR; | |
88a7a62a | 1119 | /* |
655da945 KM |
1120 | * Must simulate part of maknode here to acquire the inode, but |
1121 | * not have it entered in the parent directory. The entry is made | |
1122 | * later after writing "." and ".." entries. | |
88a7a62a | 1123 | */ |
cfef4373 JH |
1124 | if (error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) { |
1125 | free(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1126 | ufs_iput(dp); |
7188ac27 | 1127 | return (error); |
88a7a62a | 1128 | } |
c59af027 | 1129 | ip = VTOI(tvp); |
cfef4373 | 1130 | ip->i_uid = cnp->cn_cred->cr_uid; |
4b61628b | 1131 | ip->i_gid = dp->i_gid; |
88a7a62a | 1132 | #ifdef QUOTA |
4b61628b | 1133 | if ((error = getinoquota(ip)) || |
cfef4373 JH |
1134 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
1135 | free(cnp->cn_pnbuf, M_NAMEI); | |
c59af027 | 1136 | VOP_VFREE(tvp, ip->i_number, dmode); |
031fd966 KB |
1137 | ufs_iput(ip); |
1138 | ufs_iput(dp); | |
4b61628b KM |
1139 | return (error); |
1140 | } | |
88a7a62a SL |
1141 | #endif |
1142 | ip->i_flag |= IACC|IUPD|ICHG; | |
7188ac27 KM |
1143 | ip->i_mode = dmode; |
1144 | ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ | |
88a7a62a | 1145 | ip->i_nlink = 2; |
c59af027 | 1146 | error = VOP_UPDATE(ITOV(ip), &time, &time, 1); |
88a7a62a SL |
1147 | |
1148 | /* | |
1149 | * Bump link count in parent directory | |
1150 | * to reflect work done below. Should | |
1151 | * be done before reference is created | |
1152 | * so reparation is possible if we crash. | |
1153 | */ | |
1154 | dp->i_nlink++; | |
1155 | dp->i_flag |= ICHG; | |
c59af027 | 1156 | if (error = VOP_UPDATE(ITOV(dp), &time, &time, 1)) |
394d67a8 | 1157 | goto bad; |
88a7a62a | 1158 | |
031fd966 | 1159 | /* Initialize directory with "." and ".." from static template. */ |
88a7a62a SL |
1160 | dirtemplate = mastertemplate; |
1161 | dirtemplate.dot_ino = ip->i_number; | |
1162 | dirtemplate.dotdot_ino = dp->i_number; | |
86cdabf6 | 1163 | error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, |
5b169cb7 | 1164 | sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, |
cfef4373 | 1165 | IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0); |
7188ac27 | 1166 | if (error) { |
88a7a62a SL |
1167 | dp->i_nlink--; |
1168 | dp->i_flag |= ICHG; | |
1169 | goto bad; | |
1170 | } | |
cc173077 | 1171 | if (DIRBLKSIZ > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) |
031fd966 KB |
1172 | panic("ufs_mkdir: blksize"); /* XXX should grow with balloc() */ |
1173 | else { | |
23de9f20 | 1174 | ip->i_size = DIRBLKSIZ; |
15365e07 KM |
1175 | ip->i_flag |= ICHG; |
1176 | } | |
031fd966 KB |
1177 | |
1178 | /* Directory set up, now install it's entry in the parent directory. */ | |
cfef4373 | 1179 | if (error = ufs_direnter(ip, dvp, cnp)) { |
2a5d2f56 KM |
1180 | dp->i_nlink--; |
1181 | dp->i_flag |= ICHG; | |
88a7a62a SL |
1182 | } |
1183 | bad: | |
1184 | /* | |
c59af027 KM |
1185 | * No need to do an explicit VOP_TRUNCATE here, vrele will do this |
1186 | * for us because we set the link count to 0. | |
88a7a62a | 1187 | */ |
7188ac27 | 1188 | if (error) { |
88a7a62a SL |
1189 | ip->i_nlink = 0; |
1190 | ip->i_flag |= ICHG; | |
031fd966 | 1191 | ufs_iput(ip); |
cbcdacd6 | 1192 | } else |
cfef4373 JH |
1193 | *vpp = ITOV(ip); |
1194 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1195 | ufs_iput(dp); |
7188ac27 | 1196 | return (error); |
88a7a62a SL |
1197 | } |
1198 | ||
1199 | /* | |
1200 | * Rmdir system call. | |
1201 | */ | |
031fd966 | 1202 | int |
6e57e9ff JH |
1203 | ufs_rmdir(dvp, vp, cnp) |
1204 | struct vnode *dvp; | |
1205 | struct vnode *vp; | |
cfef4373 | 1206 | struct componentname *cnp; |
88a7a62a | 1207 | { |
88a7a62a | 1208 | register struct inode *ip, *dp; |
031fd966 | 1209 | int error; |
7188ac27 | 1210 | |
cfef4373 JH |
1211 | ip = VTOI(vp); |
1212 | dp = VTOI(dvp); | |
88a7a62a SL |
1213 | /* |
1214 | * No rmdir "." please. | |
1215 | */ | |
1216 | if (dp == ip) { | |
cfef4373 | 1217 | vrele(dvp); |
031fd966 | 1218 | ufs_iput(ip); |
7188ac27 | 1219 | return (EINVAL); |
88a7a62a SL |
1220 | } |
1221 | /* | |
1222 | * Verify the directory is empty (and valid). | |
1223 | * (Rmdir ".." won't be valid since | |
1224 | * ".." will contain a reference to | |
1225 | * the current directory and thus be | |
1226 | * non-empty.) | |
1227 | */ | |
031fd966 KB |
1228 | error = 0; |
1229 | if (ip->i_nlink != 2 || | |
cfef4373 | 1230 | !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { |
7188ac27 | 1231 | error = ENOTEMPTY; |
88a7a62a SL |
1232 | goto out; |
1233 | } | |
1234 | /* | |
1235 | * Delete reference to directory before purging | |
1236 | * inode. If we crash in between, the directory | |
1237 | * will be reattached to lost+found, | |
1238 | */ | |
cfef4373 | 1239 | if (error = ufs_dirremove(dvp, cnp)) |
88a7a62a SL |
1240 | goto out; |
1241 | dp->i_nlink--; | |
1242 | dp->i_flag |= ICHG; | |
cfef4373 | 1243 | cache_purge(dvp); |
031fd966 | 1244 | ufs_iput(dp); |
cfef4373 | 1245 | dvp = NULL; |
88a7a62a SL |
1246 | /* |
1247 | * Truncate inode. The only stuff left | |
1248 | * in the directory is "." and "..". The | |
1249 | * "." reference is inconsequential since | |
1250 | * we're quashing it. The ".." reference | |
1251 | * has already been adjusted above. We've | |
1252 | * removed the "." reference and the reference | |
1253 | * in the parent directory, but there may be | |
1254 | * other hard links so decrement by 2 and | |
1255 | * worry about them later. | |
1256 | */ | |
1257 | ip->i_nlink -= 2; | |
cfef4373 | 1258 | error = VOP_TRUNCATE(vp, (u_long)0, IO_SYNC); |
7188ac27 | 1259 | cache_purge(ITOV(ip)); |
88a7a62a | 1260 | out: |
cfef4373 | 1261 | if (dvp) |
031fd966 KB |
1262 | ufs_iput(dp); |
1263 | ufs_iput(ip); | |
7188ac27 | 1264 | return (error); |
88a7a62a SL |
1265 | } |
1266 | ||
7188ac27 KM |
1267 | /* |
1268 | * symlink -- make a symbolic link | |
1269 | */ | |
031fd966 | 1270 | int |
6e57e9ff | 1271 | ufs_symlink(dvp, vpp, cnp, vap, target) |
cfef4373 JH |
1272 | struct vnode *dvp; |
1273 | struct vnode **vpp; | |
1274 | struct componentname *cnp; | |
7188ac27 KM |
1275 | struct vattr *vap; |
1276 | char *target; | |
1277 | { | |
7188ac27 KM |
1278 | int error; |
1279 | ||
cfef4373 | 1280 | if (error = ufs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp)) |
7188ac27 | 1281 | return (error); |
cfef4373 JH |
1282 | error = vn_rdwr(UIO_WRITE, *vpp, target, strlen(target), (off_t)0, |
1283 | UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred, (int *)0, | |
5b169cb7 | 1284 | (struct proc *)0); |
cfef4373 | 1285 | vput(*vpp); |
7188ac27 KM |
1286 | return (error); |
1287 | } | |
1288 | ||
1289 | /* | |
d8ba9f9d KM |
1290 | * Vnode op for reading directories. |
1291 | * | |
1292 | * The routine below assumes that the on-disk format of a directory | |
1293 | * is the same as that defined by <sys/dirent.h>. If the on-disk | |
1294 | * format changes, then it will be necessary to do a conversion | |
1295 | * from the on-disk format that read returns to the format defined | |
1296 | * by <sys/dirent.h>. | |
7188ac27 | 1297 | */ |
031fd966 | 1298 | int |
930730fd | 1299 | ufs_readdir(vp, uio, cred, eofflagp) |
7188ac27 KM |
1300 | struct vnode *vp; |
1301 | register struct uio *uio; | |
7188ac27 | 1302 | struct ucred *cred; |
930730fd | 1303 | int *eofflagp; |
88a7a62a | 1304 | { |
86cdabf6 | 1305 | int count, lost, error; |
88a7a62a | 1306 | |
7188ac27 KM |
1307 | count = uio->uio_resid; |
1308 | count &= ~(DIRBLKSIZ - 1); | |
86cdabf6 KM |
1309 | lost = uio->uio_resid - count; |
1310 | if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) | |
7188ac27 | 1311 | return (EINVAL); |
7188ac27 KM |
1312 | uio->uio_resid = count; |
1313 | uio->uio_iov->iov_len = count; | |
031fd966 | 1314 | error = VOP_READ(vp, uio, 0, cred); |
86cdabf6 | 1315 | uio->uio_resid += lost; |
930730fd KM |
1316 | if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) |
1317 | *eofflagp = 1; | |
1318 | else | |
1319 | *eofflagp = 0; | |
7188ac27 KM |
1320 | return (error); |
1321 | } | |
1322 | ||
1323 | /* | |
1324 | * Return target name of a symbolic link | |
1325 | */ | |
031fd966 | 1326 | int |
7188ac27 KM |
1327 | ufs_readlink(vp, uiop, cred) |
1328 | struct vnode *vp; | |
1329 | struct uio *uiop; | |
1330 | struct ucred *cred; | |
1331 | { | |
1332 | ||
031fd966 | 1333 | return (VOP_READ(vp, uiop, 0, cred)); |
7188ac27 KM |
1334 | } |
1335 | ||
1336 | /* | |
1337 | * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually | |
655da945 | 1338 | * done. If a buffer has been saved in anticipation of a CREATE, delete it. |
7188ac27 | 1339 | */ |
66955caf | 1340 | /* ARGSUSED */ |
031fd966 | 1341 | int |
6e57e9ff | 1342 | ufs_abortop(dvp, cnp) |
cfef4373 JH |
1343 | struct vnode *dvp; |
1344 | struct componentname *cnp; | |
7188ac27 | 1345 | { |
cfef4373 JH |
1346 | if ((cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) |
1347 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
66955caf | 1348 | return (0); |
7188ac27 KM |
1349 | } |
1350 | ||
1c3ebc10 KM |
1351 | /* |
1352 | * Lock an inode. | |
1353 | */ | |
031fd966 | 1354 | int |
7188ac27 KM |
1355 | ufs_lock(vp) |
1356 | struct vnode *vp; | |
1357 | { | |
1358 | register struct inode *ip = VTOI(vp); | |
1359 | ||
1360 | ILOCK(ip); | |
1361 | return (0); | |
1362 | } | |
1363 | ||
1c3ebc10 KM |
1364 | /* |
1365 | * Unlock an inode. | |
1366 | */ | |
031fd966 | 1367 | int |
7188ac27 KM |
1368 | ufs_unlock(vp) |
1369 | struct vnode *vp; | |
1370 | { | |
1371 | register struct inode *ip = VTOI(vp); | |
1372 | ||
1373 | if (!(ip->i_flag & ILOCKED)) | |
1374 | panic("ufs_unlock NOT LOCKED"); | |
1375 | IUNLOCK(ip); | |
1376 | return (0); | |
1377 | } | |
1378 | ||
1c3ebc10 KM |
1379 | /* |
1380 | * Check for a locked inode. | |
1381 | */ | |
031fd966 | 1382 | int |
1c3ebc10 KM |
1383 | ufs_islocked(vp) |
1384 | struct vnode *vp; | |
1385 | { | |
1386 | ||
1387 | if (VTOI(vp)->i_flag & ILOCKED) | |
1388 | return (1); | |
1389 | return (0); | |
1390 | } | |
1391 | ||
88a7a62a | 1392 | /* |
4f1ff475 KM |
1393 | * Calculate the logical to physical mapping if not done already, |
1394 | * then call the device strategy routine. | |
88a7a62a | 1395 | */ |
031fd966 | 1396 | int |
7188ac27 KM |
1397 | ufs_strategy(bp) |
1398 | register struct buf *bp; | |
88a7a62a | 1399 | { |
031fd966 | 1400 | register struct inode *ip; |
e16fa59e | 1401 | struct vnode *vp; |
e16fa59e KM |
1402 | int error; |
1403 | ||
031fd966 | 1404 | ip = VTOI(bp->b_vp); |
e16fa59e KM |
1405 | if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) |
1406 | panic("ufs_strategy: spec"); | |
1407 | if (bp->b_blkno == bp->b_lblkno) { | |
c59af027 | 1408 | if (error = |
dd1d1daf KM |
1409 | VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno)) { |
1410 | bp->b_error = error; | |
1411 | bp->b_flags |= B_ERROR; | |
1412 | biodone(bp); | |
e16fa59e | 1413 | return (error); |
dd1d1daf | 1414 | } |
20aa076b | 1415 | if ((long)bp->b_blkno == -1) |
e16fa59e | 1416 | clrbuf(bp); |
e16fa59e | 1417 | } |
20aa076b KM |
1418 | if ((long)bp->b_blkno == -1) { |
1419 | biodone(bp); | |
e16fa59e | 1420 | return (0); |
20aa076b | 1421 | } |
e16fa59e KM |
1422 | vp = ip->i_devvp; |
1423 | bp->b_dev = vp->v_rdev; | |
031fd966 | 1424 | (vp->v_op->vop_strategy)(bp); |
7188ac27 KM |
1425 | return (0); |
1426 | } | |
88a7a62a | 1427 | |
e16fa59e KM |
1428 | /* |
1429 | * Print out the contents of an inode. | |
1430 | */ | |
031fd966 | 1431 | int |
e16fa59e KM |
1432 | ufs_print(vp) |
1433 | struct vnode *vp; | |
1434 | { | |
1435 | register struct inode *ip = VTOI(vp); | |
1436 | ||
a8f829ed KM |
1437 | printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, |
1438 | major(ip->i_dev), minor(ip->i_dev)); | |
1439 | #ifdef FIFO | |
1440 | if (vp->v_type == VFIFO) | |
1441 | fifo_printinfo(vp); | |
1442 | #endif /* FIFO */ | |
1443 | printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); | |
4a8fb1bb | 1444 | if (ip->i_lockholder == 0) |
031fd966 | 1445 | return (0); |
4a8fb1bb KM |
1446 | printf("\towner pid %d", ip->i_lockholder); |
1447 | if (ip->i_lockwaiter) | |
1448 | printf(" waiting pid %d", ip->i_lockwaiter); | |
96bae3e0 | 1449 | printf("\n"); |
031fd966 | 1450 | return (0); |
e16fa59e KM |
1451 | } |
1452 | ||
24a31b70 KM |
1453 | /* |
1454 | * Read wrapper for special devices. | |
1455 | */ | |
031fd966 | 1456 | int |
24a31b70 KM |
1457 | ufsspec_read(vp, uio, ioflag, cred) |
1458 | struct vnode *vp; | |
1459 | struct uio *uio; | |
1460 | int ioflag; | |
1461 | struct ucred *cred; | |
1462 | { | |
1463 | ||
1464 | /* | |
1465 | * Set access flag. | |
1466 | */ | |
1467 | VTOI(vp)->i_flag |= IACC; | |
1468 | return (spec_read(vp, uio, ioflag, cred)); | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * Write wrapper for special devices. | |
1473 | */ | |
031fd966 | 1474 | int |
24a31b70 KM |
1475 | ufsspec_write(vp, uio, ioflag, cred) |
1476 | struct vnode *vp; | |
1477 | struct uio *uio; | |
1478 | int ioflag; | |
1479 | struct ucred *cred; | |
1480 | { | |
1481 | ||
1482 | /* | |
1483 | * Set update and change flags. | |
1484 | */ | |
1485 | VTOI(vp)->i_flag |= IUPD|ICHG; | |
1486 | return (spec_write(vp, uio, ioflag, cred)); | |
1487 | } | |
1488 | ||
1489 | /* | |
1490 | * Close wrapper for special devices. | |
1491 | * | |
1492 | * Update the times on the inode then do device close. | |
1493 | */ | |
031fd966 | 1494 | int |
5b169cb7 | 1495 | ufsspec_close(vp, fflag, cred, p) |
24a31b70 KM |
1496 | struct vnode *vp; |
1497 | int fflag; | |
1498 | struct ucred *cred; | |
5b169cb7 | 1499 | struct proc *p; |
24a31b70 KM |
1500 | { |
1501 | register struct inode *ip = VTOI(vp); | |
1502 | ||
de67eefc | 1503 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) |
24a31b70 | 1504 | ITIMES(ip, &time, &time); |
5b169cb7 | 1505 | return (spec_close(vp, fflag, cred, p)); |
24a31b70 KM |
1506 | } |
1507 | ||
d1c43d7f KM |
1508 | #ifdef FIFO |
1509 | /* | |
1510 | * Read wrapper for fifo's | |
1511 | */ | |
031fd966 | 1512 | int |
d1c43d7f KM |
1513 | ufsfifo_read(vp, uio, ioflag, cred) |
1514 | struct vnode *vp; | |
1515 | struct uio *uio; | |
1516 | int ioflag; | |
1517 | struct ucred *cred; | |
1518 | { | |
1519 | ||
1520 | /* | |
1521 | * Set access flag. | |
1522 | */ | |
1523 | VTOI(vp)->i_flag |= IACC; | |
1524 | return (fifo_read(vp, uio, ioflag, cred)); | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * Write wrapper for fifo's. | |
1529 | */ | |
031fd966 | 1530 | int |
d1c43d7f KM |
1531 | ufsfifo_write(vp, uio, ioflag, cred) |
1532 | struct vnode *vp; | |
1533 | struct uio *uio; | |
1534 | int ioflag; | |
1535 | struct ucred *cred; | |
1536 | { | |
1537 | ||
1538 | /* | |
1539 | * Set update and change flags. | |
1540 | */ | |
1541 | VTOI(vp)->i_flag |= IUPD|ICHG; | |
1542 | return (fifo_write(vp, uio, ioflag, cred)); | |
1543 | } | |
1544 | ||
1545 | /* | |
1546 | * Close wrapper for fifo's. | |
1547 | * | |
1548 | * Update the times on the inode then do device close. | |
1549 | */ | |
5b169cb7 | 1550 | ufsfifo_close(vp, fflag, cred, p) |
d1c43d7f KM |
1551 | struct vnode *vp; |
1552 | int fflag; | |
1553 | struct ucred *cred; | |
5b169cb7 | 1554 | struct proc *p; |
d1c43d7f KM |
1555 | { |
1556 | register struct inode *ip = VTOI(vp); | |
1557 | ||
1558 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) | |
1559 | ITIMES(ip, &time, &time); | |
5b169cb7 | 1560 | return (fifo_close(vp, fflag, cred, p)); |
d1c43d7f KM |
1561 | } |
1562 | #endif /* FIFO */ | |
1563 | ||
0b0bf936 KM |
1564 | /* |
1565 | * Advisory record locking support | |
1566 | */ | |
031fd966 | 1567 | int |
0b0bf936 KM |
1568 | ufs_advlock(vp, id, op, fl, flags) |
1569 | struct vnode *vp; | |
1570 | caddr_t id; | |
1571 | int op; | |
1572 | register struct flock *fl; | |
1573 | int flags; | |
1574 | { | |
1575 | register struct inode *ip = VTOI(vp); | |
1576 | register struct lockf *lock; | |
1577 | off_t start, end; | |
1578 | int error; | |
1579 | ||
1580 | /* | |
1581 | * Avoid the common case of unlocking when inode has no locks. | |
1582 | */ | |
1583 | if (ip->i_lockf == (struct lockf *)0) { | |
1584 | if (op != F_SETLK) { | |
1585 | fl->l_type = F_UNLCK; | |
1586 | return (0); | |
1587 | } | |
1588 | } | |
1589 | /* | |
1590 | * Convert the flock structure into a start and end. | |
1591 | */ | |
1592 | switch (fl->l_whence) { | |
1593 | ||
1594 | case SEEK_SET: | |
1595 | case SEEK_CUR: | |
1596 | /* | |
1597 | * Caller is responsible for adding any necessary offset | |
1598 | * when SEEK_CUR is used. | |
1599 | */ | |
1600 | start = fl->l_start; | |
1601 | break; | |
1602 | ||
1603 | case SEEK_END: | |
1604 | start = ip->i_size + fl->l_start; | |
1605 | break; | |
1606 | ||
1607 | default: | |
1608 | return (EINVAL); | |
1609 | } | |
1610 | if (start < 0) | |
1611 | return (EINVAL); | |
1612 | if (fl->l_len == 0) | |
1613 | end = -1; | |
1614 | else | |
c412cb68 | 1615 | end = start + fl->l_len - 1; |
0b0bf936 KM |
1616 | /* |
1617 | * Create the lockf structure | |
1618 | */ | |
1619 | MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK); | |
1620 | lock->lf_start = start; | |
1621 | lock->lf_end = end; | |
1622 | lock->lf_id = id; | |
1623 | lock->lf_inode = ip; | |
1624 | lock->lf_type = fl->l_type; | |
1625 | lock->lf_next = (struct lockf *)0; | |
1626 | lock->lf_block = (struct lockf *)0; | |
1627 | lock->lf_flags = flags; | |
1628 | /* | |
1629 | * Do the requested operation. | |
1630 | */ | |
1631 | switch(op) { | |
1632 | case F_SETLK: | |
b9fc1077 | 1633 | return (lf_setlock(lock)); |
0b0bf936 KM |
1634 | |
1635 | case F_UNLCK: | |
b9fc1077 KM |
1636 | error = lf_clearlock(lock); |
1637 | FREE(lock, M_LOCKF); | |
1638 | return (error); | |
0b0bf936 KM |
1639 | |
1640 | case F_GETLK: | |
b9fc1077 KM |
1641 | error = lf_getlock(lock, fl); |
1642 | FREE(lock, M_LOCKF); | |
1643 | return (error); | |
0b0bf936 KM |
1644 | |
1645 | default: | |
1646 | free(lock, M_LOCKF); | |
1647 | return (EINVAL); | |
1648 | } | |
1649 | /* NOTREACHED */ | |
1650 | } | |
5b169cb7 KM |
1651 | |
1652 | /* | |
031fd966 KB |
1653 | * Initialize the vnode associated with a new inode, handle aliased |
1654 | * vnodes. | |
5b169cb7 | 1655 | */ |
031fd966 | 1656 | int |
c59af027 | 1657 | ufs_vinit(mntp, specops, fifoops, vpp) |
031fd966 | 1658 | struct mount *mntp; |
c59af027 | 1659 | struct vnodeops *specops, *fifoops; |
031fd966 KB |
1660 | struct vnode **vpp; |
1661 | { | |
1662 | struct inode *ip, *nip; | |
1663 | struct vnode *vp, *nvp; | |
4a8fb1bb | 1664 | extern struct vnodeops spec_vnodeops; |
031fd966 KB |
1665 | |
1666 | vp = *vpp; | |
1667 | ip = VTOI(vp); | |
1668 | switch(vp->v_type = IFTOVT(ip->i_mode)) { | |
1669 | case VCHR: | |
1670 | case VBLK: | |
c59af027 | 1671 | vp->v_op = specops; |
031fd966 | 1672 | if (nvp = checkalias(vp, ip->i_rdev, mntp)) { |
c59af027 | 1673 | /* |
4a8fb1bb | 1674 | * Discard unneeded vnode, but save its inode. |
c59af027 | 1675 | */ |
4a8fb1bb KM |
1676 | remque(ip); |
1677 | IUNLOCK(ip); | |
1678 | nvp->v_data = vp->v_data; | |
1679 | vp->v_data = NULL; | |
1680 | vp->v_op = &spec_vnodeops; | |
1681 | vrele(vp); | |
1682 | vgone(vp); | |
c59af027 | 1683 | /* |
4a8fb1bb | 1684 | * Reinitialize aliased inode. |
c59af027 | 1685 | */ |
4a8fb1bb KM |
1686 | vp = nvp; |
1687 | ip->i_vnode = vp; | |
1688 | ufs_ihashins(ip); | |
031fd966 KB |
1689 | } |
1690 | break; | |
1691 | case VFIFO: | |
1692 | #ifdef FIFO | |
c59af027 | 1693 | vp->v_op = fifoops; |
031fd966 KB |
1694 | break; |
1695 | #else | |
1696 | return (EOPNOTSUPP); | |
1697 | #endif | |
1698 | } | |
031fd966 KB |
1699 | if (ip->i_number == ROOTINO) |
1700 | vp->v_flag |= VROOT; | |
1a3cb193 KM |
1701 | /* |
1702 | * Initialize modrev times | |
1703 | */ | |
1704 | SETHIGH(ip->i_modrev, mono_time.tv_sec); | |
1705 | SETLOW(ip->i_modrev, mono_time.tv_usec * 4294); | |
031fd966 KB |
1706 | *vpp = vp; |
1707 | return (0); | |
1708 | } | |
5b169cb7 | 1709 | |
031fd966 KB |
1710 | /* |
1711 | * Allocate a new inode. | |
1712 | */ | |
1713 | int | |
6e57e9ff | 1714 | ufs_makeinode(mode, dvp, vpp, cnp) |
031fd966 | 1715 | int mode; |
cfef4373 | 1716 | struct vnode *dvp; |
c59af027 | 1717 | struct vnode **vpp; |
cfef4373 | 1718 | struct componentname *cnp; |
031fd966 KB |
1719 | { |
1720 | register struct inode *ip, *pdir; | |
c59af027 | 1721 | struct vnode *tvp; |
031fd966 KB |
1722 | int error; |
1723 | ||
cfef4373 | 1724 | pdir = VTOI(dvp); |
031fd966 | 1725 | #ifdef DIANOSTIC |
cfef4373 | 1726 | if ((cnp->cn_flags & HASBUF) == 0) |
031fd966 KB |
1727 | panic("ufs_makeinode: no name"); |
1728 | #endif | |
c59af027 | 1729 | *vpp = NULL; |
031fd966 KB |
1730 | if ((mode & IFMT) == 0) |
1731 | mode |= IFREG; | |
1732 | ||
cfef4373 JH |
1733 | if (error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) { |
1734 | free(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 KB |
1735 | ufs_iput(pdir); |
1736 | return (error); | |
1737 | } | |
c59af027 | 1738 | ip = VTOI(tvp); |
cfef4373 | 1739 | ip->i_uid = cnp->cn_cred->cr_uid; |
031fd966 KB |
1740 | ip->i_gid = pdir->i_gid; |
1741 | #ifdef QUOTA | |
1742 | if ((error = getinoquota(ip)) || | |
cfef4373 JH |
1743 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
1744 | free(cnp->cn_pnbuf, M_NAMEI); | |
c59af027 | 1745 | VOP_VFREE(tvp, ip->i_number, mode); |
031fd966 KB |
1746 | ufs_iput(ip); |
1747 | ufs_iput(pdir); | |
1748 | return (error); | |
1749 | } | |
1750 | #endif | |
1751 | ip->i_flag |= IACC|IUPD|ICHG; | |
1752 | ip->i_mode = mode; | |
c59af027 | 1753 | tvp->v_type = IFTOVT(mode); /* Rest init'd in iget() */ |
031fd966 | 1754 | ip->i_nlink = 1; |
cfef4373 JH |
1755 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && |
1756 | suser(cnp->cn_cred, NULL)) | |
031fd966 KB |
1757 | ip->i_mode &= ~ISGID; |
1758 | ||
1759 | /* | |
1760 | * Make sure inode goes to disk before directory entry. | |
1761 | */ | |
c59af027 | 1762 | if (error = VOP_UPDATE(tvp, &time, &time, 1)) |
031fd966 | 1763 | goto bad; |
cfef4373 | 1764 | if (error = ufs_direnter(ip, dvp, cnp)) |
031fd966 | 1765 | goto bad; |
cfef4373 JH |
1766 | if ((cnp->cn_flags & SAVESTART) == 0) |
1767 | FREE(cnp->cn_pnbuf, M_NAMEI); | |
031fd966 | 1768 | ufs_iput(pdir); |
c59af027 | 1769 | *vpp = tvp; |
031fd966 KB |
1770 | return (0); |
1771 | ||
1772 | bad: | |
1773 | /* | |
1774 | * Write error occurred trying to update the inode | |
1775 | * or the directory so must deallocate the inode. | |
1776 | */ | |
cfef4373 | 1777 | free(cnp->cn_pnbuf, M_NAMEI); |
031fd966 KB |
1778 | ufs_iput(pdir); |
1779 | ip->i_nlink = 0; | |
1780 | ip->i_flag |= ICHG; | |
1781 | ufs_iput(ip); | |
1782 | return (error); | |
1783 | } |