Commit | Line | Data |
---|---|---|
a2907882 KM |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Rick Macklem at The University of Guelph. | |
7 | * | |
8 | * Redistribution and use in source and binary forms are permitted | |
9 | * provided that the above copyright notice and this paragraph are | |
10 | * duplicated in all such forms and that any documentation, | |
11 | * advertising materials, and other materials related to such | |
12 | * distribution and use acknowledge that the software was developed | |
13 | * by the University of California, Berkeley. The name of the | |
14 | * University may not be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
17 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
19 | * | |
7c15c5df | 20 | * @(#)nfs_vnops.c 7.40 (Berkeley) %G% |
a2907882 KM |
21 | */ |
22 | ||
23 | /* | |
24 | * vnode op calls for sun nfs version 2 | |
25 | */ | |
26 | ||
27 | #include "machine/pte.h" | |
28 | #include "machine/mtpr.h" | |
a2907882 KM |
29 | #include "param.h" |
30 | #include "user.h" | |
31 | #include "proc.h" | |
e49b1a6c | 32 | #include "kernel.h" |
a2907882 KM |
33 | #include "mount.h" |
34 | #include "buf.h" | |
35 | #include "vm.h" | |
a2907882 KM |
36 | #include "malloc.h" |
37 | #include "mbuf.h" | |
a2907882 KM |
38 | #include "errno.h" |
39 | #include "file.h" | |
40 | #include "conf.h" | |
41 | #include "vnode.h" | |
8c379a26 | 42 | #include "text.h" |
9238aa59 | 43 | #include "map.h" |
7c15c5df KM |
44 | #include "../ufs/quota.h" |
45 | #include "../ufs/inode.h" | |
a2907882 KM |
46 | #include "nfsv2.h" |
47 | #include "nfs.h" | |
48 | #include "nfsnode.h" | |
49 | #include "nfsmount.h" | |
50 | #include "xdr_subs.h" | |
51 | #include "nfsm_subs.h" | |
9238aa59 | 52 | #include "nfsiom.h" |
a2907882 KM |
53 | |
54 | /* Defs */ | |
55 | #define TRUE 1 | |
56 | #define FALSE 0 | |
57 | ||
58 | /* Global vars */ | |
59 | int nfs_lookup(), | |
60 | nfs_create(), | |
f67785e5 | 61 | nfs_mknod(), |
a2907882 KM |
62 | nfs_open(), |
63 | nfs_close(), | |
64 | nfs_access(), | |
65 | nfs_getattr(), | |
66 | nfs_setattr(), | |
67 | nfs_read(), | |
68 | nfs_write(), | |
69 | vfs_noop(), | |
70 | vfs_nullop(), | |
71 | nfs_remove(), | |
72 | nfs_link(), | |
73 | nfs_rename(), | |
74 | nfs_mkdir(), | |
75 | nfs_rmdir(), | |
76 | nfs_symlink(), | |
77 | nfs_readdir(), | |
78 | nfs_readlink(), | |
79 | nfs_abortop(), | |
80 | nfs_lock(), | |
81 | nfs_unlock(), | |
82 | nfs_bmap(), | |
83 | nfs_strategy(), | |
9238aa59 | 84 | nfs_fsync(), |
8c7139ba | 85 | nfs_inactive(), |
d002709d | 86 | nfs_reclaim(), |
2bde67fd KM |
87 | nfs_print(), |
88 | nfs_islocked(); | |
a2907882 KM |
89 | |
90 | struct vnodeops nfsv2_vnodeops = { | |
d002709d KM |
91 | nfs_lookup, /* lookup */ |
92 | nfs_create, /* create */ | |
93 | nfs_mknod, /* mknod */ | |
94 | nfs_open, /* open */ | |
95 | nfs_close, /* close */ | |
96 | nfs_access, /* access */ | |
97 | nfs_getattr, /* getattr */ | |
98 | nfs_setattr, /* setattr */ | |
99 | nfs_read, /* read */ | |
100 | nfs_write, /* write */ | |
101 | vfs_noop, /* ioctl */ | |
102 | vfs_noop, /* select */ | |
103 | vfs_noop, /* mmap */ | |
104 | nfs_fsync, /* fsync */ | |
105 | vfs_nullop, /* seek */ | |
106 | nfs_remove, /* remove */ | |
107 | nfs_link, /* link */ | |
108 | nfs_rename, /* rename */ | |
109 | nfs_mkdir, /* mkdir */ | |
110 | nfs_rmdir, /* rmdir */ | |
111 | nfs_symlink, /* symlink */ | |
112 | nfs_readdir, /* readdir */ | |
113 | nfs_readlink, /* readlink */ | |
114 | nfs_abortop, /* abortop */ | |
115 | nfs_inactive, /* inactive */ | |
116 | nfs_reclaim, /* reclaim */ | |
117 | nfs_lock, /* lock */ | |
118 | nfs_unlock, /* unlock */ | |
119 | nfs_bmap, /* bmap */ | |
120 | nfs_strategy, /* strategy */ | |
121 | nfs_print, /* print */ | |
2bde67fd | 122 | nfs_islocked, /* islocked */ |
a2907882 KM |
123 | }; |
124 | ||
125 | /* Special device vnode ops */ | |
8a1675f3 KM |
126 | int spec_lookup(), |
127 | spec_open(), | |
128 | spec_read(), | |
129 | spec_write(), | |
130 | spec_strategy(), | |
d002709d | 131 | spec_bmap(), |
8a1675f3 KM |
132 | spec_ioctl(), |
133 | spec_select(), | |
134 | spec_close(), | |
135 | spec_badop(), | |
136 | spec_nullop(); | |
a2907882 | 137 | |
8a1675f3 | 138 | struct vnodeops spec_nfsv2nodeops = { |
9106dc18 KM |
139 | spec_lookup, /* lookup */ |
140 | spec_badop, /* create */ | |
141 | spec_badop, /* mknod */ | |
142 | spec_open, /* open */ | |
143 | spec_close, /* close */ | |
144 | nfs_access, /* access */ | |
145 | nfs_getattr, /* getattr */ | |
146 | nfs_setattr, /* setattr */ | |
147 | spec_read, /* read */ | |
148 | spec_write, /* write */ | |
149 | spec_ioctl, /* ioctl */ | |
150 | spec_select, /* select */ | |
151 | spec_badop, /* mmap */ | |
152 | spec_nullop, /* fsync */ | |
153 | spec_badop, /* seek */ | |
154 | spec_badop, /* remove */ | |
155 | spec_badop, /* link */ | |
156 | spec_badop, /* rename */ | |
157 | spec_badop, /* mkdir */ | |
158 | spec_badop, /* rmdir */ | |
159 | spec_badop, /* symlink */ | |
160 | spec_badop, /* readdir */ | |
161 | spec_badop, /* readlink */ | |
162 | spec_badop, /* abortop */ | |
163 | nfs_inactive, /* inactive */ | |
164 | nfs_reclaim, /* reclaim */ | |
165 | nfs_lock, /* lock */ | |
166 | nfs_unlock, /* unlock */ | |
d002709d | 167 | spec_bmap, /* bmap */ |
9106dc18 | 168 | spec_strategy, /* strategy */ |
d002709d | 169 | nfs_print, /* print */ |
2bde67fd | 170 | nfs_islocked, /* islocked */ |
a2907882 KM |
171 | }; |
172 | ||
b2d955e4 KM |
173 | #ifdef FIFO |
174 | int fifo_lookup(), | |
175 | fifo_open(), | |
176 | fifo_read(), | |
177 | fifo_write(), | |
178 | fifo_bmap(), | |
179 | fifo_ioctl(), | |
180 | fifo_select(), | |
181 | fifo_close(), | |
182 | fifo_print(), | |
183 | fifo_badop(), | |
184 | fifo_nullop(); | |
185 | ||
186 | struct vnodeops fifo_nfsv2nodeops = { | |
187 | fifo_lookup, /* lookup */ | |
188 | fifo_badop, /* create */ | |
189 | fifo_badop, /* mknod */ | |
190 | fifo_open, /* open */ | |
191 | fifo_close, /* close */ | |
192 | nfs_access, /* access */ | |
193 | nfs_getattr, /* getattr */ | |
194 | nfs_setattr, /* setattr */ | |
195 | fifo_read, /* read */ | |
196 | fifo_write, /* write */ | |
197 | fifo_ioctl, /* ioctl */ | |
198 | fifo_select, /* select */ | |
199 | fifo_badop, /* mmap */ | |
200 | fifo_nullop, /* fsync */ | |
201 | fifo_badop, /* seek */ | |
202 | fifo_badop, /* remove */ | |
203 | fifo_badop, /* link */ | |
204 | fifo_badop, /* rename */ | |
205 | fifo_badop, /* mkdir */ | |
206 | fifo_badop, /* rmdir */ | |
207 | fifo_badop, /* symlink */ | |
208 | fifo_badop, /* readdir */ | |
209 | fifo_badop, /* readlink */ | |
210 | fifo_badop, /* abortop */ | |
211 | nfs_inactive, /* inactive */ | |
212 | nfs_reclaim, /* reclaim */ | |
213 | nfs_lock, /* lock */ | |
214 | nfs_unlock, /* unlock */ | |
215 | fifo_bmap, /* bmap */ | |
216 | fifo_badop, /* strategy */ | |
217 | nfs_print, /* print */ | |
218 | nfs_islocked, /* islocked */ | |
219 | }; | |
220 | #endif /* FIFO */ | |
221 | ||
a2907882 KM |
222 | extern u_long nfs_procids[NFS_NPROCS]; |
223 | extern u_long nfs_prog, nfs_vers; | |
a2907882 | 224 | extern char nfsiobuf[MAXPHYS+NBPG]; |
9238aa59 | 225 | struct map nfsmap[NFS_MSIZ]; |
9238aa59 | 226 | struct buf nfs_bqueue; /* Queue head for nfsiod's */ |
ffe6f482 | 227 | int nfs_asyncdaemons = 0; |
f0f1cbaa | 228 | struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; |
9238aa59 | 229 | static int nfsmap_want = 0; |
a2907882 KM |
230 | |
231 | /* | |
232 | * nfs null call from vfs. | |
233 | */ | |
234 | nfs_null(vp, cred) | |
235 | struct vnode *vp; | |
236 | struct ucred *cred; | |
237 | { | |
13576453 KM |
238 | caddr_t bpos, dpos; |
239 | u_long xid; | |
240 | int error = 0; | |
241 | struct mbuf *mreq, *mrep, *md, *mb; | |
a2907882 KM |
242 | |
243 | nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0); | |
f0f1cbaa | 244 | nfsm_request(vp, NFSPROC_NULL, u.u_procp); |
a2907882 KM |
245 | nfsm_reqdone; |
246 | return (error); | |
247 | } | |
248 | ||
249 | /* | |
250 | * nfs access vnode op. | |
251 | * Essentially just get vattr and then imitate iaccess() | |
252 | */ | |
253 | nfs_access(vp, mode, cred) | |
254 | struct vnode *vp; | |
255 | int mode; | |
256 | register struct ucred *cred; | |
257 | { | |
258 | register struct vattr *vap; | |
259 | register gid_t *gp; | |
260 | struct vattr vattr; | |
261 | register int i; | |
262 | int error; | |
263 | ||
264 | /* | |
265 | * If you're the super-user, | |
266 | * you always get access. | |
267 | */ | |
268 | if (cred->cr_uid == 0) | |
269 | return (0); | |
270 | vap = &vattr; | |
9238aa59 RM |
271 | if (error = nfs_getattr(vp, vap, cred)) |
272 | return (error); | |
a2907882 KM |
273 | /* |
274 | * Access check is based on only one of owner, group, public. | |
275 | * If not owner, then check group. If not a member of the | |
276 | * group, then check public access. | |
277 | */ | |
278 | if (cred->cr_uid != vap->va_uid) { | |
279 | mode >>= 3; | |
280 | gp = cred->cr_groups; | |
281 | for (i = 0; i < cred->cr_ngroups; i++, gp++) | |
282 | if (vap->va_gid == *gp) | |
283 | goto found; | |
284 | mode >>= 3; | |
285 | found: | |
286 | ; | |
287 | } | |
288 | if ((vap->va_mode & mode) != 0) | |
289 | return (0); | |
290 | return (EACCES); | |
291 | } | |
292 | ||
293 | /* | |
294 | * nfs open vnode op | |
295 | * Just check to see if the type is ok | |
296 | */ | |
13576453 | 297 | /* ARGSUSED */ |
a2907882 KM |
298 | nfs_open(vp, mode, cred) |
299 | struct vnode *vp; | |
300 | int mode; | |
301 | struct ucred *cred; | |
302 | { | |
303 | register enum vtype vtyp; | |
304 | ||
305 | vtyp = vp->v_type; | |
306 | if (vtyp == VREG || vtyp == VDIR || vtyp == VLNK) | |
307 | return (0); | |
308 | else | |
309 | return (EACCES); | |
310 | } | |
311 | ||
312 | /* | |
313 | * nfs close vnode op | |
9238aa59 | 314 | * For reg files, invalidate any buffer cache entries. |
a2907882 | 315 | */ |
13576453 | 316 | /* ARGSUSED */ |
a2907882 KM |
317 | nfs_close(vp, fflags, cred) |
318 | register struct vnode *vp; | |
319 | int fflags; | |
320 | struct ucred *cred; | |
321 | { | |
13576453 | 322 | register struct nfsnode *np = VTONFS(vp); |
ffe6f482 | 323 | int error = 0; |
a2907882 | 324 | |
f0f1cbaa | 325 | if (vp->v_type == VREG && (np->n_flag & NMODIFIED)) { |
8a1675f3 | 326 | nfs_lock(vp); |
f0f1cbaa | 327 | np->n_flag &= ~NMODIFIED; |
e8540f59 | 328 | vinvalbuf(vp, TRUE); |
f0f1cbaa | 329 | np->n_attrstamp = 0; |
ffe6f482 KM |
330 | if (np->n_flag & NWRITEERR) { |
331 | np->n_flag &= ~NWRITEERR; | |
e8540f59 | 332 | error = np->n_error; |
ffe6f482 | 333 | } |
8a1675f3 | 334 | nfs_unlock(vp); |
9238aa59 | 335 | } |
a2907882 KM |
336 | return (error); |
337 | } | |
338 | ||
339 | /* | |
340 | * nfs getattr call from vfs. | |
341 | */ | |
342 | nfs_getattr(vp, vap, cred) | |
13576453 KM |
343 | register struct vnode *vp; |
344 | struct vattr *vap; | |
a2907882 KM |
345 | struct ucred *cred; |
346 | { | |
13576453 KM |
347 | register caddr_t cp; |
348 | register long t1; | |
349 | caddr_t bpos, dpos; | |
350 | u_long xid; | |
351 | int error = 0; | |
352 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
353 | |
354 | /* First look in the cache.. */ | |
355 | if (nfs_getattrcache(vp, vap) == 0) | |
356 | return (0); | |
357 | nfsstats.rpccnt[NFSPROC_GETATTR]++; | |
358 | nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH); | |
359 | nfsm_fhtom(vp); | |
f0f1cbaa | 360 | nfsm_request(vp, NFSPROC_GETATTR, u.u_procp); |
a2907882 KM |
361 | nfsm_loadattr(vp, vap); |
362 | nfsm_reqdone; | |
363 | return (error); | |
364 | } | |
365 | ||
366 | /* | |
367 | * nfs setattr call. | |
368 | */ | |
369 | nfs_setattr(vp, vap, cred) | |
13576453 | 370 | register struct vnode *vp; |
a2907882 KM |
371 | register struct vattr *vap; |
372 | struct ucred *cred; | |
373 | { | |
9238aa59 | 374 | register struct nfsv2_sattr *sp; |
13576453 KM |
375 | register caddr_t cp; |
376 | register long t1; | |
377 | caddr_t bpos, dpos; | |
378 | u_long xid; | |
379 | int error = 0; | |
380 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
00b72154 | 381 | struct nfsnode *np; |
a2907882 KM |
382 | |
383 | nfsstats.rpccnt[NFSPROC_SETATTR]++; | |
384 | nfsm_reqhead(nfs_procids[NFSPROC_SETATTR], cred, NFSX_FH+NFSX_SATTR); | |
385 | nfsm_fhtom(vp); | |
9238aa59 | 386 | nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR); |
a2907882 | 387 | if (vap->va_mode == 0xffff) |
9238aa59 | 388 | sp->sa_mode = VNOVAL; |
a2907882 | 389 | else |
9238aa59 | 390 | sp->sa_mode = vtonfs_mode(vp->v_type, vap->va_mode); |
a2907882 | 391 | if (vap->va_uid == 0xffff) |
9238aa59 | 392 | sp->sa_uid = VNOVAL; |
a2907882 | 393 | else |
9238aa59 | 394 | sp->sa_uid = txdr_unsigned(vap->va_uid); |
a2907882 | 395 | if (vap->va_gid == 0xffff) |
9238aa59 | 396 | sp->sa_gid = VNOVAL; |
a2907882 | 397 | else |
9238aa59 RM |
398 | sp->sa_gid = txdr_unsigned(vap->va_gid); |
399 | sp->sa_size = txdr_unsigned(vap->va_size); | |
00b72154 KM |
400 | if (vap->va_size != VNOVAL) { |
401 | np = VTONFS(vp); | |
402 | if (np->n_flag & NMODIFIED) { | |
403 | np->n_flag &= ~NMODIFIED; | |
fe53c871 | 404 | vinvalbuf(vp, TRUE); |
f0f1cbaa | 405 | np->n_attrstamp = 0; |
00b72154 KM |
406 | } |
407 | } | |
e8540f59 KM |
408 | sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec); |
409 | sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags); | |
9238aa59 | 410 | txdr_time(&vap->va_mtime, &sp->sa_mtime); |
f0f1cbaa | 411 | nfsm_request(vp, NFSPROC_SETATTR, u.u_procp); |
a2907882 KM |
412 | nfsm_loadattr(vp, (struct vattr *)0); |
413 | /* should we fill in any vap fields ?? */ | |
414 | nfsm_reqdone; | |
415 | return (error); | |
416 | } | |
417 | ||
418 | /* | |
419 | * nfs lookup call, one step at a time... | |
420 | * First look in cache | |
421 | * If not found, unlock the directory nfsnode and do the rpc | |
422 | */ | |
423 | nfs_lookup(vp, ndp) | |
424 | register struct vnode *vp; | |
425 | register struct nameidata *ndp; | |
426 | { | |
427 | register struct vnode *vdp; | |
13576453 KM |
428 | register u_long *p; |
429 | register caddr_t cp; | |
430 | register long t1, t2; | |
431 | caddr_t bpos, dpos, cp2; | |
432 | u_long xid; | |
433 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
434 | struct vnode *newvp; |
435 | long len; | |
436 | nfsv2fh_t *fhp; | |
437 | struct nfsnode *np; | |
13576453 | 438 | int lockparent, wantparent, flag, error = 0; |
a2907882 KM |
439 | |
440 | ndp->ni_dvp = vp; | |
441 | ndp->ni_vp = NULL; | |
442 | if (vp->v_type != VDIR) | |
443 | return (ENOTDIR); | |
444 | lockparent = ndp->ni_nameiop & LOCKPARENT; | |
445 | flag = ndp->ni_nameiop & OPFLAG; | |
446 | wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); | |
ffe6f482 | 447 | if ((error = cache_lookup(ndp)) && error != ENOENT) { |
9238aa59 RM |
448 | struct vattr vattr; |
449 | int vpid; | |
450 | ||
8a1675f3 KM |
451 | if (vp == ndp->ni_rdir && ndp->ni_isdotdot) |
452 | panic("nfs_lookup: .. through root"); | |
453 | vdp = ndp->ni_vp; | |
9238aa59 | 454 | vpid = vdp->v_id; |
a2907882 | 455 | /* |
9238aa59 RM |
456 | * See the comment starting `Step through' in ufs/ufs_lookup.c |
457 | * for an explanation of the locking protocol | |
a2907882 KM |
458 | */ |
459 | if (vp == vdp) { | |
36c3043b | 460 | VREF(vdp); |
8a1675f3 | 461 | error = 0; |
a2907882 KM |
462 | } else if (ndp->ni_isdotdot) { |
463 | nfs_unlock(vp); | |
8a1675f3 | 464 | error = vget(vdp); |
a2907882 | 465 | } else { |
8a1675f3 | 466 | error = vget(vdp); |
a2907882 KM |
467 | nfs_unlock(vp); |
468 | } | |
8a1675f3 | 469 | if (!error) { |
8c379a26 KM |
470 | if (vpid == vdp->v_id) { |
471 | if (!nfs_getattr(vdp, &vattr, ndp->ni_cred) && | |
472 | vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) { | |
8a1675f3 KM |
473 | nfsstats.lookupcache_hits++; |
474 | return (0); | |
8c379a26 KM |
475 | } else { |
476 | cache_purge(vdp); | |
477 | nfs_nput(vdp); | |
478 | } | |
8a1675f3 KM |
479 | } else { |
480 | nfs_nput(vdp); | |
481 | } | |
9238aa59 | 482 | } |
54fb9dc2 | 483 | ndp->ni_vp = NULLVP; |
e8540f59 KM |
484 | } else |
485 | nfs_unlock(vp); | |
ffe6f482 | 486 | error = 0; |
a2907882 | 487 | nfsstats.lookupcache_misses++; |
a2907882 KM |
488 | nfsstats.rpccnt[NFSPROC_LOOKUP]++; |
489 | len = ndp->ni_namelen; | |
490 | nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)); | |
491 | nfsm_fhtom(vp); | |
492 | nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN); | |
f0f1cbaa | 493 | nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp); |
a2907882 KM |
494 | nfsmout: |
495 | if (error) { | |
e8540f59 KM |
496 | if (lockparent || (flag != CREATE && flag != RENAME) || |
497 | *ndp->ni_next != 0) | |
498 | nfs_lock(vp); | |
b2f2eab2 | 499 | return (error); |
a2907882 KM |
500 | } |
501 | nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH); | |
502 | ||
503 | /* | |
504 | * Handle DELETE and RENAME cases... | |
505 | */ | |
506 | if (flag == DELETE && *ndp->ni_next == 0) { | |
507 | if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { | |
36c3043b | 508 | VREF(vp); |
a2907882 KM |
509 | newvp = vp; |
510 | np = VTONFS(vp); | |
511 | } else { | |
512 | if (error = nfs_nget(vp->v_mount, fhp, &np)) { | |
e8540f59 | 513 | nfs_lock(vp); |
a2907882 KM |
514 | m_freem(mrep); |
515 | return (error); | |
516 | } | |
517 | newvp = NFSTOV(np); | |
518 | } | |
f67785e5 KM |
519 | if (error = |
520 | nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) { | |
e8540f59 | 521 | nfs_lock(vp); |
a2907882 KM |
522 | if (newvp != vp) |
523 | nfs_nput(newvp); | |
524 | else | |
36c3043b | 525 | vrele(vp); |
a2907882 KM |
526 | m_freem(mrep); |
527 | return (error); | |
528 | } | |
a2907882 | 529 | ndp->ni_vp = newvp; |
e8540f59 KM |
530 | if (lockparent || vp == newvp) |
531 | nfs_lock(vp); | |
a2907882 KM |
532 | m_freem(mrep); |
533 | return (0); | |
534 | } | |
535 | ||
536 | if (flag == RENAME && wantparent && *ndp->ni_next == 0) { | |
537 | if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { | |
e8540f59 | 538 | nfs_lock(vp); |
a2907882 KM |
539 | m_freem(mrep); |
540 | return (EISDIR); | |
541 | } | |
542 | if (error = nfs_nget(vp->v_mount, fhp, &np)) { | |
e8540f59 | 543 | nfs_lock(vp); |
a2907882 KM |
544 | m_freem(mrep); |
545 | return (error); | |
546 | } | |
547 | newvp = NFSTOV(np); | |
f67785e5 KM |
548 | if (error = |
549 | nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) { | |
e8540f59 | 550 | nfs_lock(vp); |
36c3043b | 551 | nfs_nput(newvp); |
a2907882 KM |
552 | m_freem(mrep); |
553 | return (error); | |
554 | } | |
555 | ndp->ni_vp = newvp; | |
e8540f59 KM |
556 | if (lockparent) |
557 | nfs_lock(vp); | |
a2907882 KM |
558 | return (0); |
559 | } | |
560 | ||
561 | if (!bcmp(VTONFS(vp)->n_fh.fh_bytes, (caddr_t)fhp, NFSX_FH)) { | |
36c3043b | 562 | VREF(vp); |
a2907882 KM |
563 | newvp = vp; |
564 | np = VTONFS(vp); | |
565 | } else if (ndp->ni_isdotdot) { | |
a2907882 KM |
566 | if (error = nfs_nget(vp->v_mount, fhp, &np)) { |
567 | nfs_lock(vp); | |
568 | m_freem(mrep); | |
569 | return (error); | |
570 | } | |
a2907882 KM |
571 | newvp = NFSTOV(np); |
572 | } else { | |
573 | if (error = nfs_nget(vp->v_mount, fhp, &np)) { | |
e8540f59 | 574 | nfs_lock(vp); |
a2907882 KM |
575 | m_freem(mrep); |
576 | return (error); | |
577 | } | |
578 | newvp = NFSTOV(np); | |
579 | } | |
f67785e5 | 580 | if (error = nfs_loadattrcache(&newvp, &md, &dpos, (struct vattr *)0)) { |
e8540f59 | 581 | nfs_lock(vp); |
a2907882 KM |
582 | if (newvp != vp) |
583 | nfs_nput(newvp); | |
584 | else | |
36c3043b | 585 | vrele(vp); |
a2907882 KM |
586 | m_freem(mrep); |
587 | return (error); | |
588 | } | |
589 | m_freem(mrep); | |
a2907882 | 590 | |
e8540f59 KM |
591 | if (vp == newvp || (lockparent && *ndp->ni_next == '\0')) |
592 | nfs_lock(vp); | |
a2907882 | 593 | ndp->ni_vp = newvp; |
8c379a26 KM |
594 | if (error == 0 && ndp->ni_makeentry) { |
595 | np->n_ctime = np->n_vattr.va_ctime.tv_sec; | |
a2907882 | 596 | cache_enter(ndp); |
8c379a26 | 597 | } |
a2907882 KM |
598 | return (error); |
599 | } | |
600 | ||
f0f1cbaa KM |
601 | /* |
602 | * nfs read call. | |
603 | * Just call nfs_bioread() to do the work. | |
604 | */ | |
605 | nfs_read(vp, uiop, ioflag, cred) | |
606 | register struct vnode *vp; | |
607 | struct uio *uiop; | |
608 | int ioflag; | |
609 | struct ucred *cred; | |
610 | { | |
611 | if (vp->v_type != VREG) | |
612 | return (EPERM); | |
613 | return (nfs_bioread(vp, uiop, ioflag, cred)); | |
614 | } | |
615 | ||
a2907882 KM |
616 | /* |
617 | * nfs readlink call | |
618 | */ | |
619 | nfs_readlink(vp, uiop, cred) | |
f0f1cbaa KM |
620 | struct vnode *vp; |
621 | struct uio *uiop; | |
622 | struct ucred *cred; | |
623 | { | |
624 | if (vp->v_type != VLNK) | |
625 | return (EPERM); | |
626 | return (nfs_bioread(vp, uiop, 0, cred)); | |
627 | } | |
628 | ||
629 | /* | |
630 | * Do a readlink rpc. | |
631 | * Called by nfs_doio() from below the buffer cache. | |
632 | */ | |
633 | nfs_readlinkrpc(vp, uiop, cred, procp) | |
13576453 | 634 | register struct vnode *vp; |
a2907882 KM |
635 | struct uio *uiop; |
636 | struct ucred *cred; | |
f0f1cbaa | 637 | struct proc *procp; |
a2907882 | 638 | { |
13576453 KM |
639 | register u_long *p; |
640 | register caddr_t cp; | |
641 | register long t1; | |
642 | caddr_t bpos, dpos, cp2; | |
643 | u_long xid; | |
644 | int error = 0; | |
645 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
646 | long len; |
647 | ||
648 | nfsstats.rpccnt[NFSPROC_READLINK]++; | |
f0f1cbaa | 649 | nfs_unlock(vp); |
a2907882 KM |
650 | nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH); |
651 | nfsm_fhtom(vp); | |
f0f1cbaa | 652 | nfsm_request(vp, NFSPROC_READLINK, procp); |
a2907882 KM |
653 | nfsm_strsiz(len, NFS_MAXPATHLEN); |
654 | nfsm_mtouio(uiop, len); | |
655 | nfsm_reqdone; | |
2f018cdf | 656 | nfs_lock(vp); |
a2907882 KM |
657 | return (error); |
658 | } | |
659 | ||
660 | /* | |
f0f1cbaa KM |
661 | * nfs read rpc call |
662 | * Ditto above | |
a2907882 | 663 | */ |
f0f1cbaa | 664 | nfs_readrpc(vp, uiop, cred, procp) |
13576453 | 665 | register struct vnode *vp; |
a2907882 | 666 | struct uio *uiop; |
a2907882 | 667 | struct ucred *cred; |
f0f1cbaa | 668 | struct proc *procp; |
a2907882 | 669 | { |
13576453 KM |
670 | register u_long *p; |
671 | register caddr_t cp; | |
672 | register long t1; | |
673 | caddr_t bpos, dpos, cp2; | |
674 | u_long xid; | |
675 | int error = 0; | |
676 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
677 | struct nfsmount *nmp; |
678 | long len, retlen, tsiz; | |
679 | ||
54fb9dc2 | 680 | nmp = VFSTONFS(vp->v_mount); |
a2907882 | 681 | tsiz = uiop->uio_resid; |
a2907882 KM |
682 | while (tsiz > 0) { |
683 | nfsstats.rpccnt[NFSPROC_READ]++; | |
684 | len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; | |
685 | nfsm_reqhead(nfs_procids[NFSPROC_READ], cred, NFSX_FH+NFSX_UNSIGNED*3); | |
686 | nfsm_fhtom(vp); | |
687 | nfsm_build(p, u_long *, NFSX_UNSIGNED*3); | |
f8428623 | 688 | *p++ = txdr_unsigned(uiop->uio_offset); |
a2907882 KM |
689 | *p++ = txdr_unsigned(len); |
690 | *p = 0; | |
f0f1cbaa | 691 | nfsm_request(vp, NFSPROC_READ, procp); |
a2907882 KM |
692 | nfsm_loadattr(vp, (struct vattr *)0); |
693 | nfsm_strsiz(retlen, nmp->nm_rsize); | |
694 | nfsm_mtouio(uiop, retlen); | |
695 | m_freem(mrep); | |
a2907882 KM |
696 | if (retlen < len) |
697 | tsiz = 0; | |
698 | else | |
699 | tsiz -= len; | |
700 | } | |
701 | nfsmout: | |
a2907882 KM |
702 | return (error); |
703 | } | |
704 | ||
705 | /* | |
706 | * nfs write call | |
707 | */ | |
f0f1cbaa | 708 | nfs_writerpc(vp, uiop, cred, procp) |
13576453 | 709 | register struct vnode *vp; |
a2907882 | 710 | struct uio *uiop; |
a2907882 | 711 | struct ucred *cred; |
f0f1cbaa | 712 | struct proc *procp; |
a2907882 | 713 | { |
13576453 KM |
714 | register u_long *p; |
715 | register caddr_t cp; | |
716 | register long t1; | |
717 | caddr_t bpos, dpos; | |
718 | u_long xid; | |
719 | int error = 0; | |
720 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
721 | struct nfsmount *nmp; |
722 | long len, tsiz; | |
a2907882 | 723 | |
54fb9dc2 | 724 | nmp = VFSTONFS(vp->v_mount); |
a2907882 | 725 | tsiz = uiop->uio_resid; |
a2907882 KM |
726 | while (tsiz > 0) { |
727 | nfsstats.rpccnt[NFSPROC_WRITE]++; | |
728 | len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz; | |
729 | nfsm_reqhead(nfs_procids[NFSPROC_WRITE], cred, | |
730 | NFSX_FH+NFSX_UNSIGNED*4); | |
731 | nfsm_fhtom(vp); | |
732 | nfsm_build(p, u_long *, NFSX_UNSIGNED*4); | |
f8428623 | 733 | *(p+1) = txdr_unsigned(uiop->uio_offset); |
a2907882 KM |
734 | *(p+3) = txdr_unsigned(len); |
735 | nfsm_uiotom(uiop, len); | |
f0f1cbaa | 736 | nfsm_request(vp, NFSPROC_WRITE, procp); |
a2907882 KM |
737 | nfsm_loadattr(vp, (struct vattr *)0); |
738 | m_freem(mrep); | |
739 | tsiz -= len; | |
a2907882 KM |
740 | } |
741 | nfsmout: | |
a2907882 KM |
742 | return (error); |
743 | } | |
744 | ||
f67785e5 KM |
745 | /* |
746 | * nfs mknod call | |
7c15c5df KM |
747 | * This is a kludge. Use a create rpc but with the IFMT bits of the mode |
748 | * set to specify the file type and the size field for rdev. | |
f67785e5 KM |
749 | */ |
750 | /* ARGSUSED */ | |
751 | nfs_mknod(ndp, vap, cred) | |
752 | struct nameidata *ndp; | |
753 | struct ucred *cred; | |
7c15c5df | 754 | register struct vattr *vap; |
f67785e5 | 755 | { |
7c15c5df KM |
756 | register struct nfsv2_sattr *sp; |
757 | register u_long *p; | |
758 | register caddr_t cp; | |
759 | register long t1, t2; | |
760 | caddr_t bpos, dpos; | |
761 | u_long xid; | |
762 | int error = 0; | |
763 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
764 | u_long rdev; | |
f67785e5 | 765 | |
7c15c5df KM |
766 | if (vap->va_type == VCHR || vap->va_type == VBLK) |
767 | rdev = txdr_unsigned(vap->va_rdev); | |
768 | #ifdef FIFO | |
769 | else if (vap->va_type == VFIFO) | |
770 | rdev = 0xffffffff; | |
771 | #endif /* FIFO */ | |
772 | else { | |
773 | VOP_ABORTOP(ndp); | |
774 | return (EOPNOTSUPP); | |
775 | } | |
776 | nfsstats.rpccnt[NFSPROC_CREATE]++; | |
777 | nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred, | |
778 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR); | |
779 | nfsm_fhtom(ndp->ni_dvp); | |
780 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
781 | nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR); | |
782 | sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode); | |
783 | sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid); | |
784 | sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid); | |
785 | sp->sa_size = rdev; | |
786 | /* or should these be VNOVAL ?? */ | |
787 | txdr_time(&vap->va_atime, &sp->sa_atime); | |
788 | txdr_time(&vap->va_mtime, &sp->sa_mtime); | |
789 | nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp); | |
790 | nfsm_reqdone; | |
791 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; | |
792 | nfs_nput(ndp->ni_dvp); | |
793 | return (error); | |
f67785e5 KM |
794 | } |
795 | ||
a2907882 KM |
796 | /* |
797 | * nfs file create call | |
798 | */ | |
799 | nfs_create(ndp, vap) | |
800 | register struct nameidata *ndp; | |
801 | register struct vattr *vap; | |
802 | { | |
9238aa59 | 803 | register struct nfsv2_sattr *sp; |
13576453 KM |
804 | register u_long *p; |
805 | register caddr_t cp; | |
806 | register long t1, t2; | |
807 | caddr_t bpos, dpos, cp2; | |
808 | u_long xid; | |
809 | int error = 0; | |
810 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
811 | |
812 | nfsstats.rpccnt[NFSPROC_CREATE]++; | |
813 | nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred, | |
814 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR); | |
815 | nfsm_fhtom(ndp->ni_dvp); | |
816 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
9238aa59 RM |
817 | nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR); |
818 | sp->sa_mode = vtonfs_mode(VREG, vap->va_mode); | |
819 | sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid); | |
820 | sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid); | |
821 | sp->sa_size = txdr_unsigned(0); | |
a2907882 | 822 | /* or should these be VNOVAL ?? */ |
9238aa59 RM |
823 | txdr_time(&vap->va_atime, &sp->sa_atime); |
824 | txdr_time(&vap->va_mtime, &sp->sa_mtime); | |
f0f1cbaa | 825 | nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp); |
a2907882 KM |
826 | nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp); |
827 | nfsm_reqdone; | |
f0f1cbaa | 828 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
a2907882 KM |
829 | nfs_nput(ndp->ni_dvp); |
830 | return (error); | |
831 | } | |
832 | ||
833 | /* | |
834 | * nfs file remove call | |
f0f1cbaa KM |
835 | * To try and make nfs semantics closer to ufs semantics, a file that has |
836 | * other processes using the vnode is renamed instead of removed and then | |
ffe6f482 | 837 | * removed later on the last close. |
f0f1cbaa | 838 | * - If v_usecount > 1 |
ffe6f482 KM |
839 | * If a rename is not already in the works |
840 | * call nfs_sillyrename() to set it up | |
841 | * else | |
842 | * do the remove rpc | |
a2907882 KM |
843 | */ |
844 | nfs_remove(ndp) | |
845 | register struct nameidata *ndp; | |
846 | { | |
ffe6f482 KM |
847 | register struct vnode *vp = ndp->ni_vp; |
848 | register struct nfsnode *np = VTONFS(ndp->ni_vp); | |
13576453 KM |
849 | register u_long *p; |
850 | register caddr_t cp; | |
851 | register long t1, t2; | |
852 | caddr_t bpos, dpos; | |
853 | u_long xid; | |
854 | int error = 0; | |
855 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 | 856 | |
1f7e9641 | 857 | if (vp->v_usecount > 1) { |
ffe6f482 KM |
858 | if (!np->n_sillyrename) |
859 | error = nfs_sillyrename(ndp, REMOVE); | |
860 | } else { | |
a2907882 KM |
861 | nfsstats.rpccnt[NFSPROC_REMOVE]++; |
862 | nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred, | |
863 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); | |
864 | nfsm_fhtom(ndp->ni_dvp); | |
865 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
f0f1cbaa | 866 | nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp); |
a2907882 | 867 | nfsm_reqdone; |
f0f1cbaa | 868 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
e8540f59 KM |
869 | /* |
870 | * Kludge City: If the first reply to the remove rpc is lost.. | |
871 | * the reply to the retransmitted request will be ENOENT | |
872 | * since the file was in fact removed | |
873 | * Therefore, we cheat and return success. | |
874 | */ | |
875 | if (error == ENOENT) | |
876 | error = 0; | |
a2907882 | 877 | } |
86dea069 | 878 | np->n_attrstamp = 0; |
f5f6c13e KM |
879 | if (ndp->ni_dvp == vp) |
880 | vrele(vp); | |
a2907882 | 881 | else |
f5f6c13e KM |
882 | nfs_nput(ndp->ni_dvp); |
883 | nfs_nput(vp); | |
a2907882 KM |
884 | return (error); |
885 | } | |
886 | ||
887 | /* | |
888 | * nfs file remove rpc called from nfs_inactive | |
889 | */ | |
890 | nfs_removeit(ndp) | |
891 | register struct nameidata *ndp; | |
892 | { | |
13576453 KM |
893 | register u_long *p; |
894 | register caddr_t cp; | |
895 | register long t1, t2; | |
896 | caddr_t bpos, dpos; | |
897 | u_long xid; | |
898 | int error = 0; | |
899 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
900 | |
901 | nfsstats.rpccnt[NFSPROC_REMOVE]++; | |
902 | nfsm_reqhead(nfs_procids[NFSPROC_REMOVE], ndp->ni_cred, | |
903 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); | |
904 | nfsm_fhtom(ndp->ni_dvp); | |
905 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
f0f1cbaa | 906 | nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp); |
a2907882 | 907 | nfsm_reqdone; |
f0f1cbaa | 908 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
a2907882 KM |
909 | return (error); |
910 | } | |
911 | ||
912 | /* | |
913 | * nfs file rename call | |
914 | */ | |
915 | nfs_rename(sndp, tndp) | |
916 | register struct nameidata *sndp, *tndp; | |
917 | { | |
13576453 KM |
918 | register u_long *p; |
919 | register caddr_t cp; | |
920 | register long t1, t2; | |
921 | caddr_t bpos, dpos; | |
922 | u_long xid; | |
923 | int error = 0; | |
924 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
925 | |
926 | nfsstats.rpccnt[NFSPROC_RENAME]++; | |
927 | nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred, | |
928 | (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+ | |
929 | nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/ | |
930 | nfsm_fhtom(sndp->ni_dvp); | |
931 | nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN); | |
932 | nfsm_fhtom(tndp->ni_dvp); | |
933 | nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN); | |
f0f1cbaa | 934 | nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp); |
a2907882 | 935 | nfsm_reqdone; |
f0f1cbaa KM |
936 | VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED; |
937 | VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED; | |
a2907882 KM |
938 | if (sndp->ni_vp->v_type == VDIR) { |
939 | if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR) | |
940 | cache_purge(tndp->ni_dvp); | |
941 | cache_purge(sndp->ni_dvp); | |
942 | } | |
a2907882 KM |
943 | nfs_abortop(sndp); |
944 | nfs_abortop(tndp); | |
f5f6c13e KM |
945 | /* |
946 | * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. | |
947 | */ | |
948 | if (error == ENOENT) | |
949 | error = 0; | |
a2907882 KM |
950 | return (error); |
951 | } | |
952 | ||
953 | /* | |
f0f1cbaa | 954 | * nfs file rename rpc called from nfs_remove() above |
a2907882 KM |
955 | */ |
956 | nfs_renameit(sndp, tndp) | |
957 | register struct nameidata *sndp, *tndp; | |
958 | { | |
13576453 KM |
959 | register u_long *p; |
960 | register caddr_t cp; | |
961 | register long t1, t2; | |
962 | caddr_t bpos, dpos; | |
963 | u_long xid; | |
964 | int error = 0; | |
965 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
966 | |
967 | nfsstats.rpccnt[NFSPROC_RENAME]++; | |
968 | nfsm_reqhead(nfs_procids[NFSPROC_RENAME], tndp->ni_cred, | |
969 | (NFSX_FH+NFSX_UNSIGNED)*2+nfsm_rndup(sndp->ni_dent.d_namlen)+ | |
970 | nfsm_rndup(tndp->ni_dent.d_namlen)); /* or sndp->ni_cred?*/ | |
971 | nfsm_fhtom(sndp->ni_dvp); | |
972 | nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN); | |
973 | nfsm_fhtom(tndp->ni_dvp); | |
974 | nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN); | |
f0f1cbaa | 975 | nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp); |
a2907882 | 976 | nfsm_reqdone; |
f0f1cbaa KM |
977 | VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED; |
978 | VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED; | |
a2907882 KM |
979 | return (error); |
980 | } | |
981 | ||
982 | /* | |
983 | * nfs hard link create call | |
984 | */ | |
985 | nfs_link(vp, ndp) | |
13576453 | 986 | register struct vnode *vp; |
a2907882 KM |
987 | register struct nameidata *ndp; |
988 | { | |
13576453 KM |
989 | register u_long *p; |
990 | register caddr_t cp; | |
991 | register long t1, t2; | |
992 | caddr_t bpos, dpos; | |
993 | u_long xid; | |
994 | int error = 0; | |
995 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 | 996 | |
36c3043b KM |
997 | if (ndp->ni_dvp != vp) |
998 | nfs_lock(vp); | |
a2907882 KM |
999 | nfsstats.rpccnt[NFSPROC_LINK]++; |
1000 | nfsm_reqhead(nfs_procids[NFSPROC_LINK], ndp->ni_cred, | |
1001 | NFSX_FH*2+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); | |
1002 | nfsm_fhtom(vp); | |
1003 | nfsm_fhtom(ndp->ni_dvp); | |
1004 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
f0f1cbaa | 1005 | nfsm_request(vp, NFSPROC_LINK, u.u_procp); |
a2907882 | 1006 | nfsm_reqdone; |
86dea069 | 1007 | VTONFS(vp)->n_attrstamp = 0; |
f0f1cbaa | 1008 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
36c3043b KM |
1009 | if (ndp->ni_dvp != vp) |
1010 | nfs_unlock(vp); | |
a2907882 | 1011 | nfs_nput(ndp->ni_dvp); |
f5f6c13e KM |
1012 | /* |
1013 | * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. | |
1014 | */ | |
1015 | if (error == EEXIST) | |
1016 | error = 0; | |
a2907882 KM |
1017 | return (error); |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * nfs symbolic link create call | |
1022 | */ | |
1023 | nfs_symlink(ndp, vap, nm) | |
1024 | struct nameidata *ndp; | |
1025 | struct vattr *vap; | |
1026 | char *nm; /* is this the path ?? */ | |
1027 | { | |
9238aa59 | 1028 | register struct nfsv2_sattr *sp; |
13576453 KM |
1029 | register u_long *p; |
1030 | register caddr_t cp; | |
1031 | register long t1, t2; | |
1032 | caddr_t bpos, dpos; | |
1033 | u_long xid; | |
1034 | int error = 0; | |
1035 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
1036 | |
1037 | nfsstats.rpccnt[NFSPROC_SYMLINK]++; | |
1038 | nfsm_reqhead(nfs_procids[NFSPROC_SYMLINK], ndp->ni_cred, | |
1039 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_UNSIGNED); | |
1040 | nfsm_fhtom(ndp->ni_dvp); | |
1041 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
1042 | nfsm_strtom(nm, strlen(nm), NFS_MAXPATHLEN); | |
9238aa59 RM |
1043 | nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR); |
1044 | sp->sa_mode = vtonfs_mode(VLNK, vap->va_mode); | |
1045 | sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid); | |
1046 | sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid); | |
1047 | sp->sa_size = txdr_unsigned(VNOVAL); | |
f5f6c13e | 1048 | txdr_time(&vap->va_atime, &sp->sa_atime); /* or VNOVAL ?? */ |
9238aa59 | 1049 | txdr_time(&vap->va_mtime, &sp->sa_mtime); /* or VNOVAL ?? */ |
f0f1cbaa | 1050 | nfsm_request(ndp->ni_dvp, NFSPROC_SYMLINK, u.u_procp); |
a2907882 | 1051 | nfsm_reqdone; |
f0f1cbaa | 1052 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
a2907882 | 1053 | nfs_nput(ndp->ni_dvp); |
f5f6c13e KM |
1054 | /* |
1055 | * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. | |
1056 | */ | |
1057 | if (error == EEXIST) | |
1058 | error = 0; | |
a2907882 KM |
1059 | return (error); |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * nfs make dir call | |
1064 | */ | |
1065 | nfs_mkdir(ndp, vap) | |
13576453 | 1066 | register struct nameidata *ndp; |
a2907882 KM |
1067 | struct vattr *vap; |
1068 | { | |
9238aa59 | 1069 | register struct nfsv2_sattr *sp; |
13576453 KM |
1070 | register u_long *p; |
1071 | register caddr_t cp; | |
1072 | register long t1, t2; | |
f0f1cbaa | 1073 | register int len; |
13576453 KM |
1074 | caddr_t bpos, dpos, cp2; |
1075 | u_long xid; | |
f0f1cbaa | 1076 | int error = 0, firsttry = 1; |
13576453 | 1077 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; |
a2907882 | 1078 | |
f0f1cbaa | 1079 | len = ndp->ni_dent.d_namlen; |
a2907882 KM |
1080 | nfsstats.rpccnt[NFSPROC_MKDIR]++; |
1081 | nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred, | |
f0f1cbaa | 1082 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)+NFSX_SATTR); |
a2907882 | 1083 | nfsm_fhtom(ndp->ni_dvp); |
f0f1cbaa | 1084 | nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN); |
9238aa59 RM |
1085 | nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR); |
1086 | sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode); | |
1087 | sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid); | |
1088 | sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid); | |
1089 | sp->sa_size = txdr_unsigned(VNOVAL); | |
f5f6c13e | 1090 | txdr_time(&vap->va_atime, &sp->sa_atime); /* or VNOVAL ?? */ |
9238aa59 | 1091 | txdr_time(&vap->va_mtime, &sp->sa_mtime); /* or VNOVAL ?? */ |
f0f1cbaa | 1092 | nfsm_request(ndp->ni_dvp, NFSPROC_MKDIR, u.u_procp); |
a2907882 KM |
1093 | nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp); |
1094 | nfsm_reqdone; | |
f0f1cbaa | 1095 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
f5f6c13e | 1096 | /* |
f0f1cbaa KM |
1097 | * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry |
1098 | * if we can succeed in looking up the directory. | |
1099 | * "firsttry" is necessary since the macros may "goto nfsmout" which | |
1100 | * is above the if on errors. (Ugh) | |
f5f6c13e | 1101 | */ |
f0f1cbaa KM |
1102 | if (error == EEXIST && firsttry) { |
1103 | firsttry = 0; | |
f5f6c13e | 1104 | error = 0; |
f0f1cbaa KM |
1105 | nfsstats.rpccnt[NFSPROC_LOOKUP]++; |
1106 | ndp->ni_vp = NULL; | |
1107 | nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, | |
1108 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)); | |
1109 | nfsm_fhtom(ndp->ni_dvp); | |
1110 | nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN); | |
1111 | nfsm_request(ndp->ni_dvp, NFSPROC_LOOKUP, u.u_procp); | |
1112 | nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp); | |
1113 | if (ndp->ni_vp->v_type != VDIR) { | |
1114 | vput(ndp->ni_vp); | |
1115 | error = EEXIST; | |
1116 | } | |
1117 | m_freem(mrep); | |
1118 | } | |
1119 | nfs_nput(ndp->ni_dvp); | |
a2907882 KM |
1120 | return (error); |
1121 | } | |
1122 | ||
1123 | /* | |
1124 | * nfs remove directory call | |
1125 | */ | |
1126 | nfs_rmdir(ndp) | |
1127 | register struct nameidata *ndp; | |
1128 | { | |
13576453 KM |
1129 | register u_long *p; |
1130 | register caddr_t cp; | |
1131 | register long t1, t2; | |
1132 | caddr_t bpos, dpos; | |
1133 | u_long xid; | |
1134 | int error = 0; | |
1135 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
1136 | |
1137 | if (ndp->ni_dvp == ndp->ni_vp) { | |
1138 | vrele(ndp->ni_dvp); | |
1139 | nfs_nput(ndp->ni_dvp); | |
1140 | return (EINVAL); | |
1141 | } | |
a2907882 KM |
1142 | nfsstats.rpccnt[NFSPROC_RMDIR]++; |
1143 | nfsm_reqhead(nfs_procids[NFSPROC_RMDIR], ndp->ni_cred, | |
1144 | NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)); | |
1145 | nfsm_fhtom(ndp->ni_dvp); | |
1146 | nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN); | |
f0f1cbaa | 1147 | nfsm_request(ndp->ni_dvp, NFSPROC_RMDIR, u.u_procp); |
a2907882 | 1148 | nfsm_reqdone; |
f0f1cbaa | 1149 | VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED; |
9238aa59 RM |
1150 | cache_purge(ndp->ni_dvp); |
1151 | cache_purge(ndp->ni_vp); | |
9238aa59 RM |
1152 | nfs_nput(ndp->ni_vp); |
1153 | nfs_nput(ndp->ni_dvp); | |
f5f6c13e KM |
1154 | /* |
1155 | * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. | |
1156 | */ | |
1157 | if (error == ENOENT) | |
1158 | error = 0; | |
a2907882 KM |
1159 | return (error); |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * nfs readdir call | |
1164 | * Although cookie is defined as opaque, I translate it to/from net byte | |
1165 | * order so that it looks more sensible. This appears consistent with the | |
1166 | * Ultrix implementation of NFS. | |
1167 | */ | |
e49b1a6c | 1168 | nfs_readdir(vp, uiop, cred, eofflagp) |
13576453 | 1169 | register struct vnode *vp; |
a2907882 | 1170 | struct uio *uiop; |
a2907882 | 1171 | struct ucred *cred; |
e49b1a6c | 1172 | int *eofflagp; |
f0f1cbaa KM |
1173 | { |
1174 | register struct nfsnode *np = VTONFS(vp); | |
1175 | int tresid, error; | |
1176 | struct vattr vattr; | |
1177 | ||
1178 | if (vp->v_type != VDIR) | |
1179 | return (EPERM); | |
1180 | /* | |
1181 | * First, check for hit on the EOF offset cache | |
1182 | */ | |
1183 | if (uiop->uio_offset != 0 && uiop->uio_offset == np->n_direofoffset && | |
1184 | (np->n_flag & NMODIFIED) == 0 && | |
1185 | nfs_getattr(vp, &vattr, cred) == 0 && | |
1186 | np->n_mtime == vattr.va_mtime.tv_sec) { | |
1187 | *eofflagp = 1; | |
1188 | nfsstats.direofcache_hits++; | |
1189 | return (0); | |
1190 | } | |
1191 | ||
1192 | /* | |
1193 | * Call nfs_bioread() to do the real work. | |
1194 | */ | |
1195 | tresid = uiop->uio_resid; | |
1196 | error = nfs_bioread(vp, uiop, 0, cred); | |
1197 | ||
1198 | if (!error && uiop->uio_resid == tresid) { | |
1199 | *eofflagp = 1; | |
1200 | nfsstats.direofcache_misses++; | |
1201 | } else | |
1202 | *eofflagp = 0; | |
1203 | return (error); | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * Readdir rpc call. | |
1208 | * Called from below the buffer cache by nfs_doio(). | |
1209 | */ | |
1210 | nfs_readdirrpc(vp, uiop, cred, procp) | |
1211 | register struct vnode *vp; | |
1212 | struct uio *uiop; | |
1213 | struct ucred *cred; | |
1214 | struct proc *procp; | |
a2907882 KM |
1215 | { |
1216 | register long len; | |
1217 | register struct direct *dp; | |
13576453 KM |
1218 | register u_long *p; |
1219 | register caddr_t cp; | |
1220 | register long t1; | |
f0f1cbaa | 1221 | long tlen, lastlen; |
13576453 KM |
1222 | caddr_t bpos, dpos, cp2; |
1223 | u_long xid; | |
1224 | int error = 0; | |
1225 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
1226 | struct mbuf *md2; |
1227 | caddr_t dpos2; | |
1228 | int siz; | |
e49b1a6c | 1229 | int more_dirs = 1; |
a2907882 KM |
1230 | off_t off, savoff; |
1231 | struct direct *savdp; | |
e49b1a6c KM |
1232 | struct nfsmount *nmp; |
1233 | struct nfsnode *np = VTONFS(vp); | |
1234 | long tresid; | |
a2907882 | 1235 | |
54fb9dc2 | 1236 | nmp = VFSTONFS(vp->v_mount); |
e49b1a6c | 1237 | tresid = uiop->uio_resid; |
a2907882 | 1238 | /* |
e49b1a6c KM |
1239 | * Loop around doing readdir rpc's of size uio_resid or nm_rsize, |
1240 | * whichever is smaller, truncated to a multiple of DIRBLKSIZ. | |
f0f1cbaa | 1241 | * The stopping criteria is EOF or buffer full. |
a2907882 | 1242 | */ |
e49b1a6c KM |
1243 | while (more_dirs && uiop->uio_resid >= DIRBLKSIZ) { |
1244 | nfsstats.rpccnt[NFSPROC_READDIR]++; | |
1245 | nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid); | |
1246 | nfsm_fhtom(vp); | |
1247 | nfsm_build(p, u_long *, 2*NFSX_UNSIGNED); | |
1248 | *p++ = txdr_unsigned(uiop->uio_offset); | |
1249 | *p = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ? | |
1250 | nmp->nm_rsize : uiop->uio_resid) & ~(DIRBLKSIZ-1)); | |
f0f1cbaa | 1251 | nfsm_request(vp, NFSPROC_READDIR, procp); |
e49b1a6c KM |
1252 | siz = 0; |
1253 | nfsm_disect(p, u_long *, NFSX_UNSIGNED); | |
1254 | more_dirs = fxdr_unsigned(int, *p); | |
1255 | ||
1256 | /* Save the position so that we can do nfsm_mtouio() later */ | |
1257 | dpos2 = dpos; | |
1258 | md2 = md; | |
1259 | ||
1260 | /* loop thru the dir entries, doctoring them to 4bsd form */ | |
f0f1cbaa | 1261 | off = uiop->uio_offset; |
7c15c5df KM |
1262 | #ifdef lint |
1263 | dp = (struct direct *)0; | |
1264 | #endif /* lint */ | |
e49b1a6c KM |
1265 | while (more_dirs && siz < uiop->uio_resid) { |
1266 | savoff = off; /* Hold onto offset and dp */ | |
1267 | savdp = dp; | |
1268 | nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED); | |
1269 | dp = (struct direct *)p; | |
1270 | dp->d_ino = fxdr_unsigned(u_long, *p++); | |
1271 | len = fxdr_unsigned(int, *p); | |
1272 | if (len <= 0 || len > NFS_MAXNAMLEN) { | |
1273 | error = EBADRPC; | |
1274 | m_freem(mrep); | |
1275 | goto nfsmout; | |
1276 | } | |
1277 | dp->d_namlen = (u_short)len; | |
1278 | nfsm_adv(len); /* Point past name */ | |
1279 | tlen = nfsm_rndup(len); | |
1280 | /* | |
1281 | * This should not be necessary, but some servers have | |
1282 | * broken XDR such that these bytes are not null filled. | |
1283 | */ | |
1284 | if (tlen != len) { | |
1285 | *dpos = '\0'; /* Null-terminate */ | |
1286 | nfsm_adv(tlen - len); | |
1287 | len = tlen; | |
1288 | } | |
1289 | nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED); | |
1290 | off = fxdr_unsigned(off_t, *p); | |
1291 | *p++ = 0; /* Ensures null termination of name */ | |
1292 | more_dirs = fxdr_unsigned(int, *p); | |
1293 | dp->d_reclen = len+4*NFSX_UNSIGNED; | |
1294 | siz += dp->d_reclen; | |
1295 | } | |
1296 | /* | |
1297 | * If at end of rpc data, get the eof boolean | |
1298 | */ | |
1299 | if (!more_dirs) { | |
1300 | nfsm_disecton(p, u_long *, NFSX_UNSIGNED); | |
1301 | more_dirs = (fxdr_unsigned(int, *p) == 0); | |
1302 | ||
1303 | /* | |
1304 | * If at EOF, cache directory offset | |
1305 | */ | |
f0f1cbaa | 1306 | if (!more_dirs) |
e49b1a6c | 1307 | np->n_direofoffset = off; |
e49b1a6c KM |
1308 | } |
1309 | /* | |
1310 | * If there is too much to fit in the data buffer, use savoff and | |
1311 | * savdp to trim off the last record. | |
1312 | * --> we are not at eof | |
1313 | */ | |
1314 | if (siz > uiop->uio_resid) { | |
1315 | off = savoff; | |
1316 | siz -= dp->d_reclen; | |
1317 | dp = savdp; | |
1318 | more_dirs = 0; /* Paranoia */ | |
1319 | } | |
1320 | if (siz > 0) { | |
f0f1cbaa | 1321 | lastlen = dp->d_reclen; |
e49b1a6c KM |
1322 | md = md2; |
1323 | dpos = dpos2; | |
1324 | nfsm_mtouio(uiop, siz); | |
1325 | uiop->uio_offset = off; | |
1326 | } else | |
1327 | more_dirs = 0; /* Ugh, never happens, but in case.. */ | |
1328 | m_freem(mrep); | |
a2907882 | 1329 | } |
f0f1cbaa KM |
1330 | /* |
1331 | * Fill last record, iff any, out to a multiple of DIRBLKSIZ | |
1332 | * by increasing d_reclen for the last record. | |
1333 | */ | |
1334 | if (uiop->uio_resid < tresid) { | |
1335 | len = uiop->uio_resid & (DIRBLKSIZ - 1); | |
1336 | if (len > 0) { | |
1337 | dp = (struct direct *) | |
1338 | (uiop->uio_iov->iov_base - lastlen); | |
1339 | dp->d_reclen += len; | |
1340 | uiop->uio_iov->iov_base += len; | |
1341 | uiop->uio_iov->iov_len -= len; | |
1342 | uiop->uio_resid -= len; | |
1343 | } | |
c79a85cd | 1344 | } |
f0f1cbaa | 1345 | nfsmout: |
a2907882 KM |
1346 | return (error); |
1347 | } | |
1348 | ||
13576453 | 1349 | static char hextoasc[] = "0123456789abcdef"; |
a2907882 KM |
1350 | |
1351 | /* | |
1352 | * Silly rename. To make the NFS filesystem that is stateless look a little | |
1353 | * more like the "ufs" a remove of an active vnode is translated to a rename | |
1354 | * to a funny looking filename that is removed by nfs_inactive on the | |
1355 | * nfsnode. There is the potential for another process on a different client | |
1356 | * to create the same funny name between the nfs_lookitup() fails and the | |
1357 | * nfs_rename() completes, but... | |
1358 | */ | |
1359 | nfs_sillyrename(ndp, flag) | |
13576453 | 1360 | register struct nameidata *ndp; |
a2907882 KM |
1361 | int flag; |
1362 | { | |
1363 | register struct nfsnode *np; | |
1364 | register struct sillyrename *sp; | |
1365 | register struct nameidata *tndp; | |
1366 | int error; | |
1367 | short pid; | |
1368 | ||
1369 | np = VTONFS(ndp->ni_dvp); | |
ffe6f482 | 1370 | cache_purge(ndp->ni_dvp); |
a2907882 | 1371 | MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), |
9238aa59 | 1372 | M_TEMP, M_WAITOK); |
a2907882 KM |
1373 | sp->s_flag = flag; |
1374 | bcopy((caddr_t)&np->n_fh, (caddr_t)&sp->s_fh, NFSX_FH); | |
1375 | np = VTONFS(ndp->ni_vp); | |
1376 | tndp = &sp->s_namei; | |
1377 | tndp->ni_cred = crdup(ndp->ni_cred); | |
1378 | ||
1379 | /* Fudge together a funny name */ | |
1380 | pid = u.u_procp->p_pid; | |
1381 | bcopy(".nfsAxxxx4.4", tndp->ni_dent.d_name, 13); | |
1382 | tndp->ni_dent.d_namlen = 12; | |
13576453 KM |
1383 | tndp->ni_dent.d_name[8] = hextoasc[pid & 0xf]; |
1384 | tndp->ni_dent.d_name[7] = hextoasc[(pid >> 4) & 0xf]; | |
1385 | tndp->ni_dent.d_name[6] = hextoasc[(pid >> 8) & 0xf]; | |
1386 | tndp->ni_dent.d_name[5] = hextoasc[(pid >> 12) & 0xf]; | |
a2907882 KM |
1387 | |
1388 | /* Try lookitups until we get one that isn't there */ | |
1389 | while (nfs_lookitup(ndp->ni_dvp, tndp, (nfsv2fh_t *)0) == 0) { | |
1390 | tndp->ni_dent.d_name[4]++; | |
1391 | if (tndp->ni_dent.d_name[4] > 'z') { | |
1392 | error = EINVAL; | |
1393 | goto bad; | |
1394 | } | |
1395 | } | |
1396 | if (error = nfs_renameit(ndp, tndp)) | |
1397 | goto bad; | |
1398 | nfs_lookitup(ndp->ni_dvp, tndp, &np->n_fh); | |
1399 | np->n_sillyrename = sp; | |
1400 | return (0); | |
1401 | bad: | |
9238aa59 | 1402 | crfree(tndp->ni_cred); |
a2907882 KM |
1403 | free((caddr_t)sp, M_TEMP); |
1404 | return (error); | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * Look up a file name for silly rename stuff. | |
1409 | * Just like nfs_lookup() except that it doesn't load returned values | |
1410 | * into the nfsnode table. | |
1411 | * If fhp != NULL it copies the returned file handle out | |
1412 | */ | |
1413 | nfs_lookitup(vp, ndp, fhp) | |
1414 | register struct vnode *vp; | |
1415 | register struct nameidata *ndp; | |
1416 | nfsv2fh_t *fhp; | |
1417 | { | |
13576453 KM |
1418 | register u_long *p; |
1419 | register caddr_t cp; | |
1420 | register long t1, t2; | |
1421 | caddr_t bpos, dpos, cp2; | |
1422 | u_long xid; | |
1423 | int error = 0; | |
1424 | struct mbuf *mreq, *mrep, *md, *mb, *mb2; | |
a2907882 KM |
1425 | long len; |
1426 | ||
1427 | nfsstats.rpccnt[NFSPROC_LOOKUP]++; | |
1428 | ndp->ni_dvp = vp; | |
1429 | ndp->ni_vp = NULL; | |
1430 | len = ndp->ni_dent.d_namlen; | |
1431 | nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)); | |
1432 | nfsm_fhtom(vp); | |
1433 | nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN); | |
f0f1cbaa | 1434 | nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp); |
a2907882 KM |
1435 | if (fhp != NULL) { |
1436 | nfsm_disect(cp, caddr_t, NFSX_FH); | |
1437 | bcopy(cp, (caddr_t)fhp, NFSX_FH); | |
1438 | } | |
1439 | nfsm_reqdone; | |
1440 | return (error); | |
1441 | } | |
1442 | ||
1443 | /* | |
1444 | * Kludge City.. | |
1445 | * - make nfs_bmap() essentially a no-op that does no translation | |
f0f1cbaa | 1446 | * - do nfs_strategy() by faking physical I/O with nfs_readrpc/nfs_writerpc |
a2907882 KM |
1447 | * after mapping the physical addresses into Kernel Virtual space in the |
1448 | * nfsiobuf area. | |
1449 | * (Maybe I could use the process's page mapping, but I was concerned that | |
1450 | * Kernel Write might not be enabled and also figured copyout() would do | |
1451 | * a lot more work than bcopy() and also it currently happens in the | |
1452 | * context of the swapper process (2). | |
1453 | */ | |
1454 | nfs_bmap(vp, bn, vpp, bnp) | |
1455 | struct vnode *vp; | |
1456 | daddr_t bn; | |
1457 | struct vnode **vpp; | |
1458 | daddr_t *bnp; | |
1459 | { | |
1460 | if (vpp != NULL) | |
1461 | *vpp = vp; | |
1462 | if (bnp != NULL) | |
54fb9dc2 | 1463 | *bnp = bn * btodb(vp->v_mount->mnt_stat.f_bsize); |
a2907882 KM |
1464 | return (0); |
1465 | } | |
1466 | ||
1467 | /* | |
9238aa59 RM |
1468 | * Strategy routine for phys. i/o |
1469 | * If the biod's are running, queue a request | |
1470 | * otherwise just call nfs_doio() to get it done | |
a2907882 KM |
1471 | */ |
1472 | nfs_strategy(bp) | |
1473 | register struct buf *bp; | |
9238aa59 RM |
1474 | { |
1475 | register struct buf *dp; | |
ffe6f482 | 1476 | register int i; |
9238aa59 RM |
1477 | struct proc *rp; |
1478 | int error = 0; | |
ffe6f482 | 1479 | int fnd = 0; |
9238aa59 | 1480 | |
f0f1cbaa KM |
1481 | /* |
1482 | * Set b_proc. It seems a bit silly to do it here, but since bread() | |
1483 | * doesn't set it, I will. | |
1484 | * Set b_proc == NULL for asynchronous reads, since these may still | |
1485 | * be hanging about after the process terminates. | |
1486 | */ | |
1487 | if ((bp->b_flags & (B_READ | B_ASYNC)) == (B_READ | B_ASYNC)) | |
1488 | bp->b_proc = (struct proc *)0; | |
1489 | else | |
1490 | bp->b_proc = u.u_procp; | |
1491 | ||
9238aa59 RM |
1492 | /* |
1493 | * If an i/o daemon is waiting | |
1494 | * queue the request, wake it up and wait for completion | |
1495 | * otherwise just do it ourselves | |
1496 | */ | |
ffe6f482 KM |
1497 | for (i = 0; i < nfs_asyncdaemons; i++) { |
1498 | if (rp = nfs_iodwant[i]) { | |
1499 | /* | |
1500 | * Ensure that the async_daemon is still waiting here | |
1501 | */ | |
1502 | if (rp->p_stat != SSLEEP || | |
1503 | rp->p_wchan != ((caddr_t)&nfs_iodwant[i])) { | |
1504 | nfs_iodwant[i] = (struct proc *)0; | |
1505 | continue; | |
1506 | } | |
1507 | dp = &nfs_bqueue; | |
1508 | if (dp->b_actf == NULL) { | |
1509 | dp->b_actl = bp; | |
1510 | bp->b_actf = dp; | |
1511 | } else { | |
1512 | dp->b_actf->b_actl = bp; | |
1513 | bp->b_actf = dp->b_actf; | |
1514 | } | |
1515 | dp->b_actf = bp; | |
1516 | bp->b_actl = dp; | |
1517 | fnd++; | |
1518 | nfs_iodwant[i] = (struct proc *)0; | |
1519 | wakeup((caddr_t)&nfs_iodwant[i]); | |
1520 | break; | |
9238aa59 | 1521 | } |
ffe6f482 KM |
1522 | } |
1523 | if (!fnd) | |
9238aa59 RM |
1524 | error = nfs_doio(bp); |
1525 | return (error); | |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * Fun and games with i/o | |
1530 | * Essentially play ubasetup() and disk interrupt service routine by | |
1531 | * mapping the data buffer into kernel virtual space and doing the | |
1532 | * nfs read or write rpc's from it. | |
f0f1cbaa KM |
1533 | * If the nfsiod's are not running, this is just called from nfs_strategy(), |
1534 | * otherwise it is called by the nfsiods to do what would normally be | |
9238aa59 RM |
1535 | * partially disk interrupt driven. |
1536 | */ | |
1537 | nfs_doio(bp) | |
1538 | register struct buf *bp; | |
a2907882 | 1539 | { |
a2907882 | 1540 | register struct uio *uiop; |
a2907882 | 1541 | register struct vnode *vp; |
ffe6f482 | 1542 | struct nfsnode *np; |
9238aa59 | 1543 | struct ucred *cr; |
a2907882 | 1544 | struct proc *rp; |
7848989c | 1545 | int error; |
a2907882 KM |
1546 | struct uio uio; |
1547 | struct iovec io; | |
7848989c KM |
1548 | #if !defined(hp300) |
1549 | register struct pte *pte, *ppte; | |
1550 | register caddr_t vaddr; | |
1551 | int npf, npf2; | |
1552 | int reg, o; | |
1553 | caddr_t vbase; | |
1554 | unsigned v; | |
1555 | #endif | |
a2907882 KM |
1556 | |
1557 | vp = bp->b_vp; | |
8c379a26 | 1558 | np = VTONFS(vp); |
a2907882 KM |
1559 | uiop = &uio; |
1560 | uiop->uio_iov = &io; | |
1561 | uiop->uio_iovcnt = 1; | |
a2907882 | 1562 | uiop->uio_segflg = UIO_SYSSPACE; |
e8540f59 | 1563 | |
a2907882 | 1564 | /* |
9238aa59 RM |
1565 | * For phys i/o, map the b_addr into kernel virtual space using |
1566 | * the Nfsiomap pte's | |
1567 | * Also, add a temporary b_rcred for reading using the process's uid | |
1568 | * and a guess at a group | |
a2907882 | 1569 | */ |
9238aa59 RM |
1570 | if (bp->b_flags & B_PHYS) { |
1571 | bp->b_rcred = cr = crget(); | |
1572 | rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc; | |
1573 | cr->cr_uid = rp->p_uid; | |
1574 | cr->cr_gid = 0; /* Anything ?? */ | |
1575 | cr->cr_ngroups = 1; | |
7848989c KM |
1576 | #if defined(hp300) |
1577 | /* mapping was already done by vmapbuf */ | |
1578 | io.iov_base = bp->b_un.b_addr; | |
1579 | #else | |
e8540f59 KM |
1580 | o = (int)bp->b_un.b_addr & PGOFSET; |
1581 | npf2 = npf = btoc(bp->b_bcount + o); | |
1582 | ||
9238aa59 RM |
1583 | /* |
1584 | * Get some mapping page table entries | |
1585 | */ | |
1586 | while ((reg = rmalloc(nfsmap, (long)npf)) == 0) { | |
1587 | nfsmap_want++; | |
1588 | sleep((caddr_t)&nfsmap_want, PZERO-1); | |
1589 | } | |
1590 | reg--; | |
e8540f59 KM |
1591 | if (bp->b_flags & B_PAGET) |
1592 | pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)]; | |
9238aa59 | 1593 | else { |
e8540f59 | 1594 | v = btop(bp->b_un.b_addr); |
9238aa59 RM |
1595 | if (bp->b_flags & B_UAREA) |
1596 | pte = &rp->p_addr[v]; | |
1597 | else | |
1598 | pte = vtopte(rp, v); | |
1599 | } | |
e8540f59 | 1600 | |
9238aa59 RM |
1601 | /* |
1602 | * Play vmaccess() but with the Nfsiomap page table | |
1603 | */ | |
1604 | ppte = &Nfsiomap[reg]; | |
1605 | vbase = vaddr = &nfsiobuf[reg*NBPG]; | |
1606 | while (npf != 0) { | |
1607 | mapin(ppte, (u_int)vaddr, pte->pg_pfnum, (int)(PG_V|PG_KW)); | |
a2907882 | 1608 | #if defined(tahoe) |
9238aa59 | 1609 | mtpr(P1DC, vaddr); |
a2907882 | 1610 | #endif |
9238aa59 RM |
1611 | ppte++; |
1612 | pte++; | |
1613 | vaddr += NBPG; | |
1614 | --npf; | |
1615 | } | |
7848989c KM |
1616 | io.iov_base = vbase+o; |
1617 | #endif /* !defined(hp300) */ | |
e8540f59 KM |
1618 | |
1619 | /* | |
1620 | * And do the i/o rpc | |
1621 | */ | |
e8540f59 | 1622 | io.iov_len = uiop->uio_resid = bp->b_bcount; |
589be24d | 1623 | uiop->uio_offset = bp->b_blkno * DEV_BSIZE; |
e8540f59 KM |
1624 | if (bp->b_flags & B_READ) { |
1625 | uiop->uio_rw = UIO_READ; | |
1626 | nfsstats.read_physios++; | |
f0f1cbaa KM |
1627 | bp->b_error = error = nfs_readrpc(vp, uiop, |
1628 | bp->b_rcred, bp->b_proc); | |
8c379a26 KM |
1629 | /* |
1630 | * If a text file has been modified since it was exec'd | |
1631 | * blow the process' out of the water. This is the | |
1632 | * closest we can come to "Text File Busy" in good old | |
1633 | * stateless nfs. | |
1634 | */ | |
1635 | if ((vp->v_flag & VTEXT) && | |
1636 | (vp->v_text->x_mtime != np->n_vattr.va_mtime.tv_sec)) | |
1637 | xinval(vp); | |
e8540f59 KM |
1638 | } else { |
1639 | uiop->uio_rw = UIO_WRITE; | |
1640 | nfsstats.write_physios++; | |
f0f1cbaa KM |
1641 | bp->b_error = error = nfs_writerpc(vp, uiop, |
1642 | bp->b_wcred, bp->b_proc); | |
ffe6f482 | 1643 | } |
e8540f59 KM |
1644 | |
1645 | /* | |
1646 | * Finally, release pte's used by physical i/o | |
1647 | */ | |
9238aa59 | 1648 | crfree(cr); |
7848989c | 1649 | #if !defined(hp300) |
9238aa59 RM |
1650 | rmfree(nfsmap, (long)npf2, (long)++reg); |
1651 | if (nfsmap_want) { | |
1652 | nfsmap_want = 0; | |
1653 | wakeup((caddr_t)&nfsmap_want); | |
1654 | } | |
7848989c | 1655 | #endif |
e8540f59 KM |
1656 | } else { |
1657 | if (bp->b_flags & B_READ) { | |
1658 | io.iov_len = uiop->uio_resid = bp->b_bcount; | |
e8540f59 KM |
1659 | io.iov_base = bp->b_un.b_addr; |
1660 | uiop->uio_rw = UIO_READ; | |
f0f1cbaa KM |
1661 | switch (vp->v_type) { |
1662 | case VREG: | |
1663 | uiop->uio_offset = bp->b_blkno * DEV_BSIZE; | |
1664 | nfsstats.read_bios++; | |
1665 | error = nfs_readrpc(vp, uiop, bp->b_rcred, | |
1666 | bp->b_proc); | |
1667 | break; | |
1668 | case VLNK: | |
1669 | uiop->uio_offset = 0; | |
1670 | nfsstats.readlink_bios++; | |
1671 | error = nfs_readlinkrpc(vp, uiop, bp->b_rcred, | |
1672 | bp->b_proc); | |
1673 | break; | |
1674 | case VDIR: | |
1675 | uiop->uio_offset = bp->b_lblkno; | |
1676 | nfsstats.readdir_bios++; | |
1677 | error = nfs_readdirrpc(vp, uiop, bp->b_rcred, | |
1678 | bp->b_proc); | |
1679 | /* | |
1680 | * Save offset cookie in b_blkno. | |
1681 | */ | |
1682 | bp->b_blkno = uiop->uio_offset; | |
1683 | break; | |
1684 | }; | |
1685 | bp->b_error = error; | |
e8540f59 KM |
1686 | } else { |
1687 | io.iov_len = uiop->uio_resid = bp->b_dirtyend | |
1688 | - bp->b_dirtyoff; | |
589be24d | 1689 | uiop->uio_offset = (bp->b_blkno * DEV_BSIZE) |
e8540f59 KM |
1690 | + bp->b_dirtyoff; |
1691 | io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff; | |
1692 | uiop->uio_rw = UIO_WRITE; | |
1693 | nfsstats.write_bios++; | |
f0f1cbaa KM |
1694 | bp->b_error = error = nfs_writerpc(vp, uiop, |
1695 | bp->b_wcred, bp->b_proc); | |
e8540f59 | 1696 | if (error) { |
e8540f59 KM |
1697 | np->n_error = error; |
1698 | np->n_flag |= NWRITEERR; | |
1699 | } | |
1700 | bp->b_dirtyoff = bp->b_dirtyend = 0; | |
1701 | } | |
9238aa59 | 1702 | } |
e8540f59 KM |
1703 | if (error) |
1704 | bp->b_flags |= B_ERROR; | |
1705 | bp->b_resid = uiop->uio_resid; | |
a2907882 | 1706 | biodone(bp); |
9238aa59 RM |
1707 | return (error); |
1708 | } | |
1709 | ||
1710 | /* | |
1711 | * Flush all the blocks associated with a vnode. | |
1712 | * Walk through the buffer pool and push any dirty pages | |
1713 | * associated with the vnode. | |
1714 | */ | |
13576453 | 1715 | /* ARGSUSED */ |
f8428623 | 1716 | nfs_fsync(vp, fflags, cred, waitfor) |
9238aa59 RM |
1717 | register struct vnode *vp; |
1718 | int fflags; | |
1719 | struct ucred *cred; | |
f8428623 | 1720 | int waitfor; |
9238aa59 RM |
1721 | { |
1722 | register struct nfsnode *np = VTONFS(vp); | |
e8540f59 | 1723 | int error = 0; |
9238aa59 | 1724 | |
9238aa59 RM |
1725 | if (np->n_flag & NMODIFIED) { |
1726 | np->n_flag &= ~NMODIFIED; | |
e8540f59 | 1727 | vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); |
9238aa59 | 1728 | } |
e8540f59 KM |
1729 | if (!error && (np->n_flag & NWRITEERR)) |
1730 | error = np->n_error; | |
a2907882 KM |
1731 | return (error); |
1732 | } | |
d002709d KM |
1733 | |
1734 | /* | |
1735 | * Print out the contents of an nfsnode. | |
1736 | */ | |
1737 | nfs_print(vp) | |
1738 | struct vnode *vp; | |
1739 | { | |
1740 | register struct nfsnode *np = VTONFS(vp); | |
1741 | ||
b2d955e4 KM |
1742 | printf("tag VT_NFS, fileid %d fsid 0x%x", |
1743 | np->n_vattr.va_fileid, np->n_vattr.va_fsid); | |
1744 | #ifdef FIFO | |
1745 | if (vp->v_type == VFIFO) | |
1746 | fifo_printinfo(vp); | |
1747 | #endif /* FIFO */ | |
1748 | printf("%s\n", (np->n_flag & NLOCKED) ? " (LOCKED)" : ""); | |
1a1dbccb KM |
1749 | if (np->n_lockholder == 0) |
1750 | return; | |
1751 | printf("\towner pid %d", np->n_lockholder); | |
1752 | if (np->n_lockwaiter) | |
1753 | printf(" waiting pid %d", np->n_lockwaiter); | |
1754 | printf("\n"); | |
d002709d | 1755 | } |