Print this page
5045 use atomic_{inc,dec}_* instead of atomic_add_*
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs3_vnops.c
+++ new/usr/src/uts/common/fs/nfs/nfs3_vnops.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28 28 * All rights reserved.
29 29 */
30 30
31 31 /*
32 32 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33 33 */
34 34
35 35 #include <sys/param.h>
36 36 #include <sys/types.h>
37 37 #include <sys/systm.h>
38 38 #include <sys/cred.h>
39 39 #include <sys/time.h>
40 40 #include <sys/vnode.h>
41 41 #include <sys/vfs.h>
42 42 #include <sys/vfs_opreg.h>
43 43 #include <sys/file.h>
44 44 #include <sys/filio.h>
45 45 #include <sys/uio.h>
46 46 #include <sys/buf.h>
47 47 #include <sys/mman.h>
48 48 #include <sys/pathname.h>
49 49 #include <sys/dirent.h>
50 50 #include <sys/debug.h>
51 51 #include <sys/vmsystm.h>
52 52 #include <sys/fcntl.h>
53 53 #include <sys/flock.h>
54 54 #include <sys/swap.h>
55 55 #include <sys/errno.h>
56 56 #include <sys/strsubr.h>
57 57 #include <sys/sysmacros.h>
58 58 #include <sys/kmem.h>
59 59 #include <sys/cmn_err.h>
60 60 #include <sys/pathconf.h>
61 61 #include <sys/utsname.h>
62 62 #include <sys/dnlc.h>
63 63 #include <sys/acl.h>
64 64 #include <sys/systeminfo.h>
65 65 #include <sys/atomic.h>
66 66 #include <sys/policy.h>
67 67 #include <sys/sdt.h>
68 68 #include <sys/zone.h>
69 69
70 70 #include <rpc/types.h>
71 71 #include <rpc/auth.h>
72 72 #include <rpc/clnt.h>
73 73 #include <rpc/rpc_rdma.h>
74 74
75 75 #include <nfs/nfs.h>
76 76 #include <nfs/nfs_clnt.h>
77 77 #include <nfs/rnode.h>
78 78 #include <nfs/nfs_acl.h>
79 79 #include <nfs/lm.h>
80 80
81 81 #include <vm/hat.h>
82 82 #include <vm/as.h>
83 83 #include <vm/page.h>
84 84 #include <vm/pvn.h>
85 85 #include <vm/seg.h>
86 86 #include <vm/seg_map.h>
87 87 #include <vm/seg_kpm.h>
88 88 #include <vm/seg_vn.h>
89 89
90 90 #include <fs/fs_subr.h>
91 91
92 92 #include <sys/ddi.h>
93 93
94 94 static int nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
95 95 cred_t *);
96 96 static int nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
97 97 stable_how *);
98 98 static int nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *);
99 99 static int nfs3setattr(vnode_t *, struct vattr *, int, cred_t *);
100 100 static int nfs3_accessx(void *, int, cred_t *);
101 101 static int nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
102 102 static int nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
103 103 static int nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl,
104 104 int, vnode_t **, cred_t *, int);
105 105 static int nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *);
106 106 static int nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
107 107 int, vnode_t **, cred_t *);
108 108 static int nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
109 109 caller_context_t *);
110 110 static int do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
111 111 static void nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
112 112 static void nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *);
113 113 static int nfs3_bio(struct buf *, stable_how *, cred_t *);
114 114 static int nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
115 115 page_t *[], size_t, struct seg *, caddr_t,
116 116 enum seg_rw, cred_t *);
117 117 static void nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
118 118 cred_t *);
119 119 static int nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
120 120 int, cred_t *);
121 121 static int nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
122 122 int, cred_t *);
123 123 static int nfs3_commit(vnode_t *, offset3, count3, cred_t *);
124 124 static void nfs3_set_mod(vnode_t *);
125 125 static void nfs3_get_commit(vnode_t *);
126 126 static void nfs3_get_commit_range(vnode_t *, u_offset_t, size_t);
127 127 static int nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
128 128 static int nfs3_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *);
129 129 static int nfs3_sync_commit(vnode_t *, page_t *, offset3, count3,
130 130 cred_t *);
131 131 static void nfs3_async_commit(vnode_t *, page_t *, offset3, count3,
132 132 cred_t *);
133 133 static void nfs3_delmap_callback(struct as *, void *, uint_t);
134 134
135 135 /*
136 136 * Error flags used to pass information about certain special errors
137 137 * which need to be handled specially.
138 138 */
139 139 #define NFS_EOF -98
140 140 #define NFS_VERF_MISMATCH -97
141 141
142 142 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
143 143 #define ALIGN64(x, ptr, sz) \
144 144 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
145 145 if (x) { \
146 146 x = sizeof (uint64_t) - (x); \
147 147 sz -= (x); \
148 148 ptr += (x); \
149 149 }
150 150
151 151 /*
152 152 * These are the vnode ops routines which implement the vnode interface to
153 153 * the networked file system. These routines just take their parameters,
154 154 * make them look networkish by putting the right info into interface structs,
155 155 * and then calling the appropriate remote routine(s) to do the work.
156 156 *
157 157 * Note on directory name lookup cacheing: If we detect a stale fhandle,
158 158 * we purge the directory cache relative to that vnode. This way, the
159 159 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for
160 160 * more details on rnode locking.
161 161 */
162 162
163 163 static int nfs3_open(vnode_t **, int, cred_t *, caller_context_t *);
164 164 static int nfs3_close(vnode_t *, int, int, offset_t, cred_t *,
165 165 caller_context_t *);
166 166 static int nfs3_read(vnode_t *, struct uio *, int, cred_t *,
167 167 caller_context_t *);
168 168 static int nfs3_write(vnode_t *, struct uio *, int, cred_t *,
169 169 caller_context_t *);
170 170 static int nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
171 171 caller_context_t *);
172 172 static int nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *,
173 173 caller_context_t *);
174 174 static int nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *,
175 175 caller_context_t *);
176 176 static int nfs3_access(vnode_t *, int, int, cred_t *, caller_context_t *);
177 177 static int nfs3_readlink(vnode_t *, struct uio *, cred_t *,
178 178 caller_context_t *);
179 179 static int nfs3_fsync(vnode_t *, int, cred_t *, caller_context_t *);
180 180 static void nfs3_inactive(vnode_t *, cred_t *, caller_context_t *);
181 181 static int nfs3_lookup(vnode_t *, char *, vnode_t **,
182 182 struct pathname *, int, vnode_t *, cred_t *,
183 183 caller_context_t *, int *, pathname_t *);
184 184 static int nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl,
185 185 int, vnode_t **, cred_t *, int, caller_context_t *,
186 186 vsecattr_t *);
187 187 static int nfs3_remove(vnode_t *, char *, cred_t *, caller_context_t *,
188 188 int);
189 189 static int nfs3_link(vnode_t *, vnode_t *, char *, cred_t *,
190 190 caller_context_t *, int);
191 191 static int nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
192 192 caller_context_t *, int);
193 193 static int nfs3_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
194 194 cred_t *, caller_context_t *, int, vsecattr_t *);
195 195 static int nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
196 196 caller_context_t *, int);
197 197 static int nfs3_symlink(vnode_t *, char *, struct vattr *, char *,
198 198 cred_t *, caller_context_t *, int);
199 199 static int nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *,
200 200 caller_context_t *, int);
201 201 static int nfs3_fid(vnode_t *, fid_t *, caller_context_t *);
202 202 static int nfs3_rwlock(vnode_t *, int, caller_context_t *);
203 203 static void nfs3_rwunlock(vnode_t *, int, caller_context_t *);
204 204 static int nfs3_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
205 205 static int nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *,
206 206 page_t *[], size_t, struct seg *, caddr_t,
207 207 enum seg_rw, cred_t *, caller_context_t *);
208 208 static int nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
209 209 caller_context_t *);
210 210 static int nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
211 211 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
212 212 static int nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
213 213 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
214 214 static int nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
215 215 struct flk_callback *, cred_t *, caller_context_t *);
216 216 static int nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t,
217 217 cred_t *, caller_context_t *);
218 218 static int nfs3_realvp(vnode_t *, vnode_t **, caller_context_t *);
219 219 static int nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
220 220 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
221 221 static int nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *,
222 222 caller_context_t *);
223 223 static int nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
224 224 cred_t *, caller_context_t *);
225 225 static void nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *,
226 226 caller_context_t *);
227 227 static int nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
228 228 caller_context_t *);
229 229 static int nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
230 230 caller_context_t *);
231 231 static int nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
232 232 caller_context_t *);
233 233
234 234 struct vnodeops *nfs3_vnodeops;
235 235
236 236 const fs_operation_def_t nfs3_vnodeops_template[] = {
237 237 VOPNAME_OPEN, { .vop_open = nfs3_open },
238 238 VOPNAME_CLOSE, { .vop_close = nfs3_close },
239 239 VOPNAME_READ, { .vop_read = nfs3_read },
240 240 VOPNAME_WRITE, { .vop_write = nfs3_write },
241 241 VOPNAME_IOCTL, { .vop_ioctl = nfs3_ioctl },
242 242 VOPNAME_GETATTR, { .vop_getattr = nfs3_getattr },
243 243 VOPNAME_SETATTR, { .vop_setattr = nfs3_setattr },
244 244 VOPNAME_ACCESS, { .vop_access = nfs3_access },
245 245 VOPNAME_LOOKUP, { .vop_lookup = nfs3_lookup },
246 246 VOPNAME_CREATE, { .vop_create = nfs3_create },
247 247 VOPNAME_REMOVE, { .vop_remove = nfs3_remove },
248 248 VOPNAME_LINK, { .vop_link = nfs3_link },
249 249 VOPNAME_RENAME, { .vop_rename = nfs3_rename },
250 250 VOPNAME_MKDIR, { .vop_mkdir = nfs3_mkdir },
251 251 VOPNAME_RMDIR, { .vop_rmdir = nfs3_rmdir },
252 252 VOPNAME_READDIR, { .vop_readdir = nfs3_readdir },
253 253 VOPNAME_SYMLINK, { .vop_symlink = nfs3_symlink },
254 254 VOPNAME_READLINK, { .vop_readlink = nfs3_readlink },
255 255 VOPNAME_FSYNC, { .vop_fsync = nfs3_fsync },
256 256 VOPNAME_INACTIVE, { .vop_inactive = nfs3_inactive },
257 257 VOPNAME_FID, { .vop_fid = nfs3_fid },
258 258 VOPNAME_RWLOCK, { .vop_rwlock = nfs3_rwlock },
259 259 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs3_rwunlock },
260 260 VOPNAME_SEEK, { .vop_seek = nfs3_seek },
261 261 VOPNAME_FRLOCK, { .vop_frlock = nfs3_frlock },
262 262 VOPNAME_SPACE, { .vop_space = nfs3_space },
263 263 VOPNAME_REALVP, { .vop_realvp = nfs3_realvp },
264 264 VOPNAME_GETPAGE, { .vop_getpage = nfs3_getpage },
265 265 VOPNAME_PUTPAGE, { .vop_putpage = nfs3_putpage },
266 266 VOPNAME_MAP, { .vop_map = nfs3_map },
267 267 VOPNAME_ADDMAP, { .vop_addmap = nfs3_addmap },
268 268 VOPNAME_DELMAP, { .vop_delmap = nfs3_delmap },
269 269 /* no separate nfs3_dump */
270 270 VOPNAME_DUMP, { .vop_dump = nfs_dump },
271 271 VOPNAME_PATHCONF, { .vop_pathconf = nfs3_pathconf },
272 272 VOPNAME_PAGEIO, { .vop_pageio = nfs3_pageio },
273 273 VOPNAME_DISPOSE, { .vop_dispose = nfs3_dispose },
274 274 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs3_setsecattr },
275 275 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs3_getsecattr },
276 276 VOPNAME_SHRLOCK, { .vop_shrlock = nfs3_shrlock },
277 277 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
278 278 NULL, NULL
279 279 };
280 280
281 281 /*
282 282 * XXX: This is referenced in modstubs.s
283 283 */
284 284 struct vnodeops *
285 285 nfs3_getvnodeops(void)
286 286 {
287 287 return (nfs3_vnodeops);
288 288 }
289 289
290 290 /* ARGSUSED */
291 291 static int
292 292 nfs3_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
293 293 {
294 294 int error;
295 295 struct vattr va;
296 296 rnode_t *rp;
297 297 vnode_t *vp;
298 298
299 299 vp = *vpp;
300 300 if (nfs_zone() != VTOMI(vp)->mi_zone)
301 301 return (EIO);
302 302 rp = VTOR(vp);
303 303 mutex_enter(&rp->r_statelock);
304 304 if (rp->r_cred == NULL) {
305 305 crhold(cr);
306 306 rp->r_cred = cr;
307 307 }
308 308 mutex_exit(&rp->r_statelock);
309 309
310 310 /*
311 311 * If there is no cached data or if close-to-open
312 312 * consistency checking is turned off, we can avoid
313 313 * the over the wire getattr. Otherwise, if the
314 314 * file system is mounted readonly, then just verify
315 315 * the caches are up to date using the normal mechanism.
316 316 * Else, if the file is not mmap'd, then just mark
317 317 * the attributes as timed out. They will be refreshed
318 318 * and the caches validated prior to being used.
319 319 * Else, the file system is mounted writeable so
320 320 * force an over the wire GETATTR in order to ensure
321 321 * that all cached data is valid.
322 322 */
323 323 if (vp->v_count > 1 ||
324 324 ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
325 325 !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
326 326 if (vn_is_readonly(vp))
327 327 error = nfs3_validate_caches(vp, cr);
328 328 else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
329 329 PURGE_ATTRCACHE(vp);
330 330 error = 0;
331 331 } else {
332 332 va.va_mask = AT_ALL;
333 333 error = nfs3_getattr_otw(vp, &va, cr);
334 334 }
335 335 } else
336 336 error = 0;
337 337
338 338 return (error);
339 339 }
340 340
341 341 /* ARGSUSED */
342 342 static int
343 343 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
344 344 caller_context_t *ct)
345 345 {
346 346 rnode_t *rp;
347 347 int error;
348 348 struct vattr va;
349 349
350 350 /*
351 351 * zone_enter(2) prevents processes from changing zones with NFS files
352 352 * open; if we happen to get here from the wrong zone we can't do
353 353 * anything over the wire.
354 354 */
355 355 if (VTOMI(vp)->mi_zone != nfs_zone()) {
356 356 /*
357 357 * We could attempt to clean up locks, except we're sure
358 358 * that the current process didn't acquire any locks on
359 359 * the file: any attempt to lock a file belong to another zone
360 360 * will fail, and one can't lock an NFS file and then change
361 361 * zones, as that fails too.
362 362 *
363 363 * Returning an error here is the sane thing to do. A
364 364 * subsequent call to VN_RELE() which translates to a
365 365 * nfs3_inactive() will clean up state: if the zone of the
366 366 * vnode's origin is still alive and kicking, an async worker
367 367 * thread will handle the request (from the correct zone), and
368 368 * everything (minus the commit and final nfs3_getattr_otw()
369 369 * call) should be OK. If the zone is going away
370 370 * nfs_async_inactive() will throw away cached pages inline.
371 371 */
372 372 return (EIO);
373 373 }
374 374
375 375 /*
376 376 * If we are using local locking for this filesystem, then
377 377 * release all of the SYSV style record locks. Otherwise,
378 378 * we are doing network locking and we need to release all
379 379 * of the network locks. All of the locks held by this
380 380 * process on this file are released no matter what the
381 381 * incoming reference count is.
382 382 */
383 383 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
384 384 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
385 385 cleanshares(vp, ttoproc(curthread)->p_pid);
386 386 } else
387 387 nfs_lockrelease(vp, flag, offset, cr);
388 388
389 389 if (count > 1)
390 390 return (0);
391 391
392 392 /*
393 393 * If the file has been `unlinked', then purge the
394 394 * DNLC so that this vnode will get reycled quicker
395 395 * and the .nfs* file on the server will get removed.
396 396 */
397 397 rp = VTOR(vp);
398 398 if (rp->r_unldvp != NULL)
399 399 dnlc_purge_vp(vp);
400 400
401 401 /*
402 402 * If the file was open for write and there are pages,
403 403 * then if the file system was mounted using the "no-close-
404 404 * to-open" semantics, then start an asynchronous flush
405 405 * of the all of the pages in the file.
406 406 * else the file system was not mounted using the "no-close-
407 407 * to-open" semantics, then do a synchronous flush and
408 408 * commit of all of the dirty and uncommitted pages.
409 409 *
410 410 * The asynchronous flush of the pages in the "nocto" path
411 411 * mostly just associates a cred pointer with the rnode so
412 412 * writes which happen later will have a better chance of
413 413 * working. It also starts the data being written to the
414 414 * server, but without unnecessarily delaying the application.
415 415 */
416 416 if ((flag & FWRITE) && vn_has_cached_data(vp)) {
417 417 if (VTOMI(vp)->mi_flags & MI_NOCTO) {
418 418 error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC,
419 419 cr, ct);
420 420 if (error == EAGAIN)
421 421 error = 0;
422 422 } else
423 423 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
424 424 if (!error) {
425 425 mutex_enter(&rp->r_statelock);
426 426 error = rp->r_error;
427 427 rp->r_error = 0;
428 428 mutex_exit(&rp->r_statelock);
429 429 }
430 430 } else {
431 431 mutex_enter(&rp->r_statelock);
432 432 error = rp->r_error;
433 433 rp->r_error = 0;
434 434 mutex_exit(&rp->r_statelock);
435 435 }
436 436
437 437 /*
438 438 * If RWRITEATTR is set, then issue an over the wire GETATTR to
439 439 * refresh the attribute cache with a set of attributes which
440 440 * weren't returned from a WRITE. This will enable the close-
441 441 * to-open processing to work.
442 442 */
443 443 if (rp->r_flags & RWRITEATTR)
444 444 (void) nfs3_getattr_otw(vp, &va, cr);
445 445
446 446 return (error);
447 447 }
448 448
449 449 /* ARGSUSED */
450 450 static int
451 451 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr)
452 452 {
453 453 mntinfo_t *mi;
454 454 READ3args args;
455 455 READ3uiores res;
456 456 int tsize;
457 457 offset_t offset;
458 458 ssize_t count;
459 459 int error;
460 460 int douprintf;
461 461 failinfo_t fi;
462 462 char *sv_hostname;
463 463
464 464 mi = VTOMI(vp);
465 465 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
466 466 sv_hostname = VTOR(vp)->r_server->sv_hostname;
467 467
468 468 douprintf = 1;
469 469 args.file = *VTOFH3(vp);
470 470 fi.vp = vp;
471 471 fi.fhp = (caddr_t)&args.file;
472 472 fi.copyproc = nfs3copyfh;
473 473 fi.lookupproc = nfs3lookup;
474 474 fi.xattrdirproc = acl_getxattrdir3;
475 475
476 476 res.uiop = uiop;
477 477
478 478 res.wlist = NULL;
479 479
480 480 offset = uiop->uio_loffset;
481 481 count = uiop->uio_resid;
482 482
483 483 do {
484 484 if (mi->mi_io_kstats) {
485 485 mutex_enter(&mi->mi_lock);
486 486 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
487 487 mutex_exit(&mi->mi_lock);
488 488 }
489 489
490 490 do {
491 491 tsize = MIN(mi->mi_tsize, count);
492 492 args.offset = (offset3)offset;
493 493 args.count = (count3)tsize;
494 494 res.size = (uint_t)tsize;
495 495 args.res_uiop = uiop;
496 496 args.res_data_val_alt = NULL;
497 497
498 498 error = rfs3call(mi, NFSPROC3_READ,
499 499 xdr_READ3args, (caddr_t)&args,
500 500 xdr_READ3uiores, (caddr_t)&res, cr,
501 501 &douprintf, &res.status, 0, &fi);
502 502 } while (error == ENFS_TRYAGAIN);
503 503
504 504 if (mi->mi_io_kstats) {
505 505 mutex_enter(&mi->mi_lock);
506 506 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
507 507 mutex_exit(&mi->mi_lock);
508 508 }
509 509
510 510 if (error)
511 511 return (error);
512 512
513 513 error = geterrno3(res.status);
514 514 if (error)
515 515 return (error);
516 516
517 517 if (res.count != res.size) {
518 518 zcmn_err(getzoneid(), CE_WARN,
519 519 "nfs3_directio_read: server %s returned incorrect amount",
520 520 sv_hostname);
521 521 return (EIO);
522 522 }
523 523 count -= res.count;
524 524 offset += res.count;
525 525 if (mi->mi_io_kstats) {
526 526 mutex_enter(&mi->mi_lock);
527 527 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
528 528 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
529 529 mutex_exit(&mi->mi_lock);
530 530 }
531 531 lwp_stat_update(LWP_STAT_INBLK, 1);
532 532 } while (count && !res.eof);
533 533
534 534 return (0);
535 535 }
536 536
537 537 /* ARGSUSED */
538 538 static int
539 539 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
540 540 caller_context_t *ct)
541 541 {
542 542 rnode_t *rp;
543 543 u_offset_t off;
544 544 offset_t diff;
545 545 int on;
546 546 size_t n;
547 547 caddr_t base;
548 548 uint_t flags;
549 549 int error = 0;
550 550 mntinfo_t *mi;
551 551
552 552 rp = VTOR(vp);
553 553 mi = VTOMI(vp);
554 554
555 555 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
556 556
557 557 if (nfs_zone() != mi->mi_zone)
558 558 return (EIO);
559 559
560 560 if (vp->v_type != VREG)
561 561 return (EISDIR);
562 562
563 563 if (uiop->uio_resid == 0)
564 564 return (0);
565 565
566 566 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
567 567 return (EINVAL);
568 568
569 569 /*
570 570 * Bypass VM if caching has been disabled (e.g., locking) or if
571 571 * using client-side direct I/O and the file is not mmap'd and
572 572 * there are no cached pages.
573 573 */
574 574 if ((vp->v_flag & VNOCACHE) ||
575 575 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
576 576 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
577 577 !vn_has_cached_data(vp))) {
578 578 return (nfs3_directio_read(vp, uiop, cr));
579 579 }
580 580
581 581 do {
582 582 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
583 583 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
584 584 n = MIN(MAXBSIZE - on, uiop->uio_resid);
585 585
586 586 error = nfs3_validate_caches(vp, cr);
587 587 if (error)
588 588 break;
589 589
590 590 mutex_enter(&rp->r_statelock);
591 591 while (rp->r_flags & RINCACHEPURGE) {
592 592 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
593 593 mutex_exit(&rp->r_statelock);
594 594 return (EINTR);
595 595 }
596 596 }
597 597 diff = rp->r_size - uiop->uio_loffset;
598 598 mutex_exit(&rp->r_statelock);
599 599 if (diff <= 0)
600 600 break;
601 601 if (diff < n)
602 602 n = (size_t)diff;
603 603
604 604 if (vpm_enable) {
605 605 /*
606 606 * Copy data.
607 607 */
608 608 error = vpm_data_copy(vp, off + on, n, uiop,
609 609 1, NULL, 0, S_READ);
610 610 } else {
611 611 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
612 612 S_READ);
613 613
614 614 error = uiomove(base + on, n, UIO_READ, uiop);
615 615 }
616 616
617 617 if (!error) {
618 618 /*
619 619 * If read a whole block or read to eof,
620 620 * won't need this buffer again soon.
621 621 */
622 622 mutex_enter(&rp->r_statelock);
623 623 if (n + on == MAXBSIZE ||
624 624 uiop->uio_loffset == rp->r_size)
625 625 flags = SM_DONTNEED;
626 626 else
627 627 flags = 0;
628 628 mutex_exit(&rp->r_statelock);
629 629 if (vpm_enable) {
630 630 error = vpm_sync_pages(vp, off, n, flags);
631 631 } else {
632 632 error = segmap_release(segkmap, base, flags);
633 633 }
634 634 } else {
635 635 if (vpm_enable) {
636 636 (void) vpm_sync_pages(vp, off, n, 0);
637 637 } else {
638 638 (void) segmap_release(segkmap, base, 0);
639 639 }
640 640 }
641 641 } while (!error && uiop->uio_resid > 0);
642 642
643 643 return (error);
644 644 }
645 645
646 646 /* ARGSUSED */
647 647 static int
648 648 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
649 649 caller_context_t *ct)
650 650 {
651 651 rlim64_t limit = uiop->uio_llimit;
652 652 rnode_t *rp;
653 653 u_offset_t off;
654 654 caddr_t base;
655 655 uint_t flags;
656 656 int remainder;
657 657 size_t n;
658 658 int on;
659 659 int error;
660 660 int resid;
661 661 offset_t offset;
662 662 mntinfo_t *mi;
663 663 uint_t bsize;
664 664
665 665 rp = VTOR(vp);
666 666
667 667 if (vp->v_type != VREG)
668 668 return (EISDIR);
669 669
670 670 mi = VTOMI(vp);
671 671 if (nfs_zone() != mi->mi_zone)
672 672 return (EIO);
673 673 if (uiop->uio_resid == 0)
674 674 return (0);
675 675
676 676 if (ioflag & FAPPEND) {
677 677 struct vattr va;
678 678
679 679 /*
680 680 * Must serialize if appending.
681 681 */
682 682 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
683 683 nfs_rw_exit(&rp->r_rwlock);
684 684 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
685 685 INTR(vp)))
686 686 return (EINTR);
687 687 }
688 688
689 689 va.va_mask = AT_SIZE;
690 690 error = nfs3getattr(vp, &va, cr);
691 691 if (error)
692 692 return (error);
693 693 uiop->uio_loffset = va.va_size;
694 694 }
695 695
696 696 offset = uiop->uio_loffset + uiop->uio_resid;
697 697
698 698 if (uiop->uio_loffset < 0 || offset < 0)
699 699 return (EINVAL);
700 700
701 701 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
702 702 limit = MAXOFFSET_T;
703 703
704 704 /*
705 705 * Check to make sure that the process will not exceed
706 706 * its limit on file size. It is okay to write up to
707 707 * the limit, but not beyond. Thus, the write which
708 708 * reaches the limit will be short and the next write
709 709 * will return an error.
710 710 */
711 711 remainder = 0;
712 712 if (offset > limit) {
713 713 remainder = offset - limit;
714 714 uiop->uio_resid = limit - uiop->uio_loffset;
715 715 if (uiop->uio_resid <= 0) {
716 716 proc_t *p = ttoproc(curthread);
717 717
718 718 uiop->uio_resid += remainder;
719 719 mutex_enter(&p->p_lock);
720 720 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
721 721 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
722 722 mutex_exit(&p->p_lock);
723 723 return (EFBIG);
724 724 }
725 725 }
726 726
727 727 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
728 728 return (EINTR);
729 729
730 730 /*
731 731 * Bypass VM if caching has been disabled (e.g., locking) or if
732 732 * using client-side direct I/O and the file is not mmap'd and
733 733 * there are no cached pages.
734 734 */
735 735 if ((vp->v_flag & VNOCACHE) ||
736 736 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
737 737 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
738 738 !vn_has_cached_data(vp))) {
739 739 size_t bufsize;
740 740 int count;
741 741 u_offset_t org_offset;
742 742 stable_how stab_comm;
743 743
744 744 nfs3_fwrite:
745 745 if (rp->r_flags & RSTALE) {
746 746 resid = uiop->uio_resid;
747 747 offset = uiop->uio_loffset;
748 748 error = rp->r_error;
749 749 /*
750 750 * A close may have cleared r_error, if so,
751 751 * propagate ESTALE error return properly
752 752 */
753 753 if (error == 0)
754 754 error = ESTALE;
755 755 goto bottom;
756 756 }
757 757 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
758 758 base = kmem_alloc(bufsize, KM_SLEEP);
759 759 do {
760 760 if (ioflag & FDSYNC)
761 761 stab_comm = DATA_SYNC;
762 762 else
763 763 stab_comm = FILE_SYNC;
764 764 resid = uiop->uio_resid;
765 765 offset = uiop->uio_loffset;
766 766 count = MIN(uiop->uio_resid, bufsize);
767 767 org_offset = uiop->uio_loffset;
768 768 error = uiomove(base, count, UIO_WRITE, uiop);
769 769 if (!error) {
770 770 error = nfs3write(vp, base, org_offset,
771 771 count, cr, &stab_comm);
772 772 }
773 773 } while (!error && uiop->uio_resid > 0);
774 774 kmem_free(base, bufsize);
775 775 goto bottom;
776 776 }
777 777
778 778
779 779 bsize = vp->v_vfsp->vfs_bsize;
780 780
781 781 do {
782 782 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
783 783 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
784 784 n = MIN(MAXBSIZE - on, uiop->uio_resid);
785 785
786 786 resid = uiop->uio_resid;
787 787 offset = uiop->uio_loffset;
788 788
789 789 if (rp->r_flags & RSTALE) {
790 790 error = rp->r_error;
791 791 /*
792 792 * A close may have cleared r_error, if so,
793 793 * propagate ESTALE error return properly
794 794 */
795 795 if (error == 0)
796 796 error = ESTALE;
797 797 break;
798 798 }
799 799
800 800 /*
801 801 * Don't create dirty pages faster than they
802 802 * can be cleaned so that the system doesn't
803 803 * get imbalanced. If the async queue is
804 804 * maxed out, then wait for it to drain before
805 805 * creating more dirty pages. Also, wait for
806 806 * any threads doing pagewalks in the vop_getattr
807 807 * entry points so that they don't block for
808 808 * long periods.
809 809 */
810 810 mutex_enter(&rp->r_statelock);
811 811 while ((mi->mi_max_threads != 0 &&
812 812 rp->r_awcount > 2 * mi->mi_max_threads) ||
813 813 rp->r_gcount > 0) {
814 814 if (INTR(vp)) {
815 815 klwp_t *lwp = ttolwp(curthread);
816 816
817 817 if (lwp != NULL)
818 818 lwp->lwp_nostop++;
819 819 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
820 820 mutex_exit(&rp->r_statelock);
821 821 if (lwp != NULL)
822 822 lwp->lwp_nostop--;
823 823 error = EINTR;
824 824 goto bottom;
825 825 }
826 826 if (lwp != NULL)
827 827 lwp->lwp_nostop--;
828 828 } else
829 829 cv_wait(&rp->r_cv, &rp->r_statelock);
830 830 }
831 831 mutex_exit(&rp->r_statelock);
832 832
833 833 /*
834 834 * Touch the page and fault it in if it is not in core
835 835 * before segmap_getmapflt or vpm_data_copy can lock it.
836 836 * This is to avoid the deadlock if the buffer is mapped
837 837 * to the same file through mmap which we want to write.
838 838 */
839 839 uio_prefaultpages((long)n, uiop);
840 840
841 841 if (vpm_enable) {
842 842 /*
843 843 * It will use kpm mappings, so no need to
844 844 * pass an address.
845 845 */
846 846 error = writerp(rp, NULL, n, uiop, 0);
847 847 } else {
848 848 if (segmap_kpm) {
849 849 int pon = uiop->uio_loffset & PAGEOFFSET;
850 850 size_t pn = MIN(PAGESIZE - pon,
851 851 uiop->uio_resid);
852 852 int pagecreate;
853 853
854 854 mutex_enter(&rp->r_statelock);
855 855 pagecreate = (pon == 0) && (pn == PAGESIZE ||
856 856 uiop->uio_loffset + pn >= rp->r_size);
857 857 mutex_exit(&rp->r_statelock);
858 858
859 859 base = segmap_getmapflt(segkmap, vp, off + on,
860 860 pn, !pagecreate, S_WRITE);
861 861
862 862 error = writerp(rp, base + pon, n, uiop,
863 863 pagecreate);
864 864
865 865 } else {
866 866 base = segmap_getmapflt(segkmap, vp, off + on,
867 867 n, 0, S_READ);
868 868 error = writerp(rp, base + on, n, uiop, 0);
869 869 }
870 870 }
871 871
872 872 if (!error) {
873 873 if (mi->mi_flags & MI_NOAC)
874 874 flags = SM_WRITE;
875 875 else if ((uiop->uio_loffset % bsize) == 0 ||
876 876 IS_SWAPVP(vp)) {
877 877 /*
878 878 * Have written a whole block.
879 879 * Start an asynchronous write
880 880 * and mark the buffer to
881 881 * indicate that it won't be
882 882 * needed again soon.
883 883 */
884 884 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
885 885 } else
886 886 flags = 0;
887 887 if ((ioflag & (FSYNC|FDSYNC)) ||
888 888 (rp->r_flags & ROUTOFSPACE)) {
889 889 flags &= ~SM_ASYNC;
890 890 flags |= SM_WRITE;
891 891 }
892 892 if (vpm_enable) {
893 893 error = vpm_sync_pages(vp, off, n, flags);
894 894 } else {
895 895 error = segmap_release(segkmap, base, flags);
896 896 }
897 897 } else {
898 898 if (vpm_enable) {
899 899 (void) vpm_sync_pages(vp, off, n, 0);
900 900 } else {
901 901 (void) segmap_release(segkmap, base, 0);
902 902 }
903 903 /*
904 904 * In the event that we got an access error while
905 905 * faulting in a page for a write-only file just
906 906 * force a write.
907 907 */
908 908 if (error == EACCES)
909 909 goto nfs3_fwrite;
910 910 }
911 911 } while (!error && uiop->uio_resid > 0);
912 912
913 913 bottom:
914 914 if (error) {
915 915 uiop->uio_resid = resid + remainder;
916 916 uiop->uio_loffset = offset;
917 917 } else
918 918 uiop->uio_resid += remainder;
919 919
920 920 nfs_rw_exit(&rp->r_lkserlock);
921 921
922 922 return (error);
923 923 }
924 924
925 925 /*
926 926 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
927 927 */
928 928 static int
929 929 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
930 930 int flags, cred_t *cr)
931 931 {
932 932 struct buf *bp;
933 933 int error;
934 934 page_t *savepp;
935 935 uchar_t fsdata;
936 936 stable_how stab_comm;
937 937
938 938 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
939 939 bp = pageio_setup(pp, len, vp, flags);
940 940 ASSERT(bp != NULL);
941 941
942 942 /*
943 943 * pageio_setup should have set b_addr to 0. This
944 944 * is correct since we want to do I/O on a page
945 945 * boundary. bp_mapin will use this addr to calculate
946 946 * an offset, and then set b_addr to the kernel virtual
947 947 * address it allocated for us.
948 948 */
949 949 ASSERT(bp->b_un.b_addr == 0);
950 950
951 951 bp->b_edev = 0;
952 952 bp->b_dev = 0;
953 953 bp->b_lblkno = lbtodb(off);
954 954 bp->b_file = vp;
955 955 bp->b_offset = (offset_t)off;
956 956 bp_mapin(bp);
957 957
958 958 /*
959 959 * Calculate the desired level of stability to write data
960 960 * on the server and then mark all of the pages to reflect
961 961 * this.
962 962 */
963 963 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
964 964 freemem > desfree) {
965 965 stab_comm = UNSTABLE;
966 966 fsdata = C_DELAYCOMMIT;
967 967 } else {
968 968 stab_comm = FILE_SYNC;
969 969 fsdata = C_NOCOMMIT;
970 970 }
971 971
972 972 savepp = pp;
973 973 do {
974 974 pp->p_fsdata = fsdata;
975 975 } while ((pp = pp->p_next) != savepp);
976 976
977 977 error = nfs3_bio(bp, &stab_comm, cr);
978 978
979 979 bp_mapout(bp);
980 980 pageio_done(bp);
981 981
982 982 /*
983 983 * If the server wrote pages in a more stable fashion than
984 984 * was requested, then clear all of the marks in the pages
985 985 * indicating that COMMIT operations were required.
986 986 */
987 987 if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) {
988 988 do {
989 989 pp->p_fsdata = C_NOCOMMIT;
990 990 } while ((pp = pp->p_next) != savepp);
991 991 }
992 992
993 993 return (error);
994 994 }
995 995
996 996 /*
997 997 * Write to file. Writes to remote server in largest size
998 998 * chunks that the server can handle. Write is synchronous.
999 999 */
1000 1000 static int
1001 1001 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
1002 1002 stable_how *stab_comm)
1003 1003 {
1004 1004 mntinfo_t *mi;
1005 1005 WRITE3args args;
1006 1006 WRITE3res res;
1007 1007 int error;
1008 1008 int tsize;
1009 1009 rnode_t *rp;
1010 1010 int douprintf;
1011 1011
1012 1012 rp = VTOR(vp);
1013 1013 mi = VTOMI(vp);
1014 1014
1015 1015 ASSERT(nfs_zone() == mi->mi_zone);
1016 1016
1017 1017 args.file = *VTOFH3(vp);
1018 1018 args.stable = *stab_comm;
1019 1019
1020 1020 *stab_comm = FILE_SYNC;
1021 1021
1022 1022 douprintf = 1;
1023 1023
1024 1024 do {
1025 1025 if ((vp->v_flag & VNOCACHE) ||
1026 1026 (rp->r_flags & RDIRECTIO) ||
1027 1027 (mi->mi_flags & MI_DIRECTIO))
1028 1028 tsize = MIN(mi->mi_stsize, count);
1029 1029 else
1030 1030 tsize = MIN(mi->mi_curwrite, count);
1031 1031 args.offset = (offset3)offset;
1032 1032 args.count = (count3)tsize;
1033 1033 args.data.data_len = (uint_t)tsize;
1034 1034 args.data.data_val = base;
1035 1035
1036 1036 if (mi->mi_io_kstats) {
1037 1037 mutex_enter(&mi->mi_lock);
1038 1038 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1039 1039 mutex_exit(&mi->mi_lock);
1040 1040 }
1041 1041 args.mblk = NULL;
1042 1042 do {
1043 1043 error = rfs3call(mi, NFSPROC3_WRITE,
1044 1044 xdr_WRITE3args, (caddr_t)&args,
1045 1045 xdr_WRITE3res, (caddr_t)&res, cr,
1046 1046 &douprintf, &res.status, 0, NULL);
1047 1047 } while (error == ENFS_TRYAGAIN);
1048 1048 if (mi->mi_io_kstats) {
1049 1049 mutex_enter(&mi->mi_lock);
1050 1050 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1051 1051 mutex_exit(&mi->mi_lock);
1052 1052 }
1053 1053
1054 1054 if (error)
1055 1055 return (error);
1056 1056 error = geterrno3(res.status);
1057 1057 if (!error) {
1058 1058 if (res.resok.count > args.count) {
1059 1059 zcmn_err(getzoneid(), CE_WARN,
1060 1060 "nfs3write: server %s wrote %u, "
1061 1061 "requested was %u",
1062 1062 rp->r_server->sv_hostname,
1063 1063 res.resok.count, args.count);
1064 1064 return (EIO);
1065 1065 }
1066 1066 if (res.resok.committed == UNSTABLE) {
1067 1067 *stab_comm = UNSTABLE;
1068 1068 if (args.stable == DATA_SYNC ||
1069 1069 args.stable == FILE_SYNC) {
1070 1070 zcmn_err(getzoneid(), CE_WARN,
1071 1071 "nfs3write: server %s did not commit to stable storage",
1072 1072 rp->r_server->sv_hostname);
1073 1073 return (EIO);
1074 1074 }
1075 1075 }
1076 1076 tsize = (int)res.resok.count;
1077 1077 count -= tsize;
1078 1078 base += tsize;
1079 1079 offset += tsize;
1080 1080 if (mi->mi_io_kstats) {
1081 1081 mutex_enter(&mi->mi_lock);
1082 1082 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
1083 1083 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
1084 1084 tsize;
1085 1085 mutex_exit(&mi->mi_lock);
1086 1086 }
1087 1087 lwp_stat_update(LWP_STAT_OUBLK, 1);
1088 1088 mutex_enter(&rp->r_statelock);
1089 1089 if (rp->r_flags & RHAVEVERF) {
1090 1090 if (rp->r_verf != res.resok.verf) {
1091 1091 nfs3_set_mod(vp);
1092 1092 rp->r_verf = res.resok.verf;
1093 1093 /*
1094 1094 * If the data was written UNSTABLE,
1095 1095 * then might as well stop because
1096 1096 * the whole block will have to get
1097 1097 * rewritten anyway.
1098 1098 */
1099 1099 if (*stab_comm == UNSTABLE) {
1100 1100 mutex_exit(&rp->r_statelock);
1101 1101 break;
1102 1102 }
1103 1103 }
1104 1104 } else {
1105 1105 rp->r_verf = res.resok.verf;
1106 1106 rp->r_flags |= RHAVEVERF;
1107 1107 }
1108 1108 /*
1109 1109 * Mark the attribute cache as timed out and
1110 1110 * set RWRITEATTR to indicate that the file
1111 1111 * was modified with a WRITE operation and
1112 1112 * that the attributes can not be trusted.
1113 1113 */
1114 1114 PURGE_ATTRCACHE_LOCKED(rp);
1115 1115 rp->r_flags |= RWRITEATTR;
1116 1116 mutex_exit(&rp->r_statelock);
1117 1117 }
1118 1118 } while (!error && count);
1119 1119
1120 1120 return (error);
1121 1121 }
1122 1122
1123 1123 /*
1124 1124 * Read from a file. Reads data in largest chunks our interface can handle.
1125 1125 */
1126 1126 static int
1127 1127 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count,
1128 1128 size_t *residp, cred_t *cr)
1129 1129 {
1130 1130 mntinfo_t *mi;
1131 1131 READ3args args;
1132 1132 READ3vres res;
1133 1133 int tsize;
1134 1134 int error;
1135 1135 int douprintf;
1136 1136 failinfo_t fi;
1137 1137 rnode_t *rp;
1138 1138 struct vattr va;
1139 1139 hrtime_t t;
1140 1140
1141 1141 rp = VTOR(vp);
1142 1142 mi = VTOMI(vp);
1143 1143 ASSERT(nfs_zone() == mi->mi_zone);
1144 1144 douprintf = 1;
1145 1145
1146 1146 args.file = *VTOFH3(vp);
1147 1147 fi.vp = vp;
1148 1148 fi.fhp = (caddr_t)&args.file;
1149 1149 fi.copyproc = nfs3copyfh;
1150 1150 fi.lookupproc = nfs3lookup;
1151 1151 fi.xattrdirproc = acl_getxattrdir3;
1152 1152
1153 1153 res.pov.fres.vp = vp;
1154 1154 res.pov.fres.vap = &va;
1155 1155
1156 1156 res.wlist = NULL;
1157 1157 *residp = count;
1158 1158 do {
1159 1159 if (mi->mi_io_kstats) {
1160 1160 mutex_enter(&mi->mi_lock);
1161 1161 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1162 1162 mutex_exit(&mi->mi_lock);
1163 1163 }
1164 1164
1165 1165 do {
1166 1166 if ((vp->v_flag & VNOCACHE) ||
1167 1167 (rp->r_flags & RDIRECTIO) ||
1168 1168 (mi->mi_flags & MI_DIRECTIO))
1169 1169 tsize = MIN(mi->mi_tsize, count);
1170 1170 else
1171 1171 tsize = MIN(mi->mi_curread, count);
1172 1172 res.data.data_val = base;
1173 1173 res.data.data_len = tsize;
1174 1174 args.offset = (offset3)offset;
1175 1175 args.count = (count3)tsize;
1176 1176 args.res_uiop = NULL;
1177 1177 args.res_data_val_alt = base;
1178 1178
1179 1179 t = gethrtime();
1180 1180 error = rfs3call(mi, NFSPROC3_READ,
1181 1181 xdr_READ3args, (caddr_t)&args,
1182 1182 xdr_READ3vres, (caddr_t)&res, cr,
1183 1183 &douprintf, &res.status, 0, &fi);
1184 1184 } while (error == ENFS_TRYAGAIN);
1185 1185
1186 1186 if (mi->mi_io_kstats) {
1187 1187 mutex_enter(&mi->mi_lock);
1188 1188 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1189 1189 mutex_exit(&mi->mi_lock);
1190 1190 }
1191 1191
1192 1192 if (error)
1193 1193 return (error);
1194 1194
1195 1195 error = geterrno3(res.status);
1196 1196 if (error)
1197 1197 return (error);
1198 1198
1199 1199 if (res.count != res.data.data_len) {
1200 1200 zcmn_err(getzoneid(), CE_WARN,
1201 1201 "nfs3read: server %s returned incorrect amount",
1202 1202 rp->r_server->sv_hostname);
1203 1203 return (EIO);
1204 1204 }
1205 1205
1206 1206 count -= res.count;
1207 1207 *residp = count;
1208 1208 base += res.count;
1209 1209 offset += res.count;
1210 1210 if (mi->mi_io_kstats) {
1211 1211 mutex_enter(&mi->mi_lock);
1212 1212 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1213 1213 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
1214 1214 mutex_exit(&mi->mi_lock);
1215 1215 }
1216 1216 lwp_stat_update(LWP_STAT_INBLK, 1);
1217 1217 } while (count && !res.eof);
1218 1218
1219 1219 if (res.pov.attributes) {
1220 1220 mutex_enter(&rp->r_statelock);
1221 1221 if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) {
1222 1222 mutex_exit(&rp->r_statelock);
1223 1223 PURGE_ATTRCACHE(vp);
1224 1224 } else {
1225 1225 if (rp->r_mtime <= t)
1226 1226 nfs_attrcache_va(vp, &va);
1227 1227 mutex_exit(&rp->r_statelock);
1228 1228 }
1229 1229 }
1230 1230
1231 1231 return (0);
1232 1232 }
1233 1233
1234 1234 /* ARGSUSED */
1235 1235 static int
1236 1236 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1237 1237 caller_context_t *ct)
1238 1238 {
1239 1239
1240 1240 if (nfs_zone() != VTOMI(vp)->mi_zone)
1241 1241 return (EIO);
1242 1242 switch (cmd) {
1243 1243 case _FIODIRECTIO:
1244 1244 return (nfs_directio(vp, (int)arg, cr));
1245 1245 default:
1246 1246 return (ENOTTY);
1247 1247 }
1248 1248 }
1249 1249
1250 1250 /* ARGSUSED */
1251 1251 static int
1252 1252 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1253 1253 caller_context_t *ct)
1254 1254 {
1255 1255 int error;
1256 1256 rnode_t *rp;
1257 1257
1258 1258 if (nfs_zone() != VTOMI(vp)->mi_zone)
1259 1259 return (EIO);
1260 1260 /*
1261 1261 * If it has been specified that the return value will
1262 1262 * just be used as a hint, and we are only being asked
1263 1263 * for size, fsid or rdevid, then return the client's
1264 1264 * notion of these values without checking to make sure
1265 1265 * that the attribute cache is up to date.
1266 1266 * The whole point is to avoid an over the wire GETATTR
1267 1267 * call.
1268 1268 */
1269 1269 rp = VTOR(vp);
1270 1270 if (flags & ATTR_HINT) {
1271 1271 if (vap->va_mask ==
1272 1272 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1273 1273 mutex_enter(&rp->r_statelock);
1274 1274 if (vap->va_mask | AT_SIZE)
1275 1275 vap->va_size = rp->r_size;
1276 1276 if (vap->va_mask | AT_FSID)
1277 1277 vap->va_fsid = rp->r_attr.va_fsid;
1278 1278 if (vap->va_mask | AT_RDEV)
1279 1279 vap->va_rdev = rp->r_attr.va_rdev;
1280 1280 mutex_exit(&rp->r_statelock);
1281 1281 return (0);
1282 1282 }
1283 1283 }
1284 1284
1285 1285 /*
1286 1286 * Only need to flush pages if asking for the mtime
1287 1287 * and if there any dirty pages or any outstanding
1288 1288 * asynchronous (write) requests for this file.
1289 1289 */
1290 1290 if (vap->va_mask & AT_MTIME) {
1291 1291 if (vn_has_cached_data(vp) &&
1292 1292 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1293 1293 mutex_enter(&rp->r_statelock);
1294 1294 rp->r_gcount++;
1295 1295 mutex_exit(&rp->r_statelock);
1296 1296 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1297 1297 mutex_enter(&rp->r_statelock);
1298 1298 if (error && (error == ENOSPC || error == EDQUOT)) {
1299 1299 if (!rp->r_error)
1300 1300 rp->r_error = error;
1301 1301 }
1302 1302 if (--rp->r_gcount == 0)
1303 1303 cv_broadcast(&rp->r_cv);
1304 1304 mutex_exit(&rp->r_statelock);
1305 1305 }
1306 1306 }
1307 1307
1308 1308 return (nfs3getattr(vp, vap, cr));
1309 1309 }
1310 1310
1311 1311 /*ARGSUSED4*/
1312 1312 static int
1313 1313 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1314 1314 caller_context_t *ct)
1315 1315 {
1316 1316 int error;
1317 1317 struct vattr va;
1318 1318
1319 1319 if (vap->va_mask & AT_NOSET)
1320 1320 return (EINVAL);
1321 1321 if (nfs_zone() != VTOMI(vp)->mi_zone)
1322 1322 return (EIO);
1323 1323
1324 1324 va.va_mask = AT_UID | AT_MODE;
1325 1325 error = nfs3getattr(vp, &va, cr);
1326 1326 if (error)
1327 1327 return (error);
1328 1328
1329 1329 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx,
1330 1330 vp);
1331 1331 if (error)
1332 1332 return (error);
1333 1333
1334 1334 error = nfs3setattr(vp, vap, flags, cr);
1335 1335
1336 1336 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
1337 1337 vnevent_truncate(vp, ct);
1338 1338
1339 1339 return (error);
1340 1340 }
1341 1341
1342 1342 static int
1343 1343 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1344 1344 {
1345 1345 int error;
1346 1346 uint_t mask;
1347 1347 SETATTR3args args;
1348 1348 SETATTR3res res;
1349 1349 int douprintf;
1350 1350 rnode_t *rp;
1351 1351 struct vattr va;
1352 1352 mode_t omode;
1353 1353 vsecattr_t *vsp;
1354 1354 hrtime_t t;
1355 1355
1356 1356 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1357 1357 mask = vap->va_mask;
1358 1358
1359 1359 rp = VTOR(vp);
1360 1360
1361 1361 /*
1362 1362 * Only need to flush pages if there are any pages and
1363 1363 * if the file is marked as dirty in some fashion. The
1364 1364 * file must be flushed so that we can accurately
1365 1365 * determine the size of the file and the cached data
1366 1366 * after the SETATTR returns. A file is considered to
1367 1367 * be dirty if it is either marked with RDIRTY, has
1368 1368 * outstanding i/o's active, or is mmap'd. In this
1369 1369 * last case, we can't tell whether there are dirty
1370 1370 * pages, so we flush just to be sure.
1371 1371 */
1372 1372 if (vn_has_cached_data(vp) &&
1373 1373 ((rp->r_flags & RDIRTY) ||
1374 1374 rp->r_count > 0 ||
1375 1375 rp->r_mapcnt > 0)) {
1376 1376 ASSERT(vp->v_type != VCHR);
1377 1377 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1378 1378 if (error && (error == ENOSPC || error == EDQUOT)) {
1379 1379 mutex_enter(&rp->r_statelock);
1380 1380 if (!rp->r_error)
1381 1381 rp->r_error = error;
1382 1382 mutex_exit(&rp->r_statelock);
1383 1383 }
1384 1384 }
1385 1385
1386 1386 args.object = *RTOFH3(rp);
1387 1387 /*
1388 1388 * If the intent is for the server to set the times,
1389 1389 * there is no point in have the mask indicating set mtime or
1390 1390 * atime, because the vap values may be junk, and so result
1391 1391 * in an overflow error. Remove these flags from the vap mask
1392 1392 * before calling in this case, and restore them afterwards.
1393 1393 */
1394 1394 if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) {
1395 1395 /* Use server times, so don't set the args time fields */
1396 1396 vap->va_mask &= ~(AT_ATIME | AT_MTIME);
1397 1397 error = vattr_to_sattr3(vap, &args.new_attributes);
1398 1398 vap->va_mask |= (mask & (AT_ATIME | AT_MTIME));
1399 1399 if (mask & AT_ATIME) {
1400 1400 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
1401 1401 }
1402 1402 if (mask & AT_MTIME) {
1403 1403 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
1404 1404 }
1405 1405 } else {
1406 1406 /* Either do not set times or use the client specified times */
1407 1407 error = vattr_to_sattr3(vap, &args.new_attributes);
1408 1408 }
1409 1409
1410 1410 if (error) {
1411 1411 /* req time field(s) overflow - return immediately */
1412 1412 return (error);
1413 1413 }
1414 1414
1415 1415 va.va_mask = AT_MODE | AT_CTIME;
1416 1416 error = nfs3getattr(vp, &va, cr);
1417 1417 if (error)
1418 1418 return (error);
1419 1419 omode = va.va_mode;
1420 1420
1421 1421 tryagain:
1422 1422 if (mask & AT_SIZE) {
1423 1423 args.guard.check = TRUE;
1424 1424 args.guard.obj_ctime.seconds = va.va_ctime.tv_sec;
1425 1425 args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec;
1426 1426 } else
1427 1427 args.guard.check = FALSE;
1428 1428
1429 1429 douprintf = 1;
1430 1430
1431 1431 t = gethrtime();
1432 1432
1433 1433 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
1434 1434 xdr_SETATTR3args, (caddr_t)&args,
1435 1435 xdr_SETATTR3res, (caddr_t)&res, cr,
1436 1436 &douprintf, &res.status, 0, NULL);
1437 1437
1438 1438 /*
1439 1439 * Purge the access cache and ACL cache if changing either the
1440 1440 * owner of the file, the group owner, or the mode. These may
1441 1441 * change the access permissions of the file, so purge old
1442 1442 * information and start over again.
1443 1443 */
1444 1444 if (mask & (AT_UID | AT_GID | AT_MODE)) {
1445 1445 (void) nfs_access_purge_rp(rp);
1446 1446 if (rp->r_secattr != NULL) {
1447 1447 mutex_enter(&rp->r_statelock);
1448 1448 vsp = rp->r_secattr;
1449 1449 rp->r_secattr = NULL;
1450 1450 mutex_exit(&rp->r_statelock);
1451 1451 if (vsp != NULL)
1452 1452 nfs_acl_free(vsp);
1453 1453 }
1454 1454 }
1455 1455
1456 1456 if (error) {
1457 1457 PURGE_ATTRCACHE(vp);
1458 1458 return (error);
1459 1459 }
1460 1460
1461 1461 error = geterrno3(res.status);
1462 1462 if (!error) {
1463 1463 /*
1464 1464 * If changing the size of the file, invalidate
1465 1465 * any local cached data which is no longer part
1466 1466 * of the file. We also possibly invalidate the
1467 1467 * last page in the file. We could use
1468 1468 * pvn_vpzero(), but this would mark the page as
1469 1469 * modified and require it to be written back to
1470 1470 * the server for no particularly good reason.
1471 1471 * This way, if we access it, then we bring it
1472 1472 * back in. A read should be cheaper than a
1473 1473 * write.
1474 1474 */
1475 1475 if (mask & AT_SIZE) {
1476 1476 nfs_invalidate_pages(vp,
1477 1477 (vap->va_size & PAGEMASK), cr);
1478 1478 }
1479 1479 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
1480 1480 /*
1481 1481 * Some servers will change the mode to clear the setuid
1482 1482 * and setgid bits when changing the uid or gid. The
1483 1483 * client needs to compensate appropriately.
1484 1484 */
1485 1485 if (mask & (AT_UID | AT_GID)) {
1486 1486 int terror;
1487 1487
1488 1488 va.va_mask = AT_MODE;
1489 1489 terror = nfs3getattr(vp, &va, cr);
1490 1490 if (!terror &&
1491 1491 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
1492 1492 (!(mask & AT_MODE) && va.va_mode != omode))) {
1493 1493 va.va_mask = AT_MODE;
1494 1494 if (mask & AT_MODE)
1495 1495 va.va_mode = vap->va_mode;
1496 1496 else
1497 1497 va.va_mode = omode;
1498 1498 (void) nfs3setattr(vp, &va, 0, cr);
1499 1499 }
1500 1500 }
1501 1501 } else {
1502 1502 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
1503 1503 /*
1504 1504 * If we got back a "not synchronized" error, then
1505 1505 * we need to retry with a new guard value. The
1506 1506 * guard value used is the change time. If the
1507 1507 * server returned post_op_attr, then we can just
1508 1508 * retry because we have the latest attributes.
1509 1509 * Otherwise, we issue a GETATTR to get the latest
1510 1510 * attributes and then retry. If we couldn't get
1511 1511 * the attributes this way either, then we give
1512 1512 * up because we can't complete the operation as
1513 1513 * required.
1514 1514 */
1515 1515 if (res.status == NFS3ERR_NOT_SYNC) {
1516 1516 va.va_mask = AT_CTIME;
1517 1517 if (nfs3getattr(vp, &va, cr) == 0)
1518 1518 goto tryagain;
1519 1519 }
1520 1520 PURGE_STALE_FH(error, vp, cr);
1521 1521 }
1522 1522
1523 1523 return (error);
1524 1524 }
1525 1525
1526 1526 static int
1527 1527 nfs3_accessx(void *vp, int mode, cred_t *cr)
1528 1528 {
1529 1529 ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1530 1530 return (nfs3_access(vp, mode, 0, cr, NULL));
1531 1531 }
1532 1532
1533 1533 /* ARGSUSED */
1534 1534 static int
1535 1535 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1536 1536 {
1537 1537 int error;
1538 1538 ACCESS3args args;
1539 1539 ACCESS3res res;
1540 1540 int douprintf;
1541 1541 uint32 acc;
1542 1542 rnode_t *rp;
1543 1543 cred_t *cred, *ncr, *ncrfree = NULL;
1544 1544 failinfo_t fi;
1545 1545 nfs_access_type_t cacc;
1546 1546 hrtime_t t;
1547 1547
1548 1548 acc = 0;
1549 1549 if (nfs_zone() != VTOMI(vp)->mi_zone)
1550 1550 return (EIO);
1551 1551 if (mode & VREAD)
1552 1552 acc |= ACCESS3_READ;
1553 1553 if (mode & VWRITE) {
1554 1554 if (vn_is_readonly(vp) && !IS_DEVVP(vp))
1555 1555 return (EROFS);
1556 1556 if (vp->v_type == VDIR)
1557 1557 acc |= ACCESS3_DELETE;
1558 1558 acc |= ACCESS3_MODIFY | ACCESS3_EXTEND;
1559 1559 }
1560 1560 if (mode & VEXEC) {
1561 1561 if (vp->v_type == VDIR)
1562 1562 acc |= ACCESS3_LOOKUP;
1563 1563 else
1564 1564 acc |= ACCESS3_EXECUTE;
1565 1565 }
1566 1566
1567 1567 rp = VTOR(vp);
1568 1568 args.object = *VTOFH3(vp);
1569 1569 if (vp->v_type == VDIR) {
1570 1570 args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY |
1571 1571 ACCESS3_EXTEND | ACCESS3_LOOKUP;
1572 1572 } else {
1573 1573 args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND |
1574 1574 ACCESS3_EXECUTE;
1575 1575 }
1576 1576 fi.vp = vp;
1577 1577 fi.fhp = (caddr_t)&args.object;
1578 1578 fi.copyproc = nfs3copyfh;
1579 1579 fi.lookupproc = nfs3lookup;
1580 1580 fi.xattrdirproc = acl_getxattrdir3;
1581 1581
1582 1582 cred = cr;
1583 1583 /*
1584 1584 * ncr and ncrfree both initially
1585 1585 * point to the memory area returned
1586 1586 * by crnetadjust();
1587 1587 * ncrfree not NULL when exiting means
1588 1588 * that we need to release it
1589 1589 */
1590 1590 ncr = crnetadjust(cred);
1591 1591 ncrfree = ncr;
1592 1592 tryagain:
1593 1593 if (rp->r_acache != NULL) {
1594 1594 cacc = nfs_access_check(rp, acc, cred);
1595 1595 if (cacc == NFS_ACCESS_ALLOWED) {
1596 1596 if (ncrfree != NULL)
1597 1597 crfree(ncrfree);
1598 1598 return (0);
1599 1599 }
1600 1600 if (cacc == NFS_ACCESS_DENIED) {
1601 1601 /*
1602 1602 * If the cred can be adjusted, try again
1603 1603 * with the new cred.
1604 1604 */
1605 1605 if (ncr != NULL) {
1606 1606 cred = ncr;
1607 1607 ncr = NULL;
1608 1608 goto tryagain;
1609 1609 }
1610 1610 if (ncrfree != NULL)
1611 1611 crfree(ncrfree);
1612 1612 return (EACCES);
1613 1613 }
1614 1614 }
1615 1615
1616 1616 douprintf = 1;
1617 1617
1618 1618 t = gethrtime();
1619 1619
1620 1620 error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS,
1621 1621 xdr_ACCESS3args, (caddr_t)&args,
1622 1622 xdr_ACCESS3res, (caddr_t)&res, cred,
1623 1623 &douprintf, &res.status, 0, &fi);
1624 1624
1625 1625 if (error) {
1626 1626 if (ncrfree != NULL)
1627 1627 crfree(ncrfree);
1628 1628 return (error);
1629 1629 }
1630 1630
1631 1631 error = geterrno3(res.status);
1632 1632 if (!error) {
1633 1633 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1634 1634 nfs_access_cache(rp, args.access, res.resok.access, cred);
1635 1635 /*
1636 1636 * we just cached results with cred; if cred is the
1637 1637 * adjusted credentials from crnetadjust, we do not want
1638 1638 * to release them before exiting: hence setting ncrfree
1639 1639 * to NULL
1640 1640 */
1641 1641 if (cred != cr)
1642 1642 ncrfree = NULL;
1643 1643 if ((acc & res.resok.access) != acc) {
1644 1644 /*
1645 1645 * If the cred can be adjusted, try again
1646 1646 * with the new cred.
1647 1647 */
1648 1648 if (ncr != NULL) {
1649 1649 cred = ncr;
1650 1650 ncr = NULL;
1651 1651 goto tryagain;
1652 1652 }
1653 1653 error = EACCES;
1654 1654 }
1655 1655 } else {
1656 1656 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1657 1657 PURGE_STALE_FH(error, vp, cr);
1658 1658 }
1659 1659
1660 1660 if (ncrfree != NULL)
1661 1661 crfree(ncrfree);
1662 1662
1663 1663 return (error);
1664 1664 }
1665 1665
1666 1666 static int nfs3_do_symlink_cache = 1;
1667 1667
1668 1668 /* ARGSUSED */
1669 1669 static int
1670 1670 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1671 1671 {
1672 1672 int error;
1673 1673 READLINK3args args;
1674 1674 READLINK3res res;
1675 1675 nfspath3 resdata_backup;
1676 1676 rnode_t *rp;
1677 1677 int douprintf;
1678 1678 int len;
1679 1679 failinfo_t fi;
1680 1680 hrtime_t t;
1681 1681
1682 1682 /*
1683 1683 * Can't readlink anything other than a symbolic link.
1684 1684 */
1685 1685 if (vp->v_type != VLNK)
1686 1686 return (EINVAL);
1687 1687 if (nfs_zone() != VTOMI(vp)->mi_zone)
1688 1688 return (EIO);
1689 1689
1690 1690 rp = VTOR(vp);
1691 1691 if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) {
1692 1692 error = nfs3_validate_caches(vp, cr);
1693 1693 if (error)
1694 1694 return (error);
1695 1695 mutex_enter(&rp->r_statelock);
1696 1696 if (rp->r_symlink.contents != NULL) {
1697 1697 error = uiomove(rp->r_symlink.contents,
1698 1698 rp->r_symlink.len, UIO_READ, uiop);
1699 1699 mutex_exit(&rp->r_statelock);
1700 1700 return (error);
1701 1701 }
1702 1702 mutex_exit(&rp->r_statelock);
1703 1703 }
1704 1704
1705 1705 args.symlink = *VTOFH3(vp);
1706 1706 fi.vp = vp;
1707 1707 fi.fhp = (caddr_t)&args.symlink;
1708 1708 fi.copyproc = nfs3copyfh;
1709 1709 fi.lookupproc = nfs3lookup;
1710 1710 fi.xattrdirproc = acl_getxattrdir3;
1711 1711
1712 1712 res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1713 1713
1714 1714 resdata_backup = res.resok.data;
1715 1715
1716 1716 douprintf = 1;
1717 1717
1718 1718 t = gethrtime();
1719 1719
1720 1720 error = rfs3call(VTOMI(vp), NFSPROC3_READLINK,
1721 1721 xdr_READLINK3args, (caddr_t)&args,
1722 1722 xdr_READLINK3res, (caddr_t)&res, cr,
1723 1723 &douprintf, &res.status, 0, &fi);
1724 1724
1725 1725 if (res.resok.data == nfs3nametoolong)
1726 1726 error = EINVAL;
1727 1727
1728 1728 if (error) {
1729 1729 kmem_free(resdata_backup, MAXPATHLEN);
1730 1730 return (error);
1731 1731 }
1732 1732
1733 1733 error = geterrno3(res.status);
1734 1734 if (!error) {
1735 1735 nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t,
1736 1736 cr);
1737 1737 len = strlen(res.resok.data);
1738 1738 error = uiomove(res.resok.data, len, UIO_READ, uiop);
1739 1739 if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) {
1740 1740 mutex_enter(&rp->r_statelock);
1741 1741 if (rp->r_symlink.contents == NULL) {
1742 1742 rp->r_symlink.contents = res.resok.data;
1743 1743 rp->r_symlink.len = len;
1744 1744 rp->r_symlink.size = MAXPATHLEN;
1745 1745 mutex_exit(&rp->r_statelock);
1746 1746 } else {
1747 1747 mutex_exit(&rp->r_statelock);
1748 1748
1749 1749 kmem_free((void *)res.resok.data, MAXPATHLEN);
1750 1750 }
1751 1751 } else {
1752 1752 kmem_free((void *)res.resok.data, MAXPATHLEN);
1753 1753 }
1754 1754 } else {
1755 1755 nfs3_cache_post_op_attr(vp,
1756 1756 &res.resfail.symlink_attributes, t, cr);
1757 1757 PURGE_STALE_FH(error, vp, cr);
1758 1758
1759 1759 kmem_free((void *)res.resok.data, MAXPATHLEN);
1760 1760
1761 1761 }
1762 1762
1763 1763 /*
1764 1764 * The over the wire error for attempting to readlink something
1765 1765 * other than a symbolic link is ENXIO. However, we need to
1766 1766 * return EINVAL instead of ENXIO, so we map it here.
1767 1767 */
1768 1768 return (error == ENXIO ? EINVAL : error);
1769 1769 }
1770 1770
1771 1771 /*
1772 1772 * Flush local dirty pages to stable storage on the server.
1773 1773 *
1774 1774 * If FNODSYNC is specified, then there is nothing to do because
1775 1775 * metadata changes are not cached on the client before being
1776 1776 * sent to the server.
1777 1777 */
1778 1778 /* ARGSUSED */
1779 1779 static int
1780 1780 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1781 1781 {
1782 1782 int error;
1783 1783
1784 1784 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1785 1785 return (0);
1786 1786 if (nfs_zone() != VTOMI(vp)->mi_zone)
1787 1787 return (EIO);
1788 1788
1789 1789 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
1790 1790 if (!error)
1791 1791 error = VTOR(vp)->r_error;
1792 1792 return (error);
1793 1793 }
1794 1794
1795 1795 /*
1796 1796 * Weirdness: if the file was removed or the target of a rename
1797 1797 * operation while it was open, it got renamed instead. Here we
1798 1798 * remove the renamed file.
1799 1799 */
1800 1800 /* ARGSUSED */
1801 1801 static void
1802 1802 nfs3_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1803 1803 {
1804 1804 rnode_t *rp;
1805 1805
1806 1806 ASSERT(vp != DNLC_NO_VNODE);
1807 1807
1808 1808 /*
1809 1809 * If this is coming from the wrong zone, we let someone in the right
1810 1810 * zone take care of it asynchronously. We can get here due to
1811 1811 * VN_RELE() being called from pageout() or fsflush(). This call may
1812 1812 * potentially turn into an expensive no-op if, for instance, v_count
1813 1813 * gets incremented in the meantime, but it's still correct.
1814 1814 */
1815 1815 if (nfs_zone() != VTOMI(vp)->mi_zone) {
1816 1816 nfs_async_inactive(vp, cr, nfs3_inactive);
1817 1817 return;
1818 1818 }
1819 1819
1820 1820 rp = VTOR(vp);
1821 1821 redo:
1822 1822 if (rp->r_unldvp != NULL) {
1823 1823 /*
1824 1824 * Save the vnode pointer for the directory where the
1825 1825 * unlinked-open file got renamed, then set it to NULL
1826 1826 * to prevent another thread from getting here before
1827 1827 * we're done with the remove. While we have the
1828 1828 * statelock, make local copies of the pertinent rnode
1829 1829 * fields. If we weren't to do this in an atomic way, the
1830 1830 * the unl* fields could become inconsistent with respect
1831 1831 * to each other due to a race condition between this
1832 1832 * code and nfs_remove(). See bug report 1034328.
1833 1833 */
1834 1834 mutex_enter(&rp->r_statelock);
1835 1835 if (rp->r_unldvp != NULL) {
1836 1836 vnode_t *unldvp;
1837 1837 char *unlname;
1838 1838 cred_t *unlcred;
1839 1839 REMOVE3args args;
1840 1840 REMOVE3res res;
1841 1841 int douprintf;
1842 1842 int error;
1843 1843 hrtime_t t;
1844 1844
1845 1845 unldvp = rp->r_unldvp;
1846 1846 rp->r_unldvp = NULL;
1847 1847 unlname = rp->r_unlname;
1848 1848 rp->r_unlname = NULL;
1849 1849 unlcred = rp->r_unlcred;
1850 1850 rp->r_unlcred = NULL;
1851 1851 mutex_exit(&rp->r_statelock);
1852 1852
1853 1853 /*
1854 1854 * If there are any dirty pages left, then flush
1855 1855 * them. This is unfortunate because they just
1856 1856 * may get thrown away during the remove operation,
1857 1857 * but we have to do this for correctness.
1858 1858 */
1859 1859 if (vn_has_cached_data(vp) &&
1860 1860 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1861 1861 ASSERT(vp->v_type != VCHR);
1862 1862 error = nfs3_putpage(vp, (offset_t)0, 0, 0,
1863 1863 cr, ct);
1864 1864 if (error) {
1865 1865 mutex_enter(&rp->r_statelock);
1866 1866 if (!rp->r_error)
1867 1867 rp->r_error = error;
1868 1868 mutex_exit(&rp->r_statelock);
1869 1869 }
1870 1870 }
1871 1871
1872 1872 /*
1873 1873 * Do the remove operation on the renamed file
1874 1874 */
1875 1875 setdiropargs3(&args.object, unlname, unldvp);
1876 1876
1877 1877 douprintf = 1;
1878 1878
1879 1879 t = gethrtime();
1880 1880
1881 1881 error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE,
1882 1882 xdr_diropargs3, (caddr_t)&args,
1883 1883 xdr_REMOVE3res, (caddr_t)&res, unlcred,
1884 1884 &douprintf, &res.status, 0, NULL);
1885 1885
1886 1886 if (error) {
1887 1887 PURGE_ATTRCACHE(unldvp);
1888 1888 } else {
1889 1889 error = geterrno3(res.status);
1890 1890 if (!error) {
1891 1891 nfs3_cache_wcc_data(unldvp,
1892 1892 &res.resok.dir_wcc, t, cr);
1893 1893 if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1894 1894 nfs_purge_rddir_cache(unldvp);
1895 1895 } else {
1896 1896 nfs3_cache_wcc_data(unldvp,
1897 1897 &res.resfail.dir_wcc, t, cr);
1898 1898 PURGE_STALE_FH(error, unldvp, cr);
1899 1899 }
1900 1900 }
1901 1901
1902 1902 /*
1903 1903 * Release stuff held for the remove
1904 1904 */
1905 1905 VN_RELE(unldvp);
1906 1906 kmem_free(unlname, MAXNAMELEN);
1907 1907 crfree(unlcred);
1908 1908 goto redo;
1909 1909 }
1910 1910 mutex_exit(&rp->r_statelock);
1911 1911 }
1912 1912
1913 1913 rp_addfree(rp, cr);
1914 1914 }
1915 1915
1916 1916 /*
1917 1917 * Remote file system operations having to do with directory manipulation.
1918 1918 */
1919 1919
1920 1920 /* ARGSUSED */
1921 1921 static int
1922 1922 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1923 1923 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1924 1924 int *direntflags, pathname_t *realpnp)
1925 1925 {
1926 1926 int error;
1927 1927 vnode_t *vp;
1928 1928 vnode_t *avp = NULL;
1929 1929 rnode_t *drp;
1930 1930
1931 1931 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1932 1932 return (EPERM);
1933 1933
1934 1934 drp = VTOR(dvp);
1935 1935
1936 1936 /*
1937 1937 * Are we looking up extended attributes? If so, "dvp" is
1938 1938 * the file or directory for which we want attributes, and
1939 1939 * we need a lookup of the hidden attribute directory
1940 1940 * before we lookup the rest of the path.
1941 1941 */
1942 1942 if (flags & LOOKUP_XATTR) {
1943 1943 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1944 1944 mntinfo_t *mi;
1945 1945
1946 1946 mi = VTOMI(dvp);
1947 1947 if (!(mi->mi_flags & MI_EXTATTR))
1948 1948 return (EINVAL);
1949 1949
1950 1950 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1951 1951 return (EINTR);
1952 1952
1953 1953 (void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1954 1954 if (avp == NULL)
1955 1955 error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0);
1956 1956 else
1957 1957 error = 0;
1958 1958
1959 1959 nfs_rw_exit(&drp->r_rwlock);
1960 1960
1961 1961 if (error) {
1962 1962 if (mi->mi_flags & MI_EXTATTR)
1963 1963 return (error);
1964 1964 return (EINVAL);
1965 1965 }
1966 1966 dvp = avp;
1967 1967 drp = VTOR(dvp);
1968 1968 }
1969 1969
1970 1970 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1971 1971 error = EINTR;
1972 1972 goto out;
1973 1973 }
1974 1974
1975 1975 error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1976 1976
1977 1977 nfs_rw_exit(&drp->r_rwlock);
1978 1978
1979 1979 /*
1980 1980 * If vnode is a device, create special vnode.
1981 1981 */
1982 1982 if (!error && IS_DEVVP(*vpp)) {
1983 1983 vp = *vpp;
1984 1984 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1985 1985 VN_RELE(vp);
1986 1986 }
1987 1987
1988 1988 out:
1989 1989 if (avp != NULL)
1990 1990 VN_RELE(avp);
1991 1991
1992 1992 return (error);
1993 1993 }
1994 1994
1995 1995 static int nfs3_lookup_neg_cache = 1;
1996 1996
1997 1997 #ifdef DEBUG
1998 1998 static int nfs3_lookup_dnlc_hits = 0;
1999 1999 static int nfs3_lookup_dnlc_misses = 0;
2000 2000 static int nfs3_lookup_dnlc_neg_hits = 0;
2001 2001 static int nfs3_lookup_dnlc_disappears = 0;
2002 2002 static int nfs3_lookup_dnlc_lookups = 0;
2003 2003 #endif
2004 2004
2005 2005 /* ARGSUSED */
2006 2006 int
2007 2007 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
2008 2008 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
2009 2009 {
2010 2010 int error;
2011 2011 rnode_t *drp;
2012 2012
2013 2013 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2014 2014 /*
2015 2015 * If lookup is for "", just return dvp. Don't need
2016 2016 * to send it over the wire, look it up in the dnlc,
2017 2017 * or perform any access checks.
2018 2018 */
2019 2019 if (*nm == '\0') {
2020 2020 VN_HOLD(dvp);
2021 2021 *vpp = dvp;
2022 2022 return (0);
2023 2023 }
2024 2024
2025 2025 /*
2026 2026 * Can't do lookups in non-directories.
2027 2027 */
2028 2028 if (dvp->v_type != VDIR)
2029 2029 return (ENOTDIR);
2030 2030
2031 2031 /*
2032 2032 * If we're called with RFSCALL_SOFT, it's important that
2033 2033 * the only rfscall is one we make directly; if we permit
2034 2034 * an access call because we're looking up "." or validating
2035 2035 * a dnlc hit, we'll deadlock because that rfscall will not
2036 2036 * have the RFSCALL_SOFT set.
2037 2037 */
2038 2038 if (rfscall_flags & RFSCALL_SOFT)
2039 2039 goto callit;
2040 2040
2041 2041 /*
2042 2042 * If lookup is for ".", just return dvp. Don't need
2043 2043 * to send it over the wire or look it up in the dnlc,
2044 2044 * just need to check access.
2045 2045 */
2046 2046 if (strcmp(nm, ".") == 0) {
2047 2047 error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2048 2048 if (error)
2049 2049 return (error);
2050 2050 VN_HOLD(dvp);
2051 2051 *vpp = dvp;
2052 2052 return (0);
2053 2053 }
2054 2054
2055 2055 drp = VTOR(dvp);
2056 2056 if (!(drp->r_flags & RLOOKUP)) {
2057 2057 mutex_enter(&drp->r_statelock);
2058 2058 drp->r_flags |= RLOOKUP;
2059 2059 mutex_exit(&drp->r_statelock);
2060 2060 }
2061 2061
2062 2062 /*
2063 2063 * Lookup this name in the DNLC. If there was a valid entry,
2064 2064 * then return the results of the lookup.
2065 2065 */
2066 2066 error = nfs3lookup_dnlc(dvp, nm, vpp, cr);
2067 2067 if (error || *vpp != NULL)
2068 2068 return (error);
2069 2069
2070 2070 callit:
2071 2071 error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags);
2072 2072
2073 2073 return (error);
2074 2074 }
2075 2075
2076 2076 static int
2077 2077 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
2078 2078 {
2079 2079 int error;
2080 2080 vnode_t *vp;
2081 2081
2082 2082 ASSERT(*nm != '\0');
2083 2083 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2084 2084 /*
2085 2085 * Lookup this name in the DNLC. If successful, then validate
2086 2086 * the caches and then recheck the DNLC. The DNLC is rechecked
2087 2087 * just in case this entry got invalidated during the call
2088 2088 * to nfs3_validate_caches.
2089 2089 *
2090 2090 * An assumption is being made that it is safe to say that a
2091 2091 * file exists which may not on the server. Any operations to
2092 2092 * the server will fail with ESTALE.
2093 2093 */
2094 2094 #ifdef DEBUG
2095 2095 nfs3_lookup_dnlc_lookups++;
2096 2096 #endif
2097 2097 vp = dnlc_lookup(dvp, nm);
2098 2098 if (vp != NULL) {
2099 2099 VN_RELE(vp);
2100 2100 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
2101 2101 PURGE_ATTRCACHE(dvp);
2102 2102 }
2103 2103 error = nfs3_validate_caches(dvp, cr);
2104 2104 if (error)
2105 2105 return (error);
2106 2106 vp = dnlc_lookup(dvp, nm);
2107 2107 if (vp != NULL) {
2108 2108 error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2109 2109 if (error) {
2110 2110 VN_RELE(vp);
2111 2111 return (error);
2112 2112 }
2113 2113 if (vp == DNLC_NO_VNODE) {
2114 2114 VN_RELE(vp);
2115 2115 #ifdef DEBUG
2116 2116 nfs3_lookup_dnlc_neg_hits++;
2117 2117 #endif
2118 2118 return (ENOENT);
2119 2119 }
2120 2120 *vpp = vp;
2121 2121 #ifdef DEBUG
2122 2122 nfs3_lookup_dnlc_hits++;
2123 2123 #endif
2124 2124 return (0);
2125 2125 }
2126 2126 #ifdef DEBUG
2127 2127 nfs3_lookup_dnlc_disappears++;
2128 2128 #endif
2129 2129 }
2130 2130 #ifdef DEBUG
2131 2131 else
2132 2132 nfs3_lookup_dnlc_misses++;
2133 2133 #endif
2134 2134
2135 2135 *vpp = NULL;
2136 2136
2137 2137 return (0);
2138 2138 }
2139 2139
2140 2140 static int
2141 2141 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
2142 2142 int rfscall_flags)
2143 2143 {
2144 2144 int error;
2145 2145 LOOKUP3args args;
2146 2146 LOOKUP3vres res;
2147 2147 int douprintf;
2148 2148 struct vattr vattr;
2149 2149 struct vattr dvattr;
2150 2150 vnode_t *vp;
2151 2151 failinfo_t fi;
2152 2152 hrtime_t t;
2153 2153
2154 2154 ASSERT(*nm != '\0');
2155 2155 ASSERT(dvp->v_type == VDIR);
2156 2156 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2157 2157
2158 2158 setdiropargs3(&args.what, nm, dvp);
2159 2159
2160 2160 fi.vp = dvp;
2161 2161 fi.fhp = (caddr_t)&args.what.dir;
2162 2162 fi.copyproc = nfs3copyfh;
2163 2163 fi.lookupproc = nfs3lookup;
2164 2164 fi.xattrdirproc = acl_getxattrdir3;
2165 2165 res.obj_attributes.fres.vp = dvp;
2166 2166 res.obj_attributes.fres.vap = &vattr;
2167 2167 res.dir_attributes.fres.vp = dvp;
2168 2168 res.dir_attributes.fres.vap = &dvattr;
2169 2169
2170 2170 douprintf = 1;
2171 2171
2172 2172 t = gethrtime();
2173 2173
2174 2174 error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP,
2175 2175 xdr_diropargs3, (caddr_t)&args,
2176 2176 xdr_LOOKUP3vres, (caddr_t)&res, cr,
2177 2177 &douprintf, &res.status, rfscall_flags, &fi);
2178 2178
2179 2179 if (error)
2180 2180 return (error);
2181 2181
2182 2182 nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr);
2183 2183
2184 2184 error = geterrno3(res.status);
2185 2185 if (error) {
2186 2186 PURGE_STALE_FH(error, dvp, cr);
2187 2187 if (error == ENOENT && nfs3_lookup_neg_cache)
2188 2188 dnlc_enter(dvp, nm, DNLC_NO_VNODE);
2189 2189 return (error);
2190 2190 }
2191 2191
2192 2192 if (res.obj_attributes.attributes) {
2193 2193 vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap,
2194 2194 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2195 2195 } else {
2196 2196 vp = makenfs3node_va(&res.object, NULL,
2197 2197 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2198 2198 if (vp->v_type == VNON) {
2199 2199 vattr.va_mask = AT_TYPE;
2200 2200 error = nfs3getattr(vp, &vattr, cr);
2201 2201 if (error) {
2202 2202 VN_RELE(vp);
2203 2203 return (error);
2204 2204 }
2205 2205 vp->v_type = vattr.va_type;
2206 2206 }
2207 2207 }
2208 2208
2209 2209 if (!(rfscall_flags & RFSCALL_SOFT))
2210 2210 dnlc_update(dvp, nm, vp);
2211 2211
2212 2212 *vpp = vp;
2213 2213
2214 2214 return (error);
2215 2215 }
2216 2216
2217 2217 #ifdef DEBUG
2218 2218 static int nfs3_create_misses = 0;
2219 2219 #endif
2220 2220
2221 2221 /* ARGSUSED */
2222 2222 static int
2223 2223 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2224 2224 int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
2225 2225 vsecattr_t *vsecp)
2226 2226 {
2227 2227 int error;
2228 2228 vnode_t *vp;
2229 2229 rnode_t *rp;
2230 2230 struct vattr vattr;
2231 2231 rnode_t *drp;
2232 2232 vnode_t *tempvp;
2233 2233
2234 2234 drp = VTOR(dvp);
2235 2235 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2236 2236 return (EPERM);
2237 2237 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2238 2238 return (EINTR);
2239 2239
2240 2240 top:
2241 2241 /*
2242 2242 * We make a copy of the attributes because the caller does not
2243 2243 * expect us to change what va points to.
2244 2244 */
2245 2245 vattr = *va;
2246 2246
2247 2247 /*
2248 2248 * If the pathname is "", just use dvp. Don't need
2249 2249 * to send it over the wire, look it up in the dnlc,
2250 2250 * or perform any access checks.
2251 2251 */
2252 2252 if (*nm == '\0') {
2253 2253 error = 0;
2254 2254 VN_HOLD(dvp);
2255 2255 vp = dvp;
2256 2256 /*
2257 2257 * If the pathname is ".", just use dvp. Don't need
2258 2258 * to send it over the wire or look it up in the dnlc,
2259 2259 * just need to check access.
2260 2260 */
2261 2261 } else if (strcmp(nm, ".") == 0) {
2262 2262 error = nfs3_access(dvp, VEXEC, 0, cr, ct);
2263 2263 if (error) {
2264 2264 nfs_rw_exit(&drp->r_rwlock);
2265 2265 return (error);
2266 2266 }
2267 2267 VN_HOLD(dvp);
2268 2268 vp = dvp;
2269 2269 /*
2270 2270 * We need to go over the wire, just to be sure whether the
2271 2271 * file exists or not. Using the DNLC can be dangerous in
2272 2272 * this case when making a decision regarding existence.
2273 2273 */
2274 2274 } else {
2275 2275 error = nfs3lookup_otw(dvp, nm, &vp, cr, 0);
2276 2276 }
2277 2277 if (!error) {
2278 2278 if (exclusive == EXCL)
2279 2279 error = EEXIST;
2280 2280 else if (vp->v_type == VDIR && (mode & VWRITE))
2281 2281 error = EISDIR;
2282 2282 else {
2283 2283 /*
2284 2284 * If vnode is a device, create special vnode.
2285 2285 */
2286 2286 if (IS_DEVVP(vp)) {
2287 2287 tempvp = vp;
2288 2288 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2289 2289 VN_RELE(tempvp);
2290 2290 }
2291 2291 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2292 2292 if ((vattr.va_mask & AT_SIZE) &&
2293 2293 vp->v_type == VREG) {
2294 2294 rp = VTOR(vp);
2295 2295 /*
2296 2296 * Check here for large file handled
2297 2297 * by LF-unaware process (as
2298 2298 * ufs_create() does)
2299 2299 */
2300 2300 if (!(lfaware & FOFFMAX)) {
2301 2301 mutex_enter(&rp->r_statelock);
2302 2302 if (rp->r_size > MAXOFF32_T)
2303 2303 error = EOVERFLOW;
2304 2304 mutex_exit(&rp->r_statelock);
2305 2305 }
2306 2306 if (!error) {
2307 2307 vattr.va_mask = AT_SIZE;
2308 2308 error = nfs3setattr(vp,
2309 2309 &vattr, 0, cr);
2310 2310
2311 2311 /*
2312 2312 * Existing file was truncated;
2313 2313 * emit a create event.
2314 2314 */
2315 2315 vnevent_create(vp, ct);
2316 2316 }
2317 2317 }
2318 2318 }
2319 2319 }
2320 2320 nfs_rw_exit(&drp->r_rwlock);
2321 2321 if (error) {
2322 2322 VN_RELE(vp);
2323 2323 } else {
2324 2324 *vpp = vp;
2325 2325 }
2326 2326
2327 2327 return (error);
2328 2328 }
2329 2329
2330 2330 dnlc_remove(dvp, nm);
2331 2331
2332 2332 /*
2333 2333 * Decide what the group-id of the created file should be.
2334 2334 * Set it in attribute list as advisory...
2335 2335 */
2336 2336 error = setdirgid(dvp, &vattr.va_gid, cr);
2337 2337 if (error) {
2338 2338 nfs_rw_exit(&drp->r_rwlock);
2339 2339 return (error);
2340 2340 }
2341 2341 vattr.va_mask |= AT_GID;
2342 2342
2343 2343 ASSERT(vattr.va_mask & AT_TYPE);
2344 2344 if (vattr.va_type == VREG) {
2345 2345 ASSERT(vattr.va_mask & AT_MODE);
2346 2346 if (MANDMODE(vattr.va_mode)) {
2347 2347 nfs_rw_exit(&drp->r_rwlock);
2348 2348 return (EACCES);
2349 2349 }
2350 2350 error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr,
2351 2351 lfaware);
2352 2352 /*
2353 2353 * If this is not an exclusive create, then the CREATE
2354 2354 * request will be made with the GUARDED mode set. This
2355 2355 * means that the server will return EEXIST if the file
2356 2356 * exists. The file could exist because of a retransmitted
2357 2357 * request. In this case, we recover by starting over and
2358 2358 * checking to see whether the file exists. This second
2359 2359 * time through it should and a CREATE request will not be
2360 2360 * sent.
2361 2361 *
2362 2362 * This handles the problem of a dangling CREATE request
2363 2363 * which contains attributes which indicate that the file
2364 2364 * should be truncated. This retransmitted request could
2365 2365 * possibly truncate valid data in the file if not caught
2366 2366 * by the duplicate request mechanism on the server or if
2367 2367 * not caught by other means. The scenario is:
2368 2368 *
2369 2369 * Client transmits CREATE request with size = 0
2370 2370 * Client times out, retransmits request.
2371 2371 * Response to the first request arrives from the server
2372 2372 * and the client proceeds on.
2373 2373 * Client writes data to the file.
2374 2374 * The server now processes retransmitted CREATE request
2375 2375 * and truncates file.
2376 2376 *
2377 2377 * The use of the GUARDED CREATE request prevents this from
2378 2378 * happening because the retransmitted CREATE would fail
2379 2379 * with EEXIST and would not truncate the file.
2380 2380 */
2381 2381 if (error == EEXIST && exclusive == NONEXCL) {
2382 2382 #ifdef DEBUG
2383 2383 nfs3_create_misses++;
2384 2384 #endif
2385 2385 goto top;
2386 2386 }
2387 2387 nfs_rw_exit(&drp->r_rwlock);
2388 2388 return (error);
2389 2389 }
2390 2390 error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
2391 2391 nfs_rw_exit(&drp->r_rwlock);
2392 2392 return (error);
2393 2393 }
2394 2394
2395 2395 /* ARGSUSED */
2396 2396 static int
2397 2397 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2398 2398 int mode, vnode_t **vpp, cred_t *cr, int lfaware)
2399 2399 {
2400 2400 int error;
2401 2401 CREATE3args args;
2402 2402 CREATE3res res;
2403 2403 int douprintf;
2404 2404 vnode_t *vp;
2405 2405 struct vattr vattr;
2406 2406 nfstime3 *verfp;
2407 2407 rnode_t *rp;
2408 2408 timestruc_t now;
2409 2409 hrtime_t t;
2410 2410
2411 2411 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2412 2412 setdiropargs3(&args.where, nm, dvp);
2413 2413 if (exclusive == EXCL) {
2414 2414 args.how.mode = EXCLUSIVE;
2415 2415 /*
2416 2416 * Construct the create verifier. This verifier needs
2417 2417 * to be unique between different clients. It also needs
2418 2418 * to vary for each exclusive create request generated
2419 2419 * from the client to the server.
2420 2420 *
2421 2421 * The first attempt is made to use the hostid and a
2422 2422 * unique number on the client. If the hostid has not
2423 2423 * been set, the high resolution time that the exclusive
2424 2424 * create request is being made is used. This will work
2425 2425 * unless two different clients, both with the hostid
2426 2426 * not set, attempt an exclusive create request on the
2427 2427 * same file, at exactly the same clock time. The
2428 2428 * chances of this happening seem small enough to be
2429 2429 * reasonable.
2430 2430 */
2431 2431 verfp = (nfstime3 *)&args.how.createhow3_u.verf;
2432 2432 verfp->seconds = zone_get_hostid(NULL);
2433 2433 if (verfp->seconds != 0)
2434 2434 verfp->nseconds = newnum();
2435 2435 else {
2436 2436 gethrestime(&now);
2437 2437 verfp->seconds = now.tv_sec;
2438 2438 verfp->nseconds = now.tv_nsec;
2439 2439 }
2440 2440 /*
2441 2441 * Since the server will use this value for the mtime,
2442 2442 * make sure that it can't overflow. Zero out the MSB.
2443 2443 * The actual value does not matter here, only its uniqeness.
2444 2444 */
2445 2445 verfp->seconds %= INT32_MAX;
2446 2446 } else {
2447 2447 /*
2448 2448 * Issue the non-exclusive create in guarded mode. This
2449 2449 * may result in some false EEXIST responses for
2450 2450 * retransmitted requests, but these will be handled at
2451 2451 * a higher level. By using GUARDED, duplicate requests
2452 2452 * to do file truncation and possible access problems
2453 2453 * can be avoided.
2454 2454 */
2455 2455 args.how.mode = GUARDED;
2456 2456 error = vattr_to_sattr3(va,
2457 2457 &args.how.createhow3_u.obj_attributes);
2458 2458 if (error) {
2459 2459 /* req time field(s) overflow - return immediately */
2460 2460 return (error);
2461 2461 }
2462 2462 }
2463 2463
2464 2464 douprintf = 1;
2465 2465
2466 2466 t = gethrtime();
2467 2467
2468 2468 error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE,
2469 2469 xdr_CREATE3args, (caddr_t)&args,
2470 2470 xdr_CREATE3res, (caddr_t)&res, cr,
2471 2471 &douprintf, &res.status, 0, NULL);
2472 2472
2473 2473 if (error) {
2474 2474 PURGE_ATTRCACHE(dvp);
2475 2475 return (error);
2476 2476 }
2477 2477
2478 2478 error = geterrno3(res.status);
2479 2479 if (!error) {
2480 2480 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2481 2481 if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2482 2482 nfs_purge_rddir_cache(dvp);
2483 2483
2484 2484 /*
2485 2485 * On exclusive create the times need to be explicitly
2486 2486 * set to clear any potential verifier that may be stored
2487 2487 * in one of these fields (see comment below). This
2488 2488 * is done here to cover the case where no post op attrs
2489 2489 * were returned or a 'invalid' time was returned in
2490 2490 * the attributes.
2491 2491 */
2492 2492 if (exclusive == EXCL)
2493 2493 va->va_mask |= (AT_MTIME | AT_ATIME);
2494 2494
2495 2495 if (!res.resok.obj.handle_follows) {
2496 2496 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2497 2497 if (error)
2498 2498 return (error);
2499 2499 } else {
2500 2500 if (res.resok.obj_attributes.attributes) {
2501 2501 vp = makenfs3node(&res.resok.obj.handle,
2502 2502 &res.resok.obj_attributes.attr,
2503 2503 dvp->v_vfsp, t, cr, NULL, NULL);
2504 2504 } else {
2505 2505 vp = makenfs3node(&res.resok.obj.handle, NULL,
2506 2506 dvp->v_vfsp, t, cr, NULL, NULL);
2507 2507
2508 2508 /*
2509 2509 * On an exclusive create, it is possible
2510 2510 * that attributes were returned but those
2511 2511 * postop attributes failed to decode
2512 2512 * properly. If this is the case,
2513 2513 * then most likely the atime or mtime
2514 2514 * were invalid for our client; this
2515 2515 * is caused by the server storing the
2516 2516 * create verifier in one of the time
2517 2517 * fields(most likely mtime).
2518 2518 * So... we are going to setattr just the
2519 2519 * atime/mtime to clear things up.
2520 2520 */
2521 2521 if (exclusive == EXCL) {
2522 2522 if (error =
2523 2523 nfs3excl_create_settimes(vp,
2524 2524 va, cr)) {
2525 2525 /*
2526 2526 * Setting the times failed.
2527 2527 * Remove the file and return
2528 2528 * the error.
2529 2529 */
2530 2530 VN_RELE(vp);
2531 2531 (void) nfs3_remove(dvp,
2532 2532 nm, cr, NULL, 0);
2533 2533 return (error);
2534 2534 }
2535 2535 }
2536 2536
2537 2537 /*
2538 2538 * This handles the non-exclusive case
2539 2539 * and the exclusive case where no post op
2540 2540 * attrs were returned.
2541 2541 */
2542 2542 if (vp->v_type == VNON) {
2543 2543 vattr.va_mask = AT_TYPE;
2544 2544 error = nfs3getattr(vp, &vattr, cr);
2545 2545 if (error) {
2546 2546 VN_RELE(vp);
2547 2547 return (error);
2548 2548 }
2549 2549 vp->v_type = vattr.va_type;
2550 2550 }
2551 2551 }
2552 2552 dnlc_update(dvp, nm, vp);
2553 2553 }
2554 2554
2555 2555 rp = VTOR(vp);
2556 2556
2557 2557 /*
2558 2558 * Check here for large file handled by
2559 2559 * LF-unaware process (as ufs_create() does)
2560 2560 */
2561 2561 if ((va->va_mask & AT_SIZE) && vp->v_type == VREG &&
2562 2562 !(lfaware & FOFFMAX)) {
2563 2563 mutex_enter(&rp->r_statelock);
2564 2564 if (rp->r_size > MAXOFF32_T) {
2565 2565 mutex_exit(&rp->r_statelock);
2566 2566 VN_RELE(vp);
2567 2567 return (EOVERFLOW);
2568 2568 }
2569 2569 mutex_exit(&rp->r_statelock);
2570 2570 }
2571 2571
2572 2572 if (exclusive == EXCL &&
2573 2573 (va->va_mask & ~(AT_GID | AT_SIZE))) {
2574 2574 /*
2575 2575 * If doing an exclusive create, then generate
2576 2576 * a SETATTR to set the initial attributes.
2577 2577 * Try to set the mtime and the atime to the
2578 2578 * server's current time. It is somewhat
2579 2579 * expected that these fields will be used to
2580 2580 * store the exclusive create cookie. If not,
2581 2581 * server implementors will need to know that
2582 2582 * a SETATTR will follow an exclusive create
2583 2583 * and the cookie should be destroyed if
2584 2584 * appropriate. This work may have been done
2585 2585 * earlier in this function if post op attrs
2586 2586 * were not available.
2587 2587 *
2588 2588 * The AT_GID and AT_SIZE bits are turned off
2589 2589 * so that the SETATTR request will not attempt
2590 2590 * to process these. The gid will be set
2591 2591 * separately if appropriate. The size is turned
2592 2592 * off because it is assumed that a new file will
2593 2593 * be created empty and if the file wasn't empty,
2594 2594 * then the exclusive create will have failed
2595 2595 * because the file must have existed already.
2596 2596 * Therefore, no truncate operation is needed.
2597 2597 */
2598 2598 va->va_mask &= ~(AT_GID | AT_SIZE);
2599 2599 error = nfs3setattr(vp, va, 0, cr);
2600 2600 if (error) {
2601 2601 /*
2602 2602 * Couldn't correct the attributes of
2603 2603 * the newly created file and the
2604 2604 * attributes are wrong. Remove the
2605 2605 * file and return an error to the
2606 2606 * application.
2607 2607 */
2608 2608 VN_RELE(vp);
2609 2609 (void) nfs3_remove(dvp, nm, cr, NULL, 0);
2610 2610 return (error);
2611 2611 }
2612 2612 }
2613 2613
2614 2614 if (va->va_gid != rp->r_attr.va_gid) {
2615 2615 /*
2616 2616 * If the gid on the file isn't right, then
2617 2617 * generate a SETATTR to attempt to change
2618 2618 * it. This may or may not work, depending
2619 2619 * upon the server's semantics for allowing
2620 2620 * file ownership changes.
2621 2621 */
2622 2622 va->va_mask = AT_GID;
2623 2623 (void) nfs3setattr(vp, va, 0, cr);
2624 2624 }
2625 2625
2626 2626 /*
2627 2627 * If vnode is a device create special vnode
2628 2628 */
2629 2629 if (IS_DEVVP(vp)) {
2630 2630 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2631 2631 VN_RELE(vp);
2632 2632 } else
2633 2633 *vpp = vp;
2634 2634 } else {
2635 2635 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2636 2636 PURGE_STALE_FH(error, dvp, cr);
2637 2637 }
2638 2638
2639 2639 return (error);
2640 2640 }
2641 2641
2642 2642 /*
2643 2643 * Special setattr function to take care of rest of atime/mtime
2644 2644 * after successful exclusive create. This function exists to avoid
2645 2645 * handling attributes from the server; exclusive the atime/mtime fields
2646 2646 * may be 'invalid' in client's view and therefore can not be trusted.
2647 2647 */
2648 2648 static int
2649 2649 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr)
2650 2650 {
2651 2651 int error;
2652 2652 uint_t mask;
2653 2653 SETATTR3args args;
2654 2654 SETATTR3res res;
2655 2655 int douprintf;
2656 2656 rnode_t *rp;
2657 2657 hrtime_t t;
2658 2658
2659 2659 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
2660 2660 /* save the caller's mask so that it can be reset later */
2661 2661 mask = vap->va_mask;
2662 2662
2663 2663 rp = VTOR(vp);
2664 2664
2665 2665 args.object = *RTOFH3(rp);
2666 2666 args.guard.check = FALSE;
2667 2667
2668 2668 /* Use the mask to initialize the arguments */
2669 2669 vap->va_mask = 0;
2670 2670 error = vattr_to_sattr3(vap, &args.new_attributes);
2671 2671
2672 2672 /* We want to set just atime/mtime on this request */
2673 2673 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2674 2674 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2675 2675
2676 2676 douprintf = 1;
2677 2677
2678 2678 t = gethrtime();
2679 2679
2680 2680 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
2681 2681 xdr_SETATTR3args, (caddr_t)&args,
2682 2682 xdr_SETATTR3res, (caddr_t)&res, cr,
2683 2683 &douprintf, &res.status, 0, NULL);
2684 2684
2685 2685 if (error) {
2686 2686 vap->va_mask = mask;
2687 2687 return (error);
2688 2688 }
2689 2689
2690 2690 error = geterrno3(res.status);
2691 2691 if (!error) {
2692 2692 /*
2693 2693 * It is important to pick up the attributes.
2694 2694 * Since this is the exclusive create path, the
2695 2695 * attributes on the initial create were ignored
2696 2696 * and we need these to have the correct info.
2697 2697 */
2698 2698 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
2699 2699 /*
2700 2700 * No need to do the atime/mtime work again so clear
2701 2701 * the bits.
2702 2702 */
2703 2703 mask &= ~(AT_ATIME | AT_MTIME);
2704 2704 } else {
2705 2705 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
2706 2706 }
2707 2707
2708 2708 vap->va_mask = mask;
2709 2709
2710 2710 return (error);
2711 2711 }
2712 2712
2713 2713 /* ARGSUSED */
2714 2714 static int
2715 2715 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2716 2716 int mode, vnode_t **vpp, cred_t *cr)
2717 2717 {
2718 2718 int error;
2719 2719 MKNOD3args args;
2720 2720 MKNOD3res res;
2721 2721 int douprintf;
2722 2722 vnode_t *vp;
2723 2723 struct vattr vattr;
2724 2724 hrtime_t t;
2725 2725
2726 2726 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2727 2727 switch (va->va_type) {
2728 2728 case VCHR:
2729 2729 case VBLK:
2730 2730 setdiropargs3(&args.where, nm, dvp);
2731 2731 args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK;
2732 2732 error = vattr_to_sattr3(va,
2733 2733 &args.what.mknoddata3_u.device.dev_attributes);
2734 2734 if (error) {
2735 2735 /* req time field(s) overflow - return immediately */
2736 2736 return (error);
2737 2737 }
2738 2738 args.what.mknoddata3_u.device.spec.specdata1 =
2739 2739 getmajor(va->va_rdev);
2740 2740 args.what.mknoddata3_u.device.spec.specdata2 =
2741 2741 getminor(va->va_rdev);
2742 2742 break;
2743 2743
2744 2744 case VFIFO:
2745 2745 case VSOCK:
2746 2746 setdiropargs3(&args.where, nm, dvp);
2747 2747 args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK;
2748 2748 error = vattr_to_sattr3(va,
2749 2749 &args.what.mknoddata3_u.pipe_attributes);
2750 2750 if (error) {
2751 2751 /* req time field(s) overflow - return immediately */
2752 2752 return (error);
2753 2753 }
2754 2754 break;
2755 2755
2756 2756 default:
2757 2757 return (EINVAL);
2758 2758 }
2759 2759
2760 2760 douprintf = 1;
2761 2761
2762 2762 t = gethrtime();
2763 2763
2764 2764 error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD,
2765 2765 xdr_MKNOD3args, (caddr_t)&args,
2766 2766 xdr_MKNOD3res, (caddr_t)&res, cr,
2767 2767 &douprintf, &res.status, 0, NULL);
2768 2768
2769 2769 if (error) {
2770 2770 PURGE_ATTRCACHE(dvp);
2771 2771 return (error);
2772 2772 }
2773 2773
2774 2774 error = geterrno3(res.status);
2775 2775 if (!error) {
2776 2776 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2777 2777 if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2778 2778 nfs_purge_rddir_cache(dvp);
2779 2779
2780 2780 if (!res.resok.obj.handle_follows) {
2781 2781 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2782 2782 if (error)
2783 2783 return (error);
2784 2784 } else {
2785 2785 if (res.resok.obj_attributes.attributes) {
2786 2786 vp = makenfs3node(&res.resok.obj.handle,
2787 2787 &res.resok.obj_attributes.attr,
2788 2788 dvp->v_vfsp, t, cr, NULL, NULL);
2789 2789 } else {
2790 2790 vp = makenfs3node(&res.resok.obj.handle, NULL,
2791 2791 dvp->v_vfsp, t, cr, NULL, NULL);
2792 2792 if (vp->v_type == VNON) {
2793 2793 vattr.va_mask = AT_TYPE;
2794 2794 error = nfs3getattr(vp, &vattr, cr);
2795 2795 if (error) {
2796 2796 VN_RELE(vp);
2797 2797 return (error);
2798 2798 }
2799 2799 vp->v_type = vattr.va_type;
2800 2800 }
2801 2801
2802 2802 }
2803 2803 dnlc_update(dvp, nm, vp);
2804 2804 }
2805 2805
2806 2806 if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
2807 2807 va->va_mask = AT_GID;
2808 2808 (void) nfs3setattr(vp, va, 0, cr);
2809 2809 }
2810 2810
2811 2811 /*
2812 2812 * If vnode is a device create special vnode
2813 2813 */
2814 2814 if (IS_DEVVP(vp)) {
2815 2815 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2816 2816 VN_RELE(vp);
2817 2817 } else
2818 2818 *vpp = vp;
2819 2819 } else {
2820 2820 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2821 2821 PURGE_STALE_FH(error, dvp, cr);
2822 2822 }
2823 2823 return (error);
2824 2824 }
2825 2825
2826 2826 /*
2827 2827 * Weirdness: if the vnode to be removed is open
2828 2828 * we rename it instead of removing it and nfs_inactive
2829 2829 * will remove the new name.
2830 2830 */
2831 2831 /* ARGSUSED */
2832 2832 static int
2833 2833 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2834 2834 {
2835 2835 int error;
2836 2836 REMOVE3args args;
2837 2837 REMOVE3res res;
2838 2838 vnode_t *vp;
2839 2839 char *tmpname;
2840 2840 int douprintf;
2841 2841 rnode_t *rp;
2842 2842 rnode_t *drp;
2843 2843 hrtime_t t;
2844 2844
2845 2845 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2846 2846 return (EPERM);
2847 2847 drp = VTOR(dvp);
2848 2848 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2849 2849 return (EINTR);
2850 2850
2851 2851 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2852 2852 if (error) {
2853 2853 nfs_rw_exit(&drp->r_rwlock);
2854 2854 return (error);
2855 2855 }
2856 2856
2857 2857 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2858 2858 VN_RELE(vp);
2859 2859 nfs_rw_exit(&drp->r_rwlock);
2860 2860 return (EPERM);
2861 2861 }
2862 2862
2863 2863 /*
2864 2864 * First just remove the entry from the name cache, as it
2865 2865 * is most likely the only entry for this vp.
2866 2866 */
2867 2867 dnlc_remove(dvp, nm);
2868 2868
2869 2869 /*
2870 2870 * If the file has a v_count > 1 then there may be more than one
2871 2871 * entry in the name cache due multiple links or an open file,
2872 2872 * but we don't have the real reference count so flush all
2873 2873 * possible entries.
2874 2874 */
2875 2875 if (vp->v_count > 1)
2876 2876 dnlc_purge_vp(vp);
2877 2877
2878 2878 /*
2879 2879 * Now we have the real reference count on the vnode
2880 2880 */
2881 2881 rp = VTOR(vp);
2882 2882 mutex_enter(&rp->r_statelock);
2883 2883 if (vp->v_count > 1 &&
2884 2884 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2885 2885 mutex_exit(&rp->r_statelock);
2886 2886 tmpname = newname();
2887 2887 error = nfs3rename(dvp, nm, dvp, tmpname, cr, ct);
2888 2888 if (error)
2889 2889 kmem_free(tmpname, MAXNAMELEN);
2890 2890 else {
2891 2891 mutex_enter(&rp->r_statelock);
2892 2892 if (rp->r_unldvp == NULL) {
2893 2893 VN_HOLD(dvp);
2894 2894 rp->r_unldvp = dvp;
2895 2895 if (rp->r_unlcred != NULL)
2896 2896 crfree(rp->r_unlcred);
2897 2897 crhold(cr);
2898 2898 rp->r_unlcred = cr;
2899 2899 rp->r_unlname = tmpname;
2900 2900 } else {
2901 2901 kmem_free(rp->r_unlname, MAXNAMELEN);
2902 2902 rp->r_unlname = tmpname;
2903 2903 }
2904 2904 mutex_exit(&rp->r_statelock);
2905 2905 }
2906 2906 } else {
2907 2907 mutex_exit(&rp->r_statelock);
2908 2908 /*
2909 2909 * We need to flush any dirty pages which happen to
2910 2910 * be hanging around before removing the file. This
2911 2911 * shouldn't happen very often and mostly on file
2912 2912 * systems mounted "nocto".
2913 2913 */
2914 2914 if (vn_has_cached_data(vp) &&
2915 2915 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2916 2916 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2917 2917 if (error && (error == ENOSPC || error == EDQUOT)) {
2918 2918 mutex_enter(&rp->r_statelock);
2919 2919 if (!rp->r_error)
2920 2920 rp->r_error = error;
2921 2921 mutex_exit(&rp->r_statelock);
2922 2922 }
2923 2923 }
2924 2924
2925 2925 setdiropargs3(&args.object, nm, dvp);
2926 2926
2927 2927 douprintf = 1;
2928 2928
2929 2929 t = gethrtime();
2930 2930
2931 2931 error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE,
2932 2932 xdr_diropargs3, (caddr_t)&args,
2933 2933 xdr_REMOVE3res, (caddr_t)&res, cr,
2934 2934 &douprintf, &res.status, 0, NULL);
2935 2935
2936 2936 /*
2937 2937 * The xattr dir may be gone after last attr is removed,
2938 2938 * so flush it from dnlc.
2939 2939 */
2940 2940 if (dvp->v_flag & V_XATTRDIR)
2941 2941 dnlc_purge_vp(dvp);
2942 2942
2943 2943 PURGE_ATTRCACHE(vp);
2944 2944
2945 2945 if (error) {
2946 2946 PURGE_ATTRCACHE(dvp);
2947 2947 } else {
2948 2948 error = geterrno3(res.status);
2949 2949 if (!error) {
2950 2950 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t,
2951 2951 cr);
2952 2952 if (HAVE_RDDIR_CACHE(drp))
2953 2953 nfs_purge_rddir_cache(dvp);
2954 2954 } else {
2955 2955 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc,
2956 2956 t, cr);
2957 2957 PURGE_STALE_FH(error, dvp, cr);
2958 2958 }
2959 2959 }
2960 2960 }
2961 2961
2962 2962 if (error == 0) {
2963 2963 vnevent_remove(vp, dvp, nm, ct);
2964 2964 }
2965 2965 VN_RELE(vp);
2966 2966
2967 2967 nfs_rw_exit(&drp->r_rwlock);
2968 2968
2969 2969 return (error);
2970 2970 }
2971 2971
2972 2972 /* ARGSUSED */
2973 2973 static int
2974 2974 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2975 2975 caller_context_t *ct, int flags)
2976 2976 {
2977 2977 int error;
2978 2978 LINK3args args;
2979 2979 LINK3res res;
2980 2980 vnode_t *realvp;
2981 2981 int douprintf;
2982 2982 mntinfo_t *mi;
2983 2983 rnode_t *tdrp;
2984 2984 hrtime_t t;
2985 2985
2986 2986 if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2987 2987 return (EPERM);
2988 2988 if (VOP_REALVP(svp, &realvp, ct) == 0)
2989 2989 svp = realvp;
2990 2990
2991 2991 mi = VTOMI(svp);
2992 2992
2993 2993 if (!(mi->mi_flags & MI_LINK))
2994 2994 return (EOPNOTSUPP);
2995 2995
2996 2996 args.file = *VTOFH3(svp);
2997 2997 setdiropargs3(&args.link, tnm, tdvp);
2998 2998
2999 2999 tdrp = VTOR(tdvp);
3000 3000 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
3001 3001 return (EINTR);
3002 3002
3003 3003 dnlc_remove(tdvp, tnm);
3004 3004
3005 3005 douprintf = 1;
3006 3006
3007 3007 t = gethrtime();
3008 3008
3009 3009 error = rfs3call(mi, NFSPROC3_LINK,
3010 3010 xdr_LINK3args, (caddr_t)&args,
3011 3011 xdr_LINK3res, (caddr_t)&res, cr,
3012 3012 &douprintf, &res.status, 0, NULL);
3013 3013
3014 3014 if (error) {
3015 3015 PURGE_ATTRCACHE(tdvp);
3016 3016 PURGE_ATTRCACHE(svp);
3017 3017 nfs_rw_exit(&tdrp->r_rwlock);
3018 3018 return (error);
3019 3019 }
3020 3020
3021 3021 error = geterrno3(res.status);
3022 3022
3023 3023 if (!error) {
3024 3024 nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr);
3025 3025 nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr);
3026 3026 if (HAVE_RDDIR_CACHE(tdrp))
3027 3027 nfs_purge_rddir_cache(tdvp);
3028 3028 dnlc_update(tdvp, tnm, svp);
3029 3029 } else {
3030 3030 nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t,
3031 3031 cr);
3032 3032 nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr);
3033 3033 if (error == EOPNOTSUPP) {
3034 3034 mutex_enter(&mi->mi_lock);
3035 3035 mi->mi_flags &= ~MI_LINK;
3036 3036 mutex_exit(&mi->mi_lock);
3037 3037 }
3038 3038 }
3039 3039
3040 3040 nfs_rw_exit(&tdrp->r_rwlock);
3041 3041
3042 3042 if (!error) {
3043 3043 /*
3044 3044 * Notify the source file of this link operation.
3045 3045 */
3046 3046 vnevent_link(svp, ct);
3047 3047 }
3048 3048 return (error);
3049 3049 }
3050 3050
3051 3051 /* ARGSUSED */
3052 3052 static int
3053 3053 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3054 3054 caller_context_t *ct, int flags)
3055 3055 {
3056 3056 vnode_t *realvp;
3057 3057
3058 3058 if (nfs_zone() != VTOMI(odvp)->mi_zone)
3059 3059 return (EPERM);
3060 3060 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
3061 3061 ndvp = realvp;
3062 3062
3063 3063 return (nfs3rename(odvp, onm, ndvp, nnm, cr, ct));
3064 3064 }
3065 3065
3066 3066 /*
3067 3067 * nfs3rename does the real work of renaming in NFS Version 3.
3068 3068 */
3069 3069 static int
3070 3070 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3071 3071 caller_context_t *ct)
3072 3072 {
3073 3073 int error;
3074 3074 RENAME3args args;
3075 3075 RENAME3res res;
3076 3076 int douprintf;
3077 3077 vnode_t *nvp = NULL;
3078 3078 vnode_t *ovp = NULL;
3079 3079 char *tmpname;
3080 3080 rnode_t *rp;
3081 3081 rnode_t *odrp;
3082 3082 rnode_t *ndrp;
3083 3083 hrtime_t t;
3084 3084
3085 3085 ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
3086 3086
3087 3087 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
3088 3088 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
3089 3089 return (EINVAL);
3090 3090
3091 3091 odrp = VTOR(odvp);
3092 3092 ndrp = VTOR(ndvp);
3093 3093 if ((intptr_t)odrp < (intptr_t)ndrp) {
3094 3094 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
3095 3095 return (EINTR);
3096 3096 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
3097 3097 nfs_rw_exit(&odrp->r_rwlock);
3098 3098 return (EINTR);
3099 3099 }
3100 3100 } else {
3101 3101 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
3102 3102 return (EINTR);
3103 3103 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
3104 3104 nfs_rw_exit(&ndrp->r_rwlock);
3105 3105 return (EINTR);
3106 3106 }
3107 3107 }
3108 3108
3109 3109 /*
3110 3110 * Lookup the target file. If it exists, it needs to be
3111 3111 * checked to see whether it is a mount point and whether
3112 3112 * it is active (open).
3113 3113 */
3114 3114 error = nfs3lookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
3115 3115 if (!error) {
3116 3116 /*
3117 3117 * If this file has been mounted on, then just
3118 3118 * return busy because renaming to it would remove
3119 3119 * the mounted file system from the name space.
3120 3120 */
3121 3121 if (vn_mountedvfs(nvp) != NULL) {
3122 3122 VN_RELE(nvp);
3123 3123 nfs_rw_exit(&odrp->r_rwlock);
3124 3124 nfs_rw_exit(&ndrp->r_rwlock);
3125 3125 return (EBUSY);
3126 3126 }
3127 3127
3128 3128 /*
3129 3129 * Purge the name cache of all references to this vnode
3130 3130 * so that we can check the reference count to infer
3131 3131 * whether it is active or not.
3132 3132 */
3133 3133 /*
3134 3134 * First just remove the entry from the name cache, as it
3135 3135 * is most likely the only entry for this vp.
3136 3136 */
3137 3137 dnlc_remove(ndvp, nnm);
3138 3138 /*
3139 3139 * If the file has a v_count > 1 then there may be more
3140 3140 * than one entry in the name cache due multiple links
3141 3141 * or an open file, but we don't have the real reference
3142 3142 * count so flush all possible entries.
3143 3143 */
3144 3144 if (nvp->v_count > 1)
3145 3145 dnlc_purge_vp(nvp);
3146 3146
3147 3147 /*
3148 3148 * If the vnode is active and is not a directory,
3149 3149 * arrange to rename it to a
3150 3150 * temporary file so that it will continue to be
3151 3151 * accessible. This implements the "unlink-open-file"
3152 3152 * semantics for the target of a rename operation.
3153 3153 * Before doing this though, make sure that the
3154 3154 * source and target files are not already the same.
3155 3155 */
3156 3156 if (nvp->v_count > 1 && nvp->v_type != VDIR) {
3157 3157 /*
3158 3158 * Lookup the source name.
3159 3159 */
3160 3160 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL,
3161 3161 cr, 0);
3162 3162
3163 3163 /*
3164 3164 * The source name *should* already exist.
3165 3165 */
3166 3166 if (error) {
3167 3167 VN_RELE(nvp);
3168 3168 nfs_rw_exit(&odrp->r_rwlock);
3169 3169 nfs_rw_exit(&ndrp->r_rwlock);
3170 3170 return (error);
3171 3171 }
3172 3172
3173 3173 /*
3174 3174 * Compare the two vnodes. If they are the same,
3175 3175 * just release all held vnodes and return success.
3176 3176 */
3177 3177 if (ovp == nvp) {
3178 3178 VN_RELE(ovp);
3179 3179 VN_RELE(nvp);
3180 3180 nfs_rw_exit(&odrp->r_rwlock);
3181 3181 nfs_rw_exit(&ndrp->r_rwlock);
3182 3182 return (0);
3183 3183 }
3184 3184
3185 3185 /*
3186 3186 * Can't mix and match directories and non-
3187 3187 * directories in rename operations. We already
3188 3188 * know that the target is not a directory. If
3189 3189 * the source is a directory, return an error.
3190 3190 */
3191 3191 if (ovp->v_type == VDIR) {
3192 3192 VN_RELE(ovp);
3193 3193 VN_RELE(nvp);
3194 3194 nfs_rw_exit(&odrp->r_rwlock);
3195 3195 nfs_rw_exit(&ndrp->r_rwlock);
3196 3196 return (ENOTDIR);
3197 3197 }
3198 3198
3199 3199 /*
3200 3200 * The target file exists, is not the same as
3201 3201 * the source file, and is active. Link it
3202 3202 * to a temporary filename to avoid having
3203 3203 * the server removing the file completely.
3204 3204 */
3205 3205 tmpname = newname();
3206 3206 error = nfs3_link(ndvp, nvp, tmpname, cr, NULL, 0);
3207 3207 if (error == EOPNOTSUPP) {
3208 3208 error = nfs3_rename(ndvp, nnm, ndvp, tmpname,
3209 3209 cr, NULL, 0);
3210 3210 }
3211 3211 if (error) {
3212 3212 kmem_free(tmpname, MAXNAMELEN);
3213 3213 VN_RELE(ovp);
3214 3214 VN_RELE(nvp);
3215 3215 nfs_rw_exit(&odrp->r_rwlock);
3216 3216 nfs_rw_exit(&ndrp->r_rwlock);
3217 3217 return (error);
3218 3218 }
3219 3219 rp = VTOR(nvp);
3220 3220 mutex_enter(&rp->r_statelock);
3221 3221 if (rp->r_unldvp == NULL) {
3222 3222 VN_HOLD(ndvp);
3223 3223 rp->r_unldvp = ndvp;
3224 3224 if (rp->r_unlcred != NULL)
3225 3225 crfree(rp->r_unlcred);
3226 3226 crhold(cr);
3227 3227 rp->r_unlcred = cr;
3228 3228 rp->r_unlname = tmpname;
3229 3229 } else {
3230 3230 kmem_free(rp->r_unlname, MAXNAMELEN);
3231 3231 rp->r_unlname = tmpname;
3232 3232 }
3233 3233 mutex_exit(&rp->r_statelock);
3234 3234 }
3235 3235 }
3236 3236
3237 3237 if (ovp == NULL) {
3238 3238 /*
3239 3239 * When renaming directories to be a subdirectory of a
3240 3240 * different parent, the dnlc entry for ".." will no
3241 3241 * longer be valid, so it must be removed.
3242 3242 *
3243 3243 * We do a lookup here to determine whether we are renaming
3244 3244 * a directory and we need to check if we are renaming
3245 3245 * an unlinked file. This might have already been done
3246 3246 * in previous code, so we check ovp == NULL to avoid
3247 3247 * doing it twice.
3248 3248 */
3249 3249
3250 3250 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
3251 3251 /*
3252 3252 * The source name *should* already exist.
3253 3253 */
3254 3254 if (error) {
3255 3255 nfs_rw_exit(&odrp->r_rwlock);
3256 3256 nfs_rw_exit(&ndrp->r_rwlock);
3257 3257 if (nvp) {
3258 3258 VN_RELE(nvp);
3259 3259 }
3260 3260 return (error);
3261 3261 }
3262 3262 ASSERT(ovp != NULL);
3263 3263 }
3264 3264
3265 3265 dnlc_remove(odvp, onm);
3266 3266 dnlc_remove(ndvp, nnm);
3267 3267
3268 3268 setdiropargs3(&args.from, onm, odvp);
3269 3269 setdiropargs3(&args.to, nnm, ndvp);
3270 3270
3271 3271 douprintf = 1;
3272 3272
3273 3273 t = gethrtime();
3274 3274
3275 3275 error = rfs3call(VTOMI(odvp), NFSPROC3_RENAME,
3276 3276 xdr_RENAME3args, (caddr_t)&args,
3277 3277 xdr_RENAME3res, (caddr_t)&res, cr,
3278 3278 &douprintf, &res.status, 0, NULL);
3279 3279
3280 3280 if (error) {
3281 3281 PURGE_ATTRCACHE(odvp);
3282 3282 PURGE_ATTRCACHE(ndvp);
3283 3283 VN_RELE(ovp);
3284 3284 nfs_rw_exit(&odrp->r_rwlock);
3285 3285 nfs_rw_exit(&ndrp->r_rwlock);
3286 3286 if (nvp) {
3287 3287 VN_RELE(nvp);
3288 3288 }
3289 3289 return (error);
3290 3290 }
3291 3291
3292 3292 error = geterrno3(res.status);
3293 3293
3294 3294 if (!error) {
3295 3295 nfs3_cache_wcc_data(odvp, &res.resok.fromdir_wcc, t, cr);
3296 3296 if (HAVE_RDDIR_CACHE(odrp))
3297 3297 nfs_purge_rddir_cache(odvp);
3298 3298 if (ndvp != odvp) {
3299 3299 nfs3_cache_wcc_data(ndvp, &res.resok.todir_wcc, t, cr);
3300 3300 if (HAVE_RDDIR_CACHE(ndrp))
3301 3301 nfs_purge_rddir_cache(ndvp);
3302 3302 }
3303 3303 /*
3304 3304 * when renaming directories to be a subdirectory of a
3305 3305 * different parent, the dnlc entry for ".." will no
3306 3306 * longer be valid, so it must be removed
3307 3307 */
3308 3308 rp = VTOR(ovp);
3309 3309 if (ndvp != odvp) {
3310 3310 if (ovp->v_type == VDIR) {
3311 3311 dnlc_remove(ovp, "..");
3312 3312 if (HAVE_RDDIR_CACHE(rp))
3313 3313 nfs_purge_rddir_cache(ovp);
3314 3314 }
3315 3315 }
3316 3316
3317 3317 /*
3318 3318 * If we are renaming the unlinked file, update the
3319 3319 * r_unldvp and r_unlname as needed.
3320 3320 */
3321 3321 mutex_enter(&rp->r_statelock);
3322 3322 if (rp->r_unldvp != NULL) {
3323 3323 if (strcmp(rp->r_unlname, onm) == 0) {
3324 3324 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
3325 3325 rp->r_unlname[MAXNAMELEN - 1] = '\0';
3326 3326
3327 3327 if (ndvp != rp->r_unldvp) {
3328 3328 VN_RELE(rp->r_unldvp);
3329 3329 rp->r_unldvp = ndvp;
3330 3330 VN_HOLD(ndvp);
3331 3331 }
3332 3332 }
3333 3333 }
3334 3334 mutex_exit(&rp->r_statelock);
3335 3335 } else {
3336 3336 nfs3_cache_wcc_data(odvp, &res.resfail.fromdir_wcc, t, cr);
3337 3337 if (ndvp != odvp) {
3338 3338 nfs3_cache_wcc_data(ndvp, &res.resfail.todir_wcc, t,
3339 3339 cr);
3340 3340 }
3341 3341 /*
3342 3342 * System V defines rename to return EEXIST, not
3343 3343 * ENOTEMPTY if the target directory is not empty.
3344 3344 * Over the wire, the error is NFSERR_ENOTEMPTY
3345 3345 * which geterrno maps to ENOTEMPTY.
3346 3346 */
3347 3347 if (error == ENOTEMPTY)
3348 3348 error = EEXIST;
3349 3349 }
3350 3350
3351 3351 if (error == 0) {
3352 3352 if (nvp)
3353 3353 vnevent_rename_dest(nvp, ndvp, nnm, ct);
3354 3354
3355 3355 if (odvp != ndvp)
3356 3356 vnevent_rename_dest_dir(ndvp, ct);
3357 3357 ASSERT(ovp != NULL);
3358 3358 vnevent_rename_src(ovp, odvp, onm, ct);
3359 3359 }
3360 3360
3361 3361 if (nvp) {
3362 3362 VN_RELE(nvp);
3363 3363 }
3364 3364 VN_RELE(ovp);
3365 3365
3366 3366 nfs_rw_exit(&odrp->r_rwlock);
3367 3367 nfs_rw_exit(&ndrp->r_rwlock);
3368 3368
3369 3369 return (error);
3370 3370 }
3371 3371
3372 3372 /* ARGSUSED */
3373 3373 static int
3374 3374 nfs3_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
3375 3375 caller_context_t *ct, int flags, vsecattr_t *vsecp)
3376 3376 {
3377 3377 int error;
3378 3378 MKDIR3args args;
3379 3379 MKDIR3res res;
3380 3380 int douprintf;
3381 3381 struct vattr vattr;
3382 3382 vnode_t *vp;
3383 3383 rnode_t *drp;
3384 3384 hrtime_t t;
3385 3385
3386 3386 if (nfs_zone() != VTOMI(dvp)->mi_zone)
3387 3387 return (EPERM);
3388 3388 setdiropargs3(&args.where, nm, dvp);
3389 3389
3390 3390 /*
3391 3391 * Decide what the group-id and set-gid bit of the created directory
3392 3392 * should be. May have to do a setattr to get the gid right.
3393 3393 */
3394 3394 error = setdirgid(dvp, &va->va_gid, cr);
3395 3395 if (error)
3396 3396 return (error);
3397 3397 error = setdirmode(dvp, &va->va_mode, cr);
3398 3398 if (error)
3399 3399 return (error);
3400 3400 va->va_mask |= AT_MODE|AT_GID;
3401 3401
3402 3402 error = vattr_to_sattr3(va, &args.attributes);
3403 3403 if (error) {
3404 3404 /* req time field(s) overflow - return immediately */
3405 3405 return (error);
3406 3406 }
3407 3407
3408 3408 drp = VTOR(dvp);
3409 3409 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3410 3410 return (EINTR);
3411 3411
3412 3412 dnlc_remove(dvp, nm);
3413 3413
3414 3414 douprintf = 1;
3415 3415
3416 3416 t = gethrtime();
3417 3417
3418 3418 error = rfs3call(VTOMI(dvp), NFSPROC3_MKDIR,
3419 3419 xdr_MKDIR3args, (caddr_t)&args,
3420 3420 xdr_MKDIR3res, (caddr_t)&res, cr,
3421 3421 &douprintf, &res.status, 0, NULL);
3422 3422
3423 3423 if (error) {
3424 3424 PURGE_ATTRCACHE(dvp);
3425 3425 nfs_rw_exit(&drp->r_rwlock);
3426 3426 return (error);
3427 3427 }
3428 3428
3429 3429 error = geterrno3(res.status);
3430 3430 if (!error) {
3431 3431 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3432 3432 if (HAVE_RDDIR_CACHE(drp))
3433 3433 nfs_purge_rddir_cache(dvp);
3434 3434
3435 3435 if (!res.resok.obj.handle_follows) {
3436 3436 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3437 3437 if (error) {
3438 3438 nfs_rw_exit(&drp->r_rwlock);
3439 3439 return (error);
3440 3440 }
3441 3441 } else {
3442 3442 if (res.resok.obj_attributes.attributes) {
3443 3443 vp = makenfs3node(&res.resok.obj.handle,
3444 3444 &res.resok.obj_attributes.attr,
3445 3445 dvp->v_vfsp, t, cr, NULL, NULL);
3446 3446 } else {
3447 3447 vp = makenfs3node(&res.resok.obj.handle, NULL,
3448 3448 dvp->v_vfsp, t, cr, NULL, NULL);
3449 3449 if (vp->v_type == VNON) {
3450 3450 vattr.va_mask = AT_TYPE;
3451 3451 error = nfs3getattr(vp, &vattr, cr);
3452 3452 if (error) {
3453 3453 VN_RELE(vp);
3454 3454 nfs_rw_exit(&drp->r_rwlock);
3455 3455 return (error);
3456 3456 }
3457 3457 vp->v_type = vattr.va_type;
3458 3458 }
3459 3459 }
3460 3460 dnlc_update(dvp, nm, vp);
3461 3461 }
3462 3462 if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
3463 3463 va->va_mask = AT_GID;
3464 3464 (void) nfs3setattr(vp, va, 0, cr);
3465 3465 }
3466 3466 *vpp = vp;
3467 3467 } else {
3468 3468 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3469 3469 PURGE_STALE_FH(error, dvp, cr);
3470 3470 }
3471 3471
3472 3472 nfs_rw_exit(&drp->r_rwlock);
3473 3473
3474 3474 return (error);
3475 3475 }
3476 3476
3477 3477 /* ARGSUSED */
3478 3478 static int
3479 3479 nfs3_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
3480 3480 caller_context_t *ct, int flags)
3481 3481 {
3482 3482 int error;
3483 3483 RMDIR3args args;
3484 3484 RMDIR3res res;
3485 3485 vnode_t *vp;
3486 3486 int douprintf;
3487 3487 rnode_t *drp;
3488 3488 hrtime_t t;
3489 3489
3490 3490 if (nfs_zone() != VTOMI(dvp)->mi_zone)
3491 3491 return (EPERM);
3492 3492 drp = VTOR(dvp);
3493 3493 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3494 3494 return (EINTR);
3495 3495
3496 3496 /*
3497 3497 * Attempt to prevent a rmdir(".") from succeeding.
3498 3498 */
3499 3499 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3500 3500 if (error) {
3501 3501 nfs_rw_exit(&drp->r_rwlock);
3502 3502 return (error);
3503 3503 }
3504 3504
3505 3505 if (vp == cdir) {
3506 3506 VN_RELE(vp);
3507 3507 nfs_rw_exit(&drp->r_rwlock);
3508 3508 return (EINVAL);
3509 3509 }
3510 3510
3511 3511 setdiropargs3(&args.object, nm, dvp);
3512 3512
3513 3513 /*
3514 3514 * First just remove the entry from the name cache, as it
3515 3515 * is most likely an entry for this vp.
3516 3516 */
3517 3517 dnlc_remove(dvp, nm);
3518 3518
3519 3519 /*
3520 3520 * If there vnode reference count is greater than one, then
3521 3521 * there may be additional references in the DNLC which will
3522 3522 * need to be purged. First, trying removing the entry for
3523 3523 * the parent directory and see if that removes the additional
3524 3524 * reference(s). If that doesn't do it, then use dnlc_purge_vp
3525 3525 * to completely remove any references to the directory which
3526 3526 * might still exist in the DNLC.
3527 3527 */
3528 3528 if (vp->v_count > 1) {
3529 3529 dnlc_remove(vp, "..");
3530 3530 if (vp->v_count > 1)
3531 3531 dnlc_purge_vp(vp);
3532 3532 }
3533 3533
3534 3534 douprintf = 1;
3535 3535
3536 3536 t = gethrtime();
3537 3537
3538 3538 error = rfs3call(VTOMI(dvp), NFSPROC3_RMDIR,
3539 3539 xdr_diropargs3, (caddr_t)&args,
3540 3540 xdr_RMDIR3res, (caddr_t)&res, cr,
3541 3541 &douprintf, &res.status, 0, NULL);
3542 3542
3543 3543 PURGE_ATTRCACHE(vp);
3544 3544
3545 3545 if (error) {
3546 3546 PURGE_ATTRCACHE(dvp);
3547 3547 VN_RELE(vp);
3548 3548 nfs_rw_exit(&drp->r_rwlock);
3549 3549 return (error);
3550 3550 }
3551 3551
3552 3552 error = geterrno3(res.status);
3553 3553 if (!error) {
3554 3554 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3555 3555 if (HAVE_RDDIR_CACHE(drp))
3556 3556 nfs_purge_rddir_cache(dvp);
3557 3557 if (HAVE_RDDIR_CACHE(VTOR(vp)))
3558 3558 nfs_purge_rddir_cache(vp);
3559 3559 } else {
3560 3560 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3561 3561 PURGE_STALE_FH(error, dvp, cr);
3562 3562 /*
3563 3563 * System V defines rmdir to return EEXIST, not
3564 3564 * ENOTEMPTY if the directory is not empty. Over
3565 3565 * the wire, the error is NFSERR_ENOTEMPTY which
3566 3566 * geterrno maps to ENOTEMPTY.
3567 3567 */
3568 3568 if (error == ENOTEMPTY)
3569 3569 error = EEXIST;
3570 3570 }
3571 3571
3572 3572 if (error == 0) {
3573 3573 vnevent_rmdir(vp, dvp, nm, ct);
3574 3574 }
3575 3575 VN_RELE(vp);
3576 3576
3577 3577 nfs_rw_exit(&drp->r_rwlock);
3578 3578
3579 3579 return (error);
3580 3580 }
3581 3581
3582 3582 /* ARGSUSED */
3583 3583 static int
3584 3584 nfs3_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
3585 3585 caller_context_t *ct, int flags)
3586 3586 {
3587 3587 int error;
3588 3588 SYMLINK3args args;
3589 3589 SYMLINK3res res;
3590 3590 int douprintf;
3591 3591 mntinfo_t *mi;
3592 3592 vnode_t *vp;
3593 3593 rnode_t *rp;
3594 3594 char *contents;
3595 3595 rnode_t *drp;
3596 3596 hrtime_t t;
3597 3597
3598 3598 mi = VTOMI(dvp);
3599 3599
3600 3600 if (nfs_zone() != mi->mi_zone)
3601 3601 return (EPERM);
3602 3602 if (!(mi->mi_flags & MI_SYMLINK))
3603 3603 return (EOPNOTSUPP);
3604 3604
3605 3605 setdiropargs3(&args.where, lnm, dvp);
3606 3606 error = vattr_to_sattr3(tva, &args.symlink.symlink_attributes);
3607 3607 if (error) {
3608 3608 /* req time field(s) overflow - return immediately */
3609 3609 return (error);
3610 3610 }
3611 3611 args.symlink.symlink_data = tnm;
3612 3612
3613 3613 drp = VTOR(dvp);
3614 3614 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3615 3615 return (EINTR);
3616 3616
3617 3617 dnlc_remove(dvp, lnm);
3618 3618
3619 3619 douprintf = 1;
3620 3620
3621 3621 t = gethrtime();
3622 3622
3623 3623 error = rfs3call(mi, NFSPROC3_SYMLINK,
3624 3624 xdr_SYMLINK3args, (caddr_t)&args,
3625 3625 xdr_SYMLINK3res, (caddr_t)&res, cr,
3626 3626 &douprintf, &res.status, 0, NULL);
3627 3627
3628 3628 if (error) {
3629 3629 PURGE_ATTRCACHE(dvp);
3630 3630 nfs_rw_exit(&drp->r_rwlock);
3631 3631 return (error);
3632 3632 }
3633 3633
3634 3634 error = geterrno3(res.status);
3635 3635 if (!error) {
3636 3636 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3637 3637 if (HAVE_RDDIR_CACHE(drp))
3638 3638 nfs_purge_rddir_cache(dvp);
3639 3639
3640 3640 if (res.resok.obj.handle_follows) {
3641 3641 if (res.resok.obj_attributes.attributes) {
3642 3642 vp = makenfs3node(&res.resok.obj.handle,
3643 3643 &res.resok.obj_attributes.attr,
3644 3644 dvp->v_vfsp, t, cr, NULL, NULL);
3645 3645 } else {
3646 3646 vp = makenfs3node(&res.resok.obj.handle, NULL,
3647 3647 dvp->v_vfsp, t, cr, NULL, NULL);
3648 3648 vp->v_type = VLNK;
3649 3649 vp->v_rdev = 0;
3650 3650 }
3651 3651 dnlc_update(dvp, lnm, vp);
3652 3652 rp = VTOR(vp);
3653 3653 if (nfs3_do_symlink_cache &&
3654 3654 rp->r_symlink.contents == NULL) {
3655 3655
3656 3656 contents = kmem_alloc(MAXPATHLEN,
3657 3657 KM_NOSLEEP);
3658 3658
3659 3659 if (contents != NULL) {
3660 3660 mutex_enter(&rp->r_statelock);
3661 3661 if (rp->r_symlink.contents == NULL) {
3662 3662 rp->r_symlink.len = strlen(tnm);
3663 3663 bcopy(tnm, contents,
3664 3664 rp->r_symlink.len);
3665 3665 rp->r_symlink.contents =
3666 3666 contents;
3667 3667 rp->r_symlink.size = MAXPATHLEN;
3668 3668 mutex_exit(&rp->r_statelock);
3669 3669 } else {
3670 3670 mutex_exit(&rp->r_statelock);
3671 3671 kmem_free((void *)contents,
3672 3672 MAXPATHLEN);
3673 3673 }
3674 3674 }
3675 3675 }
3676 3676 VN_RELE(vp);
3677 3677 }
3678 3678 } else {
3679 3679 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3680 3680 PURGE_STALE_FH(error, dvp, cr);
3681 3681 if (error == EOPNOTSUPP) {
3682 3682 mutex_enter(&mi->mi_lock);
3683 3683 mi->mi_flags &= ~MI_SYMLINK;
3684 3684 mutex_exit(&mi->mi_lock);
3685 3685 }
3686 3686 }
3687 3687
3688 3688 nfs_rw_exit(&drp->r_rwlock);
3689 3689
3690 3690 return (error);
3691 3691 }
3692 3692
3693 3693 #ifdef DEBUG
3694 3694 static int nfs3_readdir_cache_hits = 0;
3695 3695 static int nfs3_readdir_cache_shorts = 0;
3696 3696 static int nfs3_readdir_cache_waits = 0;
3697 3697 static int nfs3_readdir_cache_misses = 0;
3698 3698 static int nfs3_readdir_readahead = 0;
3699 3699 #endif
3700 3700
3701 3701 static int nfs3_shrinkreaddir = 0;
3702 3702
3703 3703 /*
3704 3704 * Read directory entries.
3705 3705 * There are some weird things to look out for here. The uio_loffset
3706 3706 * field is either 0 or it is the offset returned from a previous
3707 3707 * readdir. It is an opaque value used by the server to find the
3708 3708 * correct directory block to read. The count field is the number
3709 3709 * of blocks to read on the server. This is advisory only, the server
3710 3710 * may return only one block's worth of entries. Entries may be compressed
3711 3711 * on the server.
3712 3712 */
3713 3713 /* ARGSUSED */
3714 3714 static int
3715 3715 nfs3_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
3716 3716 caller_context_t *ct, int flags)
3717 3717 {
3718 3718 int error;
3719 3719 size_t count;
3720 3720 rnode_t *rp;
3721 3721 rddir_cache *rdc;
3722 3722 rddir_cache *nrdc;
3723 3723 rddir_cache *rrdc;
3724 3724 #ifdef DEBUG
3725 3725 int missed;
3726 3726 #endif
3727 3727 int doreadahead;
3728 3728 rddir_cache srdc;
3729 3729 avl_index_t where;
3730 3730
3731 3731 if (nfs_zone() != VTOMI(vp)->mi_zone)
3732 3732 return (EIO);
3733 3733 rp = VTOR(vp);
3734 3734
3735 3735 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
3736 3736
3737 3737 /*
3738 3738 * Make sure that the directory cache is valid.
3739 3739 */
3740 3740 if (HAVE_RDDIR_CACHE(rp)) {
3741 3741 if (nfs_disable_rddir_cache) {
3742 3742 /*
3743 3743 * Setting nfs_disable_rddir_cache in /etc/system
3744 3744 * allows interoperability with servers that do not
3745 3745 * properly update the attributes of directories.
3746 3746 * Any cached information gets purged before an
3747 3747 * access is made to it.
3748 3748 */
3749 3749 nfs_purge_rddir_cache(vp);
3750 3750 } else {
3751 3751 error = nfs3_validate_caches(vp, cr);
3752 3752 if (error)
3753 3753 return (error);
3754 3754 }
3755 3755 }
3756 3756
3757 3757 /*
3758 3758 * It is possible that some servers may not be able to correctly
3759 3759 * handle a large READDIR or READDIRPLUS request due to bugs in
3760 3760 * their implementation. In order to continue to interoperate
3761 3761 * with them, this workaround is provided to limit the maximum
3762 3762 * size of a READDIRPLUS request to 1024. In any case, the request
3763 3763 * size is limited to MAXBSIZE.
3764 3764 */
3765 3765 count = MIN(uiop->uio_iov->iov_len,
3766 3766 nfs3_shrinkreaddir ? 1024 : MAXBSIZE);
3767 3767
3768 3768 nrdc = NULL;
3769 3769 #ifdef DEBUG
3770 3770 missed = 0;
3771 3771 #endif
3772 3772 top:
3773 3773 /*
3774 3774 * Short circuit last readdir which always returns 0 bytes.
3775 3775 * This can be done after the directory has been read through
3776 3776 * completely at least once. This will set r_direof which
3777 3777 * can be used to find the value of the last cookie.
3778 3778 */
3779 3779 mutex_enter(&rp->r_statelock);
3780 3780 if (rp->r_direof != NULL &&
3781 3781 uiop->uio_loffset == rp->r_direof->nfs3_ncookie) {
3782 3782 mutex_exit(&rp->r_statelock);
3783 3783 #ifdef DEBUG
3784 3784 nfs3_readdir_cache_shorts++;
3785 3785 #endif
3786 3786 if (eofp)
3787 3787 *eofp = 1;
3788 3788 if (nrdc != NULL)
3789 3789 rddir_cache_rele(nrdc);
3790 3790 return (0);
3791 3791 }
3792 3792 /*
3793 3793 * Look for a cache entry. Cache entries are identified
3794 3794 * by the NFS cookie value and the byte count requested.
3795 3795 */
3796 3796 srdc.nfs3_cookie = uiop->uio_loffset;
3797 3797 srdc.buflen = count;
3798 3798 rdc = avl_find(&rp->r_dir, &srdc, &where);
3799 3799 if (rdc != NULL) {
3800 3800 rddir_cache_hold(rdc);
3801 3801 /*
3802 3802 * If the cache entry is in the process of being
3803 3803 * filled in, wait until this completes. The
3804 3804 * RDDIRWAIT bit is set to indicate that someone
3805 3805 * is waiting and then the thread currently
3806 3806 * filling the entry is done, it should do a
3807 3807 * cv_broadcast to wakeup all of the threads
3808 3808 * waiting for it to finish.
3809 3809 */
3810 3810 if (rdc->flags & RDDIR) {
3811 3811 nfs_rw_exit(&rp->r_rwlock);
3812 3812 rdc->flags |= RDDIRWAIT;
3813 3813 #ifdef DEBUG
3814 3814 nfs3_readdir_cache_waits++;
3815 3815 #endif
3816 3816 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3817 3817 /*
3818 3818 * We got interrupted, probably
3819 3819 * the user typed ^C or an alarm
3820 3820 * fired. We free the new entry
3821 3821 * if we allocated one.
3822 3822 */
3823 3823 mutex_exit(&rp->r_statelock);
3824 3824 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3825 3825 RW_READER, FALSE);
3826 3826 rddir_cache_rele(rdc);
3827 3827 if (nrdc != NULL)
3828 3828 rddir_cache_rele(nrdc);
3829 3829 return (EINTR);
3830 3830 }
3831 3831 mutex_exit(&rp->r_statelock);
3832 3832 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3833 3833 RW_READER, FALSE);
3834 3834 rddir_cache_rele(rdc);
3835 3835 goto top;
3836 3836 }
3837 3837 /*
3838 3838 * Check to see if a readdir is required to
3839 3839 * fill the entry. If so, mark this entry
3840 3840 * as being filled, remove our reference,
3841 3841 * and branch to the code to fill the entry.
3842 3842 */
3843 3843 if (rdc->flags & RDDIRREQ) {
3844 3844 rdc->flags &= ~RDDIRREQ;
3845 3845 rdc->flags |= RDDIR;
3846 3846 if (nrdc != NULL)
3847 3847 rddir_cache_rele(nrdc);
3848 3848 nrdc = rdc;
3849 3849 mutex_exit(&rp->r_statelock);
3850 3850 goto bottom;
3851 3851 }
3852 3852 #ifdef DEBUG
3853 3853 if (!missed)
3854 3854 nfs3_readdir_cache_hits++;
3855 3855 #endif
3856 3856 /*
3857 3857 * If an error occurred while attempting
3858 3858 * to fill the cache entry, just return it.
3859 3859 */
3860 3860 if (rdc->error) {
3861 3861 error = rdc->error;
3862 3862 mutex_exit(&rp->r_statelock);
3863 3863 rddir_cache_rele(rdc);
3864 3864 if (nrdc != NULL)
3865 3865 rddir_cache_rele(nrdc);
3866 3866 return (error);
3867 3867 }
3868 3868
3869 3869 /*
3870 3870 * The cache entry is complete and good,
3871 3871 * copyout the dirent structs to the calling
3872 3872 * thread.
3873 3873 */
3874 3874 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3875 3875
3876 3876 /*
3877 3877 * If no error occurred during the copyout,
3878 3878 * update the offset in the uio struct to
3879 3879 * contain the value of the next cookie
3880 3880 * and set the eof value appropriately.
3881 3881 */
3882 3882 if (!error) {
3883 3883 uiop->uio_loffset = rdc->nfs3_ncookie;
3884 3884 if (eofp)
3885 3885 *eofp = rdc->eof;
3886 3886 }
3887 3887
3888 3888 /*
3889 3889 * Decide whether to do readahead.
3890 3890 *
3891 3891 * Don't if have already read to the end of
3892 3892 * directory. There is nothing more to read.
3893 3893 *
3894 3894 * Don't if the application is not doing
3895 3895 * lookups in the directory. The readahead
3896 3896 * is only effective if the application can
3897 3897 * be doing work while an async thread is
3898 3898 * handling the over the wire request.
3899 3899 */
3900 3900 if (rdc->eof) {
3901 3901 rp->r_direof = rdc;
3902 3902 doreadahead = FALSE;
3903 3903 } else if (!(rp->r_flags & RLOOKUP))
3904 3904 doreadahead = FALSE;
3905 3905 else
3906 3906 doreadahead = TRUE;
3907 3907
3908 3908 if (!doreadahead) {
3909 3909 mutex_exit(&rp->r_statelock);
3910 3910 rddir_cache_rele(rdc);
3911 3911 if (nrdc != NULL)
3912 3912 rddir_cache_rele(nrdc);
3913 3913 return (error);
3914 3914 }
3915 3915
3916 3916 /*
3917 3917 * Check to see whether we found an entry
3918 3918 * for the readahead. If so, we don't need
3919 3919 * to do anything further, so free the new
3920 3920 * entry if one was allocated. Otherwise,
3921 3921 * allocate a new entry, add it to the cache,
3922 3922 * and then initiate an asynchronous readdir
3923 3923 * operation to fill it.
3924 3924 */
3925 3925 srdc.nfs3_cookie = rdc->nfs3_ncookie;
3926 3926 srdc.buflen = count;
3927 3927 rrdc = avl_find(&rp->r_dir, &srdc, &where);
3928 3928 if (rrdc != NULL) {
3929 3929 if (nrdc != NULL)
3930 3930 rddir_cache_rele(nrdc);
3931 3931 } else {
3932 3932 if (nrdc != NULL)
3933 3933 rrdc = nrdc;
3934 3934 else {
3935 3935 rrdc = rddir_cache_alloc(KM_NOSLEEP);
3936 3936 }
3937 3937 if (rrdc != NULL) {
3938 3938 rrdc->nfs3_cookie = rdc->nfs3_ncookie;
3939 3939 rrdc->buflen = count;
3940 3940 avl_insert(&rp->r_dir, rrdc, where);
3941 3941 rddir_cache_hold(rrdc);
3942 3942 mutex_exit(&rp->r_statelock);
3943 3943 rddir_cache_rele(rdc);
3944 3944 #ifdef DEBUG
3945 3945 nfs3_readdir_readahead++;
3946 3946 #endif
3947 3947 nfs_async_readdir(vp, rrdc, cr, do_nfs3readdir);
3948 3948 return (error);
3949 3949 }
3950 3950 }
3951 3951
3952 3952 mutex_exit(&rp->r_statelock);
3953 3953 rddir_cache_rele(rdc);
3954 3954 return (error);
3955 3955 }
3956 3956
3957 3957 /*
3958 3958 * Didn't find an entry in the cache. Construct a new empty
3959 3959 * entry and link it into the cache. Other processes attempting
3960 3960 * to access this entry will need to wait until it is filled in.
3961 3961 *
3962 3962 * Since kmem_alloc may block, another pass through the cache
3963 3963 * will need to be taken to make sure that another process
3964 3964 * hasn't already added an entry to the cache for this request.
3965 3965 */
3966 3966 if (nrdc == NULL) {
3967 3967 mutex_exit(&rp->r_statelock);
3968 3968 nrdc = rddir_cache_alloc(KM_SLEEP);
3969 3969 nrdc->nfs3_cookie = uiop->uio_loffset;
3970 3970 nrdc->buflen = count;
3971 3971 goto top;
3972 3972 }
3973 3973
3974 3974 /*
3975 3975 * Add this entry to the cache.
3976 3976 */
3977 3977 avl_insert(&rp->r_dir, nrdc, where);
3978 3978 rddir_cache_hold(nrdc);
3979 3979 mutex_exit(&rp->r_statelock);
3980 3980
3981 3981 bottom:
3982 3982 #ifdef DEBUG
3983 3983 missed = 1;
3984 3984 nfs3_readdir_cache_misses++;
3985 3985 #endif
3986 3986 /*
3987 3987 * Do the readdir. This routine decides whether to use
3988 3988 * READDIR or READDIRPLUS.
3989 3989 */
3990 3990 error = do_nfs3readdir(vp, nrdc, cr);
3991 3991
3992 3992 /*
3993 3993 * If this operation failed, just return the error which occurred.
3994 3994 */
3995 3995 if (error != 0)
3996 3996 return (error);
3997 3997
3998 3998 /*
3999 3999 * Since the RPC operation will have taken sometime and blocked
4000 4000 * this process, another pass through the cache will need to be
4001 4001 * taken to find the correct cache entry. It is possible that
4002 4002 * the correct cache entry will not be there (although one was
4003 4003 * added) because the directory changed during the RPC operation
4004 4004 * and the readdir cache was flushed. In this case, just start
4005 4005 * over. It is hoped that this will not happen too often... :-)
4006 4006 */
4007 4007 nrdc = NULL;
4008 4008 goto top;
4009 4009 /* NOTREACHED */
4010 4010 }
4011 4011
4012 4012 static int
4013 4013 do_nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4014 4014 {
4015 4015 int error;
4016 4016 rnode_t *rp;
4017 4017 mntinfo_t *mi;
4018 4018
4019 4019 rp = VTOR(vp);
4020 4020 mi = VTOMI(vp);
4021 4021 ASSERT(nfs_zone() == mi->mi_zone);
4022 4022 /*
4023 4023 * Issue the proper request.
4024 4024 *
4025 4025 * If the server does not support READDIRPLUS, then use READDIR.
4026 4026 *
4027 4027 * Otherwise --
4028 4028 * Issue a READDIRPLUS if reading to fill an empty cache or if
4029 4029 * an application has performed a lookup in the directory which
4030 4030 * required an over the wire lookup. The use of READDIRPLUS
4031 4031 * will help to (re)populate the DNLC.
4032 4032 */
4033 4033 if (!(mi->mi_flags & MI_READDIRONLY) &&
4034 4034 (rp->r_flags & (RLOOKUP | RREADDIRPLUS))) {
4035 4035 if (rp->r_flags & RREADDIRPLUS) {
4036 4036 mutex_enter(&rp->r_statelock);
4037 4037 rp->r_flags &= ~RREADDIRPLUS;
4038 4038 mutex_exit(&rp->r_statelock);
4039 4039 }
4040 4040 nfs3readdirplus(vp, rdc, cr);
4041 4041 if (rdc->error == EOPNOTSUPP)
4042 4042 nfs3readdir(vp, rdc, cr);
4043 4043 } else
4044 4044 nfs3readdir(vp, rdc, cr);
4045 4045
4046 4046 mutex_enter(&rp->r_statelock);
4047 4047 rdc->flags &= ~RDDIR;
4048 4048 if (rdc->flags & RDDIRWAIT) {
4049 4049 rdc->flags &= ~RDDIRWAIT;
4050 4050 cv_broadcast(&rdc->cv);
4051 4051 }
4052 4052 error = rdc->error;
4053 4053 if (error)
4054 4054 rdc->flags |= RDDIRREQ;
4055 4055 mutex_exit(&rp->r_statelock);
4056 4056
4057 4057 rddir_cache_rele(rdc);
4058 4058
4059 4059 return (error);
4060 4060 }
4061 4061
4062 4062 static void
4063 4063 nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4064 4064 {
4065 4065 int error;
4066 4066 READDIR3args args;
4067 4067 READDIR3vres res;
4068 4068 vattr_t dva;
4069 4069 rnode_t *rp;
4070 4070 int douprintf;
4071 4071 failinfo_t fi, *fip = NULL;
4072 4072 mntinfo_t *mi;
4073 4073 hrtime_t t;
4074 4074
4075 4075 rp = VTOR(vp);
4076 4076 mi = VTOMI(vp);
4077 4077 ASSERT(nfs_zone() == mi->mi_zone);
4078 4078
4079 4079 args.dir = *RTOFH3(rp);
4080 4080 args.cookie = (cookie3)rdc->nfs3_cookie;
4081 4081 args.cookieverf = rp->r_cookieverf;
4082 4082 args.count = rdc->buflen;
4083 4083
4084 4084 /*
4085 4085 * NFS client failover support
4086 4086 * suppress failover unless we have a zero cookie
4087 4087 */
4088 4088 if (args.cookie == (cookie3) 0) {
4089 4089 fi.vp = vp;
4090 4090 fi.fhp = (caddr_t)&args.dir;
4091 4091 fi.copyproc = nfs3copyfh;
4092 4092 fi.lookupproc = nfs3lookup;
4093 4093 fi.xattrdirproc = acl_getxattrdir3;
4094 4094 fip = &fi;
4095 4095 }
4096 4096
4097 4097 #ifdef DEBUG
4098 4098 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4099 4099 #else
4100 4100 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4101 4101 #endif
4102 4102
4103 4103 res.entries = (dirent64_t *)rdc->entries;
4104 4104 res.entries_size = rdc->buflen;
4105 4105 res.dir_attributes.fres.vap = &dva;
4106 4106 res.dir_attributes.fres.vp = vp;
4107 4107 res.loff = rdc->nfs3_cookie;
4108 4108
4109 4109 douprintf = 1;
4110 4110
4111 4111 if (mi->mi_io_kstats) {
4112 4112 mutex_enter(&mi->mi_lock);
4113 4113 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4114 4114 mutex_exit(&mi->mi_lock);
4115 4115 }
4116 4116
4117 4117 t = gethrtime();
4118 4118
4119 4119 error = rfs3call(VTOMI(vp), NFSPROC3_READDIR,
4120 4120 xdr_READDIR3args, (caddr_t)&args,
4121 4121 xdr_READDIR3vres, (caddr_t)&res, cr,
4122 4122 &douprintf, &res.status, 0, fip);
4123 4123
4124 4124 if (mi->mi_io_kstats) {
4125 4125 mutex_enter(&mi->mi_lock);
4126 4126 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4127 4127 mutex_exit(&mi->mi_lock);
4128 4128 }
4129 4129
4130 4130 if (error)
4131 4131 goto err;
4132 4132
4133 4133 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, t, cr);
4134 4134
4135 4135 error = geterrno3(res.status);
4136 4136 if (error) {
4137 4137 PURGE_STALE_FH(error, vp, cr);
4138 4138 goto err;
4139 4139 }
4140 4140
4141 4141 if (mi->mi_io_kstats) {
4142 4142 mutex_enter(&mi->mi_lock);
4143 4143 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4144 4144 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4145 4145 mutex_exit(&mi->mi_lock);
4146 4146 }
4147 4147
4148 4148 rdc->nfs3_ncookie = res.loff;
4149 4149 rp->r_cookieverf = res.cookieverf;
4150 4150 rdc->eof = res.eof ? 1 : 0;
4151 4151 rdc->entlen = res.size;
4152 4152 ASSERT(rdc->entlen <= rdc->buflen);
4153 4153 rdc->error = 0;
4154 4154 return;
4155 4155
4156 4156 err:
4157 4157 kmem_free(rdc->entries, rdc->buflen);
4158 4158 rdc->entries = NULL;
4159 4159 rdc->error = error;
4160 4160 }
4161 4161
4162 4162 /*
4163 4163 * Read directory entries.
4164 4164 * There are some weird things to look out for here. The uio_loffset
4165 4165 * field is either 0 or it is the offset returned from a previous
4166 4166 * readdir. It is an opaque value used by the server to find the
4167 4167 * correct directory block to read. The count field is the number
4168 4168 * of blocks to read on the server. This is advisory only, the server
4169 4169 * may return only one block's worth of entries. Entries may be compressed
4170 4170 * on the server.
4171 4171 */
4172 4172 static void
4173 4173 nfs3readdirplus(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4174 4174 {
4175 4175 int error;
4176 4176 READDIRPLUS3args args;
4177 4177 READDIRPLUS3vres res;
4178 4178 vattr_t dva;
4179 4179 rnode_t *rp;
4180 4180 mntinfo_t *mi;
4181 4181 int douprintf;
4182 4182 failinfo_t fi, *fip = NULL;
4183 4183
4184 4184 rp = VTOR(vp);
4185 4185 mi = VTOMI(vp);
4186 4186 ASSERT(nfs_zone() == mi->mi_zone);
4187 4187
4188 4188 args.dir = *RTOFH3(rp);
4189 4189 args.cookie = (cookie3)rdc->nfs3_cookie;
4190 4190 args.cookieverf = rp->r_cookieverf;
4191 4191 args.dircount = rdc->buflen;
4192 4192 args.maxcount = mi->mi_tsize;
4193 4193
4194 4194 /*
4195 4195 * NFS client failover support
4196 4196 * suppress failover unless we have a zero cookie
4197 4197 */
4198 4198 if (args.cookie == (cookie3)0) {
4199 4199 fi.vp = vp;
4200 4200 fi.fhp = (caddr_t)&args.dir;
4201 4201 fi.copyproc = nfs3copyfh;
4202 4202 fi.lookupproc = nfs3lookup;
4203 4203 fi.xattrdirproc = acl_getxattrdir3;
4204 4204 fip = &fi;
4205 4205 }
4206 4206
4207 4207 #ifdef DEBUG
4208 4208 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4209 4209 #else
4210 4210 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4211 4211 #endif
4212 4212
4213 4213 res.entries = (dirent64_t *)rdc->entries;
4214 4214 res.entries_size = rdc->buflen;
4215 4215 res.dir_attributes.fres.vap = &dva;
4216 4216 res.dir_attributes.fres.vp = vp;
4217 4217 res.loff = rdc->nfs3_cookie;
4218 4218 res.credentials = cr;
4219 4219
4220 4220 douprintf = 1;
4221 4221
4222 4222 if (mi->mi_io_kstats) {
4223 4223 mutex_enter(&mi->mi_lock);
4224 4224 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4225 4225 mutex_exit(&mi->mi_lock);
4226 4226 }
4227 4227
4228 4228 res.time = gethrtime();
4229 4229
4230 4230 error = rfs3call(mi, NFSPROC3_READDIRPLUS,
4231 4231 xdr_READDIRPLUS3args, (caddr_t)&args,
4232 4232 xdr_READDIRPLUS3vres, (caddr_t)&res, cr,
4233 4233 &douprintf, &res.status, 0, fip);
4234 4234
4235 4235 if (mi->mi_io_kstats) {
4236 4236 mutex_enter(&mi->mi_lock);
4237 4237 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4238 4238 mutex_exit(&mi->mi_lock);
4239 4239 }
4240 4240
4241 4241 if (error) {
4242 4242 goto err;
4243 4243 }
4244 4244
4245 4245 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, res.time, cr);
4246 4246
4247 4247 error = geterrno3(res.status);
4248 4248 if (error) {
4249 4249 PURGE_STALE_FH(error, vp, cr);
4250 4250 if (error == EOPNOTSUPP) {
4251 4251 mutex_enter(&mi->mi_lock);
4252 4252 mi->mi_flags |= MI_READDIRONLY;
4253 4253 mutex_exit(&mi->mi_lock);
4254 4254 }
4255 4255 goto err;
4256 4256 }
4257 4257
4258 4258 if (mi->mi_io_kstats) {
4259 4259 mutex_enter(&mi->mi_lock);
4260 4260 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4261 4261 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4262 4262 mutex_exit(&mi->mi_lock);
4263 4263 }
4264 4264
4265 4265 rdc->nfs3_ncookie = res.loff;
4266 4266 rp->r_cookieverf = res.cookieverf;
4267 4267 rdc->eof = res.eof ? 1 : 0;
4268 4268 rdc->entlen = res.size;
4269 4269 ASSERT(rdc->entlen <= rdc->buflen);
4270 4270 rdc->error = 0;
4271 4271
4272 4272 return;
4273 4273
4274 4274 err:
4275 4275 kmem_free(rdc->entries, rdc->buflen);
4276 4276 rdc->entries = NULL;
4277 4277 rdc->error = error;
4278 4278 }
4279 4279
4280 4280 #ifdef DEBUG
4281 4281 static int nfs3_bio_do_stop = 0;
4282 4282 #endif
4283 4283
4284 4284 static int
4285 4285 nfs3_bio(struct buf *bp, stable_how *stab_comm, cred_t *cr)
4286 4286 {
4287 4287 rnode_t *rp = VTOR(bp->b_vp);
4288 4288 int count;
4289 4289 int error;
4290 4290 cred_t *cred;
4291 4291 offset_t offset;
4292 4292
4293 4293 ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
4294 4294 offset = ldbtob(bp->b_lblkno);
4295 4295
4296 4296 DTRACE_IO1(start, struct buf *, bp);
4297 4297
4298 4298 if (bp->b_flags & B_READ) {
4299 4299 mutex_enter(&rp->r_statelock);
4300 4300 if (rp->r_cred != NULL) {
4301 4301 cred = rp->r_cred;
4302 4302 crhold(cred);
4303 4303 } else {
4304 4304 rp->r_cred = cr;
4305 4305 crhold(cr);
4306 4306 cred = cr;
4307 4307 crhold(cred);
4308 4308 }
4309 4309 mutex_exit(&rp->r_statelock);
4310 4310 read_again:
4311 4311 error = bp->b_error = nfs3read(bp->b_vp, bp->b_un.b_addr,
4312 4312 offset, bp->b_bcount, &bp->b_resid, cred);
4313 4313 crfree(cred);
4314 4314 if (!error) {
4315 4315 if (bp->b_resid) {
4316 4316 /*
4317 4317 * Didn't get it all because we hit EOF,
4318 4318 * zero all the memory beyond the EOF.
4319 4319 */
4320 4320 /* bzero(rdaddr + */
4321 4321 bzero(bp->b_un.b_addr +
4322 4322 bp->b_bcount - bp->b_resid, bp->b_resid);
4323 4323 }
4324 4324 mutex_enter(&rp->r_statelock);
4325 4325 if (bp->b_resid == bp->b_bcount &&
4326 4326 offset >= rp->r_size) {
4327 4327 /*
4328 4328 * We didn't read anything at all as we are
4329 4329 * past EOF. Return an error indicator back
4330 4330 * but don't destroy the pages (yet).
4331 4331 */
4332 4332 error = NFS_EOF;
4333 4333 }
4334 4334 mutex_exit(&rp->r_statelock);
4335 4335 } else if (error == EACCES) {
4336 4336 mutex_enter(&rp->r_statelock);
4337 4337 if (cred != cr) {
4338 4338 if (rp->r_cred != NULL)
4339 4339 crfree(rp->r_cred);
4340 4340 rp->r_cred = cr;
4341 4341 crhold(cr);
4342 4342 cred = cr;
4343 4343 crhold(cred);
4344 4344 mutex_exit(&rp->r_statelock);
4345 4345 goto read_again;
4346 4346 }
4347 4347 mutex_exit(&rp->r_statelock);
4348 4348 }
4349 4349 } else {
4350 4350 if (!(rp->r_flags & RSTALE)) {
4351 4351 mutex_enter(&rp->r_statelock);
4352 4352 if (rp->r_cred != NULL) {
4353 4353 cred = rp->r_cred;
4354 4354 crhold(cred);
4355 4355 } else {
4356 4356 rp->r_cred = cr;
4357 4357 crhold(cr);
4358 4358 cred = cr;
4359 4359 crhold(cred);
4360 4360 }
4361 4361 mutex_exit(&rp->r_statelock);
4362 4362 write_again:
4363 4363 mutex_enter(&rp->r_statelock);
4364 4364 count = MIN(bp->b_bcount, rp->r_size - offset);
4365 4365 mutex_exit(&rp->r_statelock);
4366 4366 if (count < 0)
4367 4367 cmn_err(CE_PANIC, "nfs3_bio: write count < 0");
4368 4368 #ifdef DEBUG
4369 4369 if (count == 0) {
4370 4370 zcmn_err(getzoneid(), CE_WARN,
4371 4371 "nfs3_bio: zero length write at %lld",
4372 4372 offset);
4373 4373 nfs_printfhandle(&rp->r_fh);
4374 4374 if (nfs3_bio_do_stop)
4375 4375 debug_enter("nfs3_bio");
4376 4376 }
4377 4377 #endif
4378 4378 error = nfs3write(bp->b_vp, bp->b_un.b_addr, offset,
4379 4379 count, cred, stab_comm);
4380 4380 if (error == EACCES) {
4381 4381 mutex_enter(&rp->r_statelock);
4382 4382 if (cred != cr) {
4383 4383 if (rp->r_cred != NULL)
4384 4384 crfree(rp->r_cred);
4385 4385 rp->r_cred = cr;
4386 4386 crhold(cr);
4387 4387 crfree(cred);
4388 4388 cred = cr;
4389 4389 crhold(cred);
4390 4390 mutex_exit(&rp->r_statelock);
4391 4391 goto write_again;
4392 4392 }
4393 4393 mutex_exit(&rp->r_statelock);
4394 4394 }
4395 4395 bp->b_error = error;
4396 4396 if (error && error != EINTR) {
4397 4397 /*
4398 4398 * Don't print EDQUOT errors on the console.
4399 4399 * Don't print asynchronous EACCES errors.
4400 4400 * Don't print EFBIG errors.
4401 4401 * Print all other write errors.
4402 4402 */
4403 4403 if (error != EDQUOT && error != EFBIG &&
4404 4404 (error != EACCES ||
4405 4405 !(bp->b_flags & B_ASYNC)))
4406 4406 nfs_write_error(bp->b_vp, error, cred);
4407 4407 /*
4408 4408 * Update r_error and r_flags as appropriate.
4409 4409 * If the error was ESTALE, then mark the
4410 4410 * rnode as not being writeable and save
4411 4411 * the error status. Otherwise, save any
4412 4412 * errors which occur from asynchronous
4413 4413 * page invalidations. Any errors occurring
4414 4414 * from other operations should be saved
4415 4415 * by the caller.
4416 4416 */
4417 4417 mutex_enter(&rp->r_statelock);
4418 4418 if (error == ESTALE) {
4419 4419 rp->r_flags |= RSTALE;
4420 4420 if (!rp->r_error)
4421 4421 rp->r_error = error;
4422 4422 } else if (!rp->r_error &&
4423 4423 (bp->b_flags &
4424 4424 (B_INVAL|B_FORCE|B_ASYNC)) ==
4425 4425 (B_INVAL|B_FORCE|B_ASYNC)) {
4426 4426 rp->r_error = error;
4427 4427 }
4428 4428 mutex_exit(&rp->r_statelock);
4429 4429 }
4430 4430 crfree(cred);
4431 4431 } else {
4432 4432 error = rp->r_error;
4433 4433 /*
4434 4434 * A close may have cleared r_error, if so,
4435 4435 * propagate ESTALE error return properly
4436 4436 */
4437 4437 if (error == 0)
4438 4438 error = ESTALE;
4439 4439 }
4440 4440 }
4441 4441
4442 4442 if (error != 0 && error != NFS_EOF)
4443 4443 bp->b_flags |= B_ERROR;
4444 4444
4445 4445 DTRACE_IO1(done, struct buf *, bp);
4446 4446
4447 4447 return (error);
4448 4448 }
4449 4449
4450 4450 /* ARGSUSED */
4451 4451 static int
4452 4452 nfs3_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4453 4453 {
4454 4454 rnode_t *rp;
4455 4455
4456 4456 if (nfs_zone() != VTOMI(vp)->mi_zone)
4457 4457 return (EIO);
4458 4458 rp = VTOR(vp);
4459 4459
4460 4460 if (fidp->fid_len < (ushort_t)rp->r_fh.fh_len) {
4461 4461 fidp->fid_len = rp->r_fh.fh_len;
4462 4462 return (ENOSPC);
4463 4463 }
4464 4464 fidp->fid_len = rp->r_fh.fh_len;
4465 4465 bcopy(rp->r_fh.fh_buf, fidp->fid_data, fidp->fid_len);
4466 4466 return (0);
4467 4467 }
4468 4468
4469 4469 /* ARGSUSED2 */
4470 4470 static int
4471 4471 nfs3_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4472 4472 {
4473 4473 rnode_t *rp = VTOR(vp);
4474 4474
4475 4475 if (!write_lock) {
4476 4476 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4477 4477 return (V_WRITELOCK_FALSE);
4478 4478 }
4479 4479
4480 4480 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
4481 4481 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4482 4482 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
4483 4483 return (V_WRITELOCK_FALSE);
4484 4484 nfs_rw_exit(&rp->r_rwlock);
4485 4485 }
4486 4486
4487 4487 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
4488 4488 return (V_WRITELOCK_TRUE);
4489 4489 }
4490 4490
4491 4491 /* ARGSUSED */
4492 4492 static void
4493 4493 nfs3_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4494 4494 {
4495 4495 rnode_t *rp = VTOR(vp);
4496 4496
4497 4497 nfs_rw_exit(&rp->r_rwlock);
4498 4498 }
4499 4499
4500 4500 /* ARGSUSED */
4501 4501 static int
4502 4502 nfs3_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
4503 4503 {
4504 4504
4505 4505 /*
4506 4506 * Because we stuff the readdir cookie into the offset field
4507 4507 * someone may attempt to do an lseek with the cookie which
4508 4508 * we want to succeed.
4509 4509 */
4510 4510 if (vp->v_type == VDIR)
4511 4511 return (0);
4512 4512 if (*noffp < 0)
4513 4513 return (EINVAL);
4514 4514 return (0);
4515 4515 }
4516 4516
4517 4517 /*
4518 4518 * number of nfs3_bsize blocks to read ahead.
4519 4519 */
4520 4520 static int nfs3_nra = 4;
4521 4521
4522 4522 #ifdef DEBUG
4523 4523 static int nfs3_lostpage = 0; /* number of times we lost original page */
4524 4524 #endif
4525 4525
4526 4526 /*
4527 4527 * Return all the pages from [off..off+len) in file
4528 4528 */
4529 4529 /* ARGSUSED */
4530 4530 static int
4531 4531 nfs3_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4532 4532 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4533 4533 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4534 4534 {
4535 4535 rnode_t *rp;
4536 4536 int error;
4537 4537 mntinfo_t *mi;
4538 4538
4539 4539 if (vp->v_flag & VNOMAP)
4540 4540 return (ENOSYS);
4541 4541
4542 4542 if (nfs_zone() != VTOMI(vp)->mi_zone)
4543 4543 return (EIO);
4544 4544 if (protp != NULL)
4545 4545 *protp = PROT_ALL;
4546 4546
4547 4547 /*
4548 4548 * Now valididate that the caches are up to date.
4549 4549 */
4550 4550 error = nfs3_validate_caches(vp, cr);
4551 4551 if (error)
4552 4552 return (error);
4553 4553
4554 4554 rp = VTOR(vp);
4555 4555 mi = VTOMI(vp);
4556 4556 retry:
4557 4557 mutex_enter(&rp->r_statelock);
4558 4558
4559 4559 /*
4560 4560 * Don't create dirty pages faster than they
4561 4561 * can be cleaned so that the system doesn't
4562 4562 * get imbalanced. If the async queue is
4563 4563 * maxed out, then wait for it to drain before
4564 4564 * creating more dirty pages. Also, wait for
4565 4565 * any threads doing pagewalks in the vop_getattr
4566 4566 * entry points so that they don't block for
4567 4567 * long periods.
4568 4568 */
4569 4569 if (rw == S_CREATE) {
4570 4570 while ((mi->mi_max_threads != 0 &&
4571 4571 rp->r_awcount > 2 * mi->mi_max_threads) ||
4572 4572 rp->r_gcount > 0)
4573 4573 cv_wait(&rp->r_cv, &rp->r_statelock);
4574 4574 }
4575 4575
4576 4576 /*
4577 4577 * If we are getting called as a side effect of an nfs_write()
4578 4578 * operation the local file size might not be extended yet.
4579 4579 * In this case we want to be able to return pages of zeroes.
4580 4580 */
4581 4581 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
4582 4582 mutex_exit(&rp->r_statelock);
4583 4583 return (EFAULT); /* beyond EOF */
4584 4584 }
4585 4585
4586 4586 mutex_exit(&rp->r_statelock);
4587 4587
4588 4588 if (len <= PAGESIZE) {
4589 4589 error = nfs3_getapage(vp, off, len, protp, pl, plsz,
4590 4590 seg, addr, rw, cr);
4591 4591 } else {
4592 4592 error = pvn_getpages(nfs3_getapage, vp, off, len, protp,
4593 4593 pl, plsz, seg, addr, rw, cr);
4594 4594 }
4595 4595
4596 4596 switch (error) {
4597 4597 case NFS_EOF:
4598 4598 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
4599 4599 goto retry;
4600 4600 case ESTALE:
4601 4601 PURGE_STALE_FH(error, vp, cr);
4602 4602 }
4603 4603
4604 4604 return (error);
4605 4605 }
4606 4606
4607 4607 /*
4608 4608 * Called from pvn_getpages or nfs3_getpage to get a particular page.
4609 4609 */
4610 4610 /* ARGSUSED */
4611 4611 static int
4612 4612 nfs3_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
4613 4613 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4614 4614 enum seg_rw rw, cred_t *cr)
4615 4615 {
4616 4616 rnode_t *rp;
4617 4617 uint_t bsize;
4618 4618 struct buf *bp;
4619 4619 page_t *pp;
4620 4620 u_offset_t lbn;
4621 4621 u_offset_t io_off;
4622 4622 u_offset_t blkoff;
4623 4623 u_offset_t rablkoff;
4624 4624 size_t io_len;
4625 4625 uint_t blksize;
4626 4626 int error;
4627 4627 int readahead;
4628 4628 int readahead_issued = 0;
4629 4629 int ra_window; /* readahead window */
4630 4630 page_t *pagefound;
4631 4631 page_t *savepp;
4632 4632
4633 4633 if (nfs_zone() != VTOMI(vp)->mi_zone)
4634 4634 return (EIO);
4635 4635 rp = VTOR(vp);
4636 4636 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4637 4637
4638 4638 reread:
4639 4639 bp = NULL;
4640 4640 pp = NULL;
4641 4641 pagefound = NULL;
4642 4642
4643 4643 if (pl != NULL)
4644 4644 pl[0] = NULL;
4645 4645
4646 4646 error = 0;
4647 4647 lbn = off / bsize;
4648 4648 blkoff = lbn * bsize;
4649 4649
4650 4650 /*
4651 4651 * Queueing up the readahead before doing the synchronous read
4652 4652 * results in a significant increase in read throughput because
4653 4653 * of the increased parallelism between the async threads and
4654 4654 * the process context.
4655 4655 */
4656 4656 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
4657 4657 rw != S_CREATE &&
4658 4658 !(vp->v_flag & VNOCACHE)) {
4659 4659 mutex_enter(&rp->r_statelock);
4660 4660
4661 4661 /*
4662 4662 * Calculate the number of readaheads to do.
4663 4663 * a) No readaheads at offset = 0.
4664 4664 * b) Do maximum(nfs3_nra) readaheads when the readahead
4665 4665 * window is closed.
4666 4666 * c) Do readaheads between 1 to (nfs3_nra - 1) depending
4667 4667 * upon how far the readahead window is open or close.
4668 4668 * d) No readaheads if rp->r_nextr is not within the scope
4669 4669 * of the readahead window (random i/o).
4670 4670 */
4671 4671
4672 4672 if (off == 0)
4673 4673 readahead = 0;
4674 4674 else if (blkoff == rp->r_nextr)
4675 4675 readahead = nfs3_nra;
4676 4676 else if (rp->r_nextr > blkoff &&
4677 4677 ((ra_window = (rp->r_nextr - blkoff) / bsize)
4678 4678 <= (nfs3_nra - 1)))
4679 4679 readahead = nfs3_nra - ra_window;
4680 4680 else
4681 4681 readahead = 0;
4682 4682
4683 4683 rablkoff = rp->r_nextr;
4684 4684 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
4685 4685 mutex_exit(&rp->r_statelock);
4686 4686 if (nfs_async_readahead(vp, rablkoff + bsize,
4687 4687 addr + (rablkoff + bsize - off), seg, cr,
4688 4688 nfs3_readahead) < 0) {
4689 4689 mutex_enter(&rp->r_statelock);
4690 4690 break;
4691 4691 }
4692 4692 readahead--;
4693 4693 rablkoff += bsize;
4694 4694 /*
4695 4695 * Indicate that we did a readahead so
4696 4696 * readahead offset is not updated
4697 4697 * by the synchronous read below.
4698 4698 */
4699 4699 readahead_issued = 1;
4700 4700 mutex_enter(&rp->r_statelock);
4701 4701 /*
4702 4702 * set readahead offset to
4703 4703 * offset of last async readahead
4704 4704 * request.
4705 4705 */
4706 4706 rp->r_nextr = rablkoff;
4707 4707 }
4708 4708 mutex_exit(&rp->r_statelock);
4709 4709 }
4710 4710
4711 4711 again:
4712 4712 if ((pagefound = page_exists(vp, off)) == NULL) {
4713 4713 if (pl == NULL) {
4714 4714 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
4715 4715 nfs3_readahead);
4716 4716 } else if (rw == S_CREATE) {
4717 4717 /*
4718 4718 * Block for this page is not allocated, or the offset
4719 4719 * is beyond the current allocation size, or we're
4720 4720 * allocating a swap slot and the page was not found,
4721 4721 * so allocate it and return a zero page.
4722 4722 */
4723 4723 if ((pp = page_create_va(vp, off,
4724 4724 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
4725 4725 cmn_err(CE_PANIC, "nfs3_getapage: page_create");
4726 4726 io_len = PAGESIZE;
4727 4727 mutex_enter(&rp->r_statelock);
4728 4728 rp->r_nextr = off + PAGESIZE;
4729 4729 mutex_exit(&rp->r_statelock);
4730 4730 } else {
4731 4731 /*
4732 4732 * Need to go to server to get a BLOCK, exception to
4733 4733 * that being while reading at offset = 0 or doing
4734 4734 * random i/o, in that case read only a PAGE.
4735 4735 */
4736 4736 mutex_enter(&rp->r_statelock);
4737 4737 if (blkoff < rp->r_size &&
4738 4738 blkoff + bsize >= rp->r_size) {
4739 4739 /*
4740 4740 * If only a block or less is left in
4741 4741 * the file, read all that is remaining.
4742 4742 */
4743 4743 if (rp->r_size <= off) {
4744 4744 /*
4745 4745 * Trying to access beyond EOF,
4746 4746 * set up to get at least one page.
4747 4747 */
4748 4748 blksize = off + PAGESIZE - blkoff;
4749 4749 } else
4750 4750 blksize = rp->r_size - blkoff;
4751 4751 } else if ((off == 0) ||
4752 4752 (off != rp->r_nextr && !readahead_issued)) {
4753 4753 blksize = PAGESIZE;
4754 4754 blkoff = off; /* block = page here */
4755 4755 } else
4756 4756 blksize = bsize;
4757 4757 mutex_exit(&rp->r_statelock);
4758 4758
4759 4759 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4760 4760 &io_len, blkoff, blksize, 0);
4761 4761
4762 4762 /*
4763 4763 * Some other thread has entered the page,
4764 4764 * so just use it.
4765 4765 */
4766 4766 if (pp == NULL)
4767 4767 goto again;
4768 4768
4769 4769 /*
4770 4770 * Now round the request size up to page boundaries.
4771 4771 * This ensures that the entire page will be
4772 4772 * initialized to zeroes if EOF is encountered.
4773 4773 */
4774 4774 io_len = ptob(btopr(io_len));
4775 4775
4776 4776 bp = pageio_setup(pp, io_len, vp, B_READ);
4777 4777 ASSERT(bp != NULL);
4778 4778
4779 4779 /*
4780 4780 * pageio_setup should have set b_addr to 0. This
4781 4781 * is correct since we want to do I/O on a page
4782 4782 * boundary. bp_mapin will use this addr to calculate
4783 4783 * an offset, and then set b_addr to the kernel virtual
4784 4784 * address it allocated for us.
4785 4785 */
4786 4786 ASSERT(bp->b_un.b_addr == 0);
4787 4787
4788 4788 bp->b_edev = 0;
4789 4789 bp->b_dev = 0;
4790 4790 bp->b_lblkno = lbtodb(io_off);
4791 4791 bp->b_file = vp;
4792 4792 bp->b_offset = (offset_t)off;
4793 4793 bp_mapin(bp);
4794 4794
4795 4795 /*
4796 4796 * If doing a write beyond what we believe is EOF,
4797 4797 * don't bother trying to read the pages from the
4798 4798 * server, we'll just zero the pages here. We
4799 4799 * don't check that the rw flag is S_WRITE here
4800 4800 * because some implementations may attempt a
4801 4801 * read access to the buffer before copying data.
4802 4802 */
4803 4803 mutex_enter(&rp->r_statelock);
4804 4804 if (io_off >= rp->r_size && seg == segkmap) {
4805 4805 mutex_exit(&rp->r_statelock);
4806 4806 bzero(bp->b_un.b_addr, io_len);
4807 4807 } else {
4808 4808 mutex_exit(&rp->r_statelock);
4809 4809 error = nfs3_bio(bp, NULL, cr);
4810 4810 }
4811 4811
4812 4812 /*
4813 4813 * Unmap the buffer before freeing it.
4814 4814 */
4815 4815 bp_mapout(bp);
4816 4816 pageio_done(bp);
4817 4817
4818 4818 savepp = pp;
4819 4819 do {
4820 4820 pp->p_fsdata = C_NOCOMMIT;
4821 4821 } while ((pp = pp->p_next) != savepp);
4822 4822
4823 4823 if (error == NFS_EOF) {
4824 4824 /*
4825 4825 * If doing a write system call just return
4826 4826 * zeroed pages, else user tried to get pages
4827 4827 * beyond EOF, return error. We don't check
4828 4828 * that the rw flag is S_WRITE here because
4829 4829 * some implementations may attempt a read
4830 4830 * access to the buffer before copying data.
4831 4831 */
4832 4832 if (seg == segkmap)
4833 4833 error = 0;
4834 4834 else
4835 4835 error = EFAULT;
4836 4836 }
4837 4837
4838 4838 if (!readahead_issued && !error) {
4839 4839 mutex_enter(&rp->r_statelock);
4840 4840 rp->r_nextr = io_off + io_len;
4841 4841 mutex_exit(&rp->r_statelock);
4842 4842 }
4843 4843 }
4844 4844 }
4845 4845
4846 4846 out:
4847 4847 if (pl == NULL)
4848 4848 return (error);
4849 4849
4850 4850 if (error) {
4851 4851 if (pp != NULL)
4852 4852 pvn_read_done(pp, B_ERROR);
4853 4853 return (error);
4854 4854 }
4855 4855
4856 4856 if (pagefound) {
4857 4857 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
4858 4858
4859 4859 /*
4860 4860 * Page exists in the cache, acquire the appropriate lock.
4861 4861 * If this fails, start all over again.
4862 4862 */
4863 4863 if ((pp = page_lookup(vp, off, se)) == NULL) {
4864 4864 #ifdef DEBUG
4865 4865 nfs3_lostpage++;
4866 4866 #endif
4867 4867 goto reread;
4868 4868 }
4869 4869 pl[0] = pp;
4870 4870 pl[1] = NULL;
4871 4871 return (0);
4872 4872 }
4873 4873
4874 4874 if (pp != NULL)
4875 4875 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4876 4876
4877 4877 return (error);
4878 4878 }
4879 4879
4880 4880 static void
4881 4881 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
4882 4882 cred_t *cr)
4883 4883 {
4884 4884 int error;
4885 4885 page_t *pp;
4886 4886 u_offset_t io_off;
4887 4887 size_t io_len;
4888 4888 struct buf *bp;
4889 4889 uint_t bsize, blksize;
4890 4890 rnode_t *rp = VTOR(vp);
4891 4891 page_t *savepp;
4892 4892
4893 4893 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4894 4894 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4895 4895
4896 4896 mutex_enter(&rp->r_statelock);
4897 4897 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
4898 4898 /*
4899 4899 * If less than a block left in file read less
4900 4900 * than a block.
4901 4901 */
4902 4902 blksize = rp->r_size - blkoff;
4903 4903 } else
4904 4904 blksize = bsize;
4905 4905 mutex_exit(&rp->r_statelock);
4906 4906
4907 4907 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
4908 4908 &io_off, &io_len, blkoff, blksize, 1);
4909 4909 /*
4910 4910 * The isra flag passed to the kluster function is 1, we may have
4911 4911 * gotten a return value of NULL for a variety of reasons (# of free
4912 4912 * pages < minfree, someone entered the page on the vnode etc). In all
4913 4913 * cases, we want to punt on the readahead.
4914 4914 */
4915 4915 if (pp == NULL)
4916 4916 return;
4917 4917
4918 4918 /*
4919 4919 * Now round the request size up to page boundaries.
4920 4920 * This ensures that the entire page will be
4921 4921 * initialized to zeroes if EOF is encountered.
4922 4922 */
4923 4923 io_len = ptob(btopr(io_len));
4924 4924
4925 4925 bp = pageio_setup(pp, io_len, vp, B_READ);
4926 4926 ASSERT(bp != NULL);
4927 4927
4928 4928 /*
4929 4929 * pageio_setup should have set b_addr to 0. This is correct since
4930 4930 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4931 4931 * to calculate an offset, and then set b_addr to the kernel virtual
4932 4932 * address it allocated for us.
4933 4933 */
4934 4934 ASSERT(bp->b_un.b_addr == 0);
4935 4935
4936 4936 bp->b_edev = 0;
4937 4937 bp->b_dev = 0;
4938 4938 bp->b_lblkno = lbtodb(io_off);
4939 4939 bp->b_file = vp;
4940 4940 bp->b_offset = (offset_t)blkoff;
4941 4941 bp_mapin(bp);
4942 4942
4943 4943 /*
4944 4944 * If doing a write beyond what we believe is EOF, don't bother trying
4945 4945 * to read the pages from the server, we'll just zero the pages here.
4946 4946 * We don't check that the rw flag is S_WRITE here because some
4947 4947 * implementations may attempt a read access to the buffer before
4948 4948 * copying data.
4949 4949 */
4950 4950 mutex_enter(&rp->r_statelock);
4951 4951 if (io_off >= rp->r_size && seg == segkmap) {
4952 4952 mutex_exit(&rp->r_statelock);
4953 4953 bzero(bp->b_un.b_addr, io_len);
4954 4954 error = 0;
4955 4955 } else {
4956 4956 mutex_exit(&rp->r_statelock);
4957 4957 error = nfs3_bio(bp, NULL, cr);
4958 4958 if (error == NFS_EOF)
4959 4959 error = 0;
4960 4960 }
4961 4961
4962 4962 /*
4963 4963 * Unmap the buffer before freeing it.
4964 4964 */
4965 4965 bp_mapout(bp);
4966 4966 pageio_done(bp);
4967 4967
4968 4968 savepp = pp;
4969 4969 do {
4970 4970 pp->p_fsdata = C_NOCOMMIT;
4971 4971 } while ((pp = pp->p_next) != savepp);
4972 4972
4973 4973 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4974 4974
4975 4975 /*
4976 4976 * In case of error set readahead offset
4977 4977 * to the lowest offset.
4978 4978 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4979 4979 */
4980 4980 if (error && rp->r_nextr > io_off) {
4981 4981 mutex_enter(&rp->r_statelock);
4982 4982 if (rp->r_nextr > io_off)
4983 4983 rp->r_nextr = io_off;
4984 4984 mutex_exit(&rp->r_statelock);
4985 4985 }
4986 4986 }
4987 4987
4988 4988 /*
4989 4989 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4990 4990 * If len == 0, do from off to EOF.
4991 4991 *
4992 4992 * The normal cases should be len == 0 && off == 0 (entire vp list),
4993 4993 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4994 4994 * (from pageout).
4995 4995 */
4996 4996 /* ARGSUSED */
4997 4997 static int
4998 4998 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4999 4999 caller_context_t *ct)
5000 5000 {
5001 5001 int error;
5002 5002 rnode_t *rp;
5003 5003
5004 5004 ASSERT(cr != NULL);
5005 5005
5006 5006 /*
5007 5007 * XXX - Why should this check be made here?
5008 5008 */
5009 5009 if (vp->v_flag & VNOMAP)
5010 5010 return (ENOSYS);
5011 5011 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
5012 5012 return (0);
5013 5013 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5014 5014 return (EIO);
5015 5015
5016 5016 rp = VTOR(vp);
5017 5017 mutex_enter(&rp->r_statelock);
5018 5018 rp->r_count++;
5019 5019 mutex_exit(&rp->r_statelock);
5020 5020 error = nfs_putpages(vp, off, len, flags, cr);
5021 5021 mutex_enter(&rp->r_statelock);
5022 5022 rp->r_count--;
5023 5023 cv_broadcast(&rp->r_cv);
5024 5024 mutex_exit(&rp->r_statelock);
5025 5025
5026 5026 return (error);
5027 5027 }
5028 5028
5029 5029 /*
5030 5030 * Write out a single page, possibly klustering adjacent dirty pages.
5031 5031 */
5032 5032 int
5033 5033 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
5034 5034 int flags, cred_t *cr)
5035 5035 {
5036 5036 u_offset_t io_off;
5037 5037 u_offset_t lbn_off;
5038 5038 u_offset_t lbn;
5039 5039 size_t io_len;
5040 5040 uint_t bsize;
5041 5041 int error;
5042 5042 rnode_t *rp;
5043 5043
5044 5044 ASSERT(!vn_is_readonly(vp));
5045 5045 ASSERT(pp != NULL);
5046 5046 ASSERT(cr != NULL);
5047 5047 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
5048 5048
5049 5049 rp = VTOR(vp);
5050 5050 ASSERT(rp->r_count > 0);
5051 5051
5052 5052 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
5053 5053 lbn = pp->p_offset / bsize;
5054 5054 lbn_off = lbn * bsize;
5055 5055
5056 5056 /*
5057 5057 * Find a kluster that fits in one block, or in
5058 5058 * one page if pages are bigger than blocks. If
5059 5059 * there is less file space allocated than a whole
5060 5060 * page, we'll shorten the i/o request below.
5061 5061 */
5062 5062 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
5063 5063 roundup(bsize, PAGESIZE), flags);
5064 5064
5065 5065 /*
5066 5066 * pvn_write_kluster shouldn't have returned a page with offset
5067 5067 * behind the original page we were given. Verify that.
5068 5068 */
5069 5069 ASSERT((pp->p_offset / bsize) >= lbn);
5070 5070
5071 5071 /*
5072 5072 * Now pp will have the list of kept dirty pages marked for
5073 5073 * write back. It will also handle invalidation and freeing
5074 5074 * of pages that are not dirty. Check for page length rounding
5075 5075 * problems.
5076 5076 */
5077 5077 if (io_off + io_len > lbn_off + bsize) {
5078 5078 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
5079 5079 io_len = lbn_off + bsize - io_off;
5080 5080 }
5081 5081 /*
5082 5082 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5083 5083 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5084 5084 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5085 5085 * progress and the r_size has not been made consistent with the
5086 5086 * new size of the file. When the uiomove() completes the r_size is
5087 5087 * updated and the RMODINPROGRESS flag is cleared.
5088 5088 *
5089 5089 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5090 5090 * consistent value of r_size. Without this handshaking, it is
5091 5091 * possible that nfs(3)_bio() picks up the old value of r_size
5092 5092 * before the uiomove() in writerp() completes. This will result
5093 5093 * in the write through nfs(3)_bio() being dropped.
5094 5094 *
5095 5095 * More precisely, there is a window between the time the uiomove()
5096 5096 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
5097 5097 * operation intervenes in this window, the page will be picked up,
5098 5098 * because it is dirty (it will be unlocked, unless it was
5099 5099 * pagecreate'd). When the page is picked up as dirty, the dirty
5100 5100 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5101 5101 * checked. This will still be the old size. Therefore the page will
5102 5102 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
5103 5103 * the page will be found to be clean and the write will be dropped.
5104 5104 */
5105 5105 if (rp->r_flags & RMODINPROGRESS) {
5106 5106 mutex_enter(&rp->r_statelock);
5107 5107 if ((rp->r_flags & RMODINPROGRESS) &&
5108 5108 rp->r_modaddr + MAXBSIZE > io_off &&
5109 5109 rp->r_modaddr < io_off + io_len) {
5110 5110 page_t *plist;
5111 5111 /*
5112 5112 * A write is in progress for this region of the file.
5113 5113 * If we did not detect RMODINPROGRESS here then this
5114 5114 * path through nfs_putapage() would eventually go to
5115 5115 * nfs(3)_bio() and may not write out all of the data
5116 5116 * in the pages. We end up losing data. So we decide
5117 5117 * to set the modified bit on each page in the page
5118 5118 * list and mark the rnode with RDIRTY. This write
5119 5119 * will be restarted at some later time.
5120 5120 */
5121 5121 plist = pp;
5122 5122 while (plist != NULL) {
5123 5123 pp = plist;
5124 5124 page_sub(&plist, pp);
5125 5125 hat_setmod(pp);
5126 5126 page_io_unlock(pp);
5127 5127 page_unlock(pp);
5128 5128 }
5129 5129 rp->r_flags |= RDIRTY;
5130 5130 mutex_exit(&rp->r_statelock);
5131 5131 if (offp)
5132 5132 *offp = io_off;
5133 5133 if (lenp)
5134 5134 *lenp = io_len;
5135 5135 return (0);
5136 5136 }
5137 5137 mutex_exit(&rp->r_statelock);
5138 5138 }
5139 5139
5140 5140 if (flags & B_ASYNC) {
5141 5141 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
5142 5142 nfs3_sync_putapage);
5143 5143 } else
5144 5144 error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr);
5145 5145
5146 5146 if (offp)
5147 5147 *offp = io_off;
5148 5148 if (lenp)
5149 5149 *lenp = io_len;
5150 5150 return (error);
5151 5151 }
5152 5152
5153 5153 static int
5154 5154 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5155 5155 int flags, cred_t *cr)
5156 5156 {
5157 5157 int error;
5158 5158 rnode_t *rp;
5159 5159
5160 5160 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5161 5161
5162 5162 flags |= B_WRITE;
5163 5163
5164 5164 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5165 5165
5166 5166 rp = VTOR(vp);
5167 5167
5168 5168 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
5169 5169 error == EACCES) &&
5170 5170 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
5171 5171 if (!(rp->r_flags & ROUTOFSPACE)) {
5172 5172 mutex_enter(&rp->r_statelock);
5173 5173 rp->r_flags |= ROUTOFSPACE;
5174 5174 mutex_exit(&rp->r_statelock);
5175 5175 }
5176 5176 flags |= B_ERROR;
5177 5177 pvn_write_done(pp, flags);
5178 5178 /*
5179 5179 * If this was not an async thread, then try again to
5180 5180 * write out the pages, but this time, also destroy
5181 5181 * them whether or not the write is successful. This
5182 5182 * will prevent memory from filling up with these
5183 5183 * pages and destroying them is the only alternative
5184 5184 * if they can't be written out.
5185 5185 *
5186 5186 * Don't do this if this is an async thread because
5187 5187 * when the pages are unlocked in pvn_write_done,
5188 5188 * some other thread could have come along, locked
5189 5189 * them, and queued for an async thread. It would be
5190 5190 * possible for all of the async threads to be tied
5191 5191 * up waiting to lock the pages again and they would
5192 5192 * all already be locked and waiting for an async
5193 5193 * thread to handle them. Deadlock.
5194 5194 */
5195 5195 if (!(flags & B_ASYNC)) {
5196 5196 error = nfs3_putpage(vp, io_off, io_len,
5197 5197 B_INVAL | B_FORCE, cr, NULL);
5198 5198 }
5199 5199 } else {
5200 5200 if (error)
5201 5201 flags |= B_ERROR;
5202 5202 else if (rp->r_flags & ROUTOFSPACE) {
5203 5203 mutex_enter(&rp->r_statelock);
5204 5204 rp->r_flags &= ~ROUTOFSPACE;
5205 5205 mutex_exit(&rp->r_statelock);
5206 5206 }
5207 5207 pvn_write_done(pp, flags);
5208 5208 if (freemem < desfree)
5209 5209 (void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr);
5210 5210 }
5211 5211
5212 5212 return (error);
5213 5213 }
5214 5214
5215 5215 /* ARGSUSED */
5216 5216 static int
5217 5217 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5218 5218 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5219 5219 cred_t *cr, caller_context_t *ct)
5220 5220 {
5221 5221 struct segvn_crargs vn_a;
5222 5222 int error;
5223 5223 rnode_t *rp;
5224 5224 struct vattr va;
5225 5225
5226 5226 if (nfs_zone() != VTOMI(vp)->mi_zone)
5227 5227 return (EIO);
5228 5228
5229 5229 if (vp->v_flag & VNOMAP)
5230 5230 return (ENOSYS);
5231 5231
5232 5232 if (off < 0 || off + len < 0)
5233 5233 return (ENXIO);
5234 5234
5235 5235 if (vp->v_type != VREG)
5236 5236 return (ENODEV);
5237 5237
5238 5238 /*
5239 5239 * If there is cached data and if close-to-open consistency
5240 5240 * checking is not turned off and if the file system is not
5241 5241 * mounted readonly, then force an over the wire getattr.
5242 5242 * Otherwise, just invoke nfs3getattr to get a copy of the
5243 5243 * attributes. The attribute cache will be used unless it
5244 5244 * is timed out and if it is, then an over the wire getattr
5245 5245 * will be issued.
5246 5246 */
5247 5247 va.va_mask = AT_ALL;
5248 5248 if (vn_has_cached_data(vp) &&
5249 5249 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
5250 5250 error = nfs3_getattr_otw(vp, &va, cr);
5251 5251 else
5252 5252 error = nfs3getattr(vp, &va, cr);
5253 5253 if (error)
5254 5254 return (error);
5255 5255
5256 5256 /*
5257 5257 * Check to see if the vnode is currently marked as not cachable.
5258 5258 * This means portions of the file are locked (through VOP_FRLOCK).
5259 5259 * In this case the map request must be refused. We use
5260 5260 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5261 5261 */
5262 5262 rp = VTOR(vp);
5263 5263
5264 5264 /*
5265 5265 * Atomically increment r_inmap after acquiring r_rwlock. The
5266 5266 * idea here is to acquire r_rwlock to block read/write and
5267 5267 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5268 5268 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
↓ open down ↓ |
5268 lines elided |
↑ open up ↑ |
5269 5269 * and we can prevent the deadlock that would have occurred
5270 5270 * when nfs3_addmap() would have acquired it out of order.
5271 5271 *
5272 5272 * Since we are not protecting r_inmap by any lock, we do not
5273 5273 * hold any lock when we decrement it. We atomically decrement
5274 5274 * r_inmap after we release r_lkserlock.
5275 5275 */
5276 5276
5277 5277 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
5278 5278 return (EINTR);
5279 - atomic_add_int(&rp->r_inmap, 1);
5279 + atomic_inc_uint(&rp->r_inmap);
5280 5280 nfs_rw_exit(&rp->r_rwlock);
5281 5281
5282 5282 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
5283 - atomic_add_int(&rp->r_inmap, -1);
5283 + atomic_dec_uint(&rp->r_inmap);
5284 5284 return (EINTR);
5285 5285 }
5286 5286
5287 5287 if (vp->v_flag & VNOCACHE) {
5288 5288 error = EAGAIN;
5289 5289 goto done;
5290 5290 }
5291 5291
5292 5292 /*
5293 5293 * Don't allow concurrent locks and mapping if mandatory locking is
5294 5294 * enabled.
5295 5295 */
5296 5296 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
5297 5297 MANDLOCK(vp, va.va_mode)) {
5298 5298 error = EAGAIN;
5299 5299 goto done;
5300 5300 }
5301 5301
5302 5302 as_rangelock(as);
5303 5303 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5304 5304 if (error != 0) {
5305 5305 as_rangeunlock(as);
5306 5306 goto done;
5307 5307 }
5308 5308
5309 5309 vn_a.vp = vp;
5310 5310 vn_a.offset = off;
5311 5311 vn_a.type = (flags & MAP_TYPE);
5312 5312 vn_a.prot = (uchar_t)prot;
5313 5313 vn_a.maxprot = (uchar_t)maxprot;
5314 5314 vn_a.flags = (flags & ~MAP_TYPE);
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
5315 5315 vn_a.cred = cr;
5316 5316 vn_a.amp = NULL;
5317 5317 vn_a.szc = 0;
5318 5318 vn_a.lgrp_mem_policy_flags = 0;
5319 5319
5320 5320 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5321 5321 as_rangeunlock(as);
5322 5322
5323 5323 done:
5324 5324 nfs_rw_exit(&rp->r_lkserlock);
5325 - atomic_add_int(&rp->r_inmap, -1);
5325 + atomic_dec_uint(&rp->r_inmap);
5326 5326 return (error);
5327 5327 }
5328 5328
5329 5329 /* ARGSUSED */
5330 5330 static int
5331 5331 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5332 5332 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5333 5333 cred_t *cr, caller_context_t *ct)
5334 5334 {
5335 5335 rnode_t *rp;
5336 5336
5337 5337 if (vp->v_flag & VNOMAP)
5338 5338 return (ENOSYS);
5339 5339 if (nfs_zone() != VTOMI(vp)->mi_zone)
5340 5340 return (EIO);
5341 5341
5342 5342 rp = VTOR(vp);
5343 5343 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
5344 5344
5345 5345 return (0);
5346 5346 }
5347 5347
5348 5348 /* ARGSUSED */
5349 5349 static int
5350 5350 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5351 5351 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
5352 5352 caller_context_t *ct)
5353 5353 {
5354 5354 netobj lm_fh3;
5355 5355 int rc;
5356 5356 u_offset_t start, end;
5357 5357 rnode_t *rp;
5358 5358 int error = 0, intr = INTR(vp);
5359 5359
5360 5360 if (nfs_zone() != VTOMI(vp)->mi_zone)
5361 5361 return (EIO);
5362 5362 /* check for valid cmd parameter */
5363 5363 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
5364 5364 return (EINVAL);
5365 5365
5366 5366 /* Verify l_type. */
5367 5367 switch (bfp->l_type) {
5368 5368 case F_RDLCK:
5369 5369 if (cmd != F_GETLK && !(flag & FREAD))
5370 5370 return (EBADF);
5371 5371 break;
5372 5372 case F_WRLCK:
5373 5373 if (cmd != F_GETLK && !(flag & FWRITE))
5374 5374 return (EBADF);
5375 5375 break;
5376 5376 case F_UNLCK:
5377 5377 intr = 0;
5378 5378 break;
5379 5379
5380 5380 default:
5381 5381 return (EINVAL);
5382 5382 }
5383 5383
5384 5384 /* check the validity of the lock range */
5385 5385 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
5386 5386 return (rc);
5387 5387 if (rc = flk_check_lock_data(start, end, MAXEND))
5388 5388 return (rc);
5389 5389
5390 5390 /*
5391 5391 * If the filesystem is mounted using local locking, pass the
5392 5392 * request off to the local locking code.
5393 5393 */
5394 5394 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
5395 5395 if (cmd == F_SETLK || cmd == F_SETLKW) {
5396 5396 /*
5397 5397 * For complete safety, we should be holding
5398 5398 * r_lkserlock. However, we can't call
5399 5399 * lm_safelock and then fs_frlock while
5400 5400 * holding r_lkserlock, so just invoke
5401 5401 * lm_safelock and expect that this will
5402 5402 * catch enough of the cases.
5403 5403 */
5404 5404 if (!lm_safelock(vp, bfp, cr))
5405 5405 return (EAGAIN);
5406 5406 }
5407 5407 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
5408 5408 }
5409 5409
5410 5410 rp = VTOR(vp);
5411 5411
5412 5412 /*
5413 5413 * Check whether the given lock request can proceed, given the
5414 5414 * current file mappings.
5415 5415 */
5416 5416 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
5417 5417 return (EINTR);
5418 5418 if (cmd == F_SETLK || cmd == F_SETLKW) {
5419 5419 if (!lm_safelock(vp, bfp, cr)) {
5420 5420 rc = EAGAIN;
5421 5421 goto done;
5422 5422 }
5423 5423 }
5424 5424
5425 5425 /*
5426 5426 * Flush the cache after waiting for async I/O to finish. For new
5427 5427 * locks, this is so that the process gets the latest bits from the
5428 5428 * server. For unlocks, this is so that other clients see the
5429 5429 * latest bits once the file has been unlocked. If currently dirty
5430 5430 * pages can't be flushed, then don't allow a lock to be set. But
5431 5431 * allow unlocks to succeed, to avoid having orphan locks on the
5432 5432 * server.
5433 5433 */
5434 5434 if (cmd != F_GETLK) {
5435 5435 mutex_enter(&rp->r_statelock);
5436 5436 while (rp->r_count > 0) {
5437 5437 if (intr) {
5438 5438 klwp_t *lwp = ttolwp(curthread);
5439 5439
5440 5440 if (lwp != NULL)
5441 5441 lwp->lwp_nostop++;
5442 5442 if (cv_wait_sig(&rp->r_cv,
5443 5443 &rp->r_statelock) == 0) {
5444 5444 if (lwp != NULL)
5445 5445 lwp->lwp_nostop--;
5446 5446 rc = EINTR;
5447 5447 break;
5448 5448 }
5449 5449 if (lwp != NULL)
5450 5450 lwp->lwp_nostop--;
5451 5451 } else
5452 5452 cv_wait(&rp->r_cv, &rp->r_statelock);
5453 5453 }
5454 5454 mutex_exit(&rp->r_statelock);
5455 5455 if (rc != 0)
5456 5456 goto done;
5457 5457 error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
5458 5458 if (error) {
5459 5459 if (error == ENOSPC || error == EDQUOT) {
5460 5460 mutex_enter(&rp->r_statelock);
5461 5461 if (!rp->r_error)
5462 5462 rp->r_error = error;
5463 5463 mutex_exit(&rp->r_statelock);
5464 5464 }
5465 5465 if (bfp->l_type != F_UNLCK) {
5466 5466 rc = ENOLCK;
5467 5467 goto done;
5468 5468 }
5469 5469 }
5470 5470 }
5471 5471
5472 5472 lm_fh3.n_len = VTOFH3(vp)->fh3_length;
5473 5473 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
5474 5474
5475 5475 /*
5476 5476 * Call the lock manager to do the real work of contacting
5477 5477 * the server and obtaining the lock.
5478 5478 */
5479 5479 rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp);
5480 5480
5481 5481 if (rc == 0)
5482 5482 nfs_lockcompletion(vp, cmd);
5483 5483
5484 5484 done:
5485 5485 nfs_rw_exit(&rp->r_lkserlock);
5486 5486 return (rc);
5487 5487 }
5488 5488
5489 5489 /*
5490 5490 * Free storage space associated with the specified vnode. The portion
5491 5491 * to be freed is specified by bfp->l_start and bfp->l_len (already
5492 5492 * normalized to a "whence" of 0).
5493 5493 *
5494 5494 * This is an experimental facility whose continued existence is not
5495 5495 * guaranteed. Currently, we only support the special case
5496 5496 * of l_len == 0, meaning free to end of file.
5497 5497 */
5498 5498 /* ARGSUSED */
5499 5499 static int
5500 5500 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5501 5501 offset_t offset, cred_t *cr, caller_context_t *ct)
5502 5502 {
5503 5503 int error;
5504 5504
5505 5505 ASSERT(vp->v_type == VREG);
5506 5506 if (cmd != F_FREESP)
5507 5507 return (EINVAL);
5508 5508 if (nfs_zone() != VTOMI(vp)->mi_zone)
5509 5509 return (EIO);
5510 5510
5511 5511 error = convoff(vp, bfp, 0, offset);
5512 5512 if (!error) {
5513 5513 ASSERT(bfp->l_start >= 0);
5514 5514 if (bfp->l_len == 0) {
5515 5515 struct vattr va;
5516 5516
5517 5517 /*
5518 5518 * ftruncate should not change the ctime and
5519 5519 * mtime if we truncate the file to its
5520 5520 * previous size.
5521 5521 */
5522 5522 va.va_mask = AT_SIZE;
5523 5523 error = nfs3getattr(vp, &va, cr);
5524 5524 if (error || va.va_size == bfp->l_start)
5525 5525 return (error);
5526 5526 va.va_mask = AT_SIZE;
5527 5527 va.va_size = bfp->l_start;
5528 5528 error = nfs3setattr(vp, &va, 0, cr);
5529 5529
5530 5530 if (error == 0 && bfp->l_start == 0)
5531 5531 vnevent_truncate(vp, ct);
5532 5532 } else
5533 5533 error = EINVAL;
5534 5534 }
5535 5535
5536 5536 return (error);
5537 5537 }
5538 5538
5539 5539 /* ARGSUSED */
5540 5540 static int
5541 5541 nfs3_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
5542 5542 {
5543 5543
5544 5544 return (EINVAL);
5545 5545 }
5546 5546
5547 5547 /*
5548 5548 * Setup and add an address space callback to do the work of the delmap call.
5549 5549 * The callback will (and must be) deleted in the actual callback function.
5550 5550 *
5551 5551 * This is done in order to take care of the problem that we have with holding
5552 5552 * the address space's a_lock for a long period of time (e.g. if the NFS server
5553 5553 * is down). Callbacks will be executed in the address space code while the
5554 5554 * a_lock is not held. Holding the address space's a_lock causes things such
5555 5555 * as ps and fork to hang because they are trying to acquire this lock as well.
5556 5556 */
5557 5557 /* ARGSUSED */
5558 5558 static int
5559 5559 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5560 5560 size_t len, uint_t prot, uint_t maxprot, uint_t flags,
5561 5561 cred_t *cr, caller_context_t *ct)
5562 5562 {
5563 5563 int caller_found;
5564 5564 int error;
5565 5565 rnode_t *rp;
5566 5566 nfs_delmap_args_t *dmapp;
5567 5567 nfs_delmapcall_t *delmap_call;
5568 5568
5569 5569 if (vp->v_flag & VNOMAP)
5570 5570 return (ENOSYS);
5571 5571 /*
5572 5572 * A process may not change zones if it has NFS pages mmap'ed
5573 5573 * in, so we can't legitimately get here from the wrong zone.
5574 5574 */
5575 5575 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5576 5576
5577 5577 rp = VTOR(vp);
5578 5578
5579 5579 /*
5580 5580 * The way that the address space of this process deletes its mapping
5581 5581 * of this file is via the following call chains:
5582 5582 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5583 5583 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5584 5584 *
5585 5585 * With the use of address space callbacks we are allowed to drop the
5586 5586 * address space lock, a_lock, while executing the NFS operations that
5587 5587 * need to go over the wire. Returning EAGAIN to the caller of this
5588 5588 * function is what drives the execution of the callback that we add
5589 5589 * below. The callback will be executed by the address space code
5590 5590 * after dropping the a_lock. When the callback is finished, since
5591 5591 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5592 5592 * is called again on the same segment to finish the rest of the work
5593 5593 * that needs to happen during unmapping.
5594 5594 *
5595 5595 * This action of calling back into the segment driver causes
5596 5596 * nfs3_delmap() to get called again, but since the callback was
5597 5597 * already executed at this point, it already did the work and there
5598 5598 * is nothing left for us to do.
5599 5599 *
5600 5600 * To Summarize:
5601 5601 * - The first time nfs3_delmap is called by the current thread is when
5602 5602 * we add the caller associated with this delmap to the delmap caller
5603 5603 * list, add the callback, and return EAGAIN.
5604 5604 * - The second time in this call chain when nfs3_delmap is called we
5605 5605 * will find this caller in the delmap caller list and realize there
5606 5606 * is no more work to do thus removing this caller from the list and
5607 5607 * returning the error that was set in the callback execution.
5608 5608 */
5609 5609 caller_found = nfs_find_and_delete_delmapcall(rp, &error);
5610 5610 if (caller_found) {
5611 5611 /*
5612 5612 * 'error' is from the actual delmap operations. To avoid
5613 5613 * hangs, we need to handle the return of EAGAIN differently
5614 5614 * since this is what drives the callback execution.
5615 5615 * In this case, we don't want to return EAGAIN and do the
5616 5616 * callback execution because there are none to execute.
5617 5617 */
5618 5618 if (error == EAGAIN)
5619 5619 return (0);
5620 5620 else
5621 5621 return (error);
5622 5622 }
5623 5623
5624 5624 /* current caller was not in the list */
5625 5625 delmap_call = nfs_init_delmapcall();
5626 5626
5627 5627 mutex_enter(&rp->r_statelock);
5628 5628 list_insert_tail(&rp->r_indelmap, delmap_call);
5629 5629 mutex_exit(&rp->r_statelock);
5630 5630
5631 5631 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
5632 5632
5633 5633 dmapp->vp = vp;
5634 5634 dmapp->off = off;
5635 5635 dmapp->addr = addr;
5636 5636 dmapp->len = len;
5637 5637 dmapp->prot = prot;
5638 5638 dmapp->maxprot = maxprot;
5639 5639 dmapp->flags = flags;
5640 5640 dmapp->cr = cr;
5641 5641 dmapp->caller = delmap_call;
5642 5642
5643 5643 error = as_add_callback(as, nfs3_delmap_callback, dmapp,
5644 5644 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
5645 5645
5646 5646 return (error ? error : EAGAIN);
5647 5647 }
5648 5648
5649 5649 /*
5650 5650 * Remove some pages from an mmap'd vnode. Just update the
5651 5651 * count of pages. If doing close-to-open, then flush and
5652 5652 * commit all of the pages associated with this file.
5653 5653 * Otherwise, start an asynchronous page flush to write out
5654 5654 * any dirty pages. This will also associate a credential
5655 5655 * with the rnode which can be used to write the pages.
5656 5656 */
5657 5657 /* ARGSUSED */
5658 5658 static void
5659 5659 nfs3_delmap_callback(struct as *as, void *arg, uint_t event)
5660 5660 {
5661 5661 int error;
5662 5662 rnode_t *rp;
5663 5663 mntinfo_t *mi;
5664 5664 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg;
5665 5665
5666 5666 rp = VTOR(dmapp->vp);
5667 5667 mi = VTOMI(dmapp->vp);
5668 5668
5669 5669 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
5670 5670 ASSERT(rp->r_mapcnt >= 0);
5671 5671
5672 5672 /*
5673 5673 * Initiate a page flush and potential commit if there are
5674 5674 * pages, the file system was not mounted readonly, the segment
5675 5675 * was mapped shared, and the pages themselves were writeable.
5676 5676 */
5677 5677 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
5678 5678 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
5679 5679 mutex_enter(&rp->r_statelock);
5680 5680 rp->r_flags |= RDIRTY;
5681 5681 mutex_exit(&rp->r_statelock);
5682 5682 /*
5683 5683 * If this is a cross-zone access a sync putpage won't work, so
5684 5684 * the best we can do is try an async putpage. That seems
5685 5685 * better than something more draconian such as discarding the
5686 5686 * dirty pages.
5687 5687 */
5688 5688 if ((mi->mi_flags & MI_NOCTO) ||
5689 5689 nfs_zone() != mi->mi_zone)
5690 5690 error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5691 5691 B_ASYNC, dmapp->cr, NULL);
5692 5692 else
5693 5693 error = nfs3_putpage_commit(dmapp->vp, dmapp->off,
5694 5694 dmapp->len, dmapp->cr);
5695 5695 if (!error) {
5696 5696 mutex_enter(&rp->r_statelock);
5697 5697 error = rp->r_error;
5698 5698 rp->r_error = 0;
5699 5699 mutex_exit(&rp->r_statelock);
5700 5700 }
5701 5701 } else
5702 5702 error = 0;
5703 5703
5704 5704 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
5705 5705 (void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5706 5706 B_INVAL, dmapp->cr, NULL);
5707 5707
5708 5708 dmapp->caller->error = error;
5709 5709 (void) as_delete_callback(as, arg);
5710 5710 kmem_free(dmapp, sizeof (nfs_delmap_args_t));
5711 5711 }
5712 5712
5713 5713 static int nfs3_pathconf_disable_cache = 0;
5714 5714
5715 5715 #ifdef DEBUG
5716 5716 static int nfs3_pathconf_cache_hits = 0;
5717 5717 static int nfs3_pathconf_cache_misses = 0;
5718 5718 #endif
5719 5719
5720 5720 /* ARGSUSED */
5721 5721 static int
5722 5722 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5723 5723 caller_context_t *ct)
5724 5724 {
5725 5725 int error;
5726 5726 PATHCONF3args args;
5727 5727 PATHCONF3res res;
5728 5728 int douprintf;
5729 5729 failinfo_t fi;
5730 5730 rnode_t *rp;
5731 5731 hrtime_t t;
5732 5732
5733 5733 if (nfs_zone() != VTOMI(vp)->mi_zone)
5734 5734 return (EIO);
5735 5735 /*
5736 5736 * Large file spec - need to base answer on info stored
5737 5737 * on original FSINFO response.
5738 5738 */
5739 5739 if (cmd == _PC_FILESIZEBITS) {
5740 5740 unsigned long long ll;
5741 5741 long l = 1;
5742 5742
5743 5743 ll = VTOMI(vp)->mi_maxfilesize;
5744 5744
5745 5745 if (ll == 0) {
5746 5746 *valp = 0;
5747 5747 return (0);
5748 5748 }
5749 5749
5750 5750 if (ll & 0xffffffff00000000) {
5751 5751 l += 32; ll >>= 32;
5752 5752 }
5753 5753 if (ll & 0xffff0000) {
5754 5754 l += 16; ll >>= 16;
5755 5755 }
5756 5756 if (ll & 0xff00) {
5757 5757 l += 8; ll >>= 8;
5758 5758 }
5759 5759 if (ll & 0xf0) {
5760 5760 l += 4; ll >>= 4;
5761 5761 }
5762 5762 if (ll & 0xc) {
5763 5763 l += 2; ll >>= 2;
5764 5764 }
5765 5765 if (ll & 0x2)
5766 5766 l += 2;
5767 5767 else if (ll & 0x1)
5768 5768 l += 1;
5769 5769 *valp = l;
5770 5770 return (0);
5771 5771 }
5772 5772
5773 5773 if (cmd == _PC_ACL_ENABLED) {
5774 5774 *valp = _ACL_ACLENT_ENABLED;
5775 5775 return (0);
5776 5776 }
5777 5777
5778 5778 if (cmd == _PC_XATTR_EXISTS) {
5779 5779 error = 0;
5780 5780 *valp = 0;
5781 5781 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5782 5782 vnode_t *avp;
5783 5783 rnode_t *rp;
5784 5784 int error = 0;
5785 5785 mntinfo_t *mi = VTOMI(vp);
5786 5786
5787 5787 if (!(mi->mi_flags & MI_EXTATTR))
5788 5788 return (0);
5789 5789
5790 5790 rp = VTOR(vp);
5791 5791 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
5792 5792 INTR(vp)))
5793 5793 return (EINTR);
5794 5794
5795 5795 error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
5796 5796 if (error || avp == NULL)
5797 5797 error = acl_getxattrdir3(vp, &avp, 0, cr, 0);
5798 5798
5799 5799 nfs_rw_exit(&rp->r_rwlock);
5800 5800
5801 5801 if (error == 0 && avp != NULL) {
5802 5802 error = do_xattr_exists_check(avp, valp, cr);
5803 5803 VN_RELE(avp);
5804 5804 } else if (error == ENOENT) {
5805 5805 error = 0;
5806 5806 *valp = 0;
5807 5807 }
5808 5808 }
5809 5809 return (error);
5810 5810 }
5811 5811
5812 5812 rp = VTOR(vp);
5813 5813 if (rp->r_pathconf != NULL) {
5814 5814 mutex_enter(&rp->r_statelock);
5815 5815 if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) {
5816 5816 kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf));
5817 5817 rp->r_pathconf = NULL;
5818 5818 }
5819 5819 if (rp->r_pathconf != NULL) {
5820 5820 error = 0;
5821 5821 switch (cmd) {
5822 5822 case _PC_LINK_MAX:
5823 5823 *valp = rp->r_pathconf->link_max;
5824 5824 break;
5825 5825 case _PC_NAME_MAX:
5826 5826 *valp = rp->r_pathconf->name_max;
5827 5827 break;
5828 5828 case _PC_PATH_MAX:
5829 5829 case _PC_SYMLINK_MAX:
5830 5830 *valp = MAXPATHLEN;
5831 5831 break;
5832 5832 case _PC_CHOWN_RESTRICTED:
5833 5833 *valp = rp->r_pathconf->chown_restricted;
5834 5834 break;
5835 5835 case _PC_NO_TRUNC:
5836 5836 *valp = rp->r_pathconf->no_trunc;
5837 5837 break;
5838 5838 default:
5839 5839 error = EINVAL;
5840 5840 break;
5841 5841 }
5842 5842 mutex_exit(&rp->r_statelock);
5843 5843 #ifdef DEBUG
5844 5844 nfs3_pathconf_cache_hits++;
5845 5845 #endif
5846 5846 return (error);
5847 5847 }
5848 5848 mutex_exit(&rp->r_statelock);
5849 5849 }
5850 5850 #ifdef DEBUG
5851 5851 nfs3_pathconf_cache_misses++;
5852 5852 #endif
5853 5853
5854 5854 args.object = *VTOFH3(vp);
5855 5855 fi.vp = vp;
5856 5856 fi.fhp = (caddr_t)&args.object;
5857 5857 fi.copyproc = nfs3copyfh;
5858 5858 fi.lookupproc = nfs3lookup;
5859 5859 fi.xattrdirproc = acl_getxattrdir3;
5860 5860
5861 5861 douprintf = 1;
5862 5862
5863 5863 t = gethrtime();
5864 5864
5865 5865 error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF,
5866 5866 xdr_nfs_fh3, (caddr_t)&args,
5867 5867 xdr_PATHCONF3res, (caddr_t)&res, cr,
5868 5868 &douprintf, &res.status, 0, &fi);
5869 5869
5870 5870 if (error)
5871 5871 return (error);
5872 5872
5873 5873 error = geterrno3(res.status);
5874 5874
5875 5875 if (!error) {
5876 5876 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
5877 5877 if (!nfs3_pathconf_disable_cache) {
5878 5878 mutex_enter(&rp->r_statelock);
5879 5879 if (rp->r_pathconf == NULL) {
5880 5880 rp->r_pathconf = kmem_alloc(
5881 5881 sizeof (*rp->r_pathconf), KM_NOSLEEP);
5882 5882 if (rp->r_pathconf != NULL)
5883 5883 *rp->r_pathconf = res.resok.info;
5884 5884 }
5885 5885 mutex_exit(&rp->r_statelock);
5886 5886 }
5887 5887 switch (cmd) {
5888 5888 case _PC_LINK_MAX:
5889 5889 *valp = res.resok.info.link_max;
5890 5890 break;
5891 5891 case _PC_NAME_MAX:
5892 5892 *valp = res.resok.info.name_max;
5893 5893 break;
5894 5894 case _PC_PATH_MAX:
5895 5895 case _PC_SYMLINK_MAX:
5896 5896 *valp = MAXPATHLEN;
5897 5897 break;
5898 5898 case _PC_CHOWN_RESTRICTED:
5899 5899 *valp = res.resok.info.chown_restricted;
5900 5900 break;
5901 5901 case _PC_NO_TRUNC:
5902 5902 *valp = res.resok.info.no_trunc;
5903 5903 break;
5904 5904 default:
5905 5905 return (EINVAL);
5906 5906 }
5907 5907 } else {
5908 5908 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
5909 5909 PURGE_STALE_FH(error, vp, cr);
5910 5910 }
5911 5911
5912 5912 return (error);
5913 5913 }
5914 5914
5915 5915 /*
5916 5916 * Called by async thread to do synchronous pageio. Do the i/o, wait
5917 5917 * for it to complete, and cleanup the page list when done.
5918 5918 */
5919 5919 static int
5920 5920 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5921 5921 int flags, cred_t *cr)
5922 5922 {
5923 5923 int error;
5924 5924
5925 5925 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5926 5926 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5927 5927 if (flags & B_READ)
5928 5928 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
5929 5929 else
5930 5930 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
5931 5931 return (error);
5932 5932 }
5933 5933
5934 5934 /* ARGSUSED */
5935 5935 static int
5936 5936 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5937 5937 int flags, cred_t *cr, caller_context_t *ct)
5938 5938 {
5939 5939 int error;
5940 5940 rnode_t *rp;
5941 5941
5942 5942 if (pp == NULL)
5943 5943 return (EINVAL);
5944 5944 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5945 5945 return (EIO);
5946 5946
5947 5947 rp = VTOR(vp);
5948 5948 mutex_enter(&rp->r_statelock);
5949 5949 rp->r_count++;
5950 5950 mutex_exit(&rp->r_statelock);
5951 5951
5952 5952 if (flags & B_ASYNC) {
5953 5953 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
5954 5954 nfs3_sync_pageio);
5955 5955 } else
5956 5956 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5957 5957 mutex_enter(&rp->r_statelock);
5958 5958 rp->r_count--;
5959 5959 cv_broadcast(&rp->r_cv);
5960 5960 mutex_exit(&rp->r_statelock);
5961 5961 return (error);
5962 5962 }
5963 5963
5964 5964 /* ARGSUSED */
5965 5965 static void
5966 5966 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
5967 5967 caller_context_t *ct)
5968 5968 {
5969 5969 int error;
5970 5970 rnode_t *rp;
5971 5971 page_t *plist;
5972 5972 page_t *pptr;
5973 5973 offset3 offset;
5974 5974 count3 len;
5975 5975 k_sigset_t smask;
5976 5976
5977 5977 /*
5978 5978 * We should get called with fl equal to either B_FREE or
5979 5979 * B_INVAL. Any other value is illegal.
5980 5980 *
5981 5981 * The page that we are either supposed to free or destroy
5982 5982 * should be exclusive locked and its io lock should not
5983 5983 * be held.
5984 5984 */
5985 5985 ASSERT(fl == B_FREE || fl == B_INVAL);
5986 5986 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
5987 5987 rp = VTOR(vp);
5988 5988
5989 5989 /*
5990 5990 * If the page doesn't need to be committed or we shouldn't
5991 5991 * even bother attempting to commit it, then just make sure
5992 5992 * that the p_fsdata byte is clear and then either free or
5993 5993 * destroy the page as appropriate.
5994 5994 */
5995 5995 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) {
5996 5996 pp->p_fsdata = C_NOCOMMIT;
5997 5997 if (fl == B_FREE)
5998 5998 page_free(pp, dn);
5999 5999 else
6000 6000 page_destroy(pp, dn);
6001 6001 return;
6002 6002 }
6003 6003
6004 6004 /*
6005 6005 * If there is a page invalidation operation going on, then
6006 6006 * if this is one of the pages being destroyed, then just
6007 6007 * clear the p_fsdata byte and then either free or destroy
6008 6008 * the page as appropriate.
6009 6009 */
6010 6010 mutex_enter(&rp->r_statelock);
6011 6011 if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
6012 6012 mutex_exit(&rp->r_statelock);
6013 6013 pp->p_fsdata = C_NOCOMMIT;
6014 6014 if (fl == B_FREE)
6015 6015 page_free(pp, dn);
6016 6016 else
6017 6017 page_destroy(pp, dn);
6018 6018 return;
6019 6019 }
6020 6020
6021 6021 /*
6022 6022 * If we are freeing this page and someone else is already
6023 6023 * waiting to do a commit, then just unlock the page and
6024 6024 * return. That other thread will take care of commiting
6025 6025 * this page. The page can be freed sometime after the
6026 6026 * commit has finished. Otherwise, if the page is marked
6027 6027 * as delay commit, then we may be getting called from
6028 6028 * pvn_write_done, one page at a time. This could result
6029 6029 * in one commit per page, so we end up doing lots of small
6030 6030 * commits instead of fewer larger commits. This is bad,
6031 6031 * we want do as few commits as possible.
6032 6032 */
6033 6033 if (fl == B_FREE) {
6034 6034 if (rp->r_flags & RCOMMITWAIT) {
6035 6035 page_unlock(pp);
6036 6036 mutex_exit(&rp->r_statelock);
6037 6037 return;
6038 6038 }
6039 6039 if (pp->p_fsdata == C_DELAYCOMMIT) {
6040 6040 pp->p_fsdata = C_COMMIT;
6041 6041 page_unlock(pp);
6042 6042 mutex_exit(&rp->r_statelock);
6043 6043 return;
6044 6044 }
6045 6045 }
6046 6046
6047 6047 /*
6048 6048 * Check to see if there is a signal which would prevent an
6049 6049 * attempt to commit the pages from being successful. If so,
6050 6050 * then don't bother with all of the work to gather pages and
6051 6051 * generate the unsuccessful RPC. Just return from here and
6052 6052 * let the page be committed at some later time.
6053 6053 */
6054 6054 sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
6055 6055 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
6056 6056 sigunintr(&smask);
6057 6057 page_unlock(pp);
6058 6058 mutex_exit(&rp->r_statelock);
6059 6059 return;
6060 6060 }
6061 6061 sigunintr(&smask);
6062 6062
6063 6063 /*
6064 6064 * We are starting to need to commit pages, so let's try
6065 6065 * to commit as many as possible at once to reduce the
6066 6066 * overhead.
6067 6067 *
6068 6068 * Set the `commit inprogress' state bit. We must
6069 6069 * first wait until any current one finishes. Then
6070 6070 * we initialize the c_pages list with this page.
6071 6071 */
6072 6072 while (rp->r_flags & RCOMMIT) {
6073 6073 rp->r_flags |= RCOMMITWAIT;
6074 6074 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6075 6075 rp->r_flags &= ~RCOMMITWAIT;
6076 6076 }
6077 6077 rp->r_flags |= RCOMMIT;
6078 6078 mutex_exit(&rp->r_statelock);
6079 6079 ASSERT(rp->r_commit.c_pages == NULL);
6080 6080 rp->r_commit.c_pages = pp;
6081 6081 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6082 6082 rp->r_commit.c_commlen = PAGESIZE;
6083 6083
6084 6084 /*
6085 6085 * Gather together all other pages which can be committed.
6086 6086 * They will all be chained off r_commit.c_pages.
6087 6087 */
6088 6088 nfs3_get_commit(vp);
6089 6089
6090 6090 /*
6091 6091 * Clear the `commit inprogress' status and disconnect
6092 6092 * the list of pages to be committed from the rnode.
6093 6093 * At this same time, we also save the starting offset
6094 6094 * and length of data to be committed on the server.
6095 6095 */
6096 6096 plist = rp->r_commit.c_pages;
6097 6097 rp->r_commit.c_pages = NULL;
6098 6098 offset = rp->r_commit.c_commbase;
6099 6099 len = rp->r_commit.c_commlen;
6100 6100 mutex_enter(&rp->r_statelock);
6101 6101 rp->r_flags &= ~RCOMMIT;
6102 6102 cv_broadcast(&rp->r_commit.c_cv);
6103 6103 mutex_exit(&rp->r_statelock);
6104 6104
6105 6105 if (curproc == proc_pageout || curproc == proc_fsflush ||
6106 6106 nfs_zone() != VTOMI(vp)->mi_zone) {
6107 6107 nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit);
6108 6108 return;
6109 6109 }
6110 6110
6111 6111 /*
6112 6112 * Actually generate the COMMIT3 over the wire operation.
6113 6113 */
6114 6114 error = nfs3_commit(vp, offset, len, cr);
6115 6115
6116 6116 /*
6117 6117 * If we got an error during the commit, just unlock all
6118 6118 * of the pages. The pages will get retransmitted to the
6119 6119 * server during a putpage operation.
6120 6120 */
6121 6121 if (error) {
6122 6122 while (plist != NULL) {
6123 6123 pptr = plist;
6124 6124 page_sub(&plist, pptr);
6125 6125 page_unlock(pptr);
6126 6126 }
6127 6127 return;
6128 6128 }
6129 6129
6130 6130 /*
6131 6131 * We've tried as hard as we can to commit the data to stable
6132 6132 * storage on the server. We release the rest of the pages
6133 6133 * and clear the commit required state. They will be put
6134 6134 * onto the tail of the cachelist if they are nolonger
6135 6135 * mapped.
6136 6136 */
6137 6137 while (plist != pp) {
6138 6138 pptr = plist;
6139 6139 page_sub(&plist, pptr);
6140 6140 pptr->p_fsdata = C_NOCOMMIT;
6141 6141 (void) page_release(pptr, 1);
6142 6142 }
6143 6143
6144 6144 /*
6145 6145 * It is possible that nfs3_commit didn't return error but
6146 6146 * some other thread has modified the page we are going
6147 6147 * to free/destroy.
6148 6148 * In this case we need to rewrite the page. Do an explicit check
6149 6149 * before attempting to free/destroy the page. If modified, needs to
6150 6150 * be rewritten so unlock the page and return.
6151 6151 */
6152 6152 if (hat_ismod(pp)) {
6153 6153 pp->p_fsdata = C_NOCOMMIT;
6154 6154 page_unlock(pp);
6155 6155 return;
6156 6156 }
6157 6157
6158 6158 /*
6159 6159 * Now, as appropriate, either free or destroy the page
6160 6160 * that we were called with.
6161 6161 */
6162 6162 pp->p_fsdata = C_NOCOMMIT;
6163 6163 if (fl == B_FREE)
6164 6164 page_free(pp, dn);
6165 6165 else
6166 6166 page_destroy(pp, dn);
6167 6167 }
6168 6168
6169 6169 static int
6170 6170 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr)
6171 6171 {
6172 6172 int error;
6173 6173 rnode_t *rp;
6174 6174 COMMIT3args args;
6175 6175 COMMIT3res res;
6176 6176 int douprintf;
6177 6177 cred_t *cred;
6178 6178
6179 6179 rp = VTOR(vp);
6180 6180 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6181 6181
6182 6182 mutex_enter(&rp->r_statelock);
6183 6183 if (rp->r_cred != NULL) {
6184 6184 cred = rp->r_cred;
6185 6185 crhold(cred);
6186 6186 } else {
6187 6187 rp->r_cred = cr;
6188 6188 crhold(cr);
6189 6189 cred = cr;
6190 6190 crhold(cred);
6191 6191 }
6192 6192 mutex_exit(&rp->r_statelock);
6193 6193
6194 6194 args.file = *VTOFH3(vp);
6195 6195 args.offset = offset;
6196 6196 args.count = count;
6197 6197
6198 6198 doitagain:
6199 6199 douprintf = 1;
6200 6200 error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT,
6201 6201 xdr_COMMIT3args, (caddr_t)&args,
6202 6202 xdr_COMMIT3res, (caddr_t)&res, cred,
6203 6203 &douprintf, &res.status, 0, NULL);
6204 6204
6205 6205 crfree(cred);
6206 6206
6207 6207 if (error)
6208 6208 return (error);
6209 6209
6210 6210 error = geterrno3(res.status);
6211 6211 if (!error) {
6212 6212 ASSERT(rp->r_flags & RHAVEVERF);
6213 6213 mutex_enter(&rp->r_statelock);
6214 6214 if (rp->r_verf == res.resok.verf) {
6215 6215 mutex_exit(&rp->r_statelock);
6216 6216 return (0);
6217 6217 }
6218 6218 nfs3_set_mod(vp);
6219 6219 rp->r_verf = res.resok.verf;
6220 6220 mutex_exit(&rp->r_statelock);
6221 6221 error = NFS_VERF_MISMATCH;
6222 6222 } else {
6223 6223 if (error == EACCES) {
6224 6224 mutex_enter(&rp->r_statelock);
6225 6225 if (cred != cr) {
6226 6226 if (rp->r_cred != NULL)
6227 6227 crfree(rp->r_cred);
6228 6228 rp->r_cred = cr;
6229 6229 crhold(cr);
6230 6230 cred = cr;
6231 6231 crhold(cred);
6232 6232 mutex_exit(&rp->r_statelock);
6233 6233 goto doitagain;
6234 6234 }
6235 6235 mutex_exit(&rp->r_statelock);
6236 6236 }
6237 6237 /*
6238 6238 * Can't do a PURGE_STALE_FH here because this
6239 6239 * can cause a deadlock. nfs3_commit can
6240 6240 * be called from nfs3_dispose which can be called
6241 6241 * indirectly via pvn_vplist_dirty. PURGE_STALE_FH
6242 6242 * can call back to pvn_vplist_dirty.
6243 6243 */
6244 6244 if (error == ESTALE) {
6245 6245 mutex_enter(&rp->r_statelock);
6246 6246 rp->r_flags |= RSTALE;
6247 6247 if (!rp->r_error)
6248 6248 rp->r_error = error;
6249 6249 mutex_exit(&rp->r_statelock);
6250 6250 PURGE_ATTRCACHE(vp);
6251 6251 } else {
6252 6252 mutex_enter(&rp->r_statelock);
6253 6253 if (!rp->r_error)
6254 6254 rp->r_error = error;
6255 6255 mutex_exit(&rp->r_statelock);
6256 6256 }
6257 6257 }
6258 6258
6259 6259 return (error);
6260 6260 }
6261 6261
6262 6262 static void
6263 6263 nfs3_set_mod(vnode_t *vp)
6264 6264 {
6265 6265 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6266 6266
6267 6267 pvn_vplist_setdirty(vp, nfs_setmod_check);
6268 6268 }
6269 6269
6270 6270 /*
6271 6271 * This routine is used to gather together a page list of the pages
6272 6272 * which are to be committed on the server. This routine must not
6273 6273 * be called if the calling thread holds any locked pages.
6274 6274 *
6275 6275 * The calling thread must have set RCOMMIT. This bit is used to
6276 6276 * serialize access to the commit structure in the rnode. As long
6277 6277 * as the thread has set RCOMMIT, then it can manipulate the commit
6278 6278 * structure without requiring any other locks.
6279 6279 */
6280 6280 static void
6281 6281 nfs3_get_commit(vnode_t *vp)
6282 6282 {
6283 6283 rnode_t *rp;
6284 6284 page_t *pp;
6285 6285 kmutex_t *vphm;
6286 6286
6287 6287 rp = VTOR(vp);
6288 6288
6289 6289 ASSERT(rp->r_flags & RCOMMIT);
6290 6290
6291 6291 vphm = page_vnode_mutex(vp);
6292 6292 mutex_enter(vphm);
6293 6293
6294 6294 /*
6295 6295 * If there are no pages associated with this vnode, then
6296 6296 * just return.
6297 6297 */
6298 6298 if ((pp = vp->v_pages) == NULL) {
6299 6299 mutex_exit(vphm);
6300 6300 return;
6301 6301 }
6302 6302
6303 6303 /*
6304 6304 * Step through all of the pages associated with this vnode
6305 6305 * looking for pages which need to be committed.
6306 6306 */
6307 6307 do {
6308 6308 /* Skip marker pages. */
6309 6309 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
6310 6310 continue;
6311 6311
6312 6312 /*
6313 6313 * If this page does not need to be committed or is
6314 6314 * modified, then just skip it.
6315 6315 */
6316 6316 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
6317 6317 continue;
6318 6318
6319 6319 /*
6320 6320 * Attempt to lock the page. If we can't, then
6321 6321 * someone else is messing with it and we will
6322 6322 * just skip it.
6323 6323 */
6324 6324 if (!page_trylock(pp, SE_EXCL))
6325 6325 continue;
6326 6326
6327 6327 /*
6328 6328 * If this page does not need to be committed or is
6329 6329 * modified, then just skip it. Recheck now that
6330 6330 * the page is locked.
6331 6331 */
6332 6332 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6333 6333 page_unlock(pp);
6334 6334 continue;
6335 6335 }
6336 6336
6337 6337 if (PP_ISFREE(pp)) {
6338 6338 cmn_err(CE_PANIC, "nfs3_get_commit: %p is free",
6339 6339 (void *)pp);
6340 6340 }
6341 6341
6342 6342 /*
6343 6343 * The page needs to be committed and we locked it.
6344 6344 * Update the base and length parameters and add it
6345 6345 * to r_pages.
6346 6346 */
6347 6347 if (rp->r_commit.c_pages == NULL) {
6348 6348 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6349 6349 rp->r_commit.c_commlen = PAGESIZE;
6350 6350 } else if (pp->p_offset < rp->r_commit.c_commbase) {
6351 6351 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
6352 6352 (offset3)pp->p_offset + rp->r_commit.c_commlen;
6353 6353 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6354 6354 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
6355 6355 <= pp->p_offset) {
6356 6356 rp->r_commit.c_commlen = (offset3)pp->p_offset -
6357 6357 rp->r_commit.c_commbase + PAGESIZE;
6358 6358 }
6359 6359 page_add(&rp->r_commit.c_pages, pp);
6360 6360 } while ((pp = pp->p_vpnext) != vp->v_pages);
6361 6361
6362 6362 mutex_exit(vphm);
6363 6363 }
6364 6364
6365 6365 /*
6366 6366 * This routine is used to gather together a page list of the pages
6367 6367 * which are to be committed on the server. This routine must not
6368 6368 * be called if the calling thread holds any locked pages.
6369 6369 *
6370 6370 * The calling thread must have set RCOMMIT. This bit is used to
6371 6371 * serialize access to the commit structure in the rnode. As long
6372 6372 * as the thread has set RCOMMIT, then it can manipulate the commit
6373 6373 * structure without requiring any other locks.
6374 6374 */
6375 6375 static void
6376 6376 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
6377 6377 {
6378 6378
6379 6379 rnode_t *rp;
6380 6380 page_t *pp;
6381 6381 u_offset_t end;
6382 6382 u_offset_t off;
6383 6383
6384 6384 ASSERT(len != 0);
6385 6385
6386 6386 rp = VTOR(vp);
6387 6387
6388 6388 ASSERT(rp->r_flags & RCOMMIT);
6389 6389 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6390 6390
6391 6391 /*
6392 6392 * If there are no pages associated with this vnode, then
6393 6393 * just return.
6394 6394 */
6395 6395 if ((pp = vp->v_pages) == NULL)
6396 6396 return;
6397 6397
6398 6398 /*
6399 6399 * Calculate the ending offset.
6400 6400 */
6401 6401 end = soff + len;
6402 6402
6403 6403 for (off = soff; off < end; off += PAGESIZE) {
6404 6404 /*
6405 6405 * Lookup each page by vp, offset.
6406 6406 */
6407 6407 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
6408 6408 continue;
6409 6409
6410 6410 /*
6411 6411 * If this page does not need to be committed or is
6412 6412 * modified, then just skip it.
6413 6413 */
6414 6414 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6415 6415 page_unlock(pp);
6416 6416 continue;
6417 6417 }
6418 6418
6419 6419 ASSERT(PP_ISFREE(pp) == 0);
6420 6420
6421 6421 /*
6422 6422 * The page needs to be committed and we locked it.
6423 6423 * Update the base and length parameters and add it
6424 6424 * to r_pages.
6425 6425 */
6426 6426 if (rp->r_commit.c_pages == NULL) {
6427 6427 rp->r_commit.c_commbase = (offset3)pp->p_offset;
6428 6428 rp->r_commit.c_commlen = PAGESIZE;
6429 6429 } else {
6430 6430 rp->r_commit.c_commlen = (offset3)pp->p_offset -
6431 6431 rp->r_commit.c_commbase + PAGESIZE;
6432 6432 }
6433 6433 page_add(&rp->r_commit.c_pages, pp);
6434 6434 }
6435 6435 }
6436 6436
6437 6437 static int
6438 6438 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
6439 6439 {
6440 6440 int error;
6441 6441 writeverf3 write_verf;
6442 6442 rnode_t *rp = VTOR(vp);
6443 6443
6444 6444 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6445 6445 /*
6446 6446 * Flush the data portion of the file and then commit any
6447 6447 * portions which need to be committed. This may need to
6448 6448 * be done twice if the server has changed state since
6449 6449 * data was last written. The data will need to be
6450 6450 * rewritten to the server and then a new commit done.
6451 6451 *
6452 6452 * In fact, this may need to be done several times if the
6453 6453 * server is having problems and crashing while we are
6454 6454 * attempting to do this.
6455 6455 */
6456 6456
6457 6457 top:
6458 6458 /*
6459 6459 * Do a flush based on the poff and plen arguments. This
6460 6460 * will asynchronously write out any modified pages in the
6461 6461 * range specified by (poff, plen). This starts all of the
6462 6462 * i/o operations which will be waited for in the next
6463 6463 * call to nfs3_putpage
6464 6464 */
6465 6465
6466 6466 mutex_enter(&rp->r_statelock);
6467 6467 write_verf = rp->r_verf;
6468 6468 mutex_exit(&rp->r_statelock);
6469 6469
6470 6470 error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
6471 6471 if (error == EAGAIN)
6472 6472 error = 0;
6473 6473
6474 6474 /*
6475 6475 * Do a flush based on the poff and plen arguments. This
6476 6476 * will synchronously write out any modified pages in the
6477 6477 * range specified by (poff, plen) and wait until all of
6478 6478 * the asynchronous i/o's in that range are done as well.
6479 6479 */
6480 6480 if (!error)
6481 6481 error = nfs3_putpage(vp, poff, plen, 0, cr, NULL);
6482 6482
6483 6483 if (error)
6484 6484 return (error);
6485 6485
6486 6486 mutex_enter(&rp->r_statelock);
6487 6487 if (rp->r_verf != write_verf) {
6488 6488 mutex_exit(&rp->r_statelock);
6489 6489 goto top;
6490 6490 }
6491 6491 mutex_exit(&rp->r_statelock);
6492 6492
6493 6493 /*
6494 6494 * Now commit any pages which might need to be committed.
6495 6495 * If the error, NFS_VERF_MISMATCH, is returned, then
6496 6496 * start over with the flush operation.
6497 6497 */
6498 6498
6499 6499 error = nfs3_commit_vp(vp, poff, plen, cr);
6500 6500
6501 6501 if (error == NFS_VERF_MISMATCH)
6502 6502 goto top;
6503 6503
6504 6504 return (error);
6505 6505 }
6506 6506
6507 6507 static int
6508 6508 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr)
6509 6509 {
6510 6510 rnode_t *rp;
6511 6511 page_t *plist;
6512 6512 offset3 offset;
6513 6513 count3 len;
6514 6514
6515 6515
6516 6516 rp = VTOR(vp);
6517 6517
6518 6518 if (nfs_zone() != VTOMI(vp)->mi_zone)
6519 6519 return (EIO);
6520 6520 /*
6521 6521 * Set the `commit inprogress' state bit. We must
6522 6522 * first wait until any current one finishes.
6523 6523 */
6524 6524 mutex_enter(&rp->r_statelock);
6525 6525 while (rp->r_flags & RCOMMIT) {
6526 6526 rp->r_flags |= RCOMMITWAIT;
6527 6527 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6528 6528 rp->r_flags &= ~RCOMMITWAIT;
6529 6529 }
6530 6530 rp->r_flags |= RCOMMIT;
6531 6531 mutex_exit(&rp->r_statelock);
6532 6532
6533 6533 /*
6534 6534 * Gather together all of the pages which need to be
6535 6535 * committed.
6536 6536 */
6537 6537 if (plen == 0)
6538 6538 nfs3_get_commit(vp);
6539 6539 else
6540 6540 nfs3_get_commit_range(vp, poff, plen);
6541 6541
6542 6542 /*
6543 6543 * Clear the `commit inprogress' bit and disconnect the
6544 6544 * page list which was gathered together in nfs3_get_commit.
6545 6545 */
6546 6546 plist = rp->r_commit.c_pages;
6547 6547 rp->r_commit.c_pages = NULL;
6548 6548 offset = rp->r_commit.c_commbase;
6549 6549 len = rp->r_commit.c_commlen;
6550 6550 mutex_enter(&rp->r_statelock);
6551 6551 rp->r_flags &= ~RCOMMIT;
6552 6552 cv_broadcast(&rp->r_commit.c_cv);
6553 6553 mutex_exit(&rp->r_statelock);
6554 6554
6555 6555 /*
6556 6556 * If any pages need to be committed, commit them and
6557 6557 * then unlock them so that they can be freed some
6558 6558 * time later.
6559 6559 */
6560 6560 if (plist != NULL) {
6561 6561 /*
6562 6562 * No error occurred during the flush portion
6563 6563 * of this operation, so now attempt to commit
6564 6564 * the data to stable storage on the server.
6565 6565 *
6566 6566 * This will unlock all of the pages on the list.
6567 6567 */
6568 6568 return (nfs3_sync_commit(vp, plist, offset, len, cr));
6569 6569 }
6570 6570 return (0);
6571 6571 }
6572 6572
6573 6573 static int
6574 6574 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6575 6575 cred_t *cr)
6576 6576 {
6577 6577 int error;
6578 6578 page_t *pp;
6579 6579
6580 6580 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6581 6581 error = nfs3_commit(vp, offset, count, cr);
6582 6582
6583 6583 /*
6584 6584 * If we got an error, then just unlock all of the pages
6585 6585 * on the list.
6586 6586 */
6587 6587 if (error) {
6588 6588 while (plist != NULL) {
6589 6589 pp = plist;
6590 6590 page_sub(&plist, pp);
6591 6591 page_unlock(pp);
6592 6592 }
6593 6593 return (error);
6594 6594 }
6595 6595 /*
6596 6596 * We've tried as hard as we can to commit the data to stable
6597 6597 * storage on the server. We just unlock the pages and clear
6598 6598 * the commit required state. They will get freed later.
6599 6599 */
6600 6600 while (plist != NULL) {
6601 6601 pp = plist;
6602 6602 page_sub(&plist, pp);
6603 6603 pp->p_fsdata = C_NOCOMMIT;
6604 6604 page_unlock(pp);
6605 6605 }
6606 6606
6607 6607 return (error);
6608 6608 }
6609 6609
6610 6610 static void
6611 6611 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6612 6612 cred_t *cr)
6613 6613 {
6614 6614 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6615 6615 (void) nfs3_sync_commit(vp, plist, offset, count, cr);
6616 6616 }
6617 6617
6618 6618 /* ARGSUSED */
6619 6619 static int
6620 6620 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6621 6621 caller_context_t *ct)
6622 6622 {
6623 6623 int error;
6624 6624 mntinfo_t *mi;
6625 6625
6626 6626 mi = VTOMI(vp);
6627 6627
6628 6628 if (nfs_zone() != mi->mi_zone)
6629 6629 return (EIO);
6630 6630
6631 6631 if (mi->mi_flags & MI_ACL) {
6632 6632 error = acl_setacl3(vp, vsecattr, flag, cr);
6633 6633 if (mi->mi_flags & MI_ACL)
6634 6634 return (error);
6635 6635 }
6636 6636
6637 6637 return (ENOSYS);
6638 6638 }
6639 6639
6640 6640 /* ARGSUSED */
6641 6641 static int
6642 6642 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6643 6643 caller_context_t *ct)
6644 6644 {
6645 6645 int error;
6646 6646 mntinfo_t *mi;
6647 6647
6648 6648 mi = VTOMI(vp);
6649 6649
6650 6650 if (nfs_zone() != mi->mi_zone)
6651 6651 return (EIO);
6652 6652
6653 6653 if (mi->mi_flags & MI_ACL) {
6654 6654 error = acl_getacl3(vp, vsecattr, flag, cr);
6655 6655 if (mi->mi_flags & MI_ACL)
6656 6656 return (error);
6657 6657 }
6658 6658
6659 6659 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
6660 6660 }
6661 6661
6662 6662 /* ARGSUSED */
6663 6663 static int
6664 6664 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
6665 6665 caller_context_t *ct)
6666 6666 {
6667 6667 int error;
6668 6668 struct shrlock nshr;
6669 6669 struct nfs_owner nfs_owner;
6670 6670 netobj lm_fh3;
6671 6671
6672 6672 if (nfs_zone() != VTOMI(vp)->mi_zone)
6673 6673 return (EIO);
6674 6674
6675 6675 /*
6676 6676 * check for valid cmd parameter
6677 6677 */
6678 6678 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
6679 6679 return (EINVAL);
6680 6680
6681 6681 /*
6682 6682 * Check access permissions
6683 6683 */
6684 6684 if (cmd == F_SHARE &&
6685 6685 (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
6686 6686 ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
6687 6687 return (EBADF);
6688 6688
6689 6689 /*
6690 6690 * If the filesystem is mounted using local locking, pass the
6691 6691 * request off to the local share code.
6692 6692 */
6693 6693 if (VTOMI(vp)->mi_flags & MI_LLOCK)
6694 6694 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
6695 6695
6696 6696 switch (cmd) {
6697 6697 case F_SHARE:
6698 6698 case F_UNSHARE:
6699 6699 lm_fh3.n_len = VTOFH3(vp)->fh3_length;
6700 6700 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
6701 6701
6702 6702 /*
6703 6703 * If passed an owner that is too large to fit in an
6704 6704 * nfs_owner it is likely a recursive call from the
6705 6705 * lock manager client and pass it straight through. If
6706 6706 * it is not a nfs_owner then simply return an error.
6707 6707 */
6708 6708 if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
6709 6709 if (((struct nfs_owner *)shr->s_owner)->magic !=
6710 6710 NFS_OWNER_MAGIC)
6711 6711 return (EINVAL);
6712 6712
6713 6713 if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) {
6714 6714 error = set_errno(error);
6715 6715 }
6716 6716 return (error);
6717 6717 }
6718 6718 /*
6719 6719 * Remote share reservations owner is a combination of
6720 6720 * a magic number, hostname, and the local owner
6721 6721 */
6722 6722 bzero(&nfs_owner, sizeof (nfs_owner));
6723 6723 nfs_owner.magic = NFS_OWNER_MAGIC;
6724 6724 (void) strncpy(nfs_owner.hname, uts_nodename(),
6725 6725 sizeof (nfs_owner.hname));
6726 6726 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
6727 6727 nshr.s_access = shr->s_access;
6728 6728 nshr.s_deny = shr->s_deny;
6729 6729 nshr.s_sysid = 0;
6730 6730 nshr.s_pid = ttoproc(curthread)->p_pid;
6731 6731 nshr.s_own_len = sizeof (nfs_owner);
6732 6732 nshr.s_owner = (caddr_t)&nfs_owner;
6733 6733
6734 6734 if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) {
6735 6735 error = set_errno(error);
6736 6736 }
6737 6737
6738 6738 break;
6739 6739
6740 6740 case F_HASREMOTELOCKS:
6741 6741 /*
6742 6742 * NFS client can't store remote locks itself
6743 6743 */
6744 6744 shr->s_access = 0;
6745 6745 error = 0;
6746 6746 break;
6747 6747
6748 6748 default:
6749 6749 error = EINVAL;
6750 6750 break;
6751 6751 }
6752 6752
6753 6753 return (error);
6754 6754 }
↓ open down ↓ |
1419 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX