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