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 | * |
7188ac27 KM |
5 | * Redistribution and use in source and binary forms are permitted |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | * | |
96bae3e0 | 17 | * @(#)lfs_vnops.c 7.28 (Berkeley) %G% |
da7c5cc6 | 18 | */ |
6459ebe0 | 19 | |
94368568 JB |
20 | #include "param.h" |
21 | #include "systm.h" | |
94368568 JB |
22 | #include "user.h" |
23 | #include "kernel.h" | |
24 | #include "file.h" | |
25 | #include "stat.h" | |
94368568 JB |
26 | #include "buf.h" |
27 | #include "proc.h" | |
94368568 JB |
28 | #include "uio.h" |
29 | #include "socket.h" | |
30 | #include "socketvar.h" | |
7188ac27 | 31 | #include "conf.h" |
94368568 | 32 | #include "mount.h" |
7188ac27 KM |
33 | #include "vnode.h" |
34 | #include "../ufs/inode.h" | |
35 | #include "../ufs/fs.h" | |
36 | #include "../ufs/quota.h" | |
3e78e260 | 37 | |
4f083fd7 | 38 | /* |
7188ac27 | 39 | * Global vfs data structures for ufs |
4f083fd7 | 40 | */ |
3e78e260 | 41 | |
7188ac27 KM |
42 | int ufs_lookup(), |
43 | ufs_create(), | |
44 | ufs_mknod(), | |
45 | ufs_open(), | |
46 | ufs_close(), | |
47 | ufs_access(), | |
48 | ufs_getattr(), | |
49 | ufs_setattr(), | |
50 | ufs_read(), | |
51 | ufs_write(), | |
52 | ufs_ioctl(), | |
53 | ufs_select(), | |
54 | ufs_mmap(), | |
55 | ufs_fsync(), | |
56 | ufs_seek(), | |
57 | ufs_remove(), | |
58 | ufs_link(), | |
59 | ufs_rename(), | |
60 | ufs_mkdir(), | |
61 | ufs_rmdir(), | |
62 | ufs_symlink(), | |
63 | ufs_readdir(), | |
64 | ufs_readlink(), | |
65 | ufs_abortop(), | |
66 | ufs_inactive(), | |
e8a3816c | 67 | ufs_reclaim(), |
7188ac27 KM |
68 | ufs_lock(), |
69 | ufs_unlock(), | |
70 | ufs_bmap(), | |
e16fa59e KM |
71 | ufs_strategy(), |
72 | ufs_print(); | |
7188ac27 KM |
73 | |
74 | struct vnodeops ufs_vnodeops = { | |
e16fa59e KM |
75 | ufs_lookup, /* lookup */ |
76 | ufs_create, /* create */ | |
77 | ufs_mknod, /* mknod */ | |
78 | ufs_open, /* open */ | |
79 | ufs_close, /* close */ | |
80 | ufs_access, /* access */ | |
81 | ufs_getattr, /* getattr */ | |
82 | ufs_setattr, /* setattr */ | |
83 | ufs_read, /* read */ | |
84 | ufs_write, /* write */ | |
85 | ufs_ioctl, /* ioctl */ | |
86 | ufs_select, /* select */ | |
87 | ufs_mmap, /* mmap */ | |
88 | ufs_fsync, /* fsync */ | |
89 | ufs_seek, /* seek */ | |
90 | ufs_remove, /* remove */ | |
91 | ufs_link, /* link */ | |
92 | ufs_rename, /* rename */ | |
93 | ufs_mkdir, /* mkdir */ | |
94 | ufs_rmdir, /* rmdir */ | |
95 | ufs_symlink, /* symlink */ | |
96 | ufs_readdir, /* readdir */ | |
97 | ufs_readlink, /* readlink */ | |
98 | ufs_abortop, /* abortop */ | |
99 | ufs_inactive, /* inactive */ | |
100 | ufs_reclaim, /* reclaim */ | |
101 | ufs_lock, /* lock */ | |
102 | ufs_unlock, /* unlock */ | |
103 | ufs_bmap, /* bmap */ | |
104 | ufs_strategy, /* strategy */ | |
105 | ufs_print, /* print */ | |
7188ac27 KM |
106 | }; |
107 | ||
f09fe6d3 KM |
108 | int spec_lookup(), |
109 | spec_open(), | |
24a31b70 KM |
110 | ufsspec_read(), |
111 | ufsspec_write(), | |
f09fe6d3 | 112 | spec_strategy(), |
e16fa59e | 113 | spec_bmap(), |
f09fe6d3 KM |
114 | spec_ioctl(), |
115 | spec_select(), | |
24a31b70 | 116 | ufsspec_close(), |
f09fe6d3 KM |
117 | spec_badop(), |
118 | spec_nullop(); | |
119 | ||
120 | struct vnodeops spec_inodeops = { | |
86cdabf6 KM |
121 | spec_lookup, /* lookup */ |
122 | spec_badop, /* create */ | |
123 | spec_badop, /* mknod */ | |
124 | spec_open, /* open */ | |
24a31b70 | 125 | ufsspec_close, /* close */ |
86cdabf6 KM |
126 | ufs_access, /* access */ |
127 | ufs_getattr, /* getattr */ | |
128 | ufs_setattr, /* setattr */ | |
24a31b70 KM |
129 | ufsspec_read, /* read */ |
130 | ufsspec_write, /* write */ | |
86cdabf6 KM |
131 | spec_ioctl, /* ioctl */ |
132 | spec_select, /* select */ | |
133 | spec_badop, /* mmap */ | |
134 | spec_nullop, /* fsync */ | |
135 | spec_badop, /* seek */ | |
136 | spec_badop, /* remove */ | |
137 | spec_badop, /* link */ | |
138 | spec_badop, /* rename */ | |
139 | spec_badop, /* mkdir */ | |
140 | spec_badop, /* rmdir */ | |
141 | spec_badop, /* symlink */ | |
142 | spec_badop, /* readdir */ | |
143 | spec_badop, /* readlink */ | |
144 | spec_badop, /* abortop */ | |
145 | ufs_inactive, /* inactive */ | |
146 | ufs_reclaim, /* reclaim */ | |
147 | ufs_lock, /* lock */ | |
148 | ufs_unlock, /* unlock */ | |
e16fa59e | 149 | spec_bmap, /* bmap */ |
86cdabf6 | 150 | spec_strategy, /* strategy */ |
e16fa59e | 151 | ufs_print, /* print */ |
f09fe6d3 KM |
152 | }; |
153 | ||
7188ac27 KM |
154 | enum vtype iftovt_tab[8] = { |
155 | VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD, | |
156 | }; | |
157 | int vttoif_tab[8] = { | |
158 | 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT, | |
159 | }; | |
3e78e260 | 160 | |
4f083fd7 | 161 | /* |
7188ac27 | 162 | * Create a regular file |
4f083fd7 | 163 | */ |
7188ac27 KM |
164 | ufs_create(ndp, vap) |
165 | struct nameidata *ndp; | |
166 | struct vattr *vap; | |
3e78e260 | 167 | { |
7188ac27 KM |
168 | struct inode *ip; |
169 | int error; | |
3e78e260 | 170 | |
7188ac27 KM |
171 | if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) |
172 | return (error); | |
173 | ndp->ni_vp = ITOV(ip); | |
174 | return (0); | |
3e78e260 BJ |
175 | } |
176 | ||
4f083fd7 | 177 | /* |
7188ac27 | 178 | * Mknod vnode call |
4f083fd7 | 179 | */ |
7188ac27 KM |
180 | /* ARGSUSED */ |
181 | ufs_mknod(ndp, vap, cred) | |
182 | struct nameidata *ndp; | |
183 | struct ucred *cred; | |
184 | struct vattr *vap; | |
3e78e260 | 185 | { |
f09fe6d3 | 186 | register struct vnode *vp; |
7188ac27 KM |
187 | struct inode *ip; |
188 | int error; | |
3e78e260 | 189 | |
7188ac27 KM |
190 | if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) |
191 | return (error); | |
f09fe6d3 | 192 | vp = ITOV(ip); |
7188ac27 KM |
193 | if (vap->va_rdev) { |
194 | /* | |
195 | * Want to be able to use this to make badblock | |
196 | * inodes, so don't truncate the dev number. | |
197 | */ | |
b373e060 | 198 | ip->i_rdev = vap->va_rdev; |
7188ac27 KM |
199 | ip->i_flag |= IACC|IUPD|ICHG; |
200 | } | |
7188ac27 KM |
201 | /* |
202 | * Remove inode so that it will be reloaded by iget and | |
203 | * checked to see if it is an alias of an existing entry | |
204 | * in the inode cache. | |
205 | */ | |
9ed9b473 | 206 | iput(ip); |
f09fe6d3 KM |
207 | vp->v_type = VNON; |
208 | vgone(vp); | |
7188ac27 | 209 | return (0); |
3e78e260 BJ |
210 | } |
211 | ||
212 | /* | |
7188ac27 KM |
213 | * Open called. |
214 | * | |
215 | * Nothing to do. | |
3e78e260 | 216 | */ |
7188ac27 KM |
217 | /* ARGSUSED */ |
218 | ufs_open(vp, mode, cred) | |
219 | struct vnode *vp; | |
220 | int mode; | |
221 | struct ucred *cred; | |
3e78e260 | 222 | { |
3e78e260 | 223 | |
7188ac27 | 224 | return (0); |
3e78e260 BJ |
225 | } |
226 | ||
227 | /* | |
7188ac27 KM |
228 | * Close called |
229 | * | |
230 | * Update the times on the inode. | |
3e78e260 | 231 | */ |
7188ac27 KM |
232 | /* ARGSUSED */ |
233 | ufs_close(vp, fflag, cred) | |
234 | struct vnode *vp; | |
235 | int fflag; | |
236 | struct ucred *cred; | |
3e78e260 | 237 | { |
7188ac27 | 238 | register struct inode *ip = VTOI(vp); |
3e78e260 | 239 | |
de67eefc | 240 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) |
7188ac27 KM |
241 | ITIMES(ip, &time, &time); |
242 | return (0); | |
3e78e260 BJ |
243 | } |
244 | ||
7188ac27 KM |
245 | ufs_access(vp, mode, cred) |
246 | struct vnode *vp; | |
247 | int mode; | |
248 | struct ucred *cred; | |
3e78e260 | 249 | { |
3e78e260 | 250 | |
7188ac27 | 251 | return (iaccess(VTOI(vp), mode, cred)); |
3e78e260 BJ |
252 | } |
253 | ||
7188ac27 KM |
254 | /* ARGSUSED */ |
255 | ufs_getattr(vp, vap, cred) | |
256 | struct vnode *vp; | |
257 | register struct vattr *vap; | |
258 | struct ucred *cred; | |
3e78e260 | 259 | { |
7188ac27 | 260 | register struct inode *ip = VTOI(vp); |
3e78e260 | 261 | |
7188ac27 | 262 | ITIMES(ip, &time, &time); |
3e78e260 | 263 | /* |
7188ac27 | 264 | * Copy from inode table |
3e78e260 | 265 | */ |
7188ac27 KM |
266 | vap->va_fsid = ip->i_dev; |
267 | vap->va_fileid = ip->i_number; | |
268 | vap->va_mode = ip->i_mode & ~IFMT; | |
269 | vap->va_nlink = ip->i_nlink; | |
270 | vap->va_uid = ip->i_uid; | |
271 | vap->va_gid = ip->i_gid; | |
272 | vap->va_rdev = (dev_t)ip->i_rdev; | |
e8a3816c KM |
273 | vap->va_size = ip->i_din.di_qsize.val[0]; |
274 | vap->va_size1 = ip->i_din.di_qsize.val[1]; | |
7188ac27 | 275 | vap->va_atime.tv_sec = ip->i_atime; |
6ef53d70 | 276 | vap->va_atime.tv_usec = 0; |
7188ac27 | 277 | vap->va_mtime.tv_sec = ip->i_mtime; |
6ef53d70 | 278 | vap->va_mtime.tv_usec = 0; |
7188ac27 | 279 | vap->va_ctime.tv_sec = ip->i_ctime; |
6ef53d70 | 280 | vap->va_ctime.tv_usec = 0; |
d07fc92a KM |
281 | vap->va_flags = ip->i_flags; |
282 | vap->va_gen = ip->i_gen; | |
7188ac27 KM |
283 | /* this doesn't belong here */ |
284 | if (vp->v_type == VBLK) | |
285 | vap->va_blocksize = BLKDEV_IOSIZE; | |
286 | else if (vp->v_type == VCHR) | |
287 | vap->va_blocksize = MAXBSIZE; | |
8eee8525 | 288 | else |
7188ac27 | 289 | vap->va_blocksize = ip->i_fs->fs_bsize; |
a61a68d6 | 290 | vap->va_bytes = dbtob(ip->i_blocks); |
7188ac27 KM |
291 | vap->va_bytes1 = -1; |
292 | vap->va_type = vp->v_type; | |
293 | return (0); | |
3e78e260 BJ |
294 | } |
295 | ||
296 | /* | |
7188ac27 | 297 | * Set attribute vnode op. called from several syscalls |
3e78e260 | 298 | */ |
7188ac27 KM |
299 | ufs_setattr(vp, vap, cred) |
300 | register struct vnode *vp; | |
301 | register struct vattr *vap; | |
302 | register struct ucred *cred; | |
3e78e260 | 303 | { |
7188ac27 KM |
304 | register struct inode *ip = VTOI(vp); |
305 | int error = 0; | |
b4d1aee9 | 306 | |
7188ac27 KM |
307 | /* |
308 | * Check for unsetable attributes. | |
309 | */ | |
310 | if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || | |
311 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || | |
312 | (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || | |
d07fc92a | 313 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
7188ac27 | 314 | return (EINVAL); |
b4d1aee9 | 315 | } |
7188ac27 KM |
316 | /* |
317 | * Go through the fields and update iff not VNOVAL. | |
318 | */ | |
319 | if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) | |
320 | if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) | |
321 | return (error); | |
322 | if (vap->va_size != VNOVAL) { | |
323 | if (vp->v_type == VDIR) | |
324 | return (EISDIR); | |
e16fa59e | 325 | if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */ |
7188ac27 | 326 | return (error); |
3e78e260 | 327 | } |
7188ac27 | 328 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { |
de412887 KM |
329 | if (cred->cr_uid != ip->i_uid && |
330 | (error = suser(cred, &u.u_acflag))) | |
331 | return (error); | |
7188ac27 KM |
332 | if (vap->va_atime.tv_sec != VNOVAL) |
333 | ip->i_flag |= IACC; | |
334 | if (vap->va_mtime.tv_sec != VNOVAL) | |
335 | ip->i_flag |= IUPD; | |
336 | ip->i_flag |= ICHG; | |
337 | if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) | |
338 | return (error); | |
5485e062 | 339 | } |
7188ac27 KM |
340 | if (vap->va_mode != (u_short)VNOVAL) |
341 | error = chmod1(vp, (int)vap->va_mode, cred); | |
d07fc92a KM |
342 | if (vap->va_flags != VNOVAL) { |
343 | if (cred->cr_uid != ip->i_uid && | |
344 | (error = suser(cred, &u.u_acflag))) | |
345 | return (error); | |
346 | if (cred->cr_uid == 0) { | |
347 | ip->i_flags = vap->va_flags; | |
348 | } else { | |
349 | ip->i_flags &= 0xffff0000; | |
350 | ip->i_flags |= (vap->va_flags & 0xffff); | |
351 | } | |
352 | ip->i_flag |= ICHG; | |
353 | } | |
7188ac27 | 354 | return (error); |
528f664c SL |
355 | } |
356 | ||
4f083fd7 SL |
357 | /* |
358 | * Change the mode on a file. | |
359 | * Inode must be locked before calling. | |
360 | */ | |
7188ac27 KM |
361 | chmod1(vp, mode, cred) |
362 | register struct vnode *vp; | |
528f664c | 363 | register int mode; |
7188ac27 | 364 | struct ucred *cred; |
528f664c | 365 | { |
7188ac27 | 366 | register struct inode *ip = VTOI(vp); |
de412887 | 367 | int error; |
197da11b | 368 | |
de412887 KM |
369 | if (cred->cr_uid != ip->i_uid && |
370 | (error = suser(cred, &u.u_acflag))) | |
371 | return (error); | |
3e78e260 | 372 | ip->i_mode &= ~07777; |
7188ac27 KM |
373 | if (cred->cr_uid) { |
374 | if (vp->v_type != VDIR) | |
47af7174 | 375 | mode &= ~ISVTX; |
7188ac27 | 376 | if (!groupmember(ip->i_gid, cred)) |
bb1b75f4 | 377 | mode &= ~ISGID; |
f94ceb3b | 378 | } |
7188ac27 | 379 | ip->i_mode |= mode & 07777; |
3e78e260 | 380 | ip->i_flag |= ICHG; |
7188ac27 KM |
381 | if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) |
382 | xrele(vp); | |
47af7174 | 383 | return (0); |
5485e062 BJ |
384 | } |
385 | ||
528f664c SL |
386 | /* |
387 | * Perform chown operation on inode ip; | |
388 | * inode must be locked prior to call. | |
389 | */ | |
7188ac27 KM |
390 | chown1(vp, uid, gid, cred) |
391 | register struct vnode *vp; | |
392 | uid_t uid; | |
393 | gid_t gid; | |
394 | struct ucred *cred; | |
528f664c | 395 | { |
7188ac27 | 396 | register struct inode *ip = VTOI(vp); |
528f664c SL |
397 | #ifdef QUOTA |
398 | register long change; | |
bb1b75f4 | 399 | #endif |
7188ac27 | 400 | int error; |
528f664c | 401 | |
7188ac27 | 402 | if (uid == (u_short)VNOVAL) |
bb1b75f4 | 403 | uid = ip->i_uid; |
7188ac27 | 404 | if (gid == (u_short)VNOVAL) |
bb1b75f4 | 405 | gid = ip->i_gid; |
18b0bce6 KB |
406 | /* |
407 | * If we don't own the file, are trying to change the owner | |
408 | * of the file, or are not a member of the target group, | |
409 | * the caller must be superuser or the call fails. | |
410 | */ | |
7188ac27 KM |
411 | if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || |
412 | !groupmember((gid_t)gid, cred)) && | |
413 | (error = suser(cred, &u.u_acflag))) | |
414 | return (error); | |
bb1b75f4 | 415 | #ifdef QUOTA |
3809bf69 | 416 | if (ip->i_uid == uid) /* this just speeds things a little */ |
0a77f278 | 417 | change = 0; |
2e073567 SL |
418 | else |
419 | change = ip->i_blocks; | |
420 | (void) chkdq(ip, -change, 1); | |
421 | (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); | |
0a77f278 | 422 | dqrele(ip->i_dquot); |
f94ceb3b | 423 | #endif |
b440c2b9 KM |
424 | if (ip->i_uid != uid && cred->cr_uid != 0) |
425 | ip->i_mode &= ~ISUID; | |
426 | if (ip->i_gid != gid && cred->cr_uid != 0) | |
427 | ip->i_mode &= ~ISGID; | |
bb1b75f4 SL |
428 | ip->i_uid = uid; |
429 | ip->i_gid = gid; | |
3e78e260 | 430 | ip->i_flag |= ICHG; |
528f664c | 431 | #ifdef QUOTA |
0a77f278 | 432 | ip->i_dquot = inoquota(ip); |
2e073567 | 433 | (void) chkdq(ip, change, 1); |
8011f5df | 434 | (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); |
2e073567 SL |
435 | return (u.u_error); /* should == 0 ALWAYS !! */ |
436 | #else | |
bb1b75f4 | 437 | return (0); |
2e073567 | 438 | #endif |
d67a03eb BJ |
439 | } |
440 | ||
b373e060 KM |
441 | /* |
442 | * Vnode op for reading. | |
443 | */ | |
444 | /* ARGSUSED */ | |
445 | ufs_read(vp, uio, ioflag, cred) | |
446 | struct vnode *vp; | |
447 | register struct uio *uio; | |
448 | int ioflag; | |
449 | struct ucred *cred; | |
450 | { | |
451 | register struct inode *ip = VTOI(vp); | |
452 | register struct fs *fs; | |
453 | struct buf *bp; | |
454 | daddr_t lbn, bn, rablock; | |
20aa076b | 455 | int size, diff, error = 0; |
b373e060 KM |
456 | long n, on, type; |
457 | ||
458 | if (uio->uio_rw != UIO_READ) | |
459 | panic("ufs_read mode"); | |
460 | type = ip->i_mode & IFMT; | |
461 | if (type != IFDIR && type != IFREG && type != IFLNK) | |
462 | panic("ufs_read type"); | |
463 | if (uio->uio_resid == 0) | |
464 | return (0); | |
465 | if (uio->uio_offset < 0) | |
466 | return (EINVAL); | |
467 | ip->i_flag |= IACC; | |
468 | fs = ip->i_fs; | |
469 | do { | |
470 | lbn = lblkno(fs, uio->uio_offset); | |
471 | on = blkoff(fs, uio->uio_offset); | |
472 | n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); | |
473 | diff = ip->i_size - uio->uio_offset; | |
474 | if (diff <= 0) | |
475 | return (0); | |
476 | if (diff < n) | |
477 | n = diff; | |
b373e060 | 478 | size = blksize(fs, ip, lbn); |
e16fa59e | 479 | rablock = lbn + 1; |
20aa076b KM |
480 | if (vp->v_lastr + 1 == lbn && |
481 | lblktosize(fs, rablock) < ip->i_size) | |
482 | error = breada(ITOV(ip), lbn, size, rablock, | |
483 | blksize(fs, ip, rablock), NOCRED, &bp); | |
b373e060 | 484 | else |
e16fa59e | 485 | error = bread(ITOV(ip), lbn, size, NOCRED, &bp); |
de67eefc | 486 | vp->v_lastr = lbn; |
b373e060 KM |
487 | n = MIN(n, size - bp->b_resid); |
488 | if (error) { | |
489 | brelse(bp); | |
490 | return (error); | |
491 | } | |
492 | error = uiomove(bp->b_un.b_addr + on, (int)n, uio); | |
493 | if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size) | |
494 | bp->b_flags |= B_AGE; | |
495 | brelse(bp); | |
496 | } while (error == 0 && uio->uio_resid > 0 && n != 0); | |
497 | return (error); | |
498 | } | |
499 | ||
500 | /* | |
501 | * Vnode op for writing. | |
502 | */ | |
503 | ufs_write(vp, uio, ioflag, cred) | |
504 | register struct vnode *vp; | |
505 | struct uio *uio; | |
506 | int ioflag; | |
507 | struct ucred *cred; | |
508 | { | |
509 | register struct inode *ip = VTOI(vp); | |
510 | register struct fs *fs; | |
511 | struct buf *bp; | |
512 | daddr_t lbn, bn; | |
513 | u_long osize; | |
514 | int i, n, on, flags; | |
515 | int count, size, resid, error = 0; | |
516 | ||
517 | if (uio->uio_rw != UIO_WRITE) | |
518 | panic("ufs_write mode"); | |
519 | switch (vp->v_type) { | |
520 | case VREG: | |
521 | if (ioflag & IO_APPEND) | |
522 | uio->uio_offset = ip->i_size; | |
523 | /* fall through */ | |
524 | case VLNK: | |
525 | break; | |
526 | ||
527 | case VDIR: | |
528 | if ((ioflag & IO_SYNC) == 0) | |
529 | panic("ufs_write nonsync dir write"); | |
530 | break; | |
531 | ||
532 | default: | |
533 | panic("ufs_write type"); | |
534 | } | |
535 | if (uio->uio_offset < 0) | |
536 | return (EINVAL); | |
537 | if (uio->uio_resid == 0) | |
538 | return (0); | |
539 | /* | |
540 | * Maybe this should be above the vnode op call, but so long as | |
541 | * file servers have no limits, i don't think it matters | |
542 | */ | |
543 | if (vp->v_type == VREG && | |
544 | uio->uio_offset + uio->uio_resid > | |
545 | u.u_rlimit[RLIMIT_FSIZE].rlim_cur) { | |
546 | psignal(u.u_procp, SIGXFSZ); | |
547 | return (EFBIG); | |
548 | } | |
549 | resid = uio->uio_resid; | |
550 | osize = ip->i_size; | |
551 | fs = ip->i_fs; | |
e16fa59e KM |
552 | flags = 0; |
553 | if (ioflag & IO_SYNC) | |
554 | flags = B_SYNC; | |
b373e060 KM |
555 | do { |
556 | lbn = lblkno(fs, uio->uio_offset); | |
557 | on = blkoff(fs, uio->uio_offset); | |
558 | n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); | |
559 | if (n < fs->fs_bsize) | |
e16fa59e | 560 | flags |= B_CLRBUF; |
b373e060 | 561 | else |
e16fa59e KM |
562 | flags &= ~B_CLRBUF; |
563 | if (error = balloc(ip, lbn, (int)(on + n), &bp, flags)) | |
b373e060 | 564 | break; |
e16fa59e | 565 | bn = bp->b_blkno; |
b373e060 KM |
566 | if (uio->uio_offset + n > ip->i_size) |
567 | ip->i_size = uio->uio_offset + n; | |
568 | size = blksize(fs, ip, lbn); | |
569 | count = howmany(size, CLBYTES); | |
570 | for (i = 0; i < count; i++) | |
571 | munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); | |
b373e060 | 572 | n = MIN(n, size - bp->b_resid); |
b373e060 KM |
573 | error = uiomove(bp->b_un.b_addr + on, n, uio); |
574 | if (ioflag & IO_SYNC) | |
575 | (void) bwrite(bp); | |
576 | else if (n + on == fs->fs_bsize) { | |
577 | bp->b_flags |= B_AGE; | |
578 | bawrite(bp); | |
579 | } else | |
580 | bdwrite(bp); | |
581 | ip->i_flag |= IUPD|ICHG; | |
582 | if (cred->cr_uid != 0) | |
583 | ip->i_mode &= ~(ISUID|ISGID); | |
584 | } while (error == 0 && uio->uio_resid > 0 && n != 0); | |
585 | if (error && (ioflag & IO_UNIT)) { | |
e16fa59e | 586 | (void) itrunc(ip, osize, ioflag & IO_SYNC); |
b373e060 KM |
587 | uio->uio_offset -= resid - uio->uio_resid; |
588 | uio->uio_resid = resid; | |
589 | } | |
590 | return (error); | |
591 | } | |
592 | ||
7188ac27 KM |
593 | /* ARGSUSED */ |
594 | ufs_ioctl(vp, com, data, fflag, cred) | |
595 | struct vnode *vp; | |
596 | int com; | |
597 | caddr_t data; | |
598 | int fflag; | |
599 | struct ucred *cred; | |
bb1b75f4 | 600 | { |
bb1b75f4 | 601 | |
7188ac27 KM |
602 | return (ENOTTY); |
603 | } | |
604 | ||
605 | /* ARGSUSED */ | |
606 | ufs_select(vp, which, cred) | |
607 | struct vnode *vp; | |
608 | int which; | |
609 | struct ucred *cred; | |
610 | { | |
611 | ||
7188ac27 | 612 | return (1); /* XXX */ |
bb1b75f4 | 613 | } |
d67a03eb | 614 | |
4f083fd7 | 615 | /* |
7188ac27 KM |
616 | * Mmap a file |
617 | * | |
618 | * NB Currently unsupported. | |
4f083fd7 | 619 | */ |
7188ac27 KM |
620 | /* ARGSUSED */ |
621 | ufs_mmap(vp, fflags, cred) | |
622 | struct vnode *vp; | |
623 | int fflags; | |
624 | struct ucred *cred; | |
d67a03eb | 625 | { |
d67a03eb | 626 | |
7188ac27 | 627 | return (EINVAL); |
d67a03eb | 628 | } |
64d3a787 | 629 | |
4f083fd7 | 630 | /* |
7188ac27 | 631 | * Synch an open file. |
4f083fd7 | 632 | */ |
7188ac27 | 633 | /* ARGSUSED */ |
86cdabf6 | 634 | ufs_fsync(vp, fflags, cred, waitfor) |
7188ac27 KM |
635 | struct vnode *vp; |
636 | int fflags; | |
637 | struct ucred *cred; | |
86cdabf6 | 638 | int waitfor; |
528f664c | 639 | { |
86cdabf6 | 640 | struct inode *ip = VTOI(vp); |
7188ac27 | 641 | |
7188ac27 KM |
642 | if (fflags&FWRITE) |
643 | ip->i_flag |= ICHG; | |
e16fa59e KM |
644 | vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); |
645 | return (iupdat(ip, &time, &time, waitfor == MNT_WAIT)); | |
528f664c SL |
646 | } |
647 | ||
4f083fd7 | 648 | /* |
7188ac27 KM |
649 | * Seek on a file |
650 | * | |
651 | * Nothing to do, so just return. | |
4f083fd7 | 652 | */ |
7188ac27 KM |
653 | /* ARGSUSED */ |
654 | ufs_seek(vp, oldoff, newoff, cred) | |
655 | struct vnode *vp; | |
656 | off_t oldoff, newoff; | |
657 | struct ucred *cred; | |
528f664c | 658 | { |
7188ac27 KM |
659 | |
660 | return (0); | |
661 | } | |
662 | ||
663 | /* | |
664 | * ufs remove | |
665 | * Hard to avoid races here, especially | |
666 | * in unlinking directories. | |
667 | */ | |
668 | ufs_remove(ndp) | |
669 | struct nameidata *ndp; | |
670 | { | |
671 | register struct inode *ip, *dp; | |
672 | int error; | |
673 | ||
674 | ip = VTOI(ndp->ni_vp); | |
675 | dp = VTOI(ndp->ni_dvp); | |
676 | error = dirremove(ndp); | |
677 | if (!error) { | |
678 | ip->i_nlink--; | |
679 | ip->i_flag |= ICHG; | |
528f664c | 680 | } |
7188ac27 KM |
681 | if (dp == ip) |
682 | vrele(ITOV(ip)); | |
683 | else | |
684 | iput(ip); | |
685 | iput(dp); | |
686 | return (error); | |
4f083fd7 SL |
687 | } |
688 | ||
689 | /* | |
7188ac27 | 690 | * link vnode call |
4f083fd7 | 691 | */ |
7188ac27 KM |
692 | ufs_link(vp, ndp) |
693 | register struct vnode *vp; | |
694 | register struct nameidata *ndp; | |
4f083fd7 | 695 | { |
7188ac27 KM |
696 | register struct inode *ip = VTOI(vp); |
697 | int error; | |
4f083fd7 | 698 | |
7188ac27 KM |
699 | if (ndp->ni_dvp != vp) |
700 | ILOCK(ip); | |
701 | if (ip->i_nlink == LINK_MAX - 1) { | |
702 | error = EMLINK; | |
703 | goto out; | |
704 | } | |
705 | ip->i_nlink++; | |
706 | ip->i_flag |= ICHG; | |
707 | error = iupdat(ip, &time, &time, 1); | |
708 | if (!error) | |
709 | error = direnter(ip, ndp); | |
710 | out: | |
711 | if (ndp->ni_dvp != vp) | |
712 | IUNLOCK(ip); | |
713 | if (error) { | |
714 | ip->i_nlink--; | |
82252d2b | 715 | ip->i_flag |= ICHG; |
7188ac27 KM |
716 | } |
717 | return (error); | |
528f664c SL |
718 | } |
719 | ||
4f083fd7 SL |
720 | /* |
721 | * Rename system call. | |
722 | * rename("foo", "bar"); | |
723 | * is essentially | |
724 | * unlink("bar"); | |
725 | * link("foo", "bar"); | |
726 | * unlink("foo"); | |
727 | * but ``atomically''. Can't do full commit without saving state in the | |
728 | * inode on disk which isn't feasible at this time. Best we can do is | |
729 | * always guarantee the target exists. | |
730 | * | |
731 | * Basic algorithm is: | |
732 | * | |
733 | * 1) Bump link count on source while we're linking it to the | |
7188ac27 | 734 | * target. This also ensure the inode won't be deleted out |
68f21562 KM |
735 | * from underneath us while we work (it may be truncated by |
736 | * a concurrent `trunc' or `open' for creation). | |
4f083fd7 SL |
737 | * 2) Link source to destination. If destination already exists, |
738 | * delete it first. | |
68f21562 KM |
739 | * 3) Unlink source reference to inode if still around. If a |
740 | * directory was moved and the parent of the destination | |
4f083fd7 SL |
741 | * is different from the source, patch the ".." entry in the |
742 | * directory. | |
4f083fd7 | 743 | */ |
7188ac27 KM |
744 | ufs_rename(fndp, tndp) |
745 | register struct nameidata *fndp, *tndp; | |
528f664c | 746 | { |
4f083fd7 | 747 | register struct inode *ip, *xp, *dp; |
68f21562 KM |
748 | struct dirtemplate dirbuf; |
749 | int doingdirectory = 0, oldparent = 0, newparent = 0; | |
a5390dce | 750 | int error = 0; |
4f083fd7 | 751 | |
7188ac27 KM |
752 | dp = VTOI(fndp->ni_dvp); |
753 | ip = VTOI(fndp->ni_vp); | |
754 | ILOCK(ip); | |
4f083fd7 | 755 | if ((ip->i_mode&IFMT) == IFDIR) { |
7188ac27 | 756 | register struct direct *d = &fndp->ni_dent; |
4f083fd7 | 757 | |
4f083fd7 | 758 | /* |
046f18d1 | 759 | * Avoid ".", "..", and aliases of "." for obvious reasons. |
4f083fd7 | 760 | */ |
7188ac27 KM |
761 | if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || |
762 | fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { | |
763 | IUNLOCK(ip); | |
764 | ufs_abortop(fndp); | |
765 | ufs_abortop(tndp); | |
766 | return (EINVAL); | |
4f083fd7 | 767 | } |
68f21562 | 768 | ip->i_flag |= IRENAME; |
4f083fd7 SL |
769 | oldparent = dp->i_number; |
770 | doingdirectory++; | |
771 | } | |
7188ac27 | 772 | vrele(fndp->ni_dvp); |
4f083fd7 SL |
773 | |
774 | /* | |
775 | * 1) Bump link count while we're moving stuff | |
776 | * around. If we crash somewhere before | |
777 | * completing our work, the link count | |
778 | * may be wrong, but correctable. | |
779 | */ | |
780 | ip->i_nlink++; | |
781 | ip->i_flag |= ICHG; | |
7188ac27 | 782 | error = iupdat(ip, &time, &time, 1); |
a388503d | 783 | IUNLOCK(ip); |
4f083fd7 SL |
784 | |
785 | /* | |
786 | * When the target exists, both the directory | |
7188ac27 | 787 | * and target vnodes are returned locked. |
4f083fd7 | 788 | */ |
7188ac27 KM |
789 | dp = VTOI(tndp->ni_dvp); |
790 | xp = NULL; | |
791 | if (tndp->ni_vp) | |
792 | xp = VTOI(tndp->ni_vp); | |
046f18d1 SL |
793 | /* |
794 | * If ".." must be changed (ie the directory gets a new | |
81552f0f KM |
795 | * parent) then the source directory must not be in the |
796 | * directory heirarchy above the target, as this would | |
797 | * orphan everything below the source directory. Also | |
798 | * the user must have write permission in the source so | |
799 | * as to be able to change "..". We must repeat the call | |
800 | * to namei, as the parent directory is unlocked by the | |
801 | * call to checkpath(). | |
046f18d1 | 802 | */ |
68f21562 KM |
803 | if (oldparent != dp->i_number) |
804 | newparent = dp->i_number; | |
805 | if (doingdirectory && newparent) { | |
7188ac27 | 806 | if (error = iaccess(ip, IWRITE, tndp->ni_cred)) |
81552f0f | 807 | goto bad; |
7188ac27 | 808 | tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; |
81552f0f | 809 | do { |
7188ac27 | 810 | dp = VTOI(tndp->ni_dvp); |
81552f0f | 811 | if (xp != NULL) |
79cf21e2 | 812 | iput(xp); |
7188ac27 | 813 | if (error = checkpath(ip, dp, tndp->ni_cred)) |
81552f0f | 814 | goto out; |
7188ac27 | 815 | if (error = namei(tndp)) |
81552f0f | 816 | goto out; |
7188ac27 KM |
817 | xp = NULL; |
818 | if (tndp->ni_vp) | |
819 | xp = VTOI(tndp->ni_vp); | |
820 | } while (dp != VTOI(tndp->ni_dvp)); | |
81552f0f | 821 | } |
4f083fd7 SL |
822 | /* |
823 | * 2) If target doesn't exist, link the target | |
824 | * to the source and unlink the source. | |
825 | * Otherwise, rewrite the target directory | |
826 | * entry to reference the source inode and | |
827 | * expunge the original entry's existence. | |
828 | */ | |
4f083fd7 | 829 | if (xp == NULL) { |
7188ac27 KM |
830 | if (dp->i_dev != ip->i_dev) |
831 | panic("rename: EXDEV"); | |
4f083fd7 | 832 | /* |
68f21562 KM |
833 | * Account for ".." in new directory. |
834 | * When source and destination have the same | |
835 | * parent we don't fool with the link count. | |
4f083fd7 | 836 | */ |
68f21562 | 837 | if (doingdirectory && newparent) { |
4f083fd7 SL |
838 | dp->i_nlink++; |
839 | dp->i_flag |= ICHG; | |
7188ac27 | 840 | error = iupdat(dp, &time, &time, 1); |
4f083fd7 | 841 | } |
7188ac27 | 842 | if (error = direnter(ip, tndp)) |
4f083fd7 SL |
843 | goto out; |
844 | } else { | |
7188ac27 KM |
845 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) |
846 | panic("rename: EXDEV"); | |
e69c3c9c SL |
847 | /* |
848 | * Short circuit rename(foo, foo). | |
849 | */ | |
850 | if (xp->i_number == ip->i_number) | |
7188ac27 | 851 | panic("rename: same file"); |
80cee150 JB |
852 | /* |
853 | * If the parent directory is "sticky", then the user must | |
854 | * own the parent directory, or the destination of the rename, | |
855 | * otherwise the destination may not be changed (except by | |
856 | * root). This implements append-only directories. | |
857 | */ | |
7188ac27 KM |
858 | if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && |
859 | tndp->ni_cred->cr_uid != dp->i_uid && | |
860 | xp->i_uid != tndp->ni_cred->cr_uid) { | |
80cee150 JB |
861 | error = EPERM; |
862 | goto bad; | |
863 | } | |
4f083fd7 | 864 | /* |
a5390dce SL |
865 | * Target must be empty if a directory |
866 | * and have no links to it. | |
4f083fd7 SL |
867 | * Also, insure source and target are |
868 | * compatible (both directories, or both | |
869 | * not directories). | |
870 | */ | |
871 | if ((xp->i_mode&IFMT) == IFDIR) { | |
7188ac27 KM |
872 | if (!dirempty(xp, dp->i_number, tndp->ni_cred) || |
873 | xp->i_nlink > 2) { | |
a5390dce | 874 | error = ENOTEMPTY; |
4f083fd7 SL |
875 | goto bad; |
876 | } | |
877 | if (!doingdirectory) { | |
a5390dce | 878 | error = ENOTDIR; |
4f083fd7 SL |
879 | goto bad; |
880 | } | |
7188ac27 | 881 | cache_purge(ITOV(dp)); |
4f083fd7 | 882 | } else if (doingdirectory) { |
a5390dce | 883 | error = EISDIR; |
4f083fd7 SL |
884 | goto bad; |
885 | } | |
7188ac27 KM |
886 | if (error = dirrewrite(dp, ip, tndp)) |
887 | goto bad; | |
888 | vput(ITOV(dp)); | |
4f083fd7 | 889 | /* |
a5390dce SL |
890 | * Adjust the link count of the target to |
891 | * reflect the dirrewrite above. If this is | |
892 | * a directory it is empty and there are | |
893 | * no links to it, so we can squash the inode and | |
894 | * any space associated with it. We disallowed | |
895 | * renaming over top of a directory with links to | |
68f21562 KM |
896 | * it above, as the remaining link would point to |
897 | * a directory without "." or ".." entries. | |
4f083fd7 | 898 | */ |
a5390dce | 899 | xp->i_nlink--; |
4f083fd7 | 900 | if (doingdirectory) { |
a5390dce SL |
901 | if (--xp->i_nlink != 0) |
902 | panic("rename: linked directory"); | |
e16fa59e | 903 | error = itrunc(xp, (u_long)0, IO_SYNC); |
a5390dce | 904 | } |
4f083fd7 | 905 | xp->i_flag |= ICHG; |
88d931ba | 906 | iput(xp); |
31db12cb | 907 | xp = NULL; |
4f083fd7 SL |
908 | } |
909 | ||
910 | /* | |
911 | * 3) Unlink the source. | |
912 | */ | |
7188ac27 KM |
913 | fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; |
914 | (void)namei(fndp); | |
915 | if (fndp->ni_vp != NULL) { | |
916 | xp = VTOI(fndp->ni_vp); | |
917 | dp = VTOI(fndp->ni_dvp); | |
918 | } else { | |
79cf21e2 KM |
919 | if (fndp->ni_dvp != NULL) |
920 | vput(fndp->ni_dvp); | |
7188ac27 | 921 | xp = NULL; |
4f1a9037 | 922 | dp = NULL; |
7188ac27 | 923 | } |
4f083fd7 | 924 | /* |
7188ac27 | 925 | * Ensure that the directory entry still exists and has not |
68f21562 KM |
926 | * changed while the new name has been entered. If the source is |
927 | * a file then the entry may have been unlinked or renamed. In | |
928 | * either case there is no further work to be done. If the source | |
929 | * is a directory then it cannot have been rmdir'ed; its link | |
930 | * count of three would cause a rmdir to fail with ENOTEMPTY. | |
7188ac27 | 931 | * The IRENAME flag ensures that it cannot be moved by another |
68f21562 | 932 | * rename. |
4f083fd7 | 933 | */ |
4f1a9037 | 934 | if (xp != ip) { |
68f21562 | 935 | if (doingdirectory) |
4f1a9037 | 936 | panic("rename: lost dir entry"); |
68f21562 | 937 | } else { |
4f083fd7 | 938 | /* |
68f21562 KM |
939 | * If the source is a directory with a |
940 | * new parent, the link count of the old | |
941 | * parent directory must be decremented | |
942 | * and ".." set to point to the new parent. | |
4f083fd7 | 943 | */ |
68f21562 | 944 | if (doingdirectory && newparent) { |
4f083fd7 SL |
945 | dp->i_nlink--; |
946 | dp->i_flag |= ICHG; | |
86cdabf6 | 947 | error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, |
7188ac27 | 948 | sizeof (struct dirtemplate), (off_t)0, |
86cdabf6 KM |
949 | UIO_SYSSPACE, IO_NODELOCKED, |
950 | tndp->ni_cred, (int *)0); | |
68f21562 KM |
951 | if (error == 0) { |
952 | if (dirbuf.dotdot_namlen != 2 || | |
953 | dirbuf.dotdot_name[0] != '.' || | |
954 | dirbuf.dotdot_name[1] != '.') { | |
8ad54d9e | 955 | dirbad(xp, 12, "rename: mangled dir"); |
68f21562 KM |
956 | } else { |
957 | dirbuf.dotdot_ino = newparent; | |
86cdabf6 | 958 | (void) vn_rdwr(UIO_WRITE, ITOV(xp), |
68f21562 KM |
959 | (caddr_t)&dirbuf, |
960 | sizeof (struct dirtemplate), | |
93e273b9 | 961 | (off_t)0, UIO_SYSSPACE, |
86cdabf6 | 962 | IO_NODELOCKED|IO_SYNC, |
7188ac27 KM |
963 | tndp->ni_cred, (int *)0); |
964 | cache_purge(ITOV(dp)); | |
68f21562 KM |
965 | } |
966 | } | |
4f083fd7 | 967 | } |
7188ac27 KM |
968 | error = dirremove(fndp); |
969 | if (!error) { | |
68f21562 KM |
970 | xp->i_nlink--; |
971 | xp->i_flag |= ICHG; | |
4f083fd7 | 972 | } |
68f21562 | 973 | xp->i_flag &= ~IRENAME; |
4f083fd7 | 974 | } |
4f083fd7 | 975 | if (dp) |
7188ac27 | 976 | vput(ITOV(dp)); |
68f21562 | 977 | if (xp) |
7188ac27 KM |
978 | vput(ITOV(xp)); |
979 | vrele(ITOV(ip)); | |
980 | return (error); | |
a5390dce | 981 | |
4f083fd7 | 982 | bad: |
4f083fd7 | 983 | if (xp) |
7188ac27 KM |
984 | vput(ITOV(xp)); |
985 | vput(ITOV(dp)); | |
4f083fd7 SL |
986 | out: |
987 | ip->i_nlink--; | |
988 | ip->i_flag |= ICHG; | |
7188ac27 KM |
989 | vrele(ITOV(ip)); |
990 | return (error); | |
64d3a787 | 991 | } |
88a7a62a SL |
992 | |
993 | /* | |
994 | * A virgin directory (no blushing please). | |
995 | */ | |
996 | struct dirtemplate mastertemplate = { | |
997 | 0, 12, 1, ".", | |
998 | 0, DIRBLKSIZ - 12, 2, ".." | |
999 | }; | |
1000 | ||
1001 | /* | |
1002 | * Mkdir system call | |
1003 | */ | |
7188ac27 KM |
1004 | ufs_mkdir(ndp, vap) |
1005 | struct nameidata *ndp; | |
1006 | struct vattr *vap; | |
88a7a62a | 1007 | { |
88a7a62a | 1008 | register struct inode *ip, *dp; |
7188ac27 KM |
1009 | struct inode *tip; |
1010 | struct vnode *dvp; | |
88a7a62a | 1011 | struct dirtemplate dirtemplate; |
7188ac27 KM |
1012 | int error; |
1013 | int dmode; | |
1014 | ||
1015 | dvp = ndp->ni_dvp; | |
1016 | dp = VTOI(dvp); | |
1017 | dmode = vap->va_mode&0777; | |
1018 | dmode |= IFDIR; | |
88a7a62a SL |
1019 | /* |
1020 | * Must simulate part of maknode here | |
1021 | * in order to acquire the inode, but | |
1022 | * not have it entered in the parent | |
1023 | * directory. The entry is made later | |
1024 | * after writing "." and ".." entries out. | |
1025 | */ | |
7188ac27 KM |
1026 | error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip); |
1027 | if (error) { | |
88a7a62a | 1028 | iput(dp); |
7188ac27 | 1029 | return (error); |
88a7a62a | 1030 | } |
7188ac27 | 1031 | ip = tip; |
88a7a62a SL |
1032 | #ifdef QUOTA |
1033 | if (ip->i_dquot != NODQUOT) | |
1034 | panic("mkdir: dquot"); | |
1035 | #endif | |
1036 | ip->i_flag |= IACC|IUPD|ICHG; | |
7188ac27 KM |
1037 | ip->i_mode = dmode; |
1038 | ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ | |
88a7a62a | 1039 | ip->i_nlink = 2; |
7188ac27 | 1040 | ip->i_uid = ndp->ni_cred->cr_uid; |
88a7a62a SL |
1041 | ip->i_gid = dp->i_gid; |
1042 | #ifdef QUOTA | |
1043 | ip->i_dquot = inoquota(ip); | |
1044 | #endif | |
7188ac27 | 1045 | error = iupdat(ip, &time, &time, 1); |
88a7a62a SL |
1046 | |
1047 | /* | |
1048 | * Bump link count in parent directory | |
1049 | * to reflect work done below. Should | |
1050 | * be done before reference is created | |
1051 | * so reparation is possible if we crash. | |
1052 | */ | |
1053 | dp->i_nlink++; | |
1054 | dp->i_flag |= ICHG; | |
7188ac27 | 1055 | error = iupdat(dp, &time, &time, 1); |
88a7a62a SL |
1056 | |
1057 | /* | |
1058 | * Initialize directory with "." | |
1059 | * and ".." from static template. | |
1060 | */ | |
1061 | dirtemplate = mastertemplate; | |
1062 | dirtemplate.dot_ino = ip->i_number; | |
1063 | dirtemplate.dotdot_ino = dp->i_number; | |
86cdabf6 | 1064 | error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, |
7188ac27 | 1065 | sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, |
86cdabf6 | 1066 | IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); |
7188ac27 | 1067 | if (error) { |
88a7a62a SL |
1068 | dp->i_nlink--; |
1069 | dp->i_flag |= ICHG; | |
1070 | goto bad; | |
1071 | } | |
7188ac27 KM |
1072 | if (DIRBLKSIZ > dp->i_fs->fs_fsize) |
1073 | panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ | |
23de9f20 KM |
1074 | else |
1075 | ip->i_size = DIRBLKSIZ; | |
88a7a62a SL |
1076 | /* |
1077 | * Directory all set up, now | |
1078 | * install the entry for it in | |
1079 | * the parent directory. | |
1080 | */ | |
7188ac27 | 1081 | error = direnter(ip, ndp); |
88a7a62a | 1082 | dp = NULL; |
7188ac27 | 1083 | if (error) { |
715baff1 | 1084 | ndp->ni_nameiop = LOOKUP | NOCACHE; |
7188ac27 KM |
1085 | error = namei(ndp); |
1086 | if (!error) { | |
1087 | dp = VTOI(ndp->ni_vp); | |
88a7a62a SL |
1088 | dp->i_nlink--; |
1089 | dp->i_flag |= ICHG; | |
1090 | } | |
1091 | } | |
1092 | bad: | |
1093 | /* | |
1094 | * No need to do an explicit itrunc here, | |
7188ac27 | 1095 | * vrele will do this for us because we set |
88a7a62a SL |
1096 | * the link count to 0. |
1097 | */ | |
7188ac27 | 1098 | if (error) { |
88a7a62a SL |
1099 | ip->i_nlink = 0; |
1100 | ip->i_flag |= ICHG; | |
cbcdacd6 KM |
1101 | iput(ip); |
1102 | } else | |
1103 | ndp->ni_vp = ITOV(ip); | |
88a7a62a SL |
1104 | if (dp) |
1105 | iput(dp); | |
7188ac27 | 1106 | return (error); |
88a7a62a SL |
1107 | } |
1108 | ||
1109 | /* | |
1110 | * Rmdir system call. | |
1111 | */ | |
7188ac27 KM |
1112 | ufs_rmdir(ndp) |
1113 | register struct nameidata *ndp; | |
88a7a62a | 1114 | { |
88a7a62a | 1115 | register struct inode *ip, *dp; |
7188ac27 KM |
1116 | int error = 0; |
1117 | ||
1118 | ip = VTOI(ndp->ni_vp); | |
1119 | dp = VTOI(ndp->ni_dvp); | |
88a7a62a SL |
1120 | /* |
1121 | * No rmdir "." please. | |
1122 | */ | |
1123 | if (dp == ip) { | |
7188ac27 | 1124 | vrele(ITOV(dp)); |
88a7a62a | 1125 | iput(ip); |
7188ac27 | 1126 | return (EINVAL); |
88a7a62a SL |
1127 | } |
1128 | /* | |
1129 | * Verify the directory is empty (and valid). | |
1130 | * (Rmdir ".." won't be valid since | |
1131 | * ".." will contain a reference to | |
1132 | * the current directory and thus be | |
1133 | * non-empty.) | |
1134 | */ | |
7188ac27 KM |
1135 | if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { |
1136 | error = ENOTEMPTY; | |
88a7a62a SL |
1137 | goto out; |
1138 | } | |
1139 | /* | |
1140 | * Delete reference to directory before purging | |
1141 | * inode. If we crash in between, the directory | |
1142 | * will be reattached to lost+found, | |
1143 | */ | |
7188ac27 | 1144 | if (error = dirremove(ndp)) |
88a7a62a SL |
1145 | goto out; |
1146 | dp->i_nlink--; | |
1147 | dp->i_flag |= ICHG; | |
7188ac27 | 1148 | cache_purge(ITOV(dp)); |
88a7a62a | 1149 | iput(dp); |
7188ac27 | 1150 | ndp->ni_dvp = NULL; |
88a7a62a SL |
1151 | /* |
1152 | * Truncate inode. The only stuff left | |
1153 | * in the directory is "." and "..". The | |
1154 | * "." reference is inconsequential since | |
1155 | * we're quashing it. The ".." reference | |
1156 | * has already been adjusted above. We've | |
1157 | * removed the "." reference and the reference | |
1158 | * in the parent directory, but there may be | |
1159 | * other hard links so decrement by 2 and | |
1160 | * worry about them later. | |
1161 | */ | |
1162 | ip->i_nlink -= 2; | |
e16fa59e | 1163 | error = itrunc(ip, (u_long)0, IO_SYNC); |
7188ac27 | 1164 | cache_purge(ITOV(ip)); |
88a7a62a | 1165 | out: |
7188ac27 | 1166 | if (ndp->ni_dvp) |
88a7a62a SL |
1167 | iput(dp); |
1168 | iput(ip); | |
7188ac27 | 1169 | return (error); |
88a7a62a SL |
1170 | } |
1171 | ||
7188ac27 KM |
1172 | /* |
1173 | * symlink -- make a symbolic link | |
1174 | */ | |
1175 | ufs_symlink(ndp, vap, target) | |
1176 | struct nameidata *ndp; | |
1177 | struct vattr *vap; | |
1178 | char *target; | |
1179 | { | |
1180 | struct inode *ip; | |
1181 | int error; | |
1182 | ||
1183 | error = maknode(IFLNK | vap->va_mode, ndp, &ip); | |
1184 | if (error) | |
1185 | return (error); | |
86cdabf6 KM |
1186 | error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, |
1187 | UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); | |
7188ac27 KM |
1188 | iput(ip); |
1189 | return (error); | |
1190 | } | |
1191 | ||
1192 | /* | |
1193 | * Vnode op for read and write | |
1194 | */ | |
86cdabf6 | 1195 | ufs_readdir(vp, uio, cred) |
7188ac27 KM |
1196 | struct vnode *vp; |
1197 | register struct uio *uio; | |
7188ac27 | 1198 | struct ucred *cred; |
88a7a62a | 1199 | { |
86cdabf6 | 1200 | int count, lost, error; |
88a7a62a | 1201 | |
7188ac27 KM |
1202 | count = uio->uio_resid; |
1203 | count &= ~(DIRBLKSIZ - 1); | |
86cdabf6 KM |
1204 | lost = uio->uio_resid - count; |
1205 | if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) | |
7188ac27 | 1206 | return (EINVAL); |
7188ac27 KM |
1207 | uio->uio_resid = count; |
1208 | uio->uio_iov->iov_len = count; | |
86cdabf6 KM |
1209 | error = ufs_read(vp, uio, 0, cred); |
1210 | uio->uio_resid += lost; | |
7188ac27 KM |
1211 | return (error); |
1212 | } | |
1213 | ||
1214 | /* | |
1215 | * Return target name of a symbolic link | |
1216 | */ | |
1217 | ufs_readlink(vp, uiop, cred) | |
1218 | struct vnode *vp; | |
1219 | struct uio *uiop; | |
1220 | struct ucred *cred; | |
1221 | { | |
1222 | ||
86cdabf6 | 1223 | return (ufs_read(vp, uiop, 0, cred)); |
7188ac27 KM |
1224 | } |
1225 | ||
1226 | /* | |
1227 | * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually | |
1228 | * done. Iff ni_vp/ni_dvp not null and locked, unlock. | |
1229 | */ | |
1230 | ufs_abortop(ndp) | |
1231 | register struct nameidata *ndp; | |
1232 | { | |
1233 | register struct inode *ip; | |
1234 | ||
1235 | if (ndp->ni_vp) { | |
1236 | ip = VTOI(ndp->ni_vp); | |
1237 | if (ip->i_flag & ILOCKED) | |
1238 | IUNLOCK(ip); | |
1239 | vrele(ndp->ni_vp); | |
8462a185 | 1240 | } |
7188ac27 KM |
1241 | if (ndp->ni_dvp) { |
1242 | ip = VTOI(ndp->ni_dvp); | |
1243 | if (ip->i_flag & ILOCKED) | |
1244 | IUNLOCK(ip); | |
1245 | vrele(ndp->ni_dvp); | |
88a7a62a | 1246 | } |
7188ac27 KM |
1247 | return; |
1248 | } | |
1249 | ||
1250 | ufs_lock(vp) | |
1251 | struct vnode *vp; | |
1252 | { | |
1253 | register struct inode *ip = VTOI(vp); | |
1254 | ||
1255 | ILOCK(ip); | |
1256 | return (0); | |
1257 | } | |
1258 | ||
1259 | ufs_unlock(vp) | |
1260 | struct vnode *vp; | |
1261 | { | |
1262 | register struct inode *ip = VTOI(vp); | |
1263 | ||
1264 | if (!(ip->i_flag & ILOCKED)) | |
1265 | panic("ufs_unlock NOT LOCKED"); | |
1266 | IUNLOCK(ip); | |
1267 | return (0); | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * Get access to bmap | |
1272 | */ | |
1273 | ufs_bmap(vp, bn, vpp, bnp) | |
1274 | struct vnode *vp; | |
1275 | daddr_t bn; | |
1276 | struct vnode **vpp; | |
1277 | daddr_t *bnp; | |
1278 | { | |
1279 | struct inode *ip = VTOI(vp); | |
1280 | ||
1281 | if (vpp != NULL) | |
1282 | *vpp = ip->i_devvp; | |
1283 | if (bnp == NULL) | |
1284 | return (0); | |
1285 | return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0)); | |
88a7a62a SL |
1286 | } |
1287 | ||
1288 | /* | |
7188ac27 | 1289 | * Just call the device strategy routine |
88a7a62a | 1290 | */ |
e16fa59e KM |
1291 | int checkoverlap = 1; |
1292 | ||
7188ac27 KM |
1293 | ufs_strategy(bp) |
1294 | register struct buf *bp; | |
88a7a62a | 1295 | { |
e16fa59e KM |
1296 | register struct inode *ip = VTOI(bp->b_vp); |
1297 | register struct buf *ep; | |
1298 | struct vnode *vp; | |
1299 | struct buf *ebp; | |
1300 | daddr_t start, last; | |
1301 | int error; | |
1302 | ||
1303 | if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) | |
1304 | panic("ufs_strategy: spec"); | |
1305 | if (bp->b_blkno == bp->b_lblkno) { | |
1306 | if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) | |
1307 | return (error); | |
20aa076b | 1308 | if ((long)bp->b_blkno == -1) |
e16fa59e | 1309 | clrbuf(bp); |
e16fa59e | 1310 | } |
20aa076b KM |
1311 | if ((long)bp->b_blkno == -1) { |
1312 | biodone(bp); | |
e16fa59e | 1313 | return (0); |
20aa076b | 1314 | } |
e16fa59e KM |
1315 | if (checkoverlap) { |
1316 | ebp = &buf[nbuf]; | |
1317 | start = bp->b_blkno; | |
1318 | last = start + btodb(bp->b_bcount) - 1; | |
1319 | for (ep = buf; ep < ebp; ep++) { | |
1320 | if (ep == bp || (ep->b_flags & B_INVAL) || | |
1321 | ep->b_vp == (struct vnode *)0) | |
1322 | continue; | |
1323 | if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) | |
1324 | continue; | |
1325 | if (vp != ip->i_devvp) | |
1326 | continue; | |
1327 | /* look for overlap */ | |
1328 | if (ep->b_bcount == 0 || ep->b_blkno > last || | |
1329 | ep->b_blkno + btodb(ep->b_bcount) <= start) | |
1330 | continue; | |
20aa076b KM |
1331 | vprint("Disk overlap", vp); |
1332 | printf("\tstart %d, end %d overlap start %d, end %d\n", | |
1333 | start, last, ep->b_blkno, | |
1334 | ep->b_blkno + btodb(ep->b_bcount) - 1); | |
e16fa59e KM |
1335 | } |
1336 | } | |
1337 | vp = ip->i_devvp; | |
1338 | bp->b_dev = vp->v_rdev; | |
1339 | (*(vp->v_op->vn_strategy))(bp); | |
7188ac27 KM |
1340 | return (0); |
1341 | } | |
88a7a62a | 1342 | |
e16fa59e KM |
1343 | /* |
1344 | * Print out the contents of an inode. | |
1345 | */ | |
1346 | ufs_print(vp) | |
1347 | struct vnode *vp; | |
1348 | { | |
1349 | register struct inode *ip = VTOI(vp); | |
1350 | ||
1351 | printf("tag VT_UFS, ino %d, on dev %d, %d%s\n", ip->i_number, | |
1352 | major(ip->i_dev), minor(ip->i_dev), | |
1353 | (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); | |
96bae3e0 KM |
1354 | if (ip->i_spare0 == 0) |
1355 | return; | |
1356 | printf("\towner pid %d", ip->i_spare0); | |
1357 | if (ip->i_spare1) | |
1358 | printf(" waiting pid %d", ip->i_spare1); | |
1359 | printf("\n"); | |
e16fa59e KM |
1360 | } |
1361 | ||
24a31b70 KM |
1362 | /* |
1363 | * Read wrapper for special devices. | |
1364 | */ | |
1365 | ufsspec_read(vp, uio, ioflag, cred) | |
1366 | struct vnode *vp; | |
1367 | struct uio *uio; | |
1368 | int ioflag; | |
1369 | struct ucred *cred; | |
1370 | { | |
1371 | ||
1372 | /* | |
1373 | * Set access flag. | |
1374 | */ | |
1375 | VTOI(vp)->i_flag |= IACC; | |
1376 | return (spec_read(vp, uio, ioflag, cred)); | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * Write wrapper for special devices. | |
1381 | */ | |
1382 | ufsspec_write(vp, uio, ioflag, cred) | |
1383 | struct vnode *vp; | |
1384 | struct uio *uio; | |
1385 | int ioflag; | |
1386 | struct ucred *cred; | |
1387 | { | |
1388 | ||
1389 | /* | |
1390 | * Set update and change flags. | |
1391 | */ | |
1392 | VTOI(vp)->i_flag |= IUPD|ICHG; | |
1393 | return (spec_write(vp, uio, ioflag, cred)); | |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * Close wrapper for special devices. | |
1398 | * | |
1399 | * Update the times on the inode then do device close. | |
1400 | */ | |
1401 | ufsspec_close(vp, fflag, cred) | |
1402 | struct vnode *vp; | |
1403 | int fflag; | |
1404 | struct ucred *cred; | |
1405 | { | |
1406 | register struct inode *ip = VTOI(vp); | |
1407 | ||
de67eefc | 1408 | if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) |
24a31b70 KM |
1409 | ITIMES(ip, &time, &time); |
1410 | return (spec_close(vp, fflag, cred)); | |
1411 | } | |
1412 | ||
7188ac27 KM |
1413 | /* |
1414 | * Make a new file. | |
1415 | */ | |
1416 | maknode(mode, ndp, ipp) | |
1417 | int mode; | |
1418 | register struct nameidata *ndp; | |
1419 | struct inode **ipp; | |
1420 | { | |
1421 | register struct inode *ip; | |
1422 | struct inode *tip; | |
1423 | register struct inode *pdir = VTOI(ndp->ni_dvp); | |
1424 | ino_t ipref; | |
1425 | int error; | |
1426 | ||
1427 | *ipp = 0; | |
1428 | if ((mode & IFMT) == IFDIR) | |
1429 | ipref = dirpref(pdir->i_fs); | |
1430 | else | |
1431 | ipref = pdir->i_number; | |
1432 | error = ialloc(pdir, ipref, mode, &tip); | |
1433 | if (error) { | |
1434 | iput(pdir); | |
1435 | return (error); | |
1436 | } | |
1437 | ip = tip; | |
1438 | #ifdef QUOTA | |
1439 | if (ip->i_dquot != NODQUOT) | |
1440 | panic("maknode: dquot"); | |
1441 | #endif | |
1442 | ip->i_flag |= IACC|IUPD|ICHG; | |
1443 | if ((mode & IFMT) == 0) | |
1444 | mode |= IFREG; | |
1445 | ip->i_mode = mode; | |
1446 | ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ | |
1447 | ip->i_nlink = 1; | |
1448 | ip->i_uid = ndp->ni_cred->cr_uid; | |
1449 | ip->i_gid = pdir->i_gid; | |
1450 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && | |
1451 | suser(ndp->ni_cred, NULL)) | |
1452 | ip->i_mode &= ~ISGID; | |
1453 | #ifdef QUOTA | |
1454 | ip->i_dquot = inoquota(ip); | |
1455 | #endif | |
1456 | ||
1457 | /* | |
1458 | * Make sure inode goes to disk before directory entry. | |
1459 | */ | |
1460 | if ((error = iupdat(ip, &time, &time, 1)) || | |
1461 | (error = direnter(ip, ndp))) { | |
1462 | /* | |
1463 | * Write error occurred trying to update the inode | |
1464 | * or the directory so must deallocate the inode. | |
1465 | */ | |
1466 | ip->i_nlink = 0; | |
1467 | ip->i_flag |= ICHG; | |
1468 | iput(ip); | |
1469 | return (error); | |
1470 | } | |
1471 | *ipp = ip; | |
1472 | return (0); | |
88a7a62a | 1473 | } |