Print this page
3484 enhance and document tail follow support
Reviewed by: Joshua M. Clulow <jmc@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs_vnops.c
+++ new/usr/src/uts/common/fs/nfs/nfs_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]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 *
24 24 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
25 25 * All rights reserved.
26 26 */
27 27
28 +/*
29 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
30 + */
31 +
28 32 #include <sys/param.h>
29 33 #include <sys/types.h>
30 34 #include <sys/systm.h>
31 35 #include <sys/cred.h>
32 36 #include <sys/time.h>
33 37 #include <sys/vnode.h>
34 38 #include <sys/vfs.h>
35 39 #include <sys/vfs_opreg.h>
36 40 #include <sys/file.h>
37 41 #include <sys/filio.h>
38 42 #include <sys/uio.h>
39 43 #include <sys/buf.h>
40 44 #include <sys/mman.h>
41 45 #include <sys/pathname.h>
42 46 #include <sys/dirent.h>
43 47 #include <sys/debug.h>
44 48 #include <sys/vmsystm.h>
45 49 #include <sys/fcntl.h>
46 50 #include <sys/flock.h>
47 51 #include <sys/swap.h>
48 52 #include <sys/errno.h>
49 53 #include <sys/strsubr.h>
50 54 #include <sys/sysmacros.h>
51 55 #include <sys/kmem.h>
52 56 #include <sys/cmn_err.h>
53 57 #include <sys/pathconf.h>
54 58 #include <sys/utsname.h>
55 59 #include <sys/dnlc.h>
56 60 #include <sys/acl.h>
57 61 #include <sys/atomic.h>
58 62 #include <sys/policy.h>
59 63 #include <sys/sdt.h>
60 64
61 65 #include <rpc/types.h>
62 66 #include <rpc/auth.h>
63 67 #include <rpc/clnt.h>
64 68
65 69 #include <nfs/nfs.h>
66 70 #include <nfs/nfs_clnt.h>
67 71 #include <nfs/rnode.h>
68 72 #include <nfs/nfs_acl.h>
69 73 #include <nfs/lm.h>
70 74
71 75 #include <vm/hat.h>
72 76 #include <vm/as.h>
73 77 #include <vm/page.h>
74 78 #include <vm/pvn.h>
75 79 #include <vm/seg.h>
76 80 #include <vm/seg_map.h>
77 81 #include <vm/seg_kpm.h>
78 82 #include <vm/seg_vn.h>
79 83
80 84 #include <fs/fs_subr.h>
81 85
82 86 #include <sys/ddi.h>
83 87
84 88 static int nfs_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
85 89 cred_t *);
86 90 static int nfswrite(vnode_t *, caddr_t, uint_t, int, cred_t *);
87 91 static int nfsread(vnode_t *, caddr_t, uint_t, int, size_t *, cred_t *);
88 92 static int nfssetattr(vnode_t *, struct vattr *, int, cred_t *);
89 93 static int nfslookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
90 94 static int nfslookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
91 95 static int nfsrename(vnode_t *, char *, vnode_t *, char *, cred_t *,
92 96 caller_context_t *);
93 97 static int nfsreaddir(vnode_t *, rddir_cache *, cred_t *);
94 98 static int nfs_bio(struct buf *, cred_t *);
95 99 static int nfs_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
96 100 page_t *[], size_t, struct seg *, caddr_t,
97 101 enum seg_rw, cred_t *);
98 102 static void nfs_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
99 103 cred_t *);
100 104 static int nfs_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
101 105 int, cred_t *);
102 106 static int nfs_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
103 107 int, cred_t *);
104 108 static void nfs_delmap_callback(struct as *, void *, uint_t);
105 109
106 110 /*
107 111 * Error flags used to pass information about certain special errors
108 112 * which need to be handled specially.
109 113 */
110 114 #define NFS_EOF -98
111 115
112 116 /*
113 117 * These are the vnode ops routines which implement the vnode interface to
114 118 * the networked file system. These routines just take their parameters,
115 119 * make them look networkish by putting the right info into interface structs,
116 120 * and then calling the appropriate remote routine(s) to do the work.
117 121 *
118 122 * Note on directory name lookup cacheing: If we detect a stale fhandle,
119 123 * we purge the directory cache relative to that vnode. This way, the
120 124 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for
121 125 * more details on rnode locking.
122 126 */
123 127
124 128 static int nfs_open(vnode_t **, int, cred_t *, caller_context_t *);
125 129 static int nfs_close(vnode_t *, int, int, offset_t, cred_t *,
126 130 caller_context_t *);
127 131 static int nfs_read(vnode_t *, struct uio *, int, cred_t *,
128 132 caller_context_t *);
129 133 static int nfs_write(vnode_t *, struct uio *, int, cred_t *,
130 134 caller_context_t *);
131 135 static int nfs_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
132 136 caller_context_t *);
133 137 static int nfs_getattr(vnode_t *, struct vattr *, int, cred_t *,
134 138 caller_context_t *);
135 139 static int nfs_setattr(vnode_t *, struct vattr *, int, cred_t *,
136 140 caller_context_t *);
137 141 static int nfs_access(vnode_t *, int, int, cred_t *, caller_context_t *);
138 142 static int nfs_accessx(void *, int, cred_t *);
139 143 static int nfs_readlink(vnode_t *, struct uio *, cred_t *,
140 144 caller_context_t *);
141 145 static int nfs_fsync(vnode_t *, int, cred_t *, caller_context_t *);
142 146 static void nfs_inactive(vnode_t *, cred_t *, caller_context_t *);
143 147 static int nfs_lookup(vnode_t *, char *, vnode_t **, struct pathname *,
144 148 int, vnode_t *, cred_t *, caller_context_t *,
145 149 int *, pathname_t *);
146 150 static int nfs_create(vnode_t *, char *, struct vattr *, enum vcexcl,
147 151 int, vnode_t **, cred_t *, int, caller_context_t *,
148 152 vsecattr_t *);
149 153 static int nfs_remove(vnode_t *, char *, cred_t *, caller_context_t *,
150 154 int);
151 155 static int nfs_link(vnode_t *, vnode_t *, char *, cred_t *,
152 156 caller_context_t *, int);
153 157 static int nfs_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
154 158 caller_context_t *, int);
155 159 static int nfs_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
156 160 cred_t *, caller_context_t *, int, vsecattr_t *);
157 161 static int nfs_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
158 162 caller_context_t *, int);
159 163 static int nfs_symlink(vnode_t *, char *, struct vattr *, char *,
160 164 cred_t *, caller_context_t *, int);
161 165 static int nfs_readdir(vnode_t *, struct uio *, cred_t *, int *,
162 166 caller_context_t *, int);
163 167 static int nfs_fid(vnode_t *, fid_t *, caller_context_t *);
164 168 static int nfs_rwlock(vnode_t *, int, caller_context_t *);
165 169 static void nfs_rwunlock(vnode_t *, int, caller_context_t *);
166 170 static int nfs_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
167 171 static int nfs_getpage(vnode_t *, offset_t, size_t, uint_t *,
168 172 page_t *[], size_t, struct seg *, caddr_t,
169 173 enum seg_rw, cred_t *, caller_context_t *);
170 174 static int nfs_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
171 175 caller_context_t *);
172 176 static int nfs_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
173 177 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
174 178 static int nfs_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
175 179 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
176 180 static int nfs_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
177 181 struct flk_callback *, cred_t *, caller_context_t *);
178 182 static int nfs_space(vnode_t *, int, struct flock64 *, int, offset_t,
179 183 cred_t *, caller_context_t *);
180 184 static int nfs_realvp(vnode_t *, vnode_t **, caller_context_t *);
181 185 static int nfs_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
182 186 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
183 187 static int nfs_pathconf(vnode_t *, int, ulong_t *, cred_t *,
184 188 caller_context_t *);
185 189 static int nfs_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
186 190 cred_t *, caller_context_t *);
187 191 static int nfs_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
188 192 caller_context_t *);
189 193 static int nfs_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
190 194 caller_context_t *);
191 195 static int nfs_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
192 196 caller_context_t *);
193 197
194 198 struct vnodeops *nfs_vnodeops;
195 199
196 200 const fs_operation_def_t nfs_vnodeops_template[] = {
197 201 VOPNAME_OPEN, { .vop_open = nfs_open },
198 202 VOPNAME_CLOSE, { .vop_close = nfs_close },
199 203 VOPNAME_READ, { .vop_read = nfs_read },
200 204 VOPNAME_WRITE, { .vop_write = nfs_write },
201 205 VOPNAME_IOCTL, { .vop_ioctl = nfs_ioctl },
202 206 VOPNAME_GETATTR, { .vop_getattr = nfs_getattr },
203 207 VOPNAME_SETATTR, { .vop_setattr = nfs_setattr },
204 208 VOPNAME_ACCESS, { .vop_access = nfs_access },
205 209 VOPNAME_LOOKUP, { .vop_lookup = nfs_lookup },
206 210 VOPNAME_CREATE, { .vop_create = nfs_create },
207 211 VOPNAME_REMOVE, { .vop_remove = nfs_remove },
208 212 VOPNAME_LINK, { .vop_link = nfs_link },
209 213 VOPNAME_RENAME, { .vop_rename = nfs_rename },
210 214 VOPNAME_MKDIR, { .vop_mkdir = nfs_mkdir },
211 215 VOPNAME_RMDIR, { .vop_rmdir = nfs_rmdir },
212 216 VOPNAME_READDIR, { .vop_readdir = nfs_readdir },
213 217 VOPNAME_SYMLINK, { .vop_symlink = nfs_symlink },
214 218 VOPNAME_READLINK, { .vop_readlink = nfs_readlink },
215 219 VOPNAME_FSYNC, { .vop_fsync = nfs_fsync },
216 220 VOPNAME_INACTIVE, { .vop_inactive = nfs_inactive },
217 221 VOPNAME_FID, { .vop_fid = nfs_fid },
218 222 VOPNAME_RWLOCK, { .vop_rwlock = nfs_rwlock },
219 223 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs_rwunlock },
220 224 VOPNAME_SEEK, { .vop_seek = nfs_seek },
221 225 VOPNAME_FRLOCK, { .vop_frlock = nfs_frlock },
222 226 VOPNAME_SPACE, { .vop_space = nfs_space },
223 227 VOPNAME_REALVP, { .vop_realvp = nfs_realvp },
224 228 VOPNAME_GETPAGE, { .vop_getpage = nfs_getpage },
225 229 VOPNAME_PUTPAGE, { .vop_putpage = nfs_putpage },
226 230 VOPNAME_MAP, { .vop_map = nfs_map },
227 231 VOPNAME_ADDMAP, { .vop_addmap = nfs_addmap },
228 232 VOPNAME_DELMAP, { .vop_delmap = nfs_delmap },
229 233 VOPNAME_DUMP, { .vop_dump = nfs_dump },
230 234 VOPNAME_PATHCONF, { .vop_pathconf = nfs_pathconf },
231 235 VOPNAME_PAGEIO, { .vop_pageio = nfs_pageio },
232 236 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs_setsecattr },
233 237 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs_getsecattr },
234 238 VOPNAME_SHRLOCK, { .vop_shrlock = nfs_shrlock },
235 239 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
236 240 NULL, NULL
237 241 };
238 242
239 243 /*
240 244 * XXX: This is referenced in modstubs.s
241 245 */
242 246 struct vnodeops *
243 247 nfs_getvnodeops(void)
244 248 {
245 249 return (nfs_vnodeops);
246 250 }
247 251
248 252 /* ARGSUSED */
249 253 static int
250 254 nfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
251 255 {
252 256 int error;
253 257 struct vattr va;
254 258 rnode_t *rp;
255 259 vnode_t *vp;
256 260
257 261 vp = *vpp;
258 262 rp = VTOR(vp);
259 263 if (nfs_zone() != VTOMI(vp)->mi_zone)
260 264 return (EIO);
261 265 mutex_enter(&rp->r_statelock);
262 266 if (rp->r_cred == NULL) {
263 267 crhold(cr);
264 268 rp->r_cred = cr;
265 269 }
266 270 mutex_exit(&rp->r_statelock);
267 271
268 272 /*
269 273 * If there is no cached data or if close-to-open
270 274 * consistency checking is turned off, we can avoid
271 275 * the over the wire getattr. Otherwise, if the
272 276 * file system is mounted readonly, then just verify
273 277 * the caches are up to date using the normal mechanism.
274 278 * Else, if the file is not mmap'd, then just mark
275 279 * the attributes as timed out. They will be refreshed
276 280 * and the caches validated prior to being used.
277 281 * Else, the file system is mounted writeable so
278 282 * force an over the wire GETATTR in order to ensure
279 283 * that all cached data is valid.
280 284 */
281 285 if (vp->v_count > 1 ||
282 286 ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
283 287 !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
284 288 if (vn_is_readonly(vp))
285 289 error = nfs_validate_caches(vp, cr);
286 290 else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
287 291 PURGE_ATTRCACHE(vp);
288 292 error = 0;
289 293 } else {
290 294 va.va_mask = AT_ALL;
291 295 error = nfs_getattr_otw(vp, &va, cr);
292 296 }
293 297 } else
294 298 error = 0;
295 299
296 300 return (error);
297 301 }
298 302
299 303 /* ARGSUSED */
300 304 static int
301 305 nfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
302 306 caller_context_t *ct)
303 307 {
304 308 rnode_t *rp;
305 309 int error;
306 310 struct vattr va;
307 311
308 312 /*
309 313 * zone_enter(2) prevents processes from changing zones with NFS files
310 314 * open; if we happen to get here from the wrong zone we can't do
311 315 * anything over the wire.
312 316 */
313 317 if (VTOMI(vp)->mi_zone != nfs_zone()) {
314 318 /*
315 319 * We could attempt to clean up locks, except we're sure
316 320 * that the current process didn't acquire any locks on
317 321 * the file: any attempt to lock a file belong to another zone
318 322 * will fail, and one can't lock an NFS file and then change
319 323 * zones, as that fails too.
320 324 *
321 325 * Returning an error here is the sane thing to do. A
322 326 * subsequent call to VN_RELE() which translates to a
323 327 * nfs_inactive() will clean up state: if the zone of the
324 328 * vnode's origin is still alive and kicking, an async worker
325 329 * thread will handle the request (from the correct zone), and
326 330 * everything (minus the final nfs_getattr_otw() call) should
327 331 * be OK. If the zone is going away nfs_async_inactive() will
328 332 * throw away cached pages inline.
329 333 */
330 334 return (EIO);
331 335 }
332 336
333 337 /*
334 338 * If we are using local locking for this filesystem, then
335 339 * release all of the SYSV style record locks. Otherwise,
336 340 * we are doing network locking and we need to release all
337 341 * of the network locks. All of the locks held by this
338 342 * process on this file are released no matter what the
339 343 * incoming reference count is.
340 344 */
341 345 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
342 346 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
343 347 cleanshares(vp, ttoproc(curthread)->p_pid);
344 348 } else
345 349 nfs_lockrelease(vp, flag, offset, cr);
346 350
347 351 if (count > 1)
348 352 return (0);
349 353
350 354 /*
351 355 * If the file has been `unlinked', then purge the
352 356 * DNLC so that this vnode will get reycled quicker
353 357 * and the .nfs* file on the server will get removed.
354 358 */
355 359 rp = VTOR(vp);
356 360 if (rp->r_unldvp != NULL)
357 361 dnlc_purge_vp(vp);
358 362
359 363 /*
360 364 * If the file was open for write and there are pages,
361 365 * then if the file system was mounted using the "no-close-
362 366 * to-open" semantics, then start an asynchronous flush
363 367 * of the all of the pages in the file.
364 368 * else the file system was not mounted using the "no-close-
365 369 * to-open" semantics, then do a synchronous flush and
366 370 * commit of all of the dirty and uncommitted pages.
367 371 *
368 372 * The asynchronous flush of the pages in the "nocto" path
369 373 * mostly just associates a cred pointer with the rnode so
370 374 * writes which happen later will have a better chance of
371 375 * working. It also starts the data being written to the
372 376 * server, but without unnecessarily delaying the application.
373 377 */
374 378 if ((flag & FWRITE) && vn_has_cached_data(vp)) {
375 379 if ((VTOMI(vp)->mi_flags & MI_NOCTO)) {
376 380 error = nfs_putpage(vp, (offset_t)0, 0, B_ASYNC,
377 381 cr, ct);
378 382 if (error == EAGAIN)
379 383 error = 0;
380 384 } else
381 385 error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
382 386 if (!error) {
383 387 mutex_enter(&rp->r_statelock);
384 388 error = rp->r_error;
385 389 rp->r_error = 0;
386 390 mutex_exit(&rp->r_statelock);
387 391 }
388 392 } else {
389 393 mutex_enter(&rp->r_statelock);
390 394 error = rp->r_error;
391 395 rp->r_error = 0;
392 396 mutex_exit(&rp->r_statelock);
393 397 }
394 398
395 399 /*
396 400 * If RWRITEATTR is set, then issue an over the wire GETATTR to
397 401 * refresh the attribute cache with a set of attributes which
398 402 * weren't returned from a WRITE. This will enable the close-
399 403 * to-open processing to work.
400 404 */
401 405 if (rp->r_flags & RWRITEATTR)
402 406 (void) nfs_getattr_otw(vp, &va, cr);
403 407
404 408 return (error);
405 409 }
406 410
407 411 /* ARGSUSED */
408 412 static int
409 413 nfs_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
410 414 caller_context_t *ct)
411 415 {
412 416 rnode_t *rp;
413 417 u_offset_t off;
414 418 offset_t diff;
415 419 int on;
416 420 size_t n;
417 421 caddr_t base;
418 422 uint_t flags;
419 423 int error;
420 424 mntinfo_t *mi;
421 425
422 426 rp = VTOR(vp);
423 427 mi = VTOMI(vp);
424 428
425 429 if (nfs_zone() != mi->mi_zone)
426 430 return (EIO);
427 431
428 432 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
429 433
430 434 if (vp->v_type != VREG)
431 435 return (EISDIR);
432 436
433 437 if (uiop->uio_resid == 0)
434 438 return (0);
435 439
436 440 if (uiop->uio_loffset > MAXOFF32_T)
437 441 return (EFBIG);
438 442
439 443 if (uiop->uio_loffset < 0 ||
440 444 uiop->uio_loffset + uiop->uio_resid > MAXOFF32_T)
441 445 return (EINVAL);
442 446
443 447 /*
444 448 * Bypass VM if caching has been disabled (e.g., locking) or if
445 449 * using client-side direct I/O and the file is not mmap'd and
446 450 * there are no cached pages.
447 451 */
448 452 if ((vp->v_flag & VNOCACHE) ||
449 453 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
450 454 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
451 455 !vn_has_cached_data(vp))) {
452 456 size_t bufsize;
453 457 size_t resid = 0;
454 458
455 459 /*
456 460 * Let's try to do read in as large a chunk as we can
457 461 * (Filesystem (NFS client) bsize if possible/needed).
458 462 * For V3, this is 32K and for V2, this is 8K.
459 463 */
460 464 bufsize = MIN(uiop->uio_resid, VTOMI(vp)->mi_curread);
461 465 base = kmem_alloc(bufsize, KM_SLEEP);
462 466 do {
463 467 n = MIN(uiop->uio_resid, bufsize);
464 468 error = nfsread(vp, base, uiop->uio_offset, n,
465 469 &resid, cr);
466 470 if (!error) {
467 471 n -= resid;
468 472 error = uiomove(base, n, UIO_READ, uiop);
469 473 }
470 474 } while (!error && uiop->uio_resid > 0 && n > 0);
471 475 kmem_free(base, bufsize);
472 476 return (error);
473 477 }
474 478
475 479 error = 0;
476 480
477 481 do {
478 482 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
479 483 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
480 484 n = MIN(MAXBSIZE - on, uiop->uio_resid);
481 485
482 486 error = nfs_validate_caches(vp, cr);
483 487 if (error)
484 488 break;
485 489
486 490 mutex_enter(&rp->r_statelock);
487 491 while (rp->r_flags & RINCACHEPURGE) {
488 492 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
489 493 mutex_exit(&rp->r_statelock);
490 494 return (EINTR);
491 495 }
492 496 }
493 497 diff = rp->r_size - uiop->uio_loffset;
494 498 mutex_exit(&rp->r_statelock);
495 499 if (diff <= 0)
496 500 break;
497 501 if (diff < n)
498 502 n = (size_t)diff;
499 503
500 504 if (vpm_enable) {
501 505 /*
502 506 * Copy data.
503 507 */
504 508 error = vpm_data_copy(vp, off + on, n, uiop,
505 509 1, NULL, 0, S_READ);
506 510 } else {
507 511 base = segmap_getmapflt(segkmap, vp, off + on, n,
508 512 1, S_READ);
509 513 error = uiomove(base + on, n, UIO_READ, uiop);
510 514 }
511 515
512 516 if (!error) {
513 517 /*
514 518 * If read a whole block or read to eof,
515 519 * won't need this buffer again soon.
516 520 */
517 521 mutex_enter(&rp->r_statelock);
518 522 if (n + on == MAXBSIZE ||
519 523 uiop->uio_loffset == rp->r_size)
520 524 flags = SM_DONTNEED;
521 525 else
522 526 flags = 0;
523 527 mutex_exit(&rp->r_statelock);
524 528 if (vpm_enable) {
525 529 error = vpm_sync_pages(vp, off, n, flags);
526 530 } else {
527 531 error = segmap_release(segkmap, base, flags);
528 532 }
529 533 } else {
530 534 if (vpm_enable) {
531 535 (void) vpm_sync_pages(vp, off, n, 0);
532 536 } else {
533 537 (void) segmap_release(segkmap, base, 0);
534 538 }
535 539 }
536 540 } while (!error && uiop->uio_resid > 0);
537 541
538 542 return (error);
539 543 }
540 544
541 545 /* ARGSUSED */
542 546 static int
543 547 nfs_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
544 548 caller_context_t *ct)
545 549 {
546 550 rnode_t *rp;
547 551 u_offset_t off;
548 552 caddr_t base;
549 553 uint_t flags;
550 554 int remainder;
551 555 size_t n;
552 556 int on;
553 557 int error;
554 558 int resid;
555 559 offset_t offset;
556 560 rlim_t limit;
557 561 mntinfo_t *mi;
558 562
559 563 rp = VTOR(vp);
560 564
561 565 mi = VTOMI(vp);
562 566 if (nfs_zone() != mi->mi_zone)
563 567 return (EIO);
564 568 if (vp->v_type != VREG)
565 569 return (EISDIR);
566 570
567 571 if (uiop->uio_resid == 0)
568 572 return (0);
569 573
570 574 if (ioflag & FAPPEND) {
571 575 struct vattr va;
572 576
573 577 /*
574 578 * Must serialize if appending.
575 579 */
576 580 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
577 581 nfs_rw_exit(&rp->r_rwlock);
578 582 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
579 583 INTR(vp)))
580 584 return (EINTR);
581 585 }
582 586
583 587 va.va_mask = AT_SIZE;
584 588 error = nfsgetattr(vp, &va, cr);
585 589 if (error)
586 590 return (error);
587 591 uiop->uio_loffset = va.va_size;
588 592 }
589 593
590 594 if (uiop->uio_loffset > MAXOFF32_T)
591 595 return (EFBIG);
592 596
593 597 offset = uiop->uio_loffset + uiop->uio_resid;
594 598
595 599 if (uiop->uio_loffset < 0 || offset > MAXOFF32_T)
596 600 return (EINVAL);
597 601
598 602 if (uiop->uio_llimit > (rlim64_t)MAXOFF32_T) {
599 603 limit = MAXOFF32_T;
600 604 } else {
601 605 limit = (rlim_t)uiop->uio_llimit;
602 606 }
603 607
604 608 /*
605 609 * Check to make sure that the process will not exceed
606 610 * its limit on file size. It is okay to write up to
607 611 * the limit, but not beyond. Thus, the write which
608 612 * reaches the limit will be short and the next write
609 613 * will return an error.
610 614 */
611 615 remainder = 0;
612 616 if (offset > limit) {
613 617 remainder = offset - limit;
614 618 uiop->uio_resid = limit - uiop->uio_offset;
615 619 if (uiop->uio_resid <= 0) {
616 620 proc_t *p = ttoproc(curthread);
617 621
618 622 uiop->uio_resid += remainder;
619 623 mutex_enter(&p->p_lock);
620 624 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
621 625 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
622 626 mutex_exit(&p->p_lock);
623 627 return (EFBIG);
624 628 }
625 629 }
626 630
627 631 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
628 632 return (EINTR);
629 633
630 634 /*
631 635 * Bypass VM if caching has been disabled (e.g., locking) or if
632 636 * using client-side direct I/O and the file is not mmap'd and
633 637 * there are no cached pages.
634 638 */
635 639 if ((vp->v_flag & VNOCACHE) ||
636 640 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
637 641 rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
638 642 !vn_has_cached_data(vp))) {
639 643 size_t bufsize;
640 644 int count;
641 645 uint_t org_offset;
642 646
643 647 nfs_fwrite:
644 648 if (rp->r_flags & RSTALE) {
645 649 resid = uiop->uio_resid;
646 650 offset = uiop->uio_loffset;
647 651 error = rp->r_error;
648 652 /*
649 653 * A close may have cleared r_error, if so,
650 654 * propagate ESTALE error return properly
651 655 */
652 656 if (error == 0)
653 657 error = ESTALE;
654 658 goto bottom;
655 659 }
656 660 bufsize = MIN(uiop->uio_resid, mi->mi_curwrite);
657 661 base = kmem_alloc(bufsize, KM_SLEEP);
658 662 do {
659 663 resid = uiop->uio_resid;
660 664 offset = uiop->uio_loffset;
661 665 count = MIN(uiop->uio_resid, bufsize);
662 666 org_offset = uiop->uio_offset;
663 667 error = uiomove(base, count, UIO_WRITE, uiop);
664 668 if (!error) {
665 669 error = nfswrite(vp, base, org_offset,
666 670 count, cr);
667 671 }
668 672 } while (!error && uiop->uio_resid > 0);
669 673 kmem_free(base, bufsize);
670 674 goto bottom;
671 675 }
672 676
673 677 do {
674 678 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
675 679 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
676 680 n = MIN(MAXBSIZE - on, uiop->uio_resid);
677 681
678 682 resid = uiop->uio_resid;
679 683 offset = uiop->uio_loffset;
680 684
681 685 if (rp->r_flags & RSTALE) {
682 686 error = rp->r_error;
683 687 /*
684 688 * A close may have cleared r_error, if so,
685 689 * propagate ESTALE error return properly
686 690 */
687 691 if (error == 0)
688 692 error = ESTALE;
689 693 break;
690 694 }
691 695
692 696 /*
693 697 * Don't create dirty pages faster than they
694 698 * can be cleaned so that the system doesn't
695 699 * get imbalanced. If the async queue is
696 700 * maxed out, then wait for it to drain before
697 701 * creating more dirty pages. Also, wait for
698 702 * any threads doing pagewalks in the vop_getattr
699 703 * entry points so that they don't block for
700 704 * long periods.
701 705 */
702 706 mutex_enter(&rp->r_statelock);
703 707 while ((mi->mi_max_threads != 0 &&
704 708 rp->r_awcount > 2 * mi->mi_max_threads) ||
705 709 rp->r_gcount > 0) {
706 710 if (INTR(vp)) {
707 711 klwp_t *lwp = ttolwp(curthread);
708 712
709 713 if (lwp != NULL)
710 714 lwp->lwp_nostop++;
711 715 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
712 716 mutex_exit(&rp->r_statelock);
713 717 if (lwp != NULL)
714 718 lwp->lwp_nostop--;
715 719 error = EINTR;
716 720 goto bottom;
717 721 }
718 722 if (lwp != NULL)
719 723 lwp->lwp_nostop--;
720 724 } else
721 725 cv_wait(&rp->r_cv, &rp->r_statelock);
722 726 }
723 727 mutex_exit(&rp->r_statelock);
724 728
725 729 /*
726 730 * Touch the page and fault it in if it is not in core
727 731 * before segmap_getmapflt or vpm_data_copy can lock it.
728 732 * This is to avoid the deadlock if the buffer is mapped
729 733 * to the same file through mmap which we want to write.
730 734 */
731 735 uio_prefaultpages((long)n, uiop);
732 736
733 737 if (vpm_enable) {
734 738 /*
735 739 * It will use kpm mappings, so no need to
736 740 * pass an address.
737 741 */
738 742 error = writerp(rp, NULL, n, uiop, 0);
739 743 } else {
740 744 if (segmap_kpm) {
741 745 int pon = uiop->uio_loffset & PAGEOFFSET;
742 746 size_t pn = MIN(PAGESIZE - pon,
743 747 uiop->uio_resid);
744 748 int pagecreate;
745 749
746 750 mutex_enter(&rp->r_statelock);
747 751 pagecreate = (pon == 0) && (pn == PAGESIZE ||
748 752 uiop->uio_loffset + pn >= rp->r_size);
749 753 mutex_exit(&rp->r_statelock);
750 754
751 755 base = segmap_getmapflt(segkmap, vp, off + on,
752 756 pn, !pagecreate, S_WRITE);
753 757
754 758 error = writerp(rp, base + pon, n, uiop,
755 759 pagecreate);
756 760
757 761 } else {
758 762 base = segmap_getmapflt(segkmap, vp, off + on,
759 763 n, 0, S_READ);
760 764 error = writerp(rp, base + on, n, uiop, 0);
761 765 }
762 766 }
763 767
764 768 if (!error) {
765 769 if (mi->mi_flags & MI_NOAC)
766 770 flags = SM_WRITE;
767 771 else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
768 772 /*
769 773 * Have written a whole block.
770 774 * Start an asynchronous write
771 775 * and mark the buffer to
772 776 * indicate that it won't be
773 777 * needed again soon.
774 778 */
775 779 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
776 780 } else
777 781 flags = 0;
778 782 if ((ioflag & (FSYNC|FDSYNC)) ||
779 783 (rp->r_flags & ROUTOFSPACE)) {
780 784 flags &= ~SM_ASYNC;
781 785 flags |= SM_WRITE;
782 786 }
783 787 if (vpm_enable) {
784 788 error = vpm_sync_pages(vp, off, n, flags);
785 789 } else {
786 790 error = segmap_release(segkmap, base, flags);
787 791 }
788 792 } else {
789 793 if (vpm_enable) {
790 794 (void) vpm_sync_pages(vp, off, n, 0);
791 795 } else {
792 796 (void) segmap_release(segkmap, base, 0);
793 797 }
794 798 /*
795 799 * In the event that we got an access error while
796 800 * faulting in a page for a write-only file just
797 801 * force a write.
798 802 */
799 803 if (error == EACCES)
800 804 goto nfs_fwrite;
801 805 }
802 806 } while (!error && uiop->uio_resid > 0);
803 807
804 808 bottom:
805 809 if (error) {
806 810 uiop->uio_resid = resid + remainder;
807 811 uiop->uio_loffset = offset;
808 812 } else
809 813 uiop->uio_resid += remainder;
810 814
811 815 nfs_rw_exit(&rp->r_lkserlock);
812 816
813 817 return (error);
814 818 }
815 819
816 820 /*
817 821 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
818 822 */
819 823 static int
820 824 nfs_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
821 825 int flags, cred_t *cr)
822 826 {
823 827 struct buf *bp;
824 828 int error;
825 829
826 830 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
827 831 bp = pageio_setup(pp, len, vp, flags);
828 832 ASSERT(bp != NULL);
829 833
830 834 /*
831 835 * pageio_setup should have set b_addr to 0. This
832 836 * is correct since we want to do I/O on a page
833 837 * boundary. bp_mapin will use this addr to calculate
834 838 * an offset, and then set b_addr to the kernel virtual
835 839 * address it allocated for us.
836 840 */
837 841 ASSERT(bp->b_un.b_addr == 0);
838 842
839 843 bp->b_edev = 0;
840 844 bp->b_dev = 0;
841 845 bp->b_lblkno = lbtodb(off);
842 846 bp->b_file = vp;
843 847 bp->b_offset = (offset_t)off;
844 848 bp_mapin(bp);
845 849
846 850 error = nfs_bio(bp, cr);
847 851
848 852 bp_mapout(bp);
849 853 pageio_done(bp);
850 854
851 855 return (error);
852 856 }
853 857
854 858 /*
855 859 * Write to file. Writes to remote server in largest size
856 860 * chunks that the server can handle. Write is synchronous.
857 861 */
858 862 static int
859 863 nfswrite(vnode_t *vp, caddr_t base, uint_t offset, int count, cred_t *cr)
860 864 {
861 865 rnode_t *rp;
862 866 mntinfo_t *mi;
863 867 struct nfswriteargs wa;
864 868 struct nfsattrstat ns;
865 869 int error;
866 870 int tsize;
867 871 int douprintf;
868 872
869 873 douprintf = 1;
870 874
871 875 rp = VTOR(vp);
872 876 mi = VTOMI(vp);
873 877
874 878 ASSERT(nfs_zone() == mi->mi_zone);
875 879
876 880 wa.wa_args = &wa.wa_args_buf;
877 881 wa.wa_fhandle = *VTOFH(vp);
878 882
879 883 do {
880 884 tsize = MIN(mi->mi_curwrite, count);
881 885 wa.wa_data = base;
882 886 wa.wa_begoff = offset;
883 887 wa.wa_totcount = tsize;
884 888 wa.wa_count = tsize;
885 889 wa.wa_offset = offset;
886 890
887 891 if (mi->mi_io_kstats) {
888 892 mutex_enter(&mi->mi_lock);
889 893 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
890 894 mutex_exit(&mi->mi_lock);
891 895 }
892 896 wa.wa_mblk = NULL;
893 897 do {
894 898 error = rfs2call(mi, RFS_WRITE,
895 899 xdr_writeargs, (caddr_t)&wa,
896 900 xdr_attrstat, (caddr_t)&ns, cr,
897 901 &douprintf, &ns.ns_status, 0, NULL);
898 902 } while (error == ENFS_TRYAGAIN);
899 903 if (mi->mi_io_kstats) {
900 904 mutex_enter(&mi->mi_lock);
901 905 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
902 906 mutex_exit(&mi->mi_lock);
903 907 }
904 908
905 909 if (!error) {
906 910 error = geterrno(ns.ns_status);
907 911 /*
908 912 * Can't check for stale fhandle and purge caches
909 913 * here because pages are held by nfs_getpage.
910 914 * Just mark the attribute cache as timed out
911 915 * and set RWRITEATTR to indicate that the file
912 916 * was modified with a WRITE operation.
913 917 */
914 918 if (!error) {
915 919 count -= tsize;
916 920 base += tsize;
917 921 offset += tsize;
918 922 if (mi->mi_io_kstats) {
919 923 mutex_enter(&mi->mi_lock);
920 924 KSTAT_IO_PTR(mi->mi_io_kstats)->
921 925 writes++;
922 926 KSTAT_IO_PTR(mi->mi_io_kstats)->
923 927 nwritten += tsize;
924 928 mutex_exit(&mi->mi_lock);
925 929 }
926 930 lwp_stat_update(LWP_STAT_OUBLK, 1);
927 931 mutex_enter(&rp->r_statelock);
928 932 PURGE_ATTRCACHE_LOCKED(rp);
929 933 rp->r_flags |= RWRITEATTR;
930 934 mutex_exit(&rp->r_statelock);
931 935 }
932 936 }
933 937 } while (!error && count);
934 938
935 939 return (error);
936 940 }
937 941
938 942 /*
939 943 * Read from a file. Reads data in largest chunks our interface can handle.
940 944 */
941 945 static int
942 946 nfsread(vnode_t *vp, caddr_t base, uint_t offset,
943 947 int count, size_t *residp, cred_t *cr)
944 948 {
945 949 mntinfo_t *mi;
946 950 struct nfsreadargs ra;
947 951 struct nfsrdresult rr;
948 952 int tsize;
949 953 int error;
950 954 int douprintf;
951 955 failinfo_t fi;
952 956 rnode_t *rp;
953 957 struct vattr va;
954 958 hrtime_t t;
955 959
956 960 rp = VTOR(vp);
957 961 mi = VTOMI(vp);
958 962
959 963 ASSERT(nfs_zone() == mi->mi_zone);
960 964
961 965 douprintf = 1;
962 966
963 967 ra.ra_fhandle = *VTOFH(vp);
964 968
965 969 fi.vp = vp;
966 970 fi.fhp = (caddr_t)&ra.ra_fhandle;
967 971 fi.copyproc = nfscopyfh;
968 972 fi.lookupproc = nfslookup;
969 973 fi.xattrdirproc = acl_getxattrdir2;
970 974
971 975 do {
972 976 if (mi->mi_io_kstats) {
973 977 mutex_enter(&mi->mi_lock);
974 978 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
975 979 mutex_exit(&mi->mi_lock);
976 980 }
977 981
978 982 do {
979 983 tsize = MIN(mi->mi_curread, count);
980 984 rr.rr_data = base;
981 985 ra.ra_offset = offset;
982 986 ra.ra_totcount = tsize;
983 987 ra.ra_count = tsize;
984 988 ra.ra_data = base;
985 989 t = gethrtime();
986 990 error = rfs2call(mi, RFS_READ,
987 991 xdr_readargs, (caddr_t)&ra,
988 992 xdr_rdresult, (caddr_t)&rr, cr,
989 993 &douprintf, &rr.rr_status, 0, &fi);
990 994 } while (error == ENFS_TRYAGAIN);
991 995
992 996 if (mi->mi_io_kstats) {
993 997 mutex_enter(&mi->mi_lock);
994 998 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
995 999 mutex_exit(&mi->mi_lock);
996 1000 }
997 1001
998 1002 if (!error) {
999 1003 error = geterrno(rr.rr_status);
1000 1004 if (!error) {
1001 1005 count -= rr.rr_count;
1002 1006 base += rr.rr_count;
1003 1007 offset += rr.rr_count;
1004 1008 if (mi->mi_io_kstats) {
1005 1009 mutex_enter(&mi->mi_lock);
1006 1010 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1007 1011 KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
1008 1012 rr.rr_count;
1009 1013 mutex_exit(&mi->mi_lock);
1010 1014 }
1011 1015 lwp_stat_update(LWP_STAT_INBLK, 1);
1012 1016 }
1013 1017 }
1014 1018 } while (!error && count && rr.rr_count == tsize);
1015 1019
1016 1020 *residp = count;
1017 1021
1018 1022 if (!error) {
1019 1023 /*
1020 1024 * Since no error occurred, we have the current
1021 1025 * attributes and we need to do a cache check and then
1022 1026 * potentially update the cached attributes. We can't
1023 1027 * use the normal attribute check and cache mechanisms
1024 1028 * because they might cause a cache flush which would
1025 1029 * deadlock. Instead, we just check the cache to see
1026 1030 * if the attributes have changed. If it is, then we
1027 1031 * just mark the attributes as out of date. The next
1028 1032 * time that the attributes are checked, they will be
1029 1033 * out of date, new attributes will be fetched, and
1030 1034 * the page cache will be flushed. If the attributes
1031 1035 * weren't changed, then we just update the cached
1032 1036 * attributes with these attributes.
1033 1037 */
1034 1038 /*
1035 1039 * If NFS_ACL is supported on the server, then the
1036 1040 * attributes returned by server may have minimal
1037 1041 * permissions sometimes denying access to users having
1038 1042 * proper access. To get the proper attributes, mark
1039 1043 * the attributes as expired so that they will be
1040 1044 * regotten via the NFS_ACL GETATTR2 procedure.
1041 1045 */
1042 1046 error = nattr_to_vattr(vp, &rr.rr_attr, &va);
1043 1047 mutex_enter(&rp->r_statelock);
1044 1048 if (error || !CACHE_VALID(rp, va.va_mtime, va.va_size) ||
1045 1049 (mi->mi_flags & MI_ACL)) {
1046 1050 mutex_exit(&rp->r_statelock);
1047 1051 PURGE_ATTRCACHE(vp);
1048 1052 } else {
1049 1053 if (rp->r_mtime <= t) {
1050 1054 nfs_attrcache_va(vp, &va);
1051 1055 }
1052 1056 mutex_exit(&rp->r_statelock);
1053 1057 }
1054 1058 }
1055 1059
1056 1060 return (error);
1057 1061 }
1058 1062
1059 1063 /* ARGSUSED */
1060 1064 static int
1061 1065 nfs_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1062 1066 caller_context_t *ct)
1063 1067 {
1064 1068
1065 1069 if (nfs_zone() != VTOMI(vp)->mi_zone)
1066 1070 return (EIO);
1067 1071 switch (cmd) {
1068 1072 case _FIODIRECTIO:
1069 1073 return (nfs_directio(vp, (int)arg, cr));
1070 1074 default:
1071 1075 return (ENOTTY);
1072 1076 }
1073 1077 }
1074 1078
1075 1079 /* ARGSUSED */
1076 1080 static int
1077 1081 nfs_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1078 1082 caller_context_t *ct)
1079 1083 {
1080 1084 int error;
1081 1085 rnode_t *rp;
1082 1086
1083 1087 if (nfs_zone() != VTOMI(vp)->mi_zone)
1084 1088 return (EIO);
1085 1089 /*
1086 1090 * If it has been specified that the return value will
1087 1091 * just be used as a hint, and we are only being asked
1088 1092 * for size, fsid or rdevid, then return the client's
1089 1093 * notion of these values without checking to make sure
1090 1094 * that the attribute cache is up to date.
1091 1095 * The whole point is to avoid an over the wire GETATTR
1092 1096 * call.
1093 1097 */
1094 1098 rp = VTOR(vp);
1095 1099 if (flags & ATTR_HINT) {
1096 1100 if (vap->va_mask ==
1097 1101 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1098 1102 mutex_enter(&rp->r_statelock);
1099 1103 if (vap->va_mask | AT_SIZE)
1100 1104 vap->va_size = rp->r_size;
1101 1105 if (vap->va_mask | AT_FSID)
1102 1106 vap->va_fsid = rp->r_attr.va_fsid;
1103 1107 if (vap->va_mask | AT_RDEV)
1104 1108 vap->va_rdev = rp->r_attr.va_rdev;
1105 1109 mutex_exit(&rp->r_statelock);
1106 1110 return (0);
1107 1111 }
1108 1112 }
1109 1113
1110 1114 /*
1111 1115 * Only need to flush pages if asking for the mtime
1112 1116 * and if there any dirty pages or any outstanding
1113 1117 * asynchronous (write) requests for this file.
1114 1118 */
1115 1119 if (vap->va_mask & AT_MTIME) {
1116 1120 if (vn_has_cached_data(vp) &&
1117 1121 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1118 1122 mutex_enter(&rp->r_statelock);
1119 1123 rp->r_gcount++;
1120 1124 mutex_exit(&rp->r_statelock);
1121 1125 error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1122 1126 mutex_enter(&rp->r_statelock);
1123 1127 if (error && (error == ENOSPC || error == EDQUOT)) {
1124 1128 if (!rp->r_error)
1125 1129 rp->r_error = error;
1126 1130 }
1127 1131 if (--rp->r_gcount == 0)
1128 1132 cv_broadcast(&rp->r_cv);
1129 1133 mutex_exit(&rp->r_statelock);
1130 1134 }
1131 1135 }
1132 1136
1133 1137 return (nfsgetattr(vp, vap, cr));
1134 1138 }
1135 1139
1136 1140 /*ARGSUSED4*/
1137 1141 static int
1138 1142 nfs_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1139 1143 caller_context_t *ct)
1140 1144 {
1141 1145 int error;
1142 1146 uint_t mask;
1143 1147 struct vattr va;
1144 1148
1145 1149 mask = vap->va_mask;
1146 1150
1147 1151 if (mask & AT_NOSET)
1148 1152 return (EINVAL);
1149 1153
1150 1154 if ((mask & AT_SIZE) &&
1151 1155 vap->va_type == VREG &&
1152 1156 vap->va_size > MAXOFF32_T)
1153 1157 return (EFBIG);
1154 1158
1155 1159 if (nfs_zone() != VTOMI(vp)->mi_zone)
1156 1160 return (EIO);
1157 1161
1158 1162 va.va_mask = AT_UID | AT_MODE;
1159 1163
1160 1164 error = nfsgetattr(vp, &va, cr);
1161 1165 if (error)
1162 1166 return (error);
1163 1167
1164 1168 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs_accessx,
1165 1169 vp);
1166 1170
1167 1171 if (error)
1168 1172 return (error);
1169 1173
1170 1174 return (nfssetattr(vp, vap, flags, cr));
1171 1175 }
1172 1176
1173 1177 static int
1174 1178 nfssetattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1175 1179 {
1176 1180 int error;
1177 1181 uint_t mask;
1178 1182 struct nfssaargs args;
1179 1183 struct nfsattrstat ns;
1180 1184 int douprintf;
1181 1185 rnode_t *rp;
1182 1186 struct vattr va;
1183 1187 mode_t omode;
1184 1188 mntinfo_t *mi;
1185 1189 vsecattr_t *vsp;
1186 1190 hrtime_t t;
1187 1191
1188 1192 mask = vap->va_mask;
1189 1193
1190 1194 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1191 1195
1192 1196 rp = VTOR(vp);
1193 1197
1194 1198 /*
1195 1199 * Only need to flush pages if there are any pages and
1196 1200 * if the file is marked as dirty in some fashion. The
1197 1201 * file must be flushed so that we can accurately
1198 1202 * determine the size of the file and the cached data
1199 1203 * after the SETATTR returns. A file is considered to
1200 1204 * be dirty if it is either marked with RDIRTY, has
1201 1205 * outstanding i/o's active, or is mmap'd. In this
1202 1206 * last case, we can't tell whether there are dirty
1203 1207 * pages, so we flush just to be sure.
1204 1208 */
1205 1209 if (vn_has_cached_data(vp) &&
1206 1210 ((rp->r_flags & RDIRTY) ||
1207 1211 rp->r_count > 0 ||
1208 1212 rp->r_mapcnt > 0)) {
1209 1213 ASSERT(vp->v_type != VCHR);
1210 1214 error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1211 1215 if (error && (error == ENOSPC || error == EDQUOT)) {
1212 1216 mutex_enter(&rp->r_statelock);
1213 1217 if (!rp->r_error)
1214 1218 rp->r_error = error;
1215 1219 mutex_exit(&rp->r_statelock);
1216 1220 }
1217 1221 }
1218 1222
1219 1223 /*
1220 1224 * If the system call was utime(2) or utimes(2) and the
1221 1225 * application did not specify the times, then set the
1222 1226 * mtime nanosecond field to 1 billion. This will get
1223 1227 * translated from 1 billion nanoseconds to 1 million
1224 1228 * microseconds in the over the wire request. The
1225 1229 * server will use 1 million in the microsecond field
1226 1230 * to tell whether both the mtime and atime should be
1227 1231 * set to the server's current time.
1228 1232 *
1229 1233 * This is an overload of the protocol and should be
1230 1234 * documented in the NFS Version 2 protocol specification.
1231 1235 */
1232 1236 if ((mask & AT_MTIME) && !(flags & ATTR_UTIME)) {
1233 1237 vap->va_mtime.tv_nsec = 1000000000;
1234 1238 if (NFS_TIME_T_OK(vap->va_mtime.tv_sec) &&
1235 1239 NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
1236 1240 error = vattr_to_sattr(vap, &args.saa_sa);
1237 1241 } else {
1238 1242 /*
1239 1243 * Use server times. vap time values will not be used.
1240 1244 * To ensure no time overflow, make sure vap has
1241 1245 * valid values, but retain the original values.
1242 1246 */
1243 1247 timestruc_t mtime = vap->va_mtime;
1244 1248 timestruc_t atime = vap->va_atime;
1245 1249 time_t now;
1246 1250
1247 1251 now = gethrestime_sec();
1248 1252 if (NFS_TIME_T_OK(now)) {
1249 1253 /* Just in case server does not know of this */
1250 1254 vap->va_mtime.tv_sec = now;
1251 1255 vap->va_atime.tv_sec = now;
1252 1256 } else {
1253 1257 vap->va_mtime.tv_sec = 0;
1254 1258 vap->va_atime.tv_sec = 0;
1255 1259 }
1256 1260 error = vattr_to_sattr(vap, &args.saa_sa);
1257 1261 /* set vap times back on */
1258 1262 vap->va_mtime = mtime;
1259 1263 vap->va_atime = atime;
1260 1264 }
1261 1265 } else {
1262 1266 /* Either do not set times or use the client specified times */
1263 1267 error = vattr_to_sattr(vap, &args.saa_sa);
1264 1268 }
1265 1269 if (error) {
1266 1270 /* req time field(s) overflow - return immediately */
1267 1271 return (error);
1268 1272 }
1269 1273 args.saa_fh = *VTOFH(vp);
1270 1274
1271 1275 va.va_mask = AT_MODE;
1272 1276 error = nfsgetattr(vp, &va, cr);
1273 1277 if (error)
1274 1278 return (error);
1275 1279 omode = va.va_mode;
1276 1280
1277 1281 mi = VTOMI(vp);
1278 1282
1279 1283 douprintf = 1;
1280 1284
1281 1285 t = gethrtime();
1282 1286
1283 1287 error = rfs2call(mi, RFS_SETATTR,
1284 1288 xdr_saargs, (caddr_t)&args,
1285 1289 xdr_attrstat, (caddr_t)&ns, cr,
1286 1290 &douprintf, &ns.ns_status, 0, NULL);
1287 1291
1288 1292 /*
1289 1293 * Purge the access cache and ACL cache if changing either the
1290 1294 * owner of the file, the group owner, or the mode. These may
1291 1295 * change the access permissions of the file, so purge old
1292 1296 * information and start over again.
1293 1297 */
1294 1298 if ((mask & (AT_UID | AT_GID | AT_MODE)) && (mi->mi_flags & MI_ACL)) {
1295 1299 (void) nfs_access_purge_rp(rp);
1296 1300 if (rp->r_secattr != NULL) {
1297 1301 mutex_enter(&rp->r_statelock);
1298 1302 vsp = rp->r_secattr;
1299 1303 rp->r_secattr = NULL;
1300 1304 mutex_exit(&rp->r_statelock);
1301 1305 if (vsp != NULL)
1302 1306 nfs_acl_free(vsp);
1303 1307 }
1304 1308 }
1305 1309
1306 1310 if (!error) {
1307 1311 error = geterrno(ns.ns_status);
1308 1312 if (!error) {
1309 1313 /*
1310 1314 * If changing the size of the file, invalidate
1311 1315 * any local cached data which is no longer part
1312 1316 * of the file. We also possibly invalidate the
1313 1317 * last page in the file. We could use
1314 1318 * pvn_vpzero(), but this would mark the page as
1315 1319 * modified and require it to be written back to
1316 1320 * the server for no particularly good reason.
1317 1321 * This way, if we access it, then we bring it
1318 1322 * back in. A read should be cheaper than a
1319 1323 * write.
1320 1324 */
1321 1325 if (mask & AT_SIZE) {
1322 1326 nfs_invalidate_pages(vp,
1323 1327 (vap->va_size & PAGEMASK), cr);
1324 1328 }
1325 1329 (void) nfs_cache_fattr(vp, &ns.ns_attr, &va, t, cr);
1326 1330 /*
1327 1331 * If NFS_ACL is supported on the server, then the
1328 1332 * attributes returned by server may have minimal
1329 1333 * permissions sometimes denying access to users having
1330 1334 * proper access. To get the proper attributes, mark
1331 1335 * the attributes as expired so that they will be
1332 1336 * regotten via the NFS_ACL GETATTR2 procedure.
1333 1337 */
1334 1338 if (mi->mi_flags & MI_ACL) {
1335 1339 PURGE_ATTRCACHE(vp);
1336 1340 }
1337 1341 /*
1338 1342 * This next check attempts to deal with NFS
1339 1343 * servers which can not handle increasing
1340 1344 * the size of the file via setattr. Most
1341 1345 * of these servers do not return an error,
1342 1346 * but do not change the size of the file.
1343 1347 * Hence, this check and then attempt to set
1344 1348 * the file size by writing 1 byte at the
1345 1349 * offset of the end of the file that we need.
1346 1350 */
1347 1351 if ((mask & AT_SIZE) &&
1348 1352 ns.ns_attr.na_size < (uint32_t)vap->va_size) {
1349 1353 char zb = '\0';
1350 1354
1351 1355 error = nfswrite(vp, &zb,
1352 1356 vap->va_size - sizeof (zb),
1353 1357 sizeof (zb), cr);
1354 1358 }
1355 1359 /*
1356 1360 * Some servers will change the mode to clear the setuid
1357 1361 * and setgid bits when changing the uid or gid. The
1358 1362 * client needs to compensate appropriately.
1359 1363 */
1360 1364 if (mask & (AT_UID | AT_GID)) {
1361 1365 int terror;
1362 1366
1363 1367 va.va_mask = AT_MODE;
1364 1368 terror = nfsgetattr(vp, &va, cr);
1365 1369 if (!terror &&
1366 1370 (((mask & AT_MODE) &&
1367 1371 va.va_mode != vap->va_mode) ||
1368 1372 (!(mask & AT_MODE) &&
1369 1373 va.va_mode != omode))) {
1370 1374 va.va_mask = AT_MODE;
1371 1375 if (mask & AT_MODE)
1372 1376 va.va_mode = vap->va_mode;
1373 1377 else
1374 1378 va.va_mode = omode;
1375 1379 (void) nfssetattr(vp, &va, 0, cr);
1376 1380 }
1377 1381 }
1378 1382 } else {
1379 1383 PURGE_ATTRCACHE(vp);
1380 1384 PURGE_STALE_FH(error, vp, cr);
1381 1385 }
1382 1386 } else {
1383 1387 PURGE_ATTRCACHE(vp);
1384 1388 }
1385 1389
1386 1390 return (error);
1387 1391 }
1388 1392
1389 1393 static int
1390 1394 nfs_accessx(void *vp, int mode, cred_t *cr)
1391 1395 {
1392 1396 ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1393 1397 return (nfs_access(vp, mode, 0, cr, NULL));
1394 1398 }
1395 1399
1396 1400 /* ARGSUSED */
1397 1401 static int
1398 1402 nfs_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1399 1403 {
1400 1404 struct vattr va;
1401 1405 int error;
1402 1406 mntinfo_t *mi;
1403 1407 int shift = 0;
1404 1408
1405 1409 mi = VTOMI(vp);
1406 1410
1407 1411 if (nfs_zone() != mi->mi_zone)
1408 1412 return (EIO);
1409 1413 if (mi->mi_flags & MI_ACL) {
1410 1414 error = acl_access2(vp, mode, flags, cr);
1411 1415 if (mi->mi_flags & MI_ACL)
1412 1416 return (error);
1413 1417 }
1414 1418
1415 1419 va.va_mask = AT_MODE | AT_UID | AT_GID;
1416 1420 error = nfsgetattr(vp, &va, cr);
1417 1421 if (error)
1418 1422 return (error);
1419 1423
1420 1424 /*
1421 1425 * Disallow write attempts on read-only
1422 1426 * file systems, unless the file is a
1423 1427 * device node.
1424 1428 */
1425 1429 if ((mode & VWRITE) && vn_is_readonly(vp) && !IS_DEVVP(vp))
1426 1430 return (EROFS);
1427 1431
1428 1432 /*
1429 1433 * Disallow attempts to access mandatory lock files.
1430 1434 */
1431 1435 if ((mode & (VWRITE | VREAD | VEXEC)) &&
1432 1436 MANDLOCK(vp, va.va_mode))
1433 1437 return (EACCES);
1434 1438
1435 1439 /*
1436 1440 * Access check is based on only
1437 1441 * one of owner, group, public.
1438 1442 * If not owner, then check group.
1439 1443 * If not a member of the group,
1440 1444 * then check public access.
1441 1445 */
1442 1446 if (crgetuid(cr) != va.va_uid) {
1443 1447 shift += 3;
1444 1448 if (!groupmember(va.va_gid, cr))
1445 1449 shift += 3;
1446 1450 }
1447 1451
1448 1452 return (secpolicy_vnode_access2(cr, vp, va.va_uid,
1449 1453 va.va_mode << shift, mode));
1450 1454 }
1451 1455
1452 1456 static int nfs_do_symlink_cache = 1;
1453 1457
1454 1458 /* ARGSUSED */
1455 1459 static int
1456 1460 nfs_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1457 1461 {
1458 1462 int error;
1459 1463 struct nfsrdlnres rl;
1460 1464 rnode_t *rp;
1461 1465 int douprintf;
1462 1466 failinfo_t fi;
1463 1467
1464 1468 /*
1465 1469 * We want to be consistent with UFS semantics so we will return
1466 1470 * EINVAL instead of ENXIO. This violates the XNFS spec and
1467 1471 * the RFC 1094, which are wrong any way. BUGID 1138002.
1468 1472 */
1469 1473 if (vp->v_type != VLNK)
1470 1474 return (EINVAL);
1471 1475
1472 1476 if (nfs_zone() != VTOMI(vp)->mi_zone)
1473 1477 return (EIO);
1474 1478
1475 1479 rp = VTOR(vp);
1476 1480 if (nfs_do_symlink_cache && rp->r_symlink.contents != NULL) {
1477 1481 error = nfs_validate_caches(vp, cr);
1478 1482 if (error)
1479 1483 return (error);
1480 1484 mutex_enter(&rp->r_statelock);
1481 1485 if (rp->r_symlink.contents != NULL) {
1482 1486 error = uiomove(rp->r_symlink.contents,
1483 1487 rp->r_symlink.len, UIO_READ, uiop);
1484 1488 mutex_exit(&rp->r_statelock);
1485 1489 return (error);
1486 1490 }
1487 1491 mutex_exit(&rp->r_statelock);
1488 1492 }
1489 1493
1490 1494
1491 1495 rl.rl_data = kmem_alloc(NFS_MAXPATHLEN, KM_SLEEP);
1492 1496
1493 1497 fi.vp = vp;
1494 1498 fi.fhp = NULL; /* no need to update, filehandle not copied */
1495 1499 fi.copyproc = nfscopyfh;
1496 1500 fi.lookupproc = nfslookup;
1497 1501 fi.xattrdirproc = acl_getxattrdir2;
1498 1502
1499 1503 douprintf = 1;
1500 1504
1501 1505 error = rfs2call(VTOMI(vp), RFS_READLINK,
1502 1506 xdr_readlink, (caddr_t)VTOFH(vp),
1503 1507 xdr_rdlnres, (caddr_t)&rl, cr,
1504 1508 &douprintf, &rl.rl_status, 0, &fi);
1505 1509
1506 1510 if (error) {
1507 1511
1508 1512 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1509 1513 return (error);
1510 1514 }
1511 1515
1512 1516 error = geterrno(rl.rl_status);
1513 1517 if (!error) {
1514 1518 error = uiomove(rl.rl_data, (int)rl.rl_count, UIO_READ, uiop);
1515 1519 if (nfs_do_symlink_cache && rp->r_symlink.contents == NULL) {
1516 1520 mutex_enter(&rp->r_statelock);
1517 1521 if (rp->r_symlink.contents == NULL) {
1518 1522 rp->r_symlink.contents = rl.rl_data;
1519 1523 rp->r_symlink.len = (int)rl.rl_count;
1520 1524 rp->r_symlink.size = NFS_MAXPATHLEN;
1521 1525 mutex_exit(&rp->r_statelock);
1522 1526 } else {
1523 1527 mutex_exit(&rp->r_statelock);
1524 1528
1525 1529 kmem_free((void *)rl.rl_data,
1526 1530 NFS_MAXPATHLEN);
1527 1531 }
1528 1532 } else {
1529 1533
1530 1534 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1531 1535 }
1532 1536 } else {
1533 1537 PURGE_STALE_FH(error, vp, cr);
1534 1538
1535 1539 kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1536 1540 }
1537 1541
1538 1542 /*
1539 1543 * Conform to UFS semantics (see comment above)
1540 1544 */
1541 1545 return (error == ENXIO ? EINVAL : error);
1542 1546 }
1543 1547
1544 1548 /*
1545 1549 * Flush local dirty pages to stable storage on the server.
1546 1550 *
1547 1551 * If FNODSYNC is specified, then there is nothing to do because
1548 1552 * metadata changes are not cached on the client before being
1549 1553 * sent to the server.
1550 1554 */
1551 1555 /* ARGSUSED */
1552 1556 static int
1553 1557 nfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1554 1558 {
1555 1559 int error;
1556 1560
1557 1561 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1558 1562 return (0);
1559 1563
1560 1564 if (nfs_zone() != VTOMI(vp)->mi_zone)
1561 1565 return (EIO);
1562 1566
1563 1567 error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1564 1568 if (!error)
1565 1569 error = VTOR(vp)->r_error;
1566 1570 return (error);
1567 1571 }
1568 1572
1569 1573
1570 1574 /*
1571 1575 * Weirdness: if the file was removed or the target of a rename
1572 1576 * operation while it was open, it got renamed instead. Here we
1573 1577 * remove the renamed file.
1574 1578 */
1575 1579 /* ARGSUSED */
1576 1580 static void
1577 1581 nfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1578 1582 {
1579 1583 rnode_t *rp;
1580 1584
1581 1585 ASSERT(vp != DNLC_NO_VNODE);
1582 1586
1583 1587 /*
1584 1588 * If this is coming from the wrong zone, we let someone in the right
1585 1589 * zone take care of it asynchronously. We can get here due to
1586 1590 * VN_RELE() being called from pageout() or fsflush(). This call may
1587 1591 * potentially turn into an expensive no-op if, for instance, v_count
1588 1592 * gets incremented in the meantime, but it's still correct.
1589 1593 */
1590 1594 if (nfs_zone() != VTOMI(vp)->mi_zone) {
1591 1595 nfs_async_inactive(vp, cr, nfs_inactive);
1592 1596 return;
1593 1597 }
1594 1598
1595 1599 rp = VTOR(vp);
1596 1600 redo:
1597 1601 if (rp->r_unldvp != NULL) {
1598 1602 /*
1599 1603 * Save the vnode pointer for the directory where the
1600 1604 * unlinked-open file got renamed, then set it to NULL
1601 1605 * to prevent another thread from getting here before
1602 1606 * we're done with the remove. While we have the
1603 1607 * statelock, make local copies of the pertinent rnode
1604 1608 * fields. If we weren't to do this in an atomic way, the
1605 1609 * the unl* fields could become inconsistent with respect
1606 1610 * to each other due to a race condition between this
1607 1611 * code and nfs_remove(). See bug report 1034328.
1608 1612 */
1609 1613 mutex_enter(&rp->r_statelock);
1610 1614 if (rp->r_unldvp != NULL) {
1611 1615 vnode_t *unldvp;
1612 1616 char *unlname;
1613 1617 cred_t *unlcred;
1614 1618 struct nfsdiropargs da;
1615 1619 enum nfsstat status;
1616 1620 int douprintf;
1617 1621 int error;
1618 1622
1619 1623 unldvp = rp->r_unldvp;
1620 1624 rp->r_unldvp = NULL;
1621 1625 unlname = rp->r_unlname;
1622 1626 rp->r_unlname = NULL;
1623 1627 unlcred = rp->r_unlcred;
1624 1628 rp->r_unlcred = NULL;
1625 1629 mutex_exit(&rp->r_statelock);
1626 1630
1627 1631 /*
1628 1632 * If there are any dirty pages left, then flush
1629 1633 * them. This is unfortunate because they just
1630 1634 * may get thrown away during the remove operation,
1631 1635 * but we have to do this for correctness.
1632 1636 */
1633 1637 if (vn_has_cached_data(vp) &&
1634 1638 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1635 1639 ASSERT(vp->v_type != VCHR);
1636 1640 error = nfs_putpage(vp, (offset_t)0, 0, 0,
1637 1641 cr, ct);
1638 1642 if (error) {
1639 1643 mutex_enter(&rp->r_statelock);
1640 1644 if (!rp->r_error)
1641 1645 rp->r_error = error;
1642 1646 mutex_exit(&rp->r_statelock);
1643 1647 }
1644 1648 }
1645 1649
1646 1650 /*
1647 1651 * Do the remove operation on the renamed file
1648 1652 */
1649 1653 setdiropargs(&da, unlname, unldvp);
1650 1654
1651 1655 douprintf = 1;
1652 1656
1653 1657 (void) rfs2call(VTOMI(unldvp), RFS_REMOVE,
1654 1658 xdr_diropargs, (caddr_t)&da,
1655 1659 xdr_enum, (caddr_t)&status, unlcred,
1656 1660 &douprintf, &status, 0, NULL);
1657 1661
1658 1662 if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1659 1663 nfs_purge_rddir_cache(unldvp);
1660 1664 PURGE_ATTRCACHE(unldvp);
1661 1665
1662 1666 /*
1663 1667 * Release stuff held for the remove
1664 1668 */
1665 1669 VN_RELE(unldvp);
1666 1670 kmem_free(unlname, MAXNAMELEN);
1667 1671 crfree(unlcred);
1668 1672 goto redo;
1669 1673 }
1670 1674 mutex_exit(&rp->r_statelock);
1671 1675 }
1672 1676
1673 1677 rp_addfree(rp, cr);
1674 1678 }
1675 1679
1676 1680 /*
1677 1681 * Remote file system operations having to do with directory manipulation.
1678 1682 */
1679 1683
1680 1684 /* ARGSUSED */
1681 1685 static int
1682 1686 nfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1683 1687 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1684 1688 int *direntflags, pathname_t *realpnp)
1685 1689 {
1686 1690 int error;
1687 1691 vnode_t *vp;
1688 1692 vnode_t *avp = NULL;
1689 1693 rnode_t *drp;
1690 1694
1691 1695 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1692 1696 return (EPERM);
1693 1697
1694 1698 drp = VTOR(dvp);
1695 1699
1696 1700 /*
1697 1701 * Are we looking up extended attributes? If so, "dvp" is
1698 1702 * the file or directory for which we want attributes, and
1699 1703 * we need a lookup of the hidden attribute directory
1700 1704 * before we lookup the rest of the path.
1701 1705 */
1702 1706 if (flags & LOOKUP_XATTR) {
1703 1707 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1704 1708 mntinfo_t *mi;
1705 1709
1706 1710 mi = VTOMI(dvp);
1707 1711 if (!(mi->mi_flags & MI_EXTATTR))
1708 1712 return (EINVAL);
1709 1713
1710 1714 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1711 1715 return (EINTR);
1712 1716
1713 1717 (void) nfslookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1714 1718 if (avp == NULL)
1715 1719 error = acl_getxattrdir2(dvp, &avp, cflag, cr, 0);
1716 1720 else
1717 1721 error = 0;
1718 1722
1719 1723 nfs_rw_exit(&drp->r_rwlock);
1720 1724
1721 1725 if (error) {
1722 1726 if (mi->mi_flags & MI_EXTATTR)
1723 1727 return (error);
1724 1728 return (EINVAL);
1725 1729 }
1726 1730 dvp = avp;
1727 1731 drp = VTOR(dvp);
1728 1732 }
1729 1733
1730 1734 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1731 1735 error = EINTR;
1732 1736 goto out;
1733 1737 }
1734 1738
1735 1739 error = nfslookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1736 1740
1737 1741 nfs_rw_exit(&drp->r_rwlock);
1738 1742
1739 1743 /*
1740 1744 * If vnode is a device, create special vnode.
1741 1745 */
1742 1746 if (!error && IS_DEVVP(*vpp)) {
1743 1747 vp = *vpp;
1744 1748 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1745 1749 VN_RELE(vp);
1746 1750 }
1747 1751
1748 1752 out:
1749 1753 if (avp != NULL)
1750 1754 VN_RELE(avp);
1751 1755
1752 1756 return (error);
1753 1757 }
1754 1758
1755 1759 static int nfs_lookup_neg_cache = 1;
1756 1760
1757 1761 #ifdef DEBUG
1758 1762 static int nfs_lookup_dnlc_hits = 0;
1759 1763 static int nfs_lookup_dnlc_misses = 0;
1760 1764 static int nfs_lookup_dnlc_neg_hits = 0;
1761 1765 static int nfs_lookup_dnlc_disappears = 0;
1762 1766 static int nfs_lookup_dnlc_lookups = 0;
1763 1767 #endif
1764 1768
1765 1769 /* ARGSUSED */
1766 1770 int
1767 1771 nfslookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1768 1772 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
1769 1773 {
1770 1774 int error;
1771 1775
1772 1776 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1773 1777
1774 1778 /*
1775 1779 * If lookup is for "", just return dvp. Don't need
1776 1780 * to send it over the wire, look it up in the dnlc,
1777 1781 * or perform any access checks.
1778 1782 */
1779 1783 if (*nm == '\0') {
1780 1784 VN_HOLD(dvp);
1781 1785 *vpp = dvp;
1782 1786 return (0);
1783 1787 }
1784 1788
1785 1789 /*
1786 1790 * Can't do lookups in non-directories.
1787 1791 */
1788 1792 if (dvp->v_type != VDIR)
1789 1793 return (ENOTDIR);
1790 1794
1791 1795 /*
1792 1796 * If we're called with RFSCALL_SOFT, it's important that
1793 1797 * the only rfscall is one we make directly; if we permit
1794 1798 * an access call because we're looking up "." or validating
1795 1799 * a dnlc hit, we'll deadlock because that rfscall will not
1796 1800 * have the RFSCALL_SOFT set.
1797 1801 */
1798 1802 if (rfscall_flags & RFSCALL_SOFT)
1799 1803 goto callit;
1800 1804
1801 1805 /*
1802 1806 * If lookup is for ".", just return dvp. Don't need
1803 1807 * to send it over the wire or look it up in the dnlc,
1804 1808 * just need to check access.
1805 1809 */
1806 1810 if (strcmp(nm, ".") == 0) {
1807 1811 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1808 1812 if (error)
1809 1813 return (error);
1810 1814 VN_HOLD(dvp);
1811 1815 *vpp = dvp;
1812 1816 return (0);
1813 1817 }
1814 1818
1815 1819 /*
1816 1820 * Lookup this name in the DNLC. If there was a valid entry,
1817 1821 * then return the results of the lookup.
1818 1822 */
1819 1823 error = nfslookup_dnlc(dvp, nm, vpp, cr);
1820 1824 if (error || *vpp != NULL)
1821 1825 return (error);
1822 1826
1823 1827 callit:
1824 1828 error = nfslookup_otw(dvp, nm, vpp, cr, rfscall_flags);
1825 1829
1826 1830 return (error);
1827 1831 }
1828 1832
1829 1833 static int
1830 1834 nfslookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
1831 1835 {
1832 1836 int error;
1833 1837 vnode_t *vp;
1834 1838
1835 1839 ASSERT(*nm != '\0');
1836 1840 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1837 1841
1838 1842 /*
1839 1843 * Lookup this name in the DNLC. If successful, then validate
1840 1844 * the caches and then recheck the DNLC. The DNLC is rechecked
1841 1845 * just in case this entry got invalidated during the call
1842 1846 * to nfs_validate_caches.
1843 1847 *
1844 1848 * An assumption is being made that it is safe to say that a
1845 1849 * file exists which may not on the server. Any operations to
1846 1850 * the server will fail with ESTALE.
1847 1851 */
1848 1852 #ifdef DEBUG
1849 1853 nfs_lookup_dnlc_lookups++;
1850 1854 #endif
1851 1855 vp = dnlc_lookup(dvp, nm);
1852 1856 if (vp != NULL) {
1853 1857 VN_RELE(vp);
1854 1858 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
1855 1859 PURGE_ATTRCACHE(dvp);
1856 1860 }
1857 1861 error = nfs_validate_caches(dvp, cr);
1858 1862 if (error)
1859 1863 return (error);
1860 1864 vp = dnlc_lookup(dvp, nm);
1861 1865 if (vp != NULL) {
1862 1866 error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1863 1867 if (error) {
1864 1868 VN_RELE(vp);
1865 1869 return (error);
1866 1870 }
1867 1871 if (vp == DNLC_NO_VNODE) {
1868 1872 VN_RELE(vp);
1869 1873 #ifdef DEBUG
1870 1874 nfs_lookup_dnlc_neg_hits++;
1871 1875 #endif
1872 1876 return (ENOENT);
1873 1877 }
1874 1878 *vpp = vp;
1875 1879 #ifdef DEBUG
1876 1880 nfs_lookup_dnlc_hits++;
1877 1881 #endif
1878 1882 return (0);
1879 1883 }
1880 1884 #ifdef DEBUG
1881 1885 nfs_lookup_dnlc_disappears++;
1882 1886 #endif
1883 1887 }
1884 1888 #ifdef DEBUG
1885 1889 else
1886 1890 nfs_lookup_dnlc_misses++;
1887 1891 #endif
1888 1892
1889 1893 *vpp = NULL;
1890 1894
1891 1895 return (0);
1892 1896 }
1893 1897
1894 1898 static int
1895 1899 nfslookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
1896 1900 int rfscall_flags)
1897 1901 {
1898 1902 int error;
1899 1903 struct nfsdiropargs da;
1900 1904 struct nfsdiropres dr;
1901 1905 int douprintf;
1902 1906 failinfo_t fi;
1903 1907 hrtime_t t;
1904 1908
1905 1909 ASSERT(*nm != '\0');
1906 1910 ASSERT(dvp->v_type == VDIR);
1907 1911 ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1908 1912
1909 1913 setdiropargs(&da, nm, dvp);
1910 1914
1911 1915 fi.vp = dvp;
1912 1916 fi.fhp = NULL; /* no need to update, filehandle not copied */
1913 1917 fi.copyproc = nfscopyfh;
1914 1918 fi.lookupproc = nfslookup;
1915 1919 fi.xattrdirproc = acl_getxattrdir2;
1916 1920
1917 1921 douprintf = 1;
1918 1922
1919 1923 t = gethrtime();
1920 1924
1921 1925 error = rfs2call(VTOMI(dvp), RFS_LOOKUP,
1922 1926 xdr_diropargs, (caddr_t)&da,
1923 1927 xdr_diropres, (caddr_t)&dr, cr,
1924 1928 &douprintf, &dr.dr_status, rfscall_flags, &fi);
1925 1929
1926 1930 if (!error) {
1927 1931 error = geterrno(dr.dr_status);
1928 1932 if (!error) {
1929 1933 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
1930 1934 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
1931 1935 /*
1932 1936 * If NFS_ACL is supported on the server, then the
1933 1937 * attributes returned by server may have minimal
1934 1938 * permissions sometimes denying access to users having
1935 1939 * proper access. To get the proper attributes, mark
1936 1940 * the attributes as expired so that they will be
1937 1941 * regotten via the NFS_ACL GETATTR2 procedure.
1938 1942 */
1939 1943 if (VTOMI(*vpp)->mi_flags & MI_ACL) {
1940 1944 PURGE_ATTRCACHE(*vpp);
1941 1945 }
1942 1946 if (!(rfscall_flags & RFSCALL_SOFT))
1943 1947 dnlc_update(dvp, nm, *vpp);
1944 1948 } else {
1945 1949 PURGE_STALE_FH(error, dvp, cr);
1946 1950 if (error == ENOENT && nfs_lookup_neg_cache)
1947 1951 dnlc_enter(dvp, nm, DNLC_NO_VNODE);
1948 1952 }
1949 1953 }
1950 1954
1951 1955 return (error);
1952 1956 }
1953 1957
1954 1958 /* ARGSUSED */
1955 1959 static int
1956 1960 nfs_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
1957 1961 int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
1958 1962 vsecattr_t *vsecp)
1959 1963 {
1960 1964 int error;
1961 1965 struct nfscreatargs args;
1962 1966 struct nfsdiropres dr;
1963 1967 int douprintf;
1964 1968 vnode_t *vp;
1965 1969 rnode_t *rp;
1966 1970 struct vattr vattr;
1967 1971 rnode_t *drp;
1968 1972 vnode_t *tempvp;
1969 1973 hrtime_t t;
1970 1974
1971 1975 drp = VTOR(dvp);
1972 1976
1973 1977 if (nfs_zone() != VTOMI(dvp)->mi_zone)
1974 1978 return (EPERM);
1975 1979 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
1976 1980 return (EINTR);
1977 1981
1978 1982 /*
1979 1983 * We make a copy of the attributes because the caller does not
1980 1984 * expect us to change what va points to.
1981 1985 */
1982 1986 vattr = *va;
1983 1987
1984 1988 /*
1985 1989 * If the pathname is "", just use dvp. Don't need
1986 1990 * to send it over the wire, look it up in the dnlc,
1987 1991 * or perform any access checks.
1988 1992 */
1989 1993 if (*nm == '\0') {
1990 1994 error = 0;
1991 1995 VN_HOLD(dvp);
1992 1996 vp = dvp;
1993 1997 /*
1994 1998 * If the pathname is ".", just use dvp. Don't need
1995 1999 * to send it over the wire or look it up in the dnlc,
1996 2000 * just need to check access.
1997 2001 */
1998 2002 } else if (strcmp(nm, ".") == 0) {
1999 2003 error = nfs_access(dvp, VEXEC, 0, cr, ct);
2000 2004 if (error) {
2001 2005 nfs_rw_exit(&drp->r_rwlock);
2002 2006 return (error);
2003 2007 }
2004 2008 VN_HOLD(dvp);
2005 2009 vp = dvp;
2006 2010 /*
2007 2011 * We need to go over the wire, just to be sure whether the
2008 2012 * file exists or not. Using the DNLC can be dangerous in
2009 2013 * this case when making a decision regarding existence.
2010 2014 */
2011 2015 } else {
2012 2016 error = nfslookup_otw(dvp, nm, &vp, cr, 0);
2013 2017 }
2014 2018 if (!error) {
2015 2019 if (exclusive == EXCL)
2016 2020 error = EEXIST;
2017 2021 else if (vp->v_type == VDIR && (mode & VWRITE))
2018 2022 error = EISDIR;
2019 2023 else {
2020 2024 /*
2021 2025 * If vnode is a device, create special vnode.
2022 2026 */
↓ open down ↓ |
1985 lines elided |
↑ open up ↑ |
2023 2027 if (IS_DEVVP(vp)) {
2024 2028 tempvp = vp;
2025 2029 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2026 2030 VN_RELE(tempvp);
2027 2031 }
2028 2032 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2029 2033 if ((vattr.va_mask & AT_SIZE) &&
2030 2034 vp->v_type == VREG) {
2031 2035 vattr.va_mask = AT_SIZE;
2032 2036 error = nfssetattr(vp, &vattr, 0, cr);
2037 +
2038 + if (!error) {
2039 + /*
2040 + * Existing file was truncated;
2041 + * emit a create event.
2042 + */
2043 + vnevent_create(vp, ct);
2044 + }
2033 2045 }
2034 2046 }
2035 2047 }
2036 2048 nfs_rw_exit(&drp->r_rwlock);
2037 2049 if (error) {
2038 2050 VN_RELE(vp);
2039 2051 } else {
2040 - /*
2041 - * existing file got truncated, notify.
2042 - */
2043 - vnevent_create(vp, ct);
2044 2052 *vpp = vp;
2045 2053 }
2046 2054 return (error);
2047 2055 }
2048 2056
2049 2057 ASSERT(vattr.va_mask & AT_TYPE);
2050 2058 if (vattr.va_type == VREG) {
2051 2059 ASSERT(vattr.va_mask & AT_MODE);
2052 2060 if (MANDMODE(vattr.va_mode)) {
2053 2061 nfs_rw_exit(&drp->r_rwlock);
2054 2062 return (EACCES);
2055 2063 }
2056 2064 }
2057 2065
2058 2066 dnlc_remove(dvp, nm);
2059 2067
2060 2068 setdiropargs(&args.ca_da, nm, dvp);
2061 2069
2062 2070 /*
2063 2071 * Decide what the group-id of the created file should be.
2064 2072 * Set it in attribute list as advisory...then do a setattr
2065 2073 * if the server didn't get it right the first time.
2066 2074 */
2067 2075 error = setdirgid(dvp, &vattr.va_gid, cr);
2068 2076 if (error) {
2069 2077 nfs_rw_exit(&drp->r_rwlock);
2070 2078 return (error);
2071 2079 }
2072 2080 vattr.va_mask |= AT_GID;
2073 2081
2074 2082 /*
2075 2083 * This is a completely gross hack to make mknod
2076 2084 * work over the wire until we can wack the protocol
2077 2085 */
2078 2086 #define IFCHR 0020000 /* character special */
2079 2087 #define IFBLK 0060000 /* block special */
2080 2088 #define IFSOCK 0140000 /* socket */
2081 2089
2082 2090 /*
2083 2091 * dev_t is uint_t in 5.x and short in 4.x. Both 4.x
2084 2092 * supports 8 bit majors. 5.x supports 14 bit majors. 5.x supports 18
2085 2093 * bits in the minor number where 4.x supports 8 bits. If the 5.x
2086 2094 * minor/major numbers <= 8 bits long, compress the device
2087 2095 * number before sending it. Otherwise, the 4.x server will not
2088 2096 * create the device with the correct device number and nothing can be
2089 2097 * done about this.
2090 2098 */
2091 2099 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2092 2100 dev_t d = vattr.va_rdev;
2093 2101 dev32_t dev32;
2094 2102
2095 2103 if (vattr.va_type == VCHR)
2096 2104 vattr.va_mode |= IFCHR;
2097 2105 else
2098 2106 vattr.va_mode |= IFBLK;
2099 2107
2100 2108 (void) cmpldev(&dev32, d);
2101 2109 if (dev32 & ~((SO4_MAXMAJ << L_BITSMINOR32) | SO4_MAXMIN))
2102 2110 vattr.va_size = (u_offset_t)dev32;
2103 2111 else
2104 2112 vattr.va_size = (u_offset_t)nfsv2_cmpdev(d);
2105 2113
2106 2114 vattr.va_mask |= AT_MODE|AT_SIZE;
2107 2115 } else if (vattr.va_type == VFIFO) {
2108 2116 vattr.va_mode |= IFCHR; /* xtra kludge for namedpipe */
2109 2117 vattr.va_size = (u_offset_t)NFS_FIFO_DEV; /* blech */
2110 2118 vattr.va_mask |= AT_MODE|AT_SIZE;
2111 2119 } else if (vattr.va_type == VSOCK) {
2112 2120 vattr.va_mode |= IFSOCK;
2113 2121 /*
2114 2122 * To avoid triggering bugs in the servers set AT_SIZE
2115 2123 * (all other RFS_CREATE calls set this).
2116 2124 */
2117 2125 vattr.va_size = 0;
2118 2126 vattr.va_mask |= AT_MODE|AT_SIZE;
2119 2127 }
2120 2128
2121 2129 args.ca_sa = &args.ca_sa_buf;
2122 2130 error = vattr_to_sattr(&vattr, args.ca_sa);
2123 2131 if (error) {
2124 2132 /* req time field(s) overflow - return immediately */
2125 2133 nfs_rw_exit(&drp->r_rwlock);
2126 2134 return (error);
2127 2135 }
2128 2136
2129 2137 douprintf = 1;
2130 2138
2131 2139 t = gethrtime();
2132 2140
2133 2141 error = rfs2call(VTOMI(dvp), RFS_CREATE,
2134 2142 xdr_creatargs, (caddr_t)&args,
2135 2143 xdr_diropres, (caddr_t)&dr, cr,
2136 2144 &douprintf, &dr.dr_status, 0, NULL);
2137 2145
2138 2146 PURGE_ATTRCACHE(dvp); /* mod time changed */
2139 2147
2140 2148 if (!error) {
2141 2149 error = geterrno(dr.dr_status);
2142 2150 if (!error) {
2143 2151 if (HAVE_RDDIR_CACHE(drp))
2144 2152 nfs_purge_rddir_cache(dvp);
2145 2153 vp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2146 2154 dvp->v_vfsp, t, cr, NULL, NULL);
2147 2155 /*
2148 2156 * If NFS_ACL is supported on the server, then the
2149 2157 * attributes returned by server may have minimal
2150 2158 * permissions sometimes denying access to users having
2151 2159 * proper access. To get the proper attributes, mark
2152 2160 * the attributes as expired so that they will be
2153 2161 * regotten via the NFS_ACL GETATTR2 procedure.
2154 2162 */
2155 2163 if (VTOMI(vp)->mi_flags & MI_ACL) {
2156 2164 PURGE_ATTRCACHE(vp);
2157 2165 }
2158 2166 dnlc_update(dvp, nm, vp);
2159 2167 rp = VTOR(vp);
2160 2168 if (vattr.va_size == 0) {
2161 2169 mutex_enter(&rp->r_statelock);
2162 2170 rp->r_size = 0;
2163 2171 mutex_exit(&rp->r_statelock);
2164 2172 if (vn_has_cached_data(vp)) {
2165 2173 ASSERT(vp->v_type != VCHR);
2166 2174 nfs_invalidate_pages(vp,
2167 2175 (u_offset_t)0, cr);
2168 2176 }
2169 2177 }
2170 2178
2171 2179 /*
2172 2180 * Make sure the gid was set correctly.
2173 2181 * If not, try to set it (but don't lose
2174 2182 * any sleep over it).
2175 2183 */
2176 2184 if (vattr.va_gid != rp->r_attr.va_gid) {
2177 2185 vattr.va_mask = AT_GID;
2178 2186 (void) nfssetattr(vp, &vattr, 0, cr);
2179 2187 }
2180 2188
2181 2189 /*
2182 2190 * If vnode is a device create special vnode
2183 2191 */
2184 2192 if (IS_DEVVP(vp)) {
2185 2193 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2186 2194 VN_RELE(vp);
2187 2195 } else
2188 2196 *vpp = vp;
2189 2197 } else {
2190 2198 PURGE_STALE_FH(error, dvp, cr);
2191 2199 }
2192 2200 }
2193 2201
2194 2202 nfs_rw_exit(&drp->r_rwlock);
2195 2203
2196 2204 return (error);
2197 2205 }
2198 2206
2199 2207 /*
2200 2208 * Weirdness: if the vnode to be removed is open
2201 2209 * we rename it instead of removing it and nfs_inactive
2202 2210 * will remove the new name.
2203 2211 */
2204 2212 /* ARGSUSED */
2205 2213 static int
2206 2214 nfs_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2207 2215 {
2208 2216 int error;
2209 2217 struct nfsdiropargs da;
2210 2218 enum nfsstat status;
2211 2219 vnode_t *vp;
2212 2220 char *tmpname;
2213 2221 int douprintf;
2214 2222 rnode_t *rp;
2215 2223 rnode_t *drp;
2216 2224
2217 2225 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2218 2226 return (EPERM);
2219 2227 drp = VTOR(dvp);
2220 2228 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2221 2229 return (EINTR);
2222 2230
2223 2231 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2224 2232 if (error) {
2225 2233 nfs_rw_exit(&drp->r_rwlock);
2226 2234 return (error);
2227 2235 }
2228 2236
2229 2237 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2230 2238 VN_RELE(vp);
2231 2239 nfs_rw_exit(&drp->r_rwlock);
2232 2240 return (EPERM);
2233 2241 }
2234 2242
2235 2243 /*
2236 2244 * First just remove the entry from the name cache, as it
2237 2245 * is most likely the only entry for this vp.
2238 2246 */
2239 2247 dnlc_remove(dvp, nm);
2240 2248
2241 2249 /*
2242 2250 * If the file has a v_count > 1 then there may be more than one
2243 2251 * entry in the name cache due multiple links or an open file,
2244 2252 * but we don't have the real reference count so flush all
2245 2253 * possible entries.
2246 2254 */
2247 2255 if (vp->v_count > 1)
2248 2256 dnlc_purge_vp(vp);
2249 2257
2250 2258 /*
2251 2259 * Now we have the real reference count on the vnode
2252 2260 */
2253 2261 rp = VTOR(vp);
2254 2262 mutex_enter(&rp->r_statelock);
2255 2263 if (vp->v_count > 1 &&
2256 2264 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2257 2265 mutex_exit(&rp->r_statelock);
2258 2266 tmpname = newname();
2259 2267 error = nfsrename(dvp, nm, dvp, tmpname, cr, ct);
2260 2268 if (error)
2261 2269 kmem_free(tmpname, MAXNAMELEN);
2262 2270 else {
2263 2271 mutex_enter(&rp->r_statelock);
2264 2272 if (rp->r_unldvp == NULL) {
2265 2273 VN_HOLD(dvp);
2266 2274 rp->r_unldvp = dvp;
2267 2275 if (rp->r_unlcred != NULL)
2268 2276 crfree(rp->r_unlcred);
2269 2277 crhold(cr);
2270 2278 rp->r_unlcred = cr;
2271 2279 rp->r_unlname = tmpname;
2272 2280 } else {
2273 2281 kmem_free(rp->r_unlname, MAXNAMELEN);
2274 2282 rp->r_unlname = tmpname;
2275 2283 }
2276 2284 mutex_exit(&rp->r_statelock);
2277 2285 }
2278 2286 } else {
2279 2287 mutex_exit(&rp->r_statelock);
2280 2288 /*
2281 2289 * We need to flush any dirty pages which happen to
2282 2290 * be hanging around before removing the file. This
2283 2291 * shouldn't happen very often and mostly on file
2284 2292 * systems mounted "nocto".
2285 2293 */
2286 2294 if (vn_has_cached_data(vp) &&
2287 2295 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2288 2296 error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2289 2297 if (error && (error == ENOSPC || error == EDQUOT)) {
2290 2298 mutex_enter(&rp->r_statelock);
2291 2299 if (!rp->r_error)
2292 2300 rp->r_error = error;
2293 2301 mutex_exit(&rp->r_statelock);
2294 2302 }
2295 2303 }
2296 2304
2297 2305 setdiropargs(&da, nm, dvp);
2298 2306
2299 2307 douprintf = 1;
2300 2308
2301 2309 error = rfs2call(VTOMI(dvp), RFS_REMOVE,
2302 2310 xdr_diropargs, (caddr_t)&da,
2303 2311 xdr_enum, (caddr_t)&status, cr,
2304 2312 &douprintf, &status, 0, NULL);
2305 2313
2306 2314 /*
2307 2315 * The xattr dir may be gone after last attr is removed,
2308 2316 * so flush it from dnlc.
2309 2317 */
2310 2318 if (dvp->v_flag & V_XATTRDIR)
2311 2319 dnlc_purge_vp(dvp);
2312 2320
2313 2321 PURGE_ATTRCACHE(dvp); /* mod time changed */
2314 2322 PURGE_ATTRCACHE(vp); /* link count changed */
2315 2323
2316 2324 if (!error) {
2317 2325 error = geterrno(status);
2318 2326 if (!error) {
2319 2327 if (HAVE_RDDIR_CACHE(drp))
2320 2328 nfs_purge_rddir_cache(dvp);
2321 2329 } else {
2322 2330 PURGE_STALE_FH(error, dvp, cr);
2323 2331 }
2324 2332 }
2325 2333 }
2326 2334
2327 2335 if (error == 0) {
2328 2336 vnevent_remove(vp, dvp, nm, ct);
2329 2337 }
2330 2338 VN_RELE(vp);
2331 2339
2332 2340 nfs_rw_exit(&drp->r_rwlock);
2333 2341
2334 2342 return (error);
2335 2343 }
2336 2344
2337 2345 /* ARGSUSED */
2338 2346 static int
2339 2347 nfs_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2340 2348 caller_context_t *ct, int flags)
2341 2349 {
2342 2350 int error;
2343 2351 struct nfslinkargs args;
2344 2352 enum nfsstat status;
2345 2353 vnode_t *realvp;
2346 2354 int douprintf;
2347 2355 rnode_t *tdrp;
2348 2356
2349 2357 if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2350 2358 return (EPERM);
2351 2359 if (VOP_REALVP(svp, &realvp, ct) == 0)
2352 2360 svp = realvp;
2353 2361
2354 2362 args.la_from = VTOFH(svp);
2355 2363 setdiropargs(&args.la_to, tnm, tdvp);
2356 2364
2357 2365 tdrp = VTOR(tdvp);
2358 2366 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
2359 2367 return (EINTR);
2360 2368
2361 2369 dnlc_remove(tdvp, tnm);
2362 2370
2363 2371 douprintf = 1;
2364 2372
2365 2373 error = rfs2call(VTOMI(svp), RFS_LINK,
2366 2374 xdr_linkargs, (caddr_t)&args,
2367 2375 xdr_enum, (caddr_t)&status, cr,
2368 2376 &douprintf, &status, 0, NULL);
2369 2377
2370 2378 PURGE_ATTRCACHE(tdvp); /* mod time changed */
2371 2379 PURGE_ATTRCACHE(svp); /* link count changed */
2372 2380
2373 2381 if (!error) {
2374 2382 error = geterrno(status);
2375 2383 if (!error) {
2376 2384 if (HAVE_RDDIR_CACHE(tdrp))
2377 2385 nfs_purge_rddir_cache(tdvp);
2378 2386 }
2379 2387 }
2380 2388
2381 2389 nfs_rw_exit(&tdrp->r_rwlock);
2382 2390
2383 2391 if (!error) {
2384 2392 /*
2385 2393 * Notify the source file of this link operation.
2386 2394 */
2387 2395 vnevent_link(svp, ct);
2388 2396 }
2389 2397 return (error);
2390 2398 }
2391 2399
2392 2400 /* ARGSUSED */
2393 2401 static int
2394 2402 nfs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2395 2403 caller_context_t *ct, int flags)
2396 2404 {
2397 2405 vnode_t *realvp;
2398 2406
2399 2407 if (nfs_zone() != VTOMI(odvp)->mi_zone)
2400 2408 return (EPERM);
2401 2409 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
2402 2410 ndvp = realvp;
2403 2411
2404 2412 return (nfsrename(odvp, onm, ndvp, nnm, cr, ct));
2405 2413 }
2406 2414
2407 2415 /*
2408 2416 * nfsrename does the real work of renaming in NFS Version 2.
2409 2417 */
2410 2418 static int
2411 2419 nfsrename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2412 2420 caller_context_t *ct)
2413 2421 {
2414 2422 int error;
2415 2423 enum nfsstat status;
2416 2424 struct nfsrnmargs args;
2417 2425 int douprintf;
2418 2426 vnode_t *nvp = NULL;
2419 2427 vnode_t *ovp = NULL;
2420 2428 char *tmpname;
2421 2429 rnode_t *rp;
2422 2430 rnode_t *odrp;
2423 2431 rnode_t *ndrp;
2424 2432
2425 2433 ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
2426 2434 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
2427 2435 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
2428 2436 return (EINVAL);
2429 2437
2430 2438 odrp = VTOR(odvp);
2431 2439 ndrp = VTOR(ndvp);
2432 2440 if ((intptr_t)odrp < (intptr_t)ndrp) {
2433 2441 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
2434 2442 return (EINTR);
2435 2443 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
2436 2444 nfs_rw_exit(&odrp->r_rwlock);
2437 2445 return (EINTR);
2438 2446 }
2439 2447 } else {
2440 2448 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
2441 2449 return (EINTR);
2442 2450 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
2443 2451 nfs_rw_exit(&ndrp->r_rwlock);
2444 2452 return (EINTR);
2445 2453 }
2446 2454 }
2447 2455
2448 2456 /*
2449 2457 * Lookup the target file. If it exists, it needs to be
2450 2458 * checked to see whether it is a mount point and whether
2451 2459 * it is active (open).
2452 2460 */
2453 2461 error = nfslookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
2454 2462 if (!error) {
2455 2463 /*
2456 2464 * If this file has been mounted on, then just
2457 2465 * return busy because renaming to it would remove
2458 2466 * the mounted file system from the name space.
2459 2467 */
2460 2468 if (vn_mountedvfs(nvp) != NULL) {
2461 2469 VN_RELE(nvp);
2462 2470 nfs_rw_exit(&odrp->r_rwlock);
2463 2471 nfs_rw_exit(&ndrp->r_rwlock);
2464 2472 return (EBUSY);
2465 2473 }
2466 2474
2467 2475 /*
2468 2476 * Purge the name cache of all references to this vnode
2469 2477 * so that we can check the reference count to infer
2470 2478 * whether it is active or not.
2471 2479 */
2472 2480 /*
2473 2481 * First just remove the entry from the name cache, as it
2474 2482 * is most likely the only entry for this vp.
2475 2483 */
2476 2484 dnlc_remove(ndvp, nnm);
2477 2485 /*
2478 2486 * If the file has a v_count > 1 then there may be more
2479 2487 * than one entry in the name cache due multiple links
2480 2488 * or an open file, but we don't have the real reference
2481 2489 * count so flush all possible entries.
2482 2490 */
2483 2491 if (nvp->v_count > 1)
2484 2492 dnlc_purge_vp(nvp);
2485 2493
2486 2494 /*
2487 2495 * If the vnode is active and is not a directory,
2488 2496 * arrange to rename it to a
2489 2497 * temporary file so that it will continue to be
2490 2498 * accessible. This implements the "unlink-open-file"
2491 2499 * semantics for the target of a rename operation.
2492 2500 * Before doing this though, make sure that the
2493 2501 * source and target files are not already the same.
2494 2502 */
2495 2503 if (nvp->v_count > 1 && nvp->v_type != VDIR) {
2496 2504 /*
2497 2505 * Lookup the source name.
2498 2506 */
2499 2507 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL,
2500 2508 cr, 0);
2501 2509
2502 2510 /*
2503 2511 * The source name *should* already exist.
2504 2512 */
2505 2513 if (error) {
2506 2514 VN_RELE(nvp);
2507 2515 nfs_rw_exit(&odrp->r_rwlock);
2508 2516 nfs_rw_exit(&ndrp->r_rwlock);
2509 2517 return (error);
2510 2518 }
2511 2519
2512 2520 /*
2513 2521 * Compare the two vnodes. If they are the same,
2514 2522 * just release all held vnodes and return success.
2515 2523 */
2516 2524 if (ovp == nvp) {
2517 2525 VN_RELE(ovp);
2518 2526 VN_RELE(nvp);
2519 2527 nfs_rw_exit(&odrp->r_rwlock);
2520 2528 nfs_rw_exit(&ndrp->r_rwlock);
2521 2529 return (0);
2522 2530 }
2523 2531
2524 2532 /*
2525 2533 * Can't mix and match directories and non-
2526 2534 * directories in rename operations. We already
2527 2535 * know that the target is not a directory. If
2528 2536 * the source is a directory, return an error.
2529 2537 */
2530 2538 if (ovp->v_type == VDIR) {
2531 2539 VN_RELE(ovp);
2532 2540 VN_RELE(nvp);
2533 2541 nfs_rw_exit(&odrp->r_rwlock);
2534 2542 nfs_rw_exit(&ndrp->r_rwlock);
2535 2543 return (ENOTDIR);
2536 2544 }
2537 2545
2538 2546 /*
2539 2547 * The target file exists, is not the same as
2540 2548 * the source file, and is active. Link it
2541 2549 * to a temporary filename to avoid having
2542 2550 * the server removing the file completely.
2543 2551 */
2544 2552 tmpname = newname();
2545 2553 error = nfs_link(ndvp, nvp, tmpname, cr, NULL, 0);
2546 2554 if (error == EOPNOTSUPP) {
2547 2555 error = nfs_rename(ndvp, nnm, ndvp, tmpname,
2548 2556 cr, NULL, 0);
2549 2557 }
2550 2558 if (error) {
2551 2559 kmem_free(tmpname, MAXNAMELEN);
2552 2560 VN_RELE(ovp);
2553 2561 VN_RELE(nvp);
2554 2562 nfs_rw_exit(&odrp->r_rwlock);
2555 2563 nfs_rw_exit(&ndrp->r_rwlock);
2556 2564 return (error);
2557 2565 }
2558 2566 rp = VTOR(nvp);
2559 2567 mutex_enter(&rp->r_statelock);
2560 2568 if (rp->r_unldvp == NULL) {
2561 2569 VN_HOLD(ndvp);
2562 2570 rp->r_unldvp = ndvp;
2563 2571 if (rp->r_unlcred != NULL)
2564 2572 crfree(rp->r_unlcred);
2565 2573 crhold(cr);
2566 2574 rp->r_unlcred = cr;
2567 2575 rp->r_unlname = tmpname;
2568 2576 } else {
2569 2577 kmem_free(rp->r_unlname, MAXNAMELEN);
2570 2578 rp->r_unlname = tmpname;
2571 2579 }
2572 2580 mutex_exit(&rp->r_statelock);
2573 2581 }
2574 2582 }
2575 2583
2576 2584 if (ovp == NULL) {
2577 2585 /*
2578 2586 * When renaming directories to be a subdirectory of a
2579 2587 * different parent, the dnlc entry for ".." will no
2580 2588 * longer be valid, so it must be removed.
2581 2589 *
2582 2590 * We do a lookup here to determine whether we are renaming
2583 2591 * a directory and we need to check if we are renaming
2584 2592 * an unlinked file. This might have already been done
2585 2593 * in previous code, so we check ovp == NULL to avoid
2586 2594 * doing it twice.
2587 2595 */
2588 2596
2589 2597 error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
2590 2598
2591 2599 /*
2592 2600 * The source name *should* already exist.
2593 2601 */
2594 2602 if (error) {
2595 2603 nfs_rw_exit(&odrp->r_rwlock);
2596 2604 nfs_rw_exit(&ndrp->r_rwlock);
2597 2605 if (nvp) {
2598 2606 VN_RELE(nvp);
2599 2607 }
2600 2608 return (error);
2601 2609 }
2602 2610 ASSERT(ovp != NULL);
2603 2611 }
2604 2612
2605 2613 dnlc_remove(odvp, onm);
2606 2614 dnlc_remove(ndvp, nnm);
2607 2615
2608 2616 setdiropargs(&args.rna_from, onm, odvp);
2609 2617 setdiropargs(&args.rna_to, nnm, ndvp);
2610 2618
2611 2619 douprintf = 1;
2612 2620
2613 2621 error = rfs2call(VTOMI(odvp), RFS_RENAME,
2614 2622 xdr_rnmargs, (caddr_t)&args,
2615 2623 xdr_enum, (caddr_t)&status, cr,
2616 2624 &douprintf, &status, 0, NULL);
2617 2625
2618 2626 PURGE_ATTRCACHE(odvp); /* mod time changed */
2619 2627 PURGE_ATTRCACHE(ndvp); /* mod time changed */
2620 2628
2621 2629 if (!error) {
2622 2630 error = geterrno(status);
2623 2631 if (!error) {
2624 2632 if (HAVE_RDDIR_CACHE(odrp))
2625 2633 nfs_purge_rddir_cache(odvp);
2626 2634 if (HAVE_RDDIR_CACHE(ndrp))
2627 2635 nfs_purge_rddir_cache(ndvp);
2628 2636 /*
2629 2637 * when renaming directories to be a subdirectory of a
2630 2638 * different parent, the dnlc entry for ".." will no
2631 2639 * longer be valid, so it must be removed
2632 2640 */
2633 2641 rp = VTOR(ovp);
2634 2642 if (ndvp != odvp) {
2635 2643 if (ovp->v_type == VDIR) {
2636 2644 dnlc_remove(ovp, "..");
2637 2645 if (HAVE_RDDIR_CACHE(rp))
2638 2646 nfs_purge_rddir_cache(ovp);
2639 2647 }
2640 2648 }
2641 2649
2642 2650 /*
2643 2651 * If we are renaming the unlinked file, update the
2644 2652 * r_unldvp and r_unlname as needed.
2645 2653 */
2646 2654 mutex_enter(&rp->r_statelock);
2647 2655 if (rp->r_unldvp != NULL) {
2648 2656 if (strcmp(rp->r_unlname, onm) == 0) {
2649 2657 (void) strncpy(rp->r_unlname,
2650 2658 nnm, MAXNAMELEN);
2651 2659 rp->r_unlname[MAXNAMELEN - 1] = '\0';
2652 2660
2653 2661 if (ndvp != rp->r_unldvp) {
2654 2662 VN_RELE(rp->r_unldvp);
2655 2663 rp->r_unldvp = ndvp;
2656 2664 VN_HOLD(ndvp);
2657 2665 }
2658 2666 }
2659 2667 }
2660 2668 mutex_exit(&rp->r_statelock);
2661 2669 } else {
2662 2670 /*
2663 2671 * System V defines rename to return EEXIST, not
2664 2672 * ENOTEMPTY if the target directory is not empty.
2665 2673 * Over the wire, the error is NFSERR_ENOTEMPTY
2666 2674 * which geterrno maps to ENOTEMPTY.
2667 2675 */
2668 2676 if (error == ENOTEMPTY)
2669 2677 error = EEXIST;
2670 2678 }
2671 2679 }
2672 2680
2673 2681 if (error == 0) {
2674 2682 if (nvp)
2675 2683 vnevent_rename_dest(nvp, ndvp, nnm, ct);
2676 2684
2677 2685 if (odvp != ndvp)
2678 2686 vnevent_rename_dest_dir(ndvp, ct);
2679 2687
2680 2688 ASSERT(ovp != NULL);
2681 2689 vnevent_rename_src(ovp, odvp, onm, ct);
2682 2690 }
2683 2691
2684 2692 if (nvp) {
2685 2693 VN_RELE(nvp);
2686 2694 }
2687 2695 VN_RELE(ovp);
2688 2696
2689 2697 nfs_rw_exit(&odrp->r_rwlock);
2690 2698 nfs_rw_exit(&ndrp->r_rwlock);
2691 2699
2692 2700 return (error);
2693 2701 }
2694 2702
2695 2703 /* ARGSUSED */
2696 2704 static int
2697 2705 nfs_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
2698 2706 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2699 2707 {
2700 2708 int error;
2701 2709 struct nfscreatargs args;
2702 2710 struct nfsdiropres dr;
2703 2711 int douprintf;
2704 2712 rnode_t *drp;
2705 2713 hrtime_t t;
2706 2714
2707 2715 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2708 2716 return (EPERM);
2709 2717
2710 2718 setdiropargs(&args.ca_da, nm, dvp);
2711 2719
2712 2720 /*
2713 2721 * Decide what the group-id and set-gid bit of the created directory
2714 2722 * should be. May have to do a setattr to get the gid right.
2715 2723 */
2716 2724 error = setdirgid(dvp, &va->va_gid, cr);
2717 2725 if (error)
2718 2726 return (error);
2719 2727 error = setdirmode(dvp, &va->va_mode, cr);
2720 2728 if (error)
2721 2729 return (error);
2722 2730 va->va_mask |= AT_MODE|AT_GID;
2723 2731
2724 2732 args.ca_sa = &args.ca_sa_buf;
2725 2733 error = vattr_to_sattr(va, args.ca_sa);
2726 2734 if (error) {
2727 2735 /* req time field(s) overflow - return immediately */
2728 2736 return (error);
2729 2737 }
2730 2738
2731 2739 drp = VTOR(dvp);
2732 2740 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2733 2741 return (EINTR);
2734 2742
2735 2743 dnlc_remove(dvp, nm);
2736 2744
2737 2745 douprintf = 1;
2738 2746
2739 2747 t = gethrtime();
2740 2748
2741 2749 error = rfs2call(VTOMI(dvp), RFS_MKDIR,
2742 2750 xdr_creatargs, (caddr_t)&args,
2743 2751 xdr_diropres, (caddr_t)&dr, cr,
2744 2752 &douprintf, &dr.dr_status, 0, NULL);
2745 2753
2746 2754 PURGE_ATTRCACHE(dvp); /* mod time changed */
2747 2755
2748 2756 if (!error) {
2749 2757 error = geterrno(dr.dr_status);
2750 2758 if (!error) {
2751 2759 if (HAVE_RDDIR_CACHE(drp))
2752 2760 nfs_purge_rddir_cache(dvp);
2753 2761 /*
2754 2762 * The attributes returned by RFS_MKDIR can not
2755 2763 * be depended upon, so mark the attribute cache
2756 2764 * as purged. A subsequent GETATTR will get the
2757 2765 * correct attributes from the server.
2758 2766 */
2759 2767 *vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2760 2768 dvp->v_vfsp, t, cr, NULL, NULL);
2761 2769 PURGE_ATTRCACHE(*vpp);
2762 2770 dnlc_update(dvp, nm, *vpp);
2763 2771
2764 2772 /*
2765 2773 * Make sure the gid was set correctly.
2766 2774 * If not, try to set it (but don't lose
2767 2775 * any sleep over it).
2768 2776 */
2769 2777 if (va->va_gid != VTOR(*vpp)->r_attr.va_gid) {
2770 2778 va->va_mask = AT_GID;
2771 2779 (void) nfssetattr(*vpp, va, 0, cr);
2772 2780 }
2773 2781 } else {
2774 2782 PURGE_STALE_FH(error, dvp, cr);
2775 2783 }
2776 2784 }
2777 2785
2778 2786 nfs_rw_exit(&drp->r_rwlock);
2779 2787
2780 2788 return (error);
2781 2789 }
2782 2790
2783 2791 /* ARGSUSED */
2784 2792 static int
2785 2793 nfs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
2786 2794 caller_context_t *ct, int flags)
2787 2795 {
2788 2796 int error;
2789 2797 enum nfsstat status;
2790 2798 struct nfsdiropargs da;
2791 2799 vnode_t *vp;
2792 2800 int douprintf;
2793 2801 rnode_t *drp;
2794 2802
2795 2803 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2796 2804 return (EPERM);
2797 2805 drp = VTOR(dvp);
2798 2806 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2799 2807 return (EINTR);
2800 2808
2801 2809 /*
2802 2810 * Attempt to prevent a rmdir(".") from succeeding.
2803 2811 */
2804 2812 error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2805 2813 if (error) {
2806 2814 nfs_rw_exit(&drp->r_rwlock);
2807 2815 return (error);
2808 2816 }
2809 2817
2810 2818 if (vp == cdir) {
2811 2819 VN_RELE(vp);
2812 2820 nfs_rw_exit(&drp->r_rwlock);
2813 2821 return (EINVAL);
2814 2822 }
2815 2823
2816 2824 setdiropargs(&da, nm, dvp);
2817 2825
2818 2826 /*
2819 2827 * First just remove the entry from the name cache, as it
2820 2828 * is most likely an entry for this vp.
2821 2829 */
2822 2830 dnlc_remove(dvp, nm);
2823 2831
2824 2832 /*
2825 2833 * If there vnode reference count is greater than one, then
2826 2834 * there may be additional references in the DNLC which will
2827 2835 * need to be purged. First, trying removing the entry for
2828 2836 * the parent directory and see if that removes the additional
2829 2837 * reference(s). If that doesn't do it, then use dnlc_purge_vp
2830 2838 * to completely remove any references to the directory which
2831 2839 * might still exist in the DNLC.
2832 2840 */
2833 2841 if (vp->v_count > 1) {
2834 2842 dnlc_remove(vp, "..");
2835 2843 if (vp->v_count > 1)
2836 2844 dnlc_purge_vp(vp);
2837 2845 }
2838 2846
2839 2847 douprintf = 1;
2840 2848
2841 2849 error = rfs2call(VTOMI(dvp), RFS_RMDIR,
2842 2850 xdr_diropargs, (caddr_t)&da,
2843 2851 xdr_enum, (caddr_t)&status, cr,
2844 2852 &douprintf, &status, 0, NULL);
2845 2853
2846 2854 PURGE_ATTRCACHE(dvp); /* mod time changed */
2847 2855
2848 2856 if (error) {
2849 2857 VN_RELE(vp);
2850 2858 nfs_rw_exit(&drp->r_rwlock);
2851 2859 return (error);
2852 2860 }
2853 2861
2854 2862 error = geterrno(status);
2855 2863 if (!error) {
2856 2864 if (HAVE_RDDIR_CACHE(drp))
2857 2865 nfs_purge_rddir_cache(dvp);
2858 2866 if (HAVE_RDDIR_CACHE(VTOR(vp)))
2859 2867 nfs_purge_rddir_cache(vp);
2860 2868 } else {
2861 2869 PURGE_STALE_FH(error, dvp, cr);
2862 2870 /*
2863 2871 * System V defines rmdir to return EEXIST, not
2864 2872 * ENOTEMPTY if the directory is not empty. Over
2865 2873 * the wire, the error is NFSERR_ENOTEMPTY which
2866 2874 * geterrno maps to ENOTEMPTY.
2867 2875 */
2868 2876 if (error == ENOTEMPTY)
2869 2877 error = EEXIST;
2870 2878 }
2871 2879
2872 2880 if (error == 0) {
2873 2881 vnevent_rmdir(vp, dvp, nm, ct);
2874 2882 }
2875 2883 VN_RELE(vp);
2876 2884
2877 2885 nfs_rw_exit(&drp->r_rwlock);
2878 2886
2879 2887 return (error);
2880 2888 }
2881 2889
2882 2890 /* ARGSUSED */
2883 2891 static int
2884 2892 nfs_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
2885 2893 caller_context_t *ct, int flags)
2886 2894 {
2887 2895 int error;
2888 2896 struct nfsslargs args;
2889 2897 enum nfsstat status;
2890 2898 int douprintf;
2891 2899 rnode_t *drp;
2892 2900
2893 2901 if (nfs_zone() != VTOMI(dvp)->mi_zone)
2894 2902 return (EPERM);
2895 2903 setdiropargs(&args.sla_from, lnm, dvp);
2896 2904 args.sla_sa = &args.sla_sa_buf;
2897 2905 error = vattr_to_sattr(tva, args.sla_sa);
2898 2906 if (error) {
2899 2907 /* req time field(s) overflow - return immediately */
2900 2908 return (error);
2901 2909 }
2902 2910 args.sla_tnm = tnm;
2903 2911
2904 2912 drp = VTOR(dvp);
2905 2913 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2906 2914 return (EINTR);
2907 2915
2908 2916 dnlc_remove(dvp, lnm);
2909 2917
2910 2918 douprintf = 1;
2911 2919
2912 2920 error = rfs2call(VTOMI(dvp), RFS_SYMLINK,
2913 2921 xdr_slargs, (caddr_t)&args,
2914 2922 xdr_enum, (caddr_t)&status, cr,
2915 2923 &douprintf, &status, 0, NULL);
2916 2924
2917 2925 PURGE_ATTRCACHE(dvp); /* mod time changed */
2918 2926
2919 2927 if (!error) {
2920 2928 error = geterrno(status);
2921 2929 if (!error) {
2922 2930 if (HAVE_RDDIR_CACHE(drp))
2923 2931 nfs_purge_rddir_cache(dvp);
2924 2932 } else {
2925 2933 PURGE_STALE_FH(error, dvp, cr);
2926 2934 }
2927 2935 }
2928 2936
2929 2937 nfs_rw_exit(&drp->r_rwlock);
2930 2938
2931 2939 return (error);
2932 2940 }
2933 2941
2934 2942 #ifdef DEBUG
2935 2943 static int nfs_readdir_cache_hits = 0;
2936 2944 static int nfs_readdir_cache_shorts = 0;
2937 2945 static int nfs_readdir_cache_waits = 0;
2938 2946 static int nfs_readdir_cache_misses = 0;
2939 2947 static int nfs_readdir_readahead = 0;
2940 2948 #endif
2941 2949
2942 2950 static int nfs_shrinkreaddir = 0;
2943 2951
2944 2952 /*
2945 2953 * Read directory entries.
2946 2954 * There are some weird things to look out for here. The uio_offset
2947 2955 * field is either 0 or it is the offset returned from a previous
2948 2956 * readdir. It is an opaque value used by the server to find the
2949 2957 * correct directory block to read. The count field is the number
2950 2958 * of blocks to read on the server. This is advisory only, the server
2951 2959 * may return only one block's worth of entries. Entries may be compressed
2952 2960 * on the server.
2953 2961 */
2954 2962 /* ARGSUSED */
2955 2963 static int
2956 2964 nfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
2957 2965 caller_context_t *ct, int flags)
2958 2966 {
2959 2967 int error;
2960 2968 size_t count;
2961 2969 rnode_t *rp;
2962 2970 rddir_cache *rdc;
2963 2971 rddir_cache *nrdc;
2964 2972 rddir_cache *rrdc;
2965 2973 #ifdef DEBUG
2966 2974 int missed;
2967 2975 #endif
2968 2976 rddir_cache srdc;
2969 2977 avl_index_t where;
2970 2978
2971 2979 rp = VTOR(vp);
2972 2980
2973 2981 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2974 2982 if (nfs_zone() != VTOMI(vp)->mi_zone)
2975 2983 return (EIO);
2976 2984 /*
2977 2985 * Make sure that the directory cache is valid.
2978 2986 */
2979 2987 if (HAVE_RDDIR_CACHE(rp)) {
2980 2988 if (nfs_disable_rddir_cache) {
2981 2989 /*
2982 2990 * Setting nfs_disable_rddir_cache in /etc/system
2983 2991 * allows interoperability with servers that do not
2984 2992 * properly update the attributes of directories.
2985 2993 * Any cached information gets purged before an
2986 2994 * access is made to it.
2987 2995 */
2988 2996 nfs_purge_rddir_cache(vp);
2989 2997 } else {
2990 2998 error = nfs_validate_caches(vp, cr);
2991 2999 if (error)
2992 3000 return (error);
2993 3001 }
2994 3002 }
2995 3003
2996 3004 /*
2997 3005 * UGLINESS: SunOS 3.2 servers apparently cannot always handle an
2998 3006 * RFS_READDIR request with rda_count set to more than 0x400. So
2999 3007 * we reduce the request size here purely for compatibility.
3000 3008 *
3001 3009 * In general, this is no longer required. However, if a server
3002 3010 * is discovered which can not handle requests larger than 1024,
3003 3011 * nfs_shrinkreaddir can be set to 1 to enable this backwards
3004 3012 * compatibility.
3005 3013 *
3006 3014 * In any case, the request size is limited to NFS_MAXDATA bytes.
3007 3015 */
3008 3016 count = MIN(uiop->uio_iov->iov_len,
3009 3017 nfs_shrinkreaddir ? 0x400 : NFS_MAXDATA);
3010 3018
3011 3019 nrdc = NULL;
3012 3020 #ifdef DEBUG
3013 3021 missed = 0;
3014 3022 #endif
3015 3023 top:
3016 3024 /*
3017 3025 * Short circuit last readdir which always returns 0 bytes.
3018 3026 * This can be done after the directory has been read through
3019 3027 * completely at least once. This will set r_direof which
3020 3028 * can be used to find the value of the last cookie.
3021 3029 */
3022 3030 mutex_enter(&rp->r_statelock);
3023 3031 if (rp->r_direof != NULL &&
3024 3032 uiop->uio_offset == rp->r_direof->nfs_ncookie) {
3025 3033 mutex_exit(&rp->r_statelock);
3026 3034 #ifdef DEBUG
3027 3035 nfs_readdir_cache_shorts++;
3028 3036 #endif
3029 3037 if (eofp)
3030 3038 *eofp = 1;
3031 3039 if (nrdc != NULL)
3032 3040 rddir_cache_rele(nrdc);
3033 3041 return (0);
3034 3042 }
3035 3043 /*
3036 3044 * Look for a cache entry. Cache entries are identified
3037 3045 * by the NFS cookie value and the byte count requested.
3038 3046 */
3039 3047 srdc.nfs_cookie = uiop->uio_offset;
3040 3048 srdc.buflen = count;
3041 3049 rdc = avl_find(&rp->r_dir, &srdc, &where);
3042 3050 if (rdc != NULL) {
3043 3051 rddir_cache_hold(rdc);
3044 3052 /*
3045 3053 * If the cache entry is in the process of being
3046 3054 * filled in, wait until this completes. The
3047 3055 * RDDIRWAIT bit is set to indicate that someone
3048 3056 * is waiting and then the thread currently
3049 3057 * filling the entry is done, it should do a
3050 3058 * cv_broadcast to wakeup all of the threads
3051 3059 * waiting for it to finish.
3052 3060 */
3053 3061 if (rdc->flags & RDDIR) {
3054 3062 nfs_rw_exit(&rp->r_rwlock);
3055 3063 rdc->flags |= RDDIRWAIT;
3056 3064 #ifdef DEBUG
3057 3065 nfs_readdir_cache_waits++;
3058 3066 #endif
3059 3067 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3060 3068 /*
3061 3069 * We got interrupted, probably
3062 3070 * the user typed ^C or an alarm
3063 3071 * fired. We free the new entry
3064 3072 * if we allocated one.
3065 3073 */
3066 3074 mutex_exit(&rp->r_statelock);
3067 3075 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3068 3076 RW_READER, FALSE);
3069 3077 rddir_cache_rele(rdc);
3070 3078 if (nrdc != NULL)
3071 3079 rddir_cache_rele(nrdc);
3072 3080 return (EINTR);
3073 3081 }
3074 3082 mutex_exit(&rp->r_statelock);
3075 3083 (void) nfs_rw_enter_sig(&rp->r_rwlock,
3076 3084 RW_READER, FALSE);
3077 3085 rddir_cache_rele(rdc);
3078 3086 goto top;
3079 3087 }
3080 3088 /*
3081 3089 * Check to see if a readdir is required to
3082 3090 * fill the entry. If so, mark this entry
3083 3091 * as being filled, remove our reference,
3084 3092 * and branch to the code to fill the entry.
3085 3093 */
3086 3094 if (rdc->flags & RDDIRREQ) {
3087 3095 rdc->flags &= ~RDDIRREQ;
3088 3096 rdc->flags |= RDDIR;
3089 3097 if (nrdc != NULL)
3090 3098 rddir_cache_rele(nrdc);
3091 3099 nrdc = rdc;
3092 3100 mutex_exit(&rp->r_statelock);
3093 3101 goto bottom;
3094 3102 }
3095 3103 #ifdef DEBUG
3096 3104 if (!missed)
3097 3105 nfs_readdir_cache_hits++;
3098 3106 #endif
3099 3107 /*
3100 3108 * If an error occurred while attempting
3101 3109 * to fill the cache entry, just return it.
3102 3110 */
3103 3111 if (rdc->error) {
3104 3112 error = rdc->error;
3105 3113 mutex_exit(&rp->r_statelock);
3106 3114 rddir_cache_rele(rdc);
3107 3115 if (nrdc != NULL)
3108 3116 rddir_cache_rele(nrdc);
3109 3117 return (error);
3110 3118 }
3111 3119
3112 3120 /*
3113 3121 * The cache entry is complete and good,
3114 3122 * copyout the dirent structs to the calling
3115 3123 * thread.
3116 3124 */
3117 3125 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3118 3126
3119 3127 /*
3120 3128 * If no error occurred during the copyout,
3121 3129 * update the offset in the uio struct to
3122 3130 * contain the value of the next cookie
3123 3131 * and set the eof value appropriately.
3124 3132 */
3125 3133 if (!error) {
3126 3134 uiop->uio_offset = rdc->nfs_ncookie;
3127 3135 if (eofp)
3128 3136 *eofp = rdc->eof;
3129 3137 }
3130 3138
3131 3139 /*
3132 3140 * Decide whether to do readahead. Don't if
3133 3141 * have already read to the end of directory.
3134 3142 */
3135 3143 if (rdc->eof) {
3136 3144 rp->r_direof = rdc;
3137 3145 mutex_exit(&rp->r_statelock);
3138 3146 rddir_cache_rele(rdc);
3139 3147 if (nrdc != NULL)
3140 3148 rddir_cache_rele(nrdc);
3141 3149 return (error);
3142 3150 }
3143 3151
3144 3152 /*
3145 3153 * Check to see whether we found an entry
3146 3154 * for the readahead. If so, we don't need
3147 3155 * to do anything further, so free the new
3148 3156 * entry if one was allocated. Otherwise,
3149 3157 * allocate a new entry, add it to the cache,
3150 3158 * and then initiate an asynchronous readdir
3151 3159 * operation to fill it.
3152 3160 */
3153 3161 srdc.nfs_cookie = rdc->nfs_ncookie;
3154 3162 srdc.buflen = count;
3155 3163 rrdc = avl_find(&rp->r_dir, &srdc, &where);
3156 3164 if (rrdc != NULL) {
3157 3165 if (nrdc != NULL)
3158 3166 rddir_cache_rele(nrdc);
3159 3167 } else {
3160 3168 if (nrdc != NULL)
3161 3169 rrdc = nrdc;
3162 3170 else {
3163 3171 rrdc = rddir_cache_alloc(KM_NOSLEEP);
3164 3172 }
3165 3173 if (rrdc != NULL) {
3166 3174 rrdc->nfs_cookie = rdc->nfs_ncookie;
3167 3175 rrdc->buflen = count;
3168 3176 avl_insert(&rp->r_dir, rrdc, where);
3169 3177 rddir_cache_hold(rrdc);
3170 3178 mutex_exit(&rp->r_statelock);
3171 3179 rddir_cache_rele(rdc);
3172 3180 #ifdef DEBUG
3173 3181 nfs_readdir_readahead++;
3174 3182 #endif
3175 3183 nfs_async_readdir(vp, rrdc, cr, nfsreaddir);
3176 3184 return (error);
3177 3185 }
3178 3186 }
3179 3187
3180 3188 mutex_exit(&rp->r_statelock);
3181 3189 rddir_cache_rele(rdc);
3182 3190 return (error);
3183 3191 }
3184 3192
3185 3193 /*
3186 3194 * Didn't find an entry in the cache. Construct a new empty
3187 3195 * entry and link it into the cache. Other processes attempting
3188 3196 * to access this entry will need to wait until it is filled in.
3189 3197 *
3190 3198 * Since kmem_alloc may block, another pass through the cache
3191 3199 * will need to be taken to make sure that another process
3192 3200 * hasn't already added an entry to the cache for this request.
3193 3201 */
3194 3202 if (nrdc == NULL) {
3195 3203 mutex_exit(&rp->r_statelock);
3196 3204 nrdc = rddir_cache_alloc(KM_SLEEP);
3197 3205 nrdc->nfs_cookie = uiop->uio_offset;
3198 3206 nrdc->buflen = count;
3199 3207 goto top;
3200 3208 }
3201 3209
3202 3210 /*
3203 3211 * Add this entry to the cache.
3204 3212 */
3205 3213 avl_insert(&rp->r_dir, nrdc, where);
3206 3214 rddir_cache_hold(nrdc);
3207 3215 mutex_exit(&rp->r_statelock);
3208 3216
3209 3217 bottom:
3210 3218 #ifdef DEBUG
3211 3219 missed = 1;
3212 3220 nfs_readdir_cache_misses++;
3213 3221 #endif
3214 3222 /*
3215 3223 * Do the readdir.
3216 3224 */
3217 3225 error = nfsreaddir(vp, nrdc, cr);
3218 3226
3219 3227 /*
3220 3228 * If this operation failed, just return the error which occurred.
3221 3229 */
3222 3230 if (error != 0)
3223 3231 return (error);
3224 3232
3225 3233 /*
3226 3234 * Since the RPC operation will have taken sometime and blocked
3227 3235 * this process, another pass through the cache will need to be
3228 3236 * taken to find the correct cache entry. It is possible that
3229 3237 * the correct cache entry will not be there (although one was
3230 3238 * added) because the directory changed during the RPC operation
3231 3239 * and the readdir cache was flushed. In this case, just start
3232 3240 * over. It is hoped that this will not happen too often... :-)
3233 3241 */
3234 3242 nrdc = NULL;
3235 3243 goto top;
3236 3244 /* NOTREACHED */
3237 3245 }
3238 3246
3239 3247 static int
3240 3248 nfsreaddir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
3241 3249 {
3242 3250 int error;
3243 3251 struct nfsrddirargs rda;
3244 3252 struct nfsrddirres rd;
3245 3253 rnode_t *rp;
3246 3254 mntinfo_t *mi;
3247 3255 uint_t count;
3248 3256 int douprintf;
3249 3257 failinfo_t fi, *fip;
3250 3258
3251 3259 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3252 3260 count = rdc->buflen;
3253 3261
3254 3262 rp = VTOR(vp);
3255 3263 mi = VTOMI(vp);
3256 3264
3257 3265 rda.rda_fh = *VTOFH(vp);
3258 3266 rda.rda_offset = rdc->nfs_cookie;
3259 3267
3260 3268 /*
3261 3269 * NFS client failover support
3262 3270 * suppress failover unless we have a zero cookie
3263 3271 */
3264 3272 if (rdc->nfs_cookie == (off_t)0) {
3265 3273 fi.vp = vp;
3266 3274 fi.fhp = (caddr_t)&rda.rda_fh;
3267 3275 fi.copyproc = nfscopyfh;
3268 3276 fi.lookupproc = nfslookup;
3269 3277 fi.xattrdirproc = acl_getxattrdir2;
3270 3278 fip = &fi;
3271 3279 } else {
3272 3280 fip = NULL;
3273 3281 }
3274 3282
3275 3283 rd.rd_entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3276 3284 rd.rd_size = count;
3277 3285 rd.rd_offset = rda.rda_offset;
3278 3286
3279 3287 douprintf = 1;
3280 3288
3281 3289 if (mi->mi_io_kstats) {
3282 3290 mutex_enter(&mi->mi_lock);
3283 3291 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3284 3292 mutex_exit(&mi->mi_lock);
3285 3293 }
3286 3294
3287 3295 do {
3288 3296 rda.rda_count = MIN(count, mi->mi_curread);
3289 3297 error = rfs2call(mi, RFS_READDIR,
3290 3298 xdr_rddirargs, (caddr_t)&rda,
3291 3299 xdr_getrddirres, (caddr_t)&rd, cr,
3292 3300 &douprintf, &rd.rd_status, 0, fip);
3293 3301 } while (error == ENFS_TRYAGAIN);
3294 3302
3295 3303 if (mi->mi_io_kstats) {
3296 3304 mutex_enter(&mi->mi_lock);
3297 3305 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3298 3306 mutex_exit(&mi->mi_lock);
3299 3307 }
3300 3308
3301 3309 /*
3302 3310 * Since we are actually doing a READDIR RPC, we must have
3303 3311 * exclusive access to the cache entry being filled. Thus,
3304 3312 * it is safe to update all fields except for the flags
3305 3313 * field. The r_statelock in the rnode must be held to
3306 3314 * prevent two different threads from simultaneously
3307 3315 * attempting to update the flags field. This can happen
3308 3316 * if we are turning off RDDIR and the other thread is
3309 3317 * trying to set RDDIRWAIT.
3310 3318 */
3311 3319 ASSERT(rdc->flags & RDDIR);
3312 3320 if (!error) {
3313 3321 error = geterrno(rd.rd_status);
3314 3322 if (!error) {
3315 3323 rdc->nfs_ncookie = rd.rd_offset;
3316 3324 rdc->eof = rd.rd_eof ? 1 : 0;
3317 3325 rdc->entlen = rd.rd_size;
3318 3326 ASSERT(rdc->entlen <= rdc->buflen);
3319 3327 #ifdef DEBUG
3320 3328 rdc->entries = rddir_cache_buf_alloc(rdc->buflen,
3321 3329 KM_SLEEP);
3322 3330 #else
3323 3331 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3324 3332 #endif
3325 3333 bcopy(rd.rd_entries, rdc->entries, rdc->entlen);
3326 3334 rdc->error = 0;
3327 3335 if (mi->mi_io_kstats) {
3328 3336 mutex_enter(&mi->mi_lock);
3329 3337 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3330 3338 KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
3331 3339 rd.rd_size;
3332 3340 mutex_exit(&mi->mi_lock);
3333 3341 }
3334 3342 } else {
3335 3343 PURGE_STALE_FH(error, vp, cr);
3336 3344 }
3337 3345 }
3338 3346 if (error) {
3339 3347 rdc->entries = NULL;
3340 3348 rdc->error = error;
3341 3349 }
3342 3350 kmem_free(rd.rd_entries, rdc->buflen);
3343 3351
3344 3352 mutex_enter(&rp->r_statelock);
3345 3353 rdc->flags &= ~RDDIR;
3346 3354 if (rdc->flags & RDDIRWAIT) {
3347 3355 rdc->flags &= ~RDDIRWAIT;
3348 3356 cv_broadcast(&rdc->cv);
3349 3357 }
3350 3358 if (error)
3351 3359 rdc->flags |= RDDIRREQ;
3352 3360 mutex_exit(&rp->r_statelock);
3353 3361
3354 3362 rddir_cache_rele(rdc);
3355 3363
3356 3364 return (error);
3357 3365 }
3358 3366
3359 3367 #ifdef DEBUG
3360 3368 static int nfs_bio_do_stop = 0;
3361 3369 #endif
3362 3370
3363 3371 static int
3364 3372 nfs_bio(struct buf *bp, cred_t *cr)
3365 3373 {
3366 3374 rnode_t *rp = VTOR(bp->b_vp);
3367 3375 int count;
3368 3376 int error;
3369 3377 cred_t *cred;
3370 3378 uint_t offset;
3371 3379
3372 3380 DTRACE_IO1(start, struct buf *, bp);
3373 3381
3374 3382 ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
3375 3383 offset = dbtob(bp->b_blkno);
3376 3384
3377 3385 if (bp->b_flags & B_READ) {
3378 3386 mutex_enter(&rp->r_statelock);
3379 3387 if (rp->r_cred != NULL) {
3380 3388 cred = rp->r_cred;
3381 3389 crhold(cred);
3382 3390 } else {
3383 3391 rp->r_cred = cr;
3384 3392 crhold(cr);
3385 3393 cred = cr;
3386 3394 crhold(cred);
3387 3395 }
3388 3396 mutex_exit(&rp->r_statelock);
3389 3397 read_again:
3390 3398 error = bp->b_error = nfsread(bp->b_vp, bp->b_un.b_addr,
3391 3399 offset, bp->b_bcount, &bp->b_resid, cred);
3392 3400
3393 3401 crfree(cred);
3394 3402 if (!error) {
3395 3403 if (bp->b_resid) {
3396 3404 /*
3397 3405 * Didn't get it all because we hit EOF,
3398 3406 * zero all the memory beyond the EOF.
3399 3407 */
3400 3408 /* bzero(rdaddr + */
3401 3409 bzero(bp->b_un.b_addr +
3402 3410 bp->b_bcount - bp->b_resid, bp->b_resid);
3403 3411 }
3404 3412 mutex_enter(&rp->r_statelock);
3405 3413 if (bp->b_resid == bp->b_bcount &&
3406 3414 offset >= rp->r_size) {
3407 3415 /*
3408 3416 * We didn't read anything at all as we are
3409 3417 * past EOF. Return an error indicator back
3410 3418 * but don't destroy the pages (yet).
3411 3419 */
3412 3420 error = NFS_EOF;
3413 3421 }
3414 3422 mutex_exit(&rp->r_statelock);
3415 3423 } else if (error == EACCES) {
3416 3424 mutex_enter(&rp->r_statelock);
3417 3425 if (cred != cr) {
3418 3426 if (rp->r_cred != NULL)
3419 3427 crfree(rp->r_cred);
3420 3428 rp->r_cred = cr;
3421 3429 crhold(cr);
3422 3430 cred = cr;
3423 3431 crhold(cred);
3424 3432 mutex_exit(&rp->r_statelock);
3425 3433 goto read_again;
3426 3434 }
3427 3435 mutex_exit(&rp->r_statelock);
3428 3436 }
3429 3437 } else {
3430 3438 if (!(rp->r_flags & RSTALE)) {
3431 3439 mutex_enter(&rp->r_statelock);
3432 3440 if (rp->r_cred != NULL) {
3433 3441 cred = rp->r_cred;
3434 3442 crhold(cred);
3435 3443 } else {
3436 3444 rp->r_cred = cr;
3437 3445 crhold(cr);
3438 3446 cred = cr;
3439 3447 crhold(cred);
3440 3448 }
3441 3449 mutex_exit(&rp->r_statelock);
3442 3450 write_again:
3443 3451 mutex_enter(&rp->r_statelock);
3444 3452 count = MIN(bp->b_bcount, rp->r_size - offset);
3445 3453 mutex_exit(&rp->r_statelock);
3446 3454 if (count < 0)
3447 3455 cmn_err(CE_PANIC, "nfs_bio: write count < 0");
3448 3456 #ifdef DEBUG
3449 3457 if (count == 0) {
3450 3458 zcmn_err(getzoneid(), CE_WARN,
3451 3459 "nfs_bio: zero length write at %d",
3452 3460 offset);
3453 3461 nfs_printfhandle(&rp->r_fh);
3454 3462 if (nfs_bio_do_stop)
3455 3463 debug_enter("nfs_bio");
3456 3464 }
3457 3465 #endif
3458 3466 error = nfswrite(bp->b_vp, bp->b_un.b_addr, offset,
3459 3467 count, cred);
3460 3468 if (error == EACCES) {
3461 3469 mutex_enter(&rp->r_statelock);
3462 3470 if (cred != cr) {
3463 3471 if (rp->r_cred != NULL)
3464 3472 crfree(rp->r_cred);
3465 3473 rp->r_cred = cr;
3466 3474 crhold(cr);
3467 3475 crfree(cred);
3468 3476 cred = cr;
3469 3477 crhold(cred);
3470 3478 mutex_exit(&rp->r_statelock);
3471 3479 goto write_again;
3472 3480 }
3473 3481 mutex_exit(&rp->r_statelock);
3474 3482 }
3475 3483 bp->b_error = error;
3476 3484 if (error && error != EINTR) {
3477 3485 /*
3478 3486 * Don't print EDQUOT errors on the console.
3479 3487 * Don't print asynchronous EACCES errors.
3480 3488 * Don't print EFBIG errors.
3481 3489 * Print all other write errors.
3482 3490 */
3483 3491 if (error != EDQUOT && error != EFBIG &&
3484 3492 (error != EACCES ||
3485 3493 !(bp->b_flags & B_ASYNC)))
3486 3494 nfs_write_error(bp->b_vp, error, cred);
3487 3495 /*
3488 3496 * Update r_error and r_flags as appropriate.
3489 3497 * If the error was ESTALE, then mark the
3490 3498 * rnode as not being writeable and save
3491 3499 * the error status. Otherwise, save any
3492 3500 * errors which occur from asynchronous
3493 3501 * page invalidations. Any errors occurring
3494 3502 * from other operations should be saved
3495 3503 * by the caller.
3496 3504 */
3497 3505 mutex_enter(&rp->r_statelock);
3498 3506 if (error == ESTALE) {
3499 3507 rp->r_flags |= RSTALE;
3500 3508 if (!rp->r_error)
3501 3509 rp->r_error = error;
3502 3510 } else if (!rp->r_error &&
3503 3511 (bp->b_flags &
3504 3512 (B_INVAL|B_FORCE|B_ASYNC)) ==
3505 3513 (B_INVAL|B_FORCE|B_ASYNC)) {
3506 3514 rp->r_error = error;
3507 3515 }
3508 3516 mutex_exit(&rp->r_statelock);
3509 3517 }
3510 3518 crfree(cred);
3511 3519 } else {
3512 3520 error = rp->r_error;
3513 3521 /*
3514 3522 * A close may have cleared r_error, if so,
3515 3523 * propagate ESTALE error return properly
3516 3524 */
3517 3525 if (error == 0)
3518 3526 error = ESTALE;
3519 3527 }
3520 3528 }
3521 3529
3522 3530 if (error != 0 && error != NFS_EOF)
3523 3531 bp->b_flags |= B_ERROR;
3524 3532
3525 3533 DTRACE_IO1(done, struct buf *, bp);
3526 3534
3527 3535 return (error);
3528 3536 }
3529 3537
3530 3538 /* ARGSUSED */
3531 3539 static int
3532 3540 nfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3533 3541 {
3534 3542 struct nfs_fid *fp;
3535 3543 rnode_t *rp;
3536 3544
3537 3545 rp = VTOR(vp);
3538 3546
3539 3547 if (fidp->fid_len < (sizeof (struct nfs_fid) - sizeof (short))) {
3540 3548 fidp->fid_len = sizeof (struct nfs_fid) - sizeof (short);
3541 3549 return (ENOSPC);
3542 3550 }
3543 3551 fp = (struct nfs_fid *)fidp;
3544 3552 fp->nf_pad = 0;
3545 3553 fp->nf_len = sizeof (struct nfs_fid) - sizeof (short);
3546 3554 bcopy(rp->r_fh.fh_buf, fp->nf_data, NFS_FHSIZE);
3547 3555 return (0);
3548 3556 }
3549 3557
3550 3558 /* ARGSUSED2 */
3551 3559 static int
3552 3560 nfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3553 3561 {
3554 3562 rnode_t *rp = VTOR(vp);
3555 3563
3556 3564 if (!write_lock) {
3557 3565 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3558 3566 return (V_WRITELOCK_FALSE);
3559 3567 }
3560 3568
3561 3569 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
3562 3570 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3563 3571 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
3564 3572 return (V_WRITELOCK_FALSE);
3565 3573 nfs_rw_exit(&rp->r_rwlock);
3566 3574 }
3567 3575
3568 3576 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
3569 3577 return (V_WRITELOCK_TRUE);
3570 3578 }
3571 3579
3572 3580 /* ARGSUSED */
3573 3581 static void
3574 3582 nfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3575 3583 {
3576 3584 rnode_t *rp = VTOR(vp);
3577 3585
3578 3586 nfs_rw_exit(&rp->r_rwlock);
3579 3587 }
3580 3588
3581 3589 /* ARGSUSED */
3582 3590 static int
3583 3591 nfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
3584 3592 {
3585 3593
3586 3594 /*
3587 3595 * Because we stuff the readdir cookie into the offset field
3588 3596 * someone may attempt to do an lseek with the cookie which
3589 3597 * we want to succeed.
3590 3598 */
3591 3599 if (vp->v_type == VDIR)
3592 3600 return (0);
3593 3601 if (*noffp < 0 || *noffp > MAXOFF32_T)
3594 3602 return (EINVAL);
3595 3603 return (0);
3596 3604 }
3597 3605
3598 3606 /*
3599 3607 * number of NFS_MAXDATA blocks to read ahead
3600 3608 * optimized for 100 base-T.
3601 3609 */
3602 3610 static int nfs_nra = 4;
3603 3611
3604 3612 #ifdef DEBUG
3605 3613 static int nfs_lostpage = 0; /* number of times we lost original page */
3606 3614 #endif
3607 3615
3608 3616 /*
3609 3617 * Return all the pages from [off..off+len) in file
3610 3618 */
3611 3619 /* ARGSUSED */
3612 3620 static int
3613 3621 nfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3614 3622 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3615 3623 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3616 3624 {
3617 3625 rnode_t *rp;
3618 3626 int error;
3619 3627 mntinfo_t *mi;
3620 3628
3621 3629 if (vp->v_flag & VNOMAP)
3622 3630 return (ENOSYS);
3623 3631
3624 3632 ASSERT(off <= MAXOFF32_T);
3625 3633 if (nfs_zone() != VTOMI(vp)->mi_zone)
3626 3634 return (EIO);
3627 3635 if (protp != NULL)
3628 3636 *protp = PROT_ALL;
3629 3637
3630 3638 /*
3631 3639 * Now valididate that the caches are up to date.
3632 3640 */
3633 3641 error = nfs_validate_caches(vp, cr);
3634 3642 if (error)
3635 3643 return (error);
3636 3644
3637 3645 rp = VTOR(vp);
3638 3646 mi = VTOMI(vp);
3639 3647 retry:
3640 3648 mutex_enter(&rp->r_statelock);
3641 3649
3642 3650 /*
3643 3651 * Don't create dirty pages faster than they
3644 3652 * can be cleaned so that the system doesn't
3645 3653 * get imbalanced. If the async queue is
3646 3654 * maxed out, then wait for it to drain before
3647 3655 * creating more dirty pages. Also, wait for
3648 3656 * any threads doing pagewalks in the vop_getattr
3649 3657 * entry points so that they don't block for
3650 3658 * long periods.
3651 3659 */
3652 3660 if (rw == S_CREATE) {
3653 3661 while ((mi->mi_max_threads != 0 &&
3654 3662 rp->r_awcount > 2 * mi->mi_max_threads) ||
3655 3663 rp->r_gcount > 0)
3656 3664 cv_wait(&rp->r_cv, &rp->r_statelock);
3657 3665 }
3658 3666
3659 3667 /*
3660 3668 * If we are getting called as a side effect of an nfs_write()
3661 3669 * operation the local file size might not be extended yet.
3662 3670 * In this case we want to be able to return pages of zeroes.
3663 3671 */
3664 3672 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
3665 3673 mutex_exit(&rp->r_statelock);
3666 3674 return (EFAULT); /* beyond EOF */
3667 3675 }
3668 3676
3669 3677 mutex_exit(&rp->r_statelock);
3670 3678
3671 3679 if (len <= PAGESIZE) {
3672 3680 error = nfs_getapage(vp, off, len, protp, pl, plsz,
3673 3681 seg, addr, rw, cr);
3674 3682 } else {
3675 3683 error = pvn_getpages(nfs_getapage, vp, off, len, protp,
3676 3684 pl, plsz, seg, addr, rw, cr);
3677 3685 }
3678 3686
3679 3687 switch (error) {
3680 3688 case NFS_EOF:
3681 3689 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
3682 3690 goto retry;
3683 3691 case ESTALE:
3684 3692 PURGE_STALE_FH(error, vp, cr);
3685 3693 }
3686 3694
3687 3695 return (error);
3688 3696 }
3689 3697
3690 3698 /*
3691 3699 * Called from pvn_getpages or nfs_getpage to get a particular page.
3692 3700 */
3693 3701 /* ARGSUSED */
3694 3702 static int
3695 3703 nfs_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
3696 3704 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3697 3705 enum seg_rw rw, cred_t *cr)
3698 3706 {
3699 3707 rnode_t *rp;
3700 3708 uint_t bsize;
3701 3709 struct buf *bp;
3702 3710 page_t *pp;
3703 3711 u_offset_t lbn;
3704 3712 u_offset_t io_off;
3705 3713 u_offset_t blkoff;
3706 3714 u_offset_t rablkoff;
3707 3715 size_t io_len;
3708 3716 uint_t blksize;
3709 3717 int error;
3710 3718 int readahead;
3711 3719 int readahead_issued = 0;
3712 3720 int ra_window; /* readahead window */
3713 3721 page_t *pagefound;
3714 3722
3715 3723 if (nfs_zone() != VTOMI(vp)->mi_zone)
3716 3724 return (EIO);
3717 3725 rp = VTOR(vp);
3718 3726 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3719 3727
3720 3728 reread:
3721 3729 bp = NULL;
3722 3730 pp = NULL;
3723 3731 pagefound = NULL;
3724 3732
3725 3733 if (pl != NULL)
3726 3734 pl[0] = NULL;
3727 3735
3728 3736 error = 0;
3729 3737 lbn = off / bsize;
3730 3738 blkoff = lbn * bsize;
3731 3739
3732 3740 /*
3733 3741 * Queueing up the readahead before doing the synchronous read
3734 3742 * results in a significant increase in read throughput because
3735 3743 * of the increased parallelism between the async threads and
3736 3744 * the process context.
3737 3745 */
3738 3746 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
3739 3747 rw != S_CREATE &&
3740 3748 !(vp->v_flag & VNOCACHE)) {
3741 3749 mutex_enter(&rp->r_statelock);
3742 3750
3743 3751 /*
3744 3752 * Calculate the number of readaheads to do.
3745 3753 * a) No readaheads at offset = 0.
3746 3754 * b) Do maximum(nfs_nra) readaheads when the readahead
3747 3755 * window is closed.
3748 3756 * c) Do readaheads between 1 to (nfs_nra - 1) depending
3749 3757 * upon how far the readahead window is open or close.
3750 3758 * d) No readaheads if rp->r_nextr is not within the scope
3751 3759 * of the readahead window (random i/o).
3752 3760 */
3753 3761
3754 3762 if (off == 0)
3755 3763 readahead = 0;
3756 3764 else if (blkoff == rp->r_nextr)
3757 3765 readahead = nfs_nra;
3758 3766 else if (rp->r_nextr > blkoff &&
3759 3767 ((ra_window = (rp->r_nextr - blkoff) / bsize)
3760 3768 <= (nfs_nra - 1)))
3761 3769 readahead = nfs_nra - ra_window;
3762 3770 else
3763 3771 readahead = 0;
3764 3772
3765 3773 rablkoff = rp->r_nextr;
3766 3774 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
3767 3775 mutex_exit(&rp->r_statelock);
3768 3776 if (nfs_async_readahead(vp, rablkoff + bsize,
3769 3777 addr + (rablkoff + bsize - off), seg, cr,
3770 3778 nfs_readahead) < 0) {
3771 3779 mutex_enter(&rp->r_statelock);
3772 3780 break;
3773 3781 }
3774 3782 readahead--;
3775 3783 rablkoff += bsize;
3776 3784 /*
3777 3785 * Indicate that we did a readahead so
3778 3786 * readahead offset is not updated
3779 3787 * by the synchronous read below.
3780 3788 */
3781 3789 readahead_issued = 1;
3782 3790 mutex_enter(&rp->r_statelock);
3783 3791 /*
3784 3792 * set readahead offset to
3785 3793 * offset of last async readahead
3786 3794 * request.
3787 3795 */
3788 3796 rp->r_nextr = rablkoff;
3789 3797 }
3790 3798 mutex_exit(&rp->r_statelock);
3791 3799 }
3792 3800
3793 3801 again:
3794 3802 if ((pagefound = page_exists(vp, off)) == NULL) {
3795 3803 if (pl == NULL) {
3796 3804 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
3797 3805 nfs_readahead);
3798 3806 } else if (rw == S_CREATE) {
3799 3807 /*
3800 3808 * Block for this page is not allocated, or the offset
3801 3809 * is beyond the current allocation size, or we're
3802 3810 * allocating a swap slot and the page was not found,
3803 3811 * so allocate it and return a zero page.
3804 3812 */
3805 3813 if ((pp = page_create_va(vp, off,
3806 3814 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
3807 3815 cmn_err(CE_PANIC, "nfs_getapage: page_create");
3808 3816 io_len = PAGESIZE;
3809 3817 mutex_enter(&rp->r_statelock);
3810 3818 rp->r_nextr = off + PAGESIZE;
3811 3819 mutex_exit(&rp->r_statelock);
3812 3820 } else {
3813 3821 /*
3814 3822 * Need to go to server to get a BLOCK, exception to
3815 3823 * that being while reading at offset = 0 or doing
3816 3824 * random i/o, in that case read only a PAGE.
3817 3825 */
3818 3826 mutex_enter(&rp->r_statelock);
3819 3827 if (blkoff < rp->r_size &&
3820 3828 blkoff + bsize >= rp->r_size) {
3821 3829 /*
3822 3830 * If only a block or less is left in
3823 3831 * the file, read all that is remaining.
3824 3832 */
3825 3833 if (rp->r_size <= off) {
3826 3834 /*
3827 3835 * Trying to access beyond EOF,
3828 3836 * set up to get at least one page.
3829 3837 */
3830 3838 blksize = off + PAGESIZE - blkoff;
3831 3839 } else
3832 3840 blksize = rp->r_size - blkoff;
3833 3841 } else if ((off == 0) ||
3834 3842 (off != rp->r_nextr && !readahead_issued)) {
3835 3843 blksize = PAGESIZE;
3836 3844 blkoff = off; /* block = page here */
3837 3845 } else
3838 3846 blksize = bsize;
3839 3847 mutex_exit(&rp->r_statelock);
3840 3848
3841 3849 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3842 3850 &io_len, blkoff, blksize, 0);
3843 3851
3844 3852 /*
3845 3853 * Some other thread has entered the page,
3846 3854 * so just use it.
3847 3855 */
3848 3856 if (pp == NULL)
3849 3857 goto again;
3850 3858
3851 3859 /*
3852 3860 * Now round the request size up to page boundaries.
3853 3861 * This ensures that the entire page will be
3854 3862 * initialized to zeroes if EOF is encountered.
3855 3863 */
3856 3864 io_len = ptob(btopr(io_len));
3857 3865
3858 3866 bp = pageio_setup(pp, io_len, vp, B_READ);
3859 3867 ASSERT(bp != NULL);
3860 3868
3861 3869 /*
3862 3870 * pageio_setup should have set b_addr to 0. This
3863 3871 * is correct since we want to do I/O on a page
3864 3872 * boundary. bp_mapin will use this addr to calculate
3865 3873 * an offset, and then set b_addr to the kernel virtual
3866 3874 * address it allocated for us.
3867 3875 */
3868 3876 ASSERT(bp->b_un.b_addr == 0);
3869 3877
3870 3878 bp->b_edev = 0;
3871 3879 bp->b_dev = 0;
3872 3880 bp->b_lblkno = lbtodb(io_off);
3873 3881 bp->b_file = vp;
3874 3882 bp->b_offset = (offset_t)off;
3875 3883 bp_mapin(bp);
3876 3884
3877 3885 /*
3878 3886 * If doing a write beyond what we believe is EOF,
3879 3887 * don't bother trying to read the pages from the
3880 3888 * server, we'll just zero the pages here. We
3881 3889 * don't check that the rw flag is S_WRITE here
3882 3890 * because some implementations may attempt a
3883 3891 * read access to the buffer before copying data.
3884 3892 */
3885 3893 mutex_enter(&rp->r_statelock);
3886 3894 if (io_off >= rp->r_size && seg == segkmap) {
3887 3895 mutex_exit(&rp->r_statelock);
3888 3896 bzero(bp->b_un.b_addr, io_len);
3889 3897 } else {
3890 3898 mutex_exit(&rp->r_statelock);
3891 3899 error = nfs_bio(bp, cr);
3892 3900 }
3893 3901
3894 3902 /*
3895 3903 * Unmap the buffer before freeing it.
3896 3904 */
3897 3905 bp_mapout(bp);
3898 3906 pageio_done(bp);
3899 3907
3900 3908 if (error == NFS_EOF) {
3901 3909 /*
3902 3910 * If doing a write system call just return
3903 3911 * zeroed pages, else user tried to get pages
3904 3912 * beyond EOF, return error. We don't check
3905 3913 * that the rw flag is S_WRITE here because
3906 3914 * some implementations may attempt a read
3907 3915 * access to the buffer before copying data.
3908 3916 */
3909 3917 if (seg == segkmap)
3910 3918 error = 0;
3911 3919 else
3912 3920 error = EFAULT;
3913 3921 }
3914 3922
3915 3923 if (!readahead_issued && !error) {
3916 3924 mutex_enter(&rp->r_statelock);
3917 3925 rp->r_nextr = io_off + io_len;
3918 3926 mutex_exit(&rp->r_statelock);
3919 3927 }
3920 3928 }
3921 3929 }
3922 3930
3923 3931 out:
3924 3932 if (pl == NULL)
3925 3933 return (error);
3926 3934
3927 3935 if (error) {
3928 3936 if (pp != NULL)
3929 3937 pvn_read_done(pp, B_ERROR);
3930 3938 return (error);
3931 3939 }
3932 3940
3933 3941 if (pagefound) {
3934 3942 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
3935 3943
3936 3944 /*
3937 3945 * Page exists in the cache, acquire the appropriate lock.
3938 3946 * If this fails, start all over again.
3939 3947 */
3940 3948 if ((pp = page_lookup(vp, off, se)) == NULL) {
3941 3949 #ifdef DEBUG
3942 3950 nfs_lostpage++;
3943 3951 #endif
3944 3952 goto reread;
3945 3953 }
3946 3954 pl[0] = pp;
3947 3955 pl[1] = NULL;
3948 3956 return (0);
3949 3957 }
3950 3958
3951 3959 if (pp != NULL)
3952 3960 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3953 3961
3954 3962 return (error);
3955 3963 }
3956 3964
3957 3965 static void
3958 3966 nfs_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
3959 3967 cred_t *cr)
3960 3968 {
3961 3969 int error;
3962 3970 page_t *pp;
3963 3971 u_offset_t io_off;
3964 3972 size_t io_len;
3965 3973 struct buf *bp;
3966 3974 uint_t bsize, blksize;
3967 3975 rnode_t *rp = VTOR(vp);
3968 3976
3969 3977 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3970 3978
3971 3979 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3972 3980
3973 3981 mutex_enter(&rp->r_statelock);
3974 3982 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
3975 3983 /*
3976 3984 * If less than a block left in file read less
3977 3985 * than a block.
3978 3986 */
3979 3987 blksize = rp->r_size - blkoff;
3980 3988 } else
3981 3989 blksize = bsize;
3982 3990 mutex_exit(&rp->r_statelock);
3983 3991
3984 3992 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
3985 3993 &io_off, &io_len, blkoff, blksize, 1);
3986 3994 /*
3987 3995 * The isra flag passed to the kluster function is 1, we may have
3988 3996 * gotten a return value of NULL for a variety of reasons (# of free
3989 3997 * pages < minfree, someone entered the page on the vnode etc). In all
3990 3998 * cases, we want to punt on the readahead.
3991 3999 */
3992 4000 if (pp == NULL)
3993 4001 return;
3994 4002
3995 4003 /*
3996 4004 * Now round the request size up to page boundaries.
3997 4005 * This ensures that the entire page will be
3998 4006 * initialized to zeroes if EOF is encountered.
3999 4007 */
4000 4008 io_len = ptob(btopr(io_len));
4001 4009
4002 4010 bp = pageio_setup(pp, io_len, vp, B_READ);
4003 4011 ASSERT(bp != NULL);
4004 4012
4005 4013 /*
4006 4014 * pageio_setup should have set b_addr to 0. This is correct since
4007 4015 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4008 4016 * to calculate an offset, and then set b_addr to the kernel virtual
4009 4017 * address it allocated for us.
4010 4018 */
4011 4019 ASSERT(bp->b_un.b_addr == 0);
4012 4020
4013 4021 bp->b_edev = 0;
4014 4022 bp->b_dev = 0;
4015 4023 bp->b_lblkno = lbtodb(io_off);
4016 4024 bp->b_file = vp;
4017 4025 bp->b_offset = (offset_t)blkoff;
4018 4026 bp_mapin(bp);
4019 4027
4020 4028 /*
4021 4029 * If doing a write beyond what we believe is EOF, don't bother trying
4022 4030 * to read the pages from the server, we'll just zero the pages here.
4023 4031 * We don't check that the rw flag is S_WRITE here because some
4024 4032 * implementations may attempt a read access to the buffer before
4025 4033 * copying data.
4026 4034 */
4027 4035 mutex_enter(&rp->r_statelock);
4028 4036 if (io_off >= rp->r_size && seg == segkmap) {
4029 4037 mutex_exit(&rp->r_statelock);
4030 4038 bzero(bp->b_un.b_addr, io_len);
4031 4039 error = 0;
4032 4040 } else {
4033 4041 mutex_exit(&rp->r_statelock);
4034 4042 error = nfs_bio(bp, cr);
4035 4043 if (error == NFS_EOF)
4036 4044 error = 0;
4037 4045 }
4038 4046
4039 4047 /*
4040 4048 * Unmap the buffer before freeing it.
4041 4049 */
4042 4050 bp_mapout(bp);
4043 4051 pageio_done(bp);
4044 4052
4045 4053 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4046 4054
4047 4055 /*
4048 4056 * In case of error set readahead offset
4049 4057 * to the lowest offset.
4050 4058 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4051 4059 */
4052 4060 if (error && rp->r_nextr > io_off) {
4053 4061 mutex_enter(&rp->r_statelock);
4054 4062 if (rp->r_nextr > io_off)
4055 4063 rp->r_nextr = io_off;
4056 4064 mutex_exit(&rp->r_statelock);
4057 4065 }
4058 4066 }
4059 4067
4060 4068 /*
4061 4069 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4062 4070 * If len == 0, do from off to EOF.
4063 4071 *
4064 4072 * The normal cases should be len == 0 && off == 0 (entire vp list),
4065 4073 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4066 4074 * (from pageout).
4067 4075 */
4068 4076 /* ARGSUSED */
4069 4077 static int
4070 4078 nfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4071 4079 caller_context_t *ct)
4072 4080 {
4073 4081 int error;
4074 4082 rnode_t *rp;
4075 4083
4076 4084 ASSERT(cr != NULL);
4077 4085
4078 4086 /*
4079 4087 * XXX - Why should this check be made here?
4080 4088 */
4081 4089 if (vp->v_flag & VNOMAP)
4082 4090 return (ENOSYS);
4083 4091
4084 4092 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
4085 4093 return (0);
4086 4094
4087 4095 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
4088 4096 return (EIO);
4089 4097 ASSERT(off <= MAXOFF32_T);
4090 4098
4091 4099 rp = VTOR(vp);
4092 4100 mutex_enter(&rp->r_statelock);
4093 4101 rp->r_count++;
4094 4102 mutex_exit(&rp->r_statelock);
4095 4103 error = nfs_putpages(vp, off, len, flags, cr);
4096 4104 mutex_enter(&rp->r_statelock);
4097 4105 rp->r_count--;
4098 4106 cv_broadcast(&rp->r_cv);
4099 4107 mutex_exit(&rp->r_statelock);
4100 4108
4101 4109 return (error);
4102 4110 }
4103 4111
4104 4112 /*
4105 4113 * Write out a single page, possibly klustering adjacent dirty pages.
4106 4114 */
4107 4115 int
4108 4116 nfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
4109 4117 int flags, cred_t *cr)
4110 4118 {
4111 4119 u_offset_t io_off;
4112 4120 u_offset_t lbn_off;
4113 4121 u_offset_t lbn;
4114 4122 size_t io_len;
4115 4123 uint_t bsize;
4116 4124 int error;
4117 4125 rnode_t *rp;
4118 4126
4119 4127 ASSERT(!vn_is_readonly(vp));
4120 4128 ASSERT(pp != NULL);
4121 4129 ASSERT(cr != NULL);
4122 4130 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
4123 4131
4124 4132 rp = VTOR(vp);
4125 4133 ASSERT(rp->r_count > 0);
4126 4134
4127 4135 ASSERT(pp->p_offset <= MAXOFF32_T);
4128 4136
4129 4137 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4130 4138 lbn = pp->p_offset / bsize;
4131 4139 lbn_off = lbn * bsize;
4132 4140
4133 4141 /*
4134 4142 * Find a kluster that fits in one block, or in
4135 4143 * one page if pages are bigger than blocks. If
4136 4144 * there is less file space allocated than a whole
4137 4145 * page, we'll shorten the i/o request below.
4138 4146 */
4139 4147 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
4140 4148 roundup(bsize, PAGESIZE), flags);
4141 4149
4142 4150 /*
4143 4151 * pvn_write_kluster shouldn't have returned a page with offset
4144 4152 * behind the original page we were given. Verify that.
4145 4153 */
4146 4154 ASSERT((pp->p_offset / bsize) >= lbn);
4147 4155
4148 4156 /*
4149 4157 * Now pp will have the list of kept dirty pages marked for
4150 4158 * write back. It will also handle invalidation and freeing
4151 4159 * of pages that are not dirty. Check for page length rounding
4152 4160 * problems.
4153 4161 */
4154 4162 if (io_off + io_len > lbn_off + bsize) {
4155 4163 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
4156 4164 io_len = lbn_off + bsize - io_off;
4157 4165 }
4158 4166 /*
4159 4167 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4160 4168 * consistent value of r_size. RMODINPROGRESS is set in writerp().
4161 4169 * When RMODINPROGRESS is set it indicates that a uiomove() is in
4162 4170 * progress and the r_size has not been made consistent with the
4163 4171 * new size of the file. When the uiomove() completes the r_size is
4164 4172 * updated and the RMODINPROGRESS flag is cleared.
4165 4173 *
4166 4174 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4167 4175 * consistent value of r_size. Without this handshaking, it is
4168 4176 * possible that nfs(3)_bio() picks up the old value of r_size
4169 4177 * before the uiomove() in writerp() completes. This will result
4170 4178 * in the write through nfs(3)_bio() being dropped.
4171 4179 *
4172 4180 * More precisely, there is a window between the time the uiomove()
4173 4181 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
4174 4182 * operation intervenes in this window, the page will be picked up,
4175 4183 * because it is dirty (it will be unlocked, unless it was
4176 4184 * pagecreate'd). When the page is picked up as dirty, the dirty
4177 4185 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
4178 4186 * checked. This will still be the old size. Therefore the page will
4179 4187 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
4180 4188 * the page will be found to be clean and the write will be dropped.
4181 4189 */
4182 4190 if (rp->r_flags & RMODINPROGRESS) {
4183 4191 mutex_enter(&rp->r_statelock);
4184 4192 if ((rp->r_flags & RMODINPROGRESS) &&
4185 4193 rp->r_modaddr + MAXBSIZE > io_off &&
4186 4194 rp->r_modaddr < io_off + io_len) {
4187 4195 page_t *plist;
4188 4196 /*
4189 4197 * A write is in progress for this region of the file.
4190 4198 * If we did not detect RMODINPROGRESS here then this
4191 4199 * path through nfs_putapage() would eventually go to
4192 4200 * nfs(3)_bio() and may not write out all of the data
4193 4201 * in the pages. We end up losing data. So we decide
4194 4202 * to set the modified bit on each page in the page
4195 4203 * list and mark the rnode with RDIRTY. This write
4196 4204 * will be restarted at some later time.
4197 4205 */
4198 4206 plist = pp;
4199 4207 while (plist != NULL) {
4200 4208 pp = plist;
4201 4209 page_sub(&plist, pp);
4202 4210 hat_setmod(pp);
4203 4211 page_io_unlock(pp);
4204 4212 page_unlock(pp);
4205 4213 }
4206 4214 rp->r_flags |= RDIRTY;
4207 4215 mutex_exit(&rp->r_statelock);
4208 4216 if (offp)
4209 4217 *offp = io_off;
4210 4218 if (lenp)
4211 4219 *lenp = io_len;
4212 4220 return (0);
4213 4221 }
4214 4222 mutex_exit(&rp->r_statelock);
4215 4223 }
4216 4224
4217 4225 if (flags & B_ASYNC) {
4218 4226 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
4219 4227 nfs_sync_putapage);
4220 4228 } else
4221 4229 error = nfs_sync_putapage(vp, pp, io_off, io_len, flags, cr);
4222 4230
4223 4231 if (offp)
4224 4232 *offp = io_off;
4225 4233 if (lenp)
4226 4234 *lenp = io_len;
4227 4235 return (error);
4228 4236 }
4229 4237
4230 4238 static int
4231 4239 nfs_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4232 4240 int flags, cred_t *cr)
4233 4241 {
4234 4242 int error;
4235 4243 rnode_t *rp;
4236 4244
4237 4245 flags |= B_WRITE;
4238 4246
4239 4247 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4240 4248 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4241 4249
4242 4250 rp = VTOR(vp);
4243 4251
4244 4252 if ((error == ENOSPC || error == EDQUOT || error == EACCES) &&
4245 4253 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
4246 4254 if (!(rp->r_flags & ROUTOFSPACE)) {
4247 4255 mutex_enter(&rp->r_statelock);
4248 4256 rp->r_flags |= ROUTOFSPACE;
4249 4257 mutex_exit(&rp->r_statelock);
4250 4258 }
4251 4259 flags |= B_ERROR;
4252 4260 pvn_write_done(pp, flags);
4253 4261 /*
4254 4262 * If this was not an async thread, then try again to
4255 4263 * write out the pages, but this time, also destroy
4256 4264 * them whether or not the write is successful. This
4257 4265 * will prevent memory from filling up with these
4258 4266 * pages and destroying them is the only alternative
4259 4267 * if they can't be written out.
4260 4268 *
4261 4269 * Don't do this if this is an async thread because
4262 4270 * when the pages are unlocked in pvn_write_done,
4263 4271 * some other thread could have come along, locked
4264 4272 * them, and queued for an async thread. It would be
4265 4273 * possible for all of the async threads to be tied
4266 4274 * up waiting to lock the pages again and they would
4267 4275 * all already be locked and waiting for an async
4268 4276 * thread to handle them. Deadlock.
4269 4277 */
4270 4278 if (!(flags & B_ASYNC)) {
4271 4279 error = nfs_putpage(vp, io_off, io_len,
4272 4280 B_INVAL | B_FORCE, cr, NULL);
4273 4281 }
4274 4282 } else {
4275 4283 if (error)
4276 4284 flags |= B_ERROR;
4277 4285 else if (rp->r_flags & ROUTOFSPACE) {
4278 4286 mutex_enter(&rp->r_statelock);
4279 4287 rp->r_flags &= ~ROUTOFSPACE;
4280 4288 mutex_exit(&rp->r_statelock);
4281 4289 }
4282 4290 pvn_write_done(pp, flags);
4283 4291 }
4284 4292
4285 4293 return (error);
4286 4294 }
4287 4295
4288 4296 /* ARGSUSED */
4289 4297 static int
4290 4298 nfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4291 4299 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4292 4300 caller_context_t *ct)
4293 4301 {
4294 4302 struct segvn_crargs vn_a;
4295 4303 int error;
4296 4304 rnode_t *rp;
4297 4305 struct vattr va;
4298 4306
4299 4307 if (nfs_zone() != VTOMI(vp)->mi_zone)
4300 4308 return (EIO);
4301 4309
4302 4310 if (vp->v_flag & VNOMAP)
4303 4311 return (ENOSYS);
4304 4312
4305 4313 if (off > MAXOFF32_T)
4306 4314 return (EFBIG);
4307 4315
4308 4316 if (off < 0 || off + len < 0)
4309 4317 return (ENXIO);
4310 4318
4311 4319 if (vp->v_type != VREG)
4312 4320 return (ENODEV);
4313 4321
4314 4322 /*
4315 4323 * If there is cached data and if close-to-open consistency
4316 4324 * checking is not turned off and if the file system is not
4317 4325 * mounted readonly, then force an over the wire getattr.
4318 4326 * Otherwise, just invoke nfsgetattr to get a copy of the
4319 4327 * attributes. The attribute cache will be used unless it
4320 4328 * is timed out and if it is, then an over the wire getattr
4321 4329 * will be issued.
4322 4330 */
4323 4331 va.va_mask = AT_ALL;
4324 4332 if (vn_has_cached_data(vp) &&
4325 4333 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
4326 4334 error = nfs_getattr_otw(vp, &va, cr);
4327 4335 else
4328 4336 error = nfsgetattr(vp, &va, cr);
4329 4337 if (error)
4330 4338 return (error);
4331 4339
4332 4340 /*
4333 4341 * Check to see if the vnode is currently marked as not cachable.
4334 4342 * This means portions of the file are locked (through VOP_FRLOCK).
4335 4343 * In this case the map request must be refused. We use
4336 4344 * rp->r_lkserlock to avoid a race with concurrent lock requests.
4337 4345 */
4338 4346 rp = VTOR(vp);
4339 4347
4340 4348 /*
4341 4349 * Atomically increment r_inmap after acquiring r_rwlock. The
4342 4350 * idea here is to acquire r_rwlock to block read/write and
4343 4351 * not to protect r_inmap. r_inmap will inform nfs_read/write()
4344 4352 * that we are in nfs_map(). Now, r_rwlock is acquired in order
4345 4353 * and we can prevent the deadlock that would have occurred
4346 4354 * when nfs_addmap() would have acquired it out of order.
4347 4355 *
4348 4356 * Since we are not protecting r_inmap by any lock, we do not
4349 4357 * hold any lock when we decrement it. We atomically decrement
4350 4358 * r_inmap after we release r_lkserlock.
4351 4359 */
4352 4360
4353 4361 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
4354 4362 return (EINTR);
4355 4363 atomic_add_int(&rp->r_inmap, 1);
4356 4364 nfs_rw_exit(&rp->r_rwlock);
4357 4365
4358 4366 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
4359 4367 atomic_add_int(&rp->r_inmap, -1);
4360 4368 return (EINTR);
4361 4369 }
4362 4370 if (vp->v_flag & VNOCACHE) {
4363 4371 error = EAGAIN;
4364 4372 goto done;
4365 4373 }
4366 4374
4367 4375 /*
4368 4376 * Don't allow concurrent locks and mapping if mandatory locking is
4369 4377 * enabled.
4370 4378 */
4371 4379 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
4372 4380 MANDLOCK(vp, va.va_mode)) {
4373 4381 error = EAGAIN;
4374 4382 goto done;
4375 4383 }
4376 4384
4377 4385 as_rangelock(as);
4378 4386 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4379 4387 if (error != 0) {
4380 4388 as_rangeunlock(as);
4381 4389 goto done;
4382 4390 }
4383 4391
4384 4392 vn_a.vp = vp;
4385 4393 vn_a.offset = off;
4386 4394 vn_a.type = (flags & MAP_TYPE);
4387 4395 vn_a.prot = (uchar_t)prot;
4388 4396 vn_a.maxprot = (uchar_t)maxprot;
4389 4397 vn_a.flags = (flags & ~MAP_TYPE);
4390 4398 vn_a.cred = cr;
4391 4399 vn_a.amp = NULL;
4392 4400 vn_a.szc = 0;
4393 4401 vn_a.lgrp_mem_policy_flags = 0;
4394 4402
4395 4403 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4396 4404 as_rangeunlock(as);
4397 4405
4398 4406 done:
4399 4407 nfs_rw_exit(&rp->r_lkserlock);
4400 4408 atomic_add_int(&rp->r_inmap, -1);
4401 4409 return (error);
4402 4410 }
4403 4411
4404 4412 /* ARGSUSED */
4405 4413 static int
4406 4414 nfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4407 4415 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4408 4416 caller_context_t *ct)
4409 4417 {
4410 4418 rnode_t *rp;
4411 4419
4412 4420 if (vp->v_flag & VNOMAP)
4413 4421 return (ENOSYS);
4414 4422 if (nfs_zone() != VTOMI(vp)->mi_zone)
4415 4423 return (EIO);
4416 4424
4417 4425 rp = VTOR(vp);
4418 4426 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
4419 4427
4420 4428 return (0);
4421 4429 }
4422 4430
4423 4431 /* ARGSUSED */
4424 4432 static int
4425 4433 nfs_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, offset_t offset,
4426 4434 struct flk_callback *flk_cbp, cred_t *cr, caller_context_t *ct)
4427 4435 {
4428 4436 netobj lm_fh;
4429 4437 int rc;
4430 4438 u_offset_t start, end;
4431 4439 rnode_t *rp;
4432 4440 int error = 0, intr = INTR(vp);
4433 4441
4434 4442 /* check for valid cmd parameter */
4435 4443 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
4436 4444 return (EINVAL);
4437 4445 if (nfs_zone() != VTOMI(vp)->mi_zone)
4438 4446 return (EIO);
4439 4447
4440 4448 /* Verify l_type. */
4441 4449 switch (bfp->l_type) {
4442 4450 case F_RDLCK:
4443 4451 if (cmd != F_GETLK && !(flag & FREAD))
4444 4452 return (EBADF);
4445 4453 break;
4446 4454 case F_WRLCK:
4447 4455 if (cmd != F_GETLK && !(flag & FWRITE))
4448 4456 return (EBADF);
4449 4457 break;
4450 4458 case F_UNLCK:
4451 4459 intr = 0;
4452 4460 break;
4453 4461
4454 4462 default:
4455 4463 return (EINVAL);
4456 4464 }
4457 4465
4458 4466 /* check the validity of the lock range */
4459 4467 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
4460 4468 return (rc);
4461 4469 if (rc = flk_check_lock_data(start, end, MAXOFF32_T))
4462 4470 return (rc);
4463 4471
4464 4472 /*
4465 4473 * If the filesystem is mounted using local locking, pass the
4466 4474 * request off to the local locking code.
4467 4475 */
4468 4476 if (VTOMI(vp)->mi_flags & MI_LLOCK) {
4469 4477 if (offset > MAXOFF32_T)
4470 4478 return (EFBIG);
4471 4479 if (cmd == F_SETLK || cmd == F_SETLKW) {
4472 4480 /*
4473 4481 * For complete safety, we should be holding
4474 4482 * r_lkserlock. However, we can't call
4475 4483 * lm_safelock and then fs_frlock while
4476 4484 * holding r_lkserlock, so just invoke
4477 4485 * lm_safelock and expect that this will
4478 4486 * catch enough of the cases.
4479 4487 */
4480 4488 if (!lm_safelock(vp, bfp, cr))
4481 4489 return (EAGAIN);
4482 4490 }
4483 4491 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4484 4492 }
4485 4493
4486 4494 rp = VTOR(vp);
4487 4495
4488 4496 /*
4489 4497 * Check whether the given lock request can proceed, given the
4490 4498 * current file mappings.
4491 4499 */
4492 4500 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
4493 4501 return (EINTR);
4494 4502 if (cmd == F_SETLK || cmd == F_SETLKW) {
4495 4503 if (!lm_safelock(vp, bfp, cr)) {
4496 4504 rc = EAGAIN;
4497 4505 goto done;
4498 4506 }
4499 4507 }
4500 4508
4501 4509 /*
4502 4510 * Flush the cache after waiting for async I/O to finish. For new
4503 4511 * locks, this is so that the process gets the latest bits from the
4504 4512 * server. For unlocks, this is so that other clients see the
4505 4513 * latest bits once the file has been unlocked. If currently dirty
4506 4514 * pages can't be flushed, then don't allow a lock to be set. But
4507 4515 * allow unlocks to succeed, to avoid having orphan locks on the
4508 4516 * server.
4509 4517 */
4510 4518 if (cmd != F_GETLK) {
4511 4519 mutex_enter(&rp->r_statelock);
4512 4520 while (rp->r_count > 0) {
4513 4521 if (intr) {
4514 4522 klwp_t *lwp = ttolwp(curthread);
4515 4523
4516 4524 if (lwp != NULL)
4517 4525 lwp->lwp_nostop++;
4518 4526 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock)
4519 4527 == 0) {
4520 4528 if (lwp != NULL)
4521 4529 lwp->lwp_nostop--;
4522 4530 rc = EINTR;
4523 4531 break;
4524 4532 }
4525 4533 if (lwp != NULL)
4526 4534 lwp->lwp_nostop--;
4527 4535 } else
4528 4536 cv_wait(&rp->r_cv, &rp->r_statelock);
4529 4537 }
4530 4538 mutex_exit(&rp->r_statelock);
4531 4539 if (rc != 0)
4532 4540 goto done;
4533 4541 error = nfs_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
4534 4542 if (error) {
4535 4543 if (error == ENOSPC || error == EDQUOT) {
4536 4544 mutex_enter(&rp->r_statelock);
4537 4545 if (!rp->r_error)
4538 4546 rp->r_error = error;
4539 4547 mutex_exit(&rp->r_statelock);
4540 4548 }
4541 4549 if (bfp->l_type != F_UNLCK) {
4542 4550 rc = ENOLCK;
4543 4551 goto done;
4544 4552 }
4545 4553 }
4546 4554 }
4547 4555
4548 4556 lm_fh.n_len = sizeof (fhandle_t);
4549 4557 lm_fh.n_bytes = (char *)VTOFH(vp);
4550 4558
4551 4559 /*
4552 4560 * Call the lock manager to do the real work of contacting
4553 4561 * the server and obtaining the lock.
4554 4562 */
4555 4563 rc = lm_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh, flk_cbp);
4556 4564
4557 4565 if (rc == 0)
4558 4566 nfs_lockcompletion(vp, cmd);
4559 4567
4560 4568 done:
4561 4569 nfs_rw_exit(&rp->r_lkserlock);
4562 4570 return (rc);
4563 4571 }
4564 4572
4565 4573 /*
4566 4574 * Free storage space associated with the specified vnode. The portion
4567 4575 * to be freed is specified by bfp->l_start and bfp->l_len (already
4568 4576 * normalized to a "whence" of 0).
4569 4577 *
4570 4578 * This is an experimental facility whose continued existence is not
4571 4579 * guaranteed. Currently, we only support the special case
4572 4580 * of l_len == 0, meaning free to end of file.
4573 4581 */
4574 4582 /* ARGSUSED */
4575 4583 static int
4576 4584 nfs_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
4577 4585 offset_t offset, cred_t *cr, caller_context_t *ct)
4578 4586 {
4579 4587 int error;
4580 4588
4581 4589 ASSERT(vp->v_type == VREG);
4582 4590 if (cmd != F_FREESP)
4583 4591 return (EINVAL);
4584 4592
4585 4593 if (offset > MAXOFF32_T)
4586 4594 return (EFBIG);
4587 4595
4588 4596 if ((bfp->l_start > MAXOFF32_T) || (bfp->l_end > MAXOFF32_T) ||
4589 4597 (bfp->l_len > MAXOFF32_T))
4590 4598 return (EFBIG);
4591 4599
4592 4600 if (nfs_zone() != VTOMI(vp)->mi_zone)
4593 4601 return (EIO);
4594 4602
4595 4603 error = convoff(vp, bfp, 0, offset);
4596 4604 if (!error) {
4597 4605 ASSERT(bfp->l_start >= 0);
4598 4606 if (bfp->l_len == 0) {
4599 4607 struct vattr va;
4600 4608
4601 4609 /*
4602 4610 * ftruncate should not change the ctime and
4603 4611 * mtime if we truncate the file to its
4604 4612 * previous size.
4605 4613 */
4606 4614 va.va_mask = AT_SIZE;
4607 4615 error = nfsgetattr(vp, &va, cr);
4608 4616 if (error || va.va_size == bfp->l_start)
4609 4617 return (error);
4610 4618 va.va_mask = AT_SIZE;
4611 4619 va.va_size = bfp->l_start;
4612 4620 error = nfssetattr(vp, &va, 0, cr);
4613 4621 } else
4614 4622 error = EINVAL;
4615 4623 }
4616 4624
4617 4625 return (error);
4618 4626 }
4619 4627
4620 4628 /* ARGSUSED */
4621 4629 static int
4622 4630 nfs_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
4623 4631 {
4624 4632
4625 4633 return (EINVAL);
4626 4634 }
4627 4635
4628 4636 /*
4629 4637 * Setup and add an address space callback to do the work of the delmap call.
4630 4638 * The callback will (and must be) deleted in the actual callback function.
4631 4639 *
4632 4640 * This is done in order to take care of the problem that we have with holding
4633 4641 * the address space's a_lock for a long period of time (e.g. if the NFS server
4634 4642 * is down). Callbacks will be executed in the address space code while the
4635 4643 * a_lock is not held. Holding the address space's a_lock causes things such
4636 4644 * as ps and fork to hang because they are trying to acquire this lock as well.
4637 4645 */
4638 4646 /* ARGSUSED */
4639 4647 static int
4640 4648 nfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4641 4649 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4642 4650 caller_context_t *ct)
4643 4651 {
4644 4652 int caller_found;
4645 4653 int error;
4646 4654 rnode_t *rp;
4647 4655 nfs_delmap_args_t *dmapp;
4648 4656 nfs_delmapcall_t *delmap_call;
4649 4657
4650 4658 if (vp->v_flag & VNOMAP)
4651 4659 return (ENOSYS);
4652 4660 /*
4653 4661 * A process may not change zones if it has NFS pages mmap'ed
4654 4662 * in, so we can't legitimately get here from the wrong zone.
4655 4663 */
4656 4664 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4657 4665
4658 4666 rp = VTOR(vp);
4659 4667
4660 4668 /*
4661 4669 * The way that the address space of this process deletes its mapping
4662 4670 * of this file is via the following call chains:
4663 4671 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
4664 4672 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
4665 4673 *
4666 4674 * With the use of address space callbacks we are allowed to drop the
4667 4675 * address space lock, a_lock, while executing the NFS operations that
4668 4676 * need to go over the wire. Returning EAGAIN to the caller of this
4669 4677 * function is what drives the execution of the callback that we add
4670 4678 * below. The callback will be executed by the address space code
4671 4679 * after dropping the a_lock. When the callback is finished, since
4672 4680 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
4673 4681 * is called again on the same segment to finish the rest of the work
4674 4682 * that needs to happen during unmapping.
4675 4683 *
4676 4684 * This action of calling back into the segment driver causes
4677 4685 * nfs_delmap() to get called again, but since the callback was
4678 4686 * already executed at this point, it already did the work and there
4679 4687 * is nothing left for us to do.
4680 4688 *
4681 4689 * To Summarize:
4682 4690 * - The first time nfs_delmap is called by the current thread is when
4683 4691 * we add the caller associated with this delmap to the delmap caller
4684 4692 * list, add the callback, and return EAGAIN.
4685 4693 * - The second time in this call chain when nfs_delmap is called we
4686 4694 * will find this caller in the delmap caller list and realize there
4687 4695 * is no more work to do thus removing this caller from the list and
4688 4696 * returning the error that was set in the callback execution.
4689 4697 */
4690 4698 caller_found = nfs_find_and_delete_delmapcall(rp, &error);
4691 4699 if (caller_found) {
4692 4700 /*
4693 4701 * 'error' is from the actual delmap operations. To avoid
4694 4702 * hangs, we need to handle the return of EAGAIN differently
4695 4703 * since this is what drives the callback execution.
4696 4704 * In this case, we don't want to return EAGAIN and do the
4697 4705 * callback execution because there are none to execute.
4698 4706 */
4699 4707 if (error == EAGAIN)
4700 4708 return (0);
4701 4709 else
4702 4710 return (error);
4703 4711 }
4704 4712
4705 4713 /* current caller was not in the list */
4706 4714 delmap_call = nfs_init_delmapcall();
4707 4715
4708 4716 mutex_enter(&rp->r_statelock);
4709 4717 list_insert_tail(&rp->r_indelmap, delmap_call);
4710 4718 mutex_exit(&rp->r_statelock);
4711 4719
4712 4720 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
4713 4721
4714 4722 dmapp->vp = vp;
4715 4723 dmapp->off = off;
4716 4724 dmapp->addr = addr;
4717 4725 dmapp->len = len;
4718 4726 dmapp->prot = prot;
4719 4727 dmapp->maxprot = maxprot;
4720 4728 dmapp->flags = flags;
4721 4729 dmapp->cr = cr;
4722 4730 dmapp->caller = delmap_call;
4723 4731
4724 4732 error = as_add_callback(as, nfs_delmap_callback, dmapp,
4725 4733 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
4726 4734
4727 4735 return (error ? error : EAGAIN);
4728 4736 }
4729 4737
4730 4738 /*
4731 4739 * Remove some pages from an mmap'd vnode. Just update the
4732 4740 * count of pages. If doing close-to-open, then flush all
4733 4741 * of the pages associated with this file. Otherwise, start
4734 4742 * an asynchronous page flush to write out any dirty pages.
4735 4743 * This will also associate a credential with the rnode which
4736 4744 * can be used to write the pages.
4737 4745 */
4738 4746 /* ARGSUSED */
4739 4747 static void
4740 4748 nfs_delmap_callback(struct as *as, void *arg, uint_t event)
4741 4749 {
4742 4750 int error;
4743 4751 rnode_t *rp;
4744 4752 mntinfo_t *mi;
4745 4753 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg;
4746 4754
4747 4755 rp = VTOR(dmapp->vp);
4748 4756 mi = VTOMI(dmapp->vp);
4749 4757
4750 4758 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
4751 4759 ASSERT(rp->r_mapcnt >= 0);
4752 4760
4753 4761 /*
4754 4762 * Initiate a page flush if there are pages, the file system
4755 4763 * was not mounted readonly, the segment was mapped shared, and
4756 4764 * the pages themselves were writeable.
4757 4765 */
4758 4766 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
4759 4767 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
4760 4768 mutex_enter(&rp->r_statelock);
4761 4769 rp->r_flags |= RDIRTY;
4762 4770 mutex_exit(&rp->r_statelock);
4763 4771 /*
4764 4772 * If this is a cross-zone access a sync putpage won't work, so
4765 4773 * the best we can do is try an async putpage. That seems
4766 4774 * better than something more draconian such as discarding the
4767 4775 * dirty pages.
4768 4776 */
4769 4777 if ((mi->mi_flags & MI_NOCTO) ||
4770 4778 nfs_zone() != mi->mi_zone)
4771 4779 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4772 4780 B_ASYNC, dmapp->cr, NULL);
4773 4781 else
4774 4782 error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4775 4783 0, dmapp->cr, NULL);
4776 4784 if (!error) {
4777 4785 mutex_enter(&rp->r_statelock);
4778 4786 error = rp->r_error;
4779 4787 rp->r_error = 0;
4780 4788 mutex_exit(&rp->r_statelock);
4781 4789 }
4782 4790 } else
4783 4791 error = 0;
4784 4792
4785 4793 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
4786 4794 (void) nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4787 4795 B_INVAL, dmapp->cr, NULL);
4788 4796
4789 4797 dmapp->caller->error = error;
4790 4798 (void) as_delete_callback(as, arg);
4791 4799 kmem_free(dmapp, sizeof (nfs_delmap_args_t));
4792 4800 }
4793 4801
4794 4802 /* ARGSUSED */
4795 4803 static int
4796 4804 nfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4797 4805 caller_context_t *ct)
4798 4806 {
4799 4807 int error = 0;
4800 4808
4801 4809 if (nfs_zone() != VTOMI(vp)->mi_zone)
4802 4810 return (EIO);
4803 4811 /*
4804 4812 * This looks a little weird because it's written in a general
4805 4813 * manner but we make little use of cases. If cntl() ever gets
4806 4814 * widely used, the outer switch will make more sense.
4807 4815 */
4808 4816
4809 4817 switch (cmd) {
4810 4818
4811 4819 /*
4812 4820 * Large file spec - need to base answer new query with
4813 4821 * hardcoded constant based on the protocol.
4814 4822 */
4815 4823 case _PC_FILESIZEBITS:
4816 4824 *valp = 32;
4817 4825 return (0);
4818 4826
4819 4827 case _PC_LINK_MAX:
4820 4828 case _PC_NAME_MAX:
4821 4829 case _PC_PATH_MAX:
4822 4830 case _PC_SYMLINK_MAX:
4823 4831 case _PC_CHOWN_RESTRICTED:
4824 4832 case _PC_NO_TRUNC: {
4825 4833 mntinfo_t *mi;
4826 4834 struct pathcnf *pc;
4827 4835
4828 4836 if ((mi = VTOMI(vp)) == NULL || (pc = mi->mi_pathconf) == NULL)
4829 4837 return (EINVAL);
4830 4838 error = _PC_ISSET(cmd, pc->pc_mask); /* error or bool */
4831 4839 switch (cmd) {
4832 4840 case _PC_LINK_MAX:
4833 4841 *valp = pc->pc_link_max;
4834 4842 break;
4835 4843 case _PC_NAME_MAX:
4836 4844 *valp = pc->pc_name_max;
4837 4845 break;
4838 4846 case _PC_PATH_MAX:
4839 4847 case _PC_SYMLINK_MAX:
4840 4848 *valp = pc->pc_path_max;
4841 4849 break;
4842 4850 case _PC_CHOWN_RESTRICTED:
4843 4851 /*
4844 4852 * if we got here, error is really a boolean which
4845 4853 * indicates whether cmd is set or not.
4846 4854 */
4847 4855 *valp = error ? 1 : 0; /* see above */
4848 4856 error = 0;
4849 4857 break;
4850 4858 case _PC_NO_TRUNC:
4851 4859 /*
4852 4860 * if we got here, error is really a boolean which
4853 4861 * indicates whether cmd is set or not.
4854 4862 */
4855 4863 *valp = error ? 1 : 0; /* see above */
4856 4864 error = 0;
4857 4865 break;
4858 4866 }
4859 4867 return (error ? EINVAL : 0);
4860 4868 }
4861 4869
4862 4870 case _PC_XATTR_EXISTS:
4863 4871 *valp = 0;
4864 4872 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
4865 4873 vnode_t *avp;
4866 4874 rnode_t *rp;
4867 4875 mntinfo_t *mi = VTOMI(vp);
4868 4876
4869 4877 if (!(mi->mi_flags & MI_EXTATTR))
4870 4878 return (0);
4871 4879
4872 4880 rp = VTOR(vp);
4873 4881 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
4874 4882 INTR(vp)))
4875 4883 return (EINTR);
4876 4884
4877 4885 error = nfslookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
4878 4886 if (error || avp == NULL)
4879 4887 error = acl_getxattrdir2(vp, &avp, 0, cr, 0);
4880 4888
4881 4889 nfs_rw_exit(&rp->r_rwlock);
4882 4890
4883 4891 if (error == 0 && avp != NULL) {
4884 4892 error = do_xattr_exists_check(avp, valp, cr);
4885 4893 VN_RELE(avp);
4886 4894 }
4887 4895 }
4888 4896 return (error ? EINVAL : 0);
4889 4897
4890 4898 case _PC_ACL_ENABLED:
4891 4899 *valp = _ACL_ACLENT_ENABLED;
4892 4900 return (0);
4893 4901
4894 4902 default:
4895 4903 return (EINVAL);
4896 4904 }
4897 4905 }
4898 4906
4899 4907 /*
4900 4908 * Called by async thread to do synchronous pageio. Do the i/o, wait
4901 4909 * for it to complete, and cleanup the page list when done.
4902 4910 */
4903 4911 static int
4904 4912 nfs_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4905 4913 int flags, cred_t *cr)
4906 4914 {
4907 4915 int error;
4908 4916
4909 4917 ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4910 4918 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4911 4919 if (flags & B_READ)
4912 4920 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
4913 4921 else
4914 4922 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
4915 4923 return (error);
4916 4924 }
4917 4925
4918 4926 /* ARGSUSED */
4919 4927 static int
4920 4928 nfs_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4921 4929 int flags, cred_t *cr, caller_context_t *ct)
4922 4930 {
4923 4931 int error;
4924 4932 rnode_t *rp;
4925 4933
4926 4934 if (pp == NULL)
4927 4935 return (EINVAL);
4928 4936
4929 4937 if (io_off > MAXOFF32_T)
4930 4938 return (EFBIG);
4931 4939 if (nfs_zone() != VTOMI(vp)->mi_zone)
4932 4940 return (EIO);
4933 4941 rp = VTOR(vp);
4934 4942 mutex_enter(&rp->r_statelock);
4935 4943 rp->r_count++;
4936 4944 mutex_exit(&rp->r_statelock);
4937 4945
4938 4946 if (flags & B_ASYNC) {
4939 4947 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
4940 4948 nfs_sync_pageio);
4941 4949 } else
4942 4950 error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4943 4951 mutex_enter(&rp->r_statelock);
4944 4952 rp->r_count--;
4945 4953 cv_broadcast(&rp->r_cv);
4946 4954 mutex_exit(&rp->r_statelock);
4947 4955 return (error);
4948 4956 }
4949 4957
4950 4958 /* ARGSUSED */
4951 4959 static int
4952 4960 nfs_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4953 4961 caller_context_t *ct)
4954 4962 {
4955 4963 int error;
4956 4964 mntinfo_t *mi;
4957 4965
4958 4966 mi = VTOMI(vp);
4959 4967
4960 4968 if (nfs_zone() != mi->mi_zone)
4961 4969 return (EIO);
4962 4970 if (mi->mi_flags & MI_ACL) {
4963 4971 error = acl_setacl2(vp, vsecattr, flag, cr);
4964 4972 if (mi->mi_flags & MI_ACL)
4965 4973 return (error);
4966 4974 }
4967 4975
4968 4976 return (ENOSYS);
4969 4977 }
4970 4978
4971 4979 /* ARGSUSED */
4972 4980 static int
4973 4981 nfs_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4974 4982 caller_context_t *ct)
4975 4983 {
4976 4984 int error;
4977 4985 mntinfo_t *mi;
4978 4986
4979 4987 mi = VTOMI(vp);
4980 4988
4981 4989 if (nfs_zone() != mi->mi_zone)
4982 4990 return (EIO);
4983 4991 if (mi->mi_flags & MI_ACL) {
4984 4992 error = acl_getacl2(vp, vsecattr, flag, cr);
4985 4993 if (mi->mi_flags & MI_ACL)
4986 4994 return (error);
4987 4995 }
4988 4996
4989 4997 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
4990 4998 }
4991 4999
4992 5000 /* ARGSUSED */
4993 5001 static int
4994 5002 nfs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
4995 5003 caller_context_t *ct)
4996 5004 {
4997 5005 int error;
4998 5006 struct shrlock nshr;
4999 5007 struct nfs_owner nfs_owner;
5000 5008 netobj lm_fh;
5001 5009
5002 5010 if (nfs_zone() != VTOMI(vp)->mi_zone)
5003 5011 return (EIO);
5004 5012
5005 5013 /*
5006 5014 * check for valid cmd parameter
5007 5015 */
5008 5016 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
5009 5017 return (EINVAL);
5010 5018
5011 5019 /*
5012 5020 * Check access permissions
5013 5021 */
5014 5022 if (cmd == F_SHARE &&
5015 5023 (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
5016 5024 ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
5017 5025 return (EBADF);
5018 5026
5019 5027 /*
5020 5028 * If the filesystem is mounted using local locking, pass the
5021 5029 * request off to the local share code.
5022 5030 */
5023 5031 if (VTOMI(vp)->mi_flags & MI_LLOCK)
5024 5032 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
5025 5033
5026 5034 switch (cmd) {
5027 5035 case F_SHARE:
5028 5036 case F_UNSHARE:
5029 5037 lm_fh.n_len = sizeof (fhandle_t);
5030 5038 lm_fh.n_bytes = (char *)VTOFH(vp);
5031 5039
5032 5040 /*
5033 5041 * If passed an owner that is too large to fit in an
5034 5042 * nfs_owner it is likely a recursive call from the
5035 5043 * lock manager client and pass it straight through. If
5036 5044 * it is not a nfs_owner then simply return an error.
5037 5045 */
5038 5046 if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
5039 5047 if (((struct nfs_owner *)shr->s_owner)->magic !=
5040 5048 NFS_OWNER_MAGIC)
5041 5049 return (EINVAL);
5042 5050
5043 5051 if (error = lm_shrlock(vp, cmd, shr, flag, &lm_fh)) {
5044 5052 error = set_errno(error);
5045 5053 }
5046 5054 return (error);
5047 5055 }
5048 5056 /*
5049 5057 * Remote share reservations owner is a combination of
5050 5058 * a magic number, hostname, and the local owner
5051 5059 */
5052 5060 bzero(&nfs_owner, sizeof (nfs_owner));
5053 5061 nfs_owner.magic = NFS_OWNER_MAGIC;
5054 5062 (void) strncpy(nfs_owner.hname, uts_nodename(),
5055 5063 sizeof (nfs_owner.hname));
5056 5064 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
5057 5065 nshr.s_access = shr->s_access;
5058 5066 nshr.s_deny = shr->s_deny;
5059 5067 nshr.s_sysid = 0;
5060 5068 nshr.s_pid = ttoproc(curthread)->p_pid;
5061 5069 nshr.s_own_len = sizeof (nfs_owner);
5062 5070 nshr.s_owner = (caddr_t)&nfs_owner;
5063 5071
5064 5072 if (error = lm_shrlock(vp, cmd, &nshr, flag, &lm_fh)) {
5065 5073 error = set_errno(error);
5066 5074 }
5067 5075
5068 5076 break;
5069 5077
5070 5078 case F_HASREMOTELOCKS:
5071 5079 /*
5072 5080 * NFS client can't store remote locks itself
5073 5081 */
5074 5082 shr->s_access = 0;
5075 5083 error = 0;
5076 5084 break;
5077 5085
5078 5086 default:
5079 5087 error = EINVAL;
5080 5088 break;
5081 5089 }
5082 5090
5083 5091 return (error);
5084 5092 }
↓ open down ↓ |
3031 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX