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