Print this page
7123 encode timestamps in rfs4_op_readdir()
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/nfs/nfs4_srv_readdir.c
+++ new/usr/src/uts/common/fs/nfs/nfs4_srv_readdir.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 28 * Use is subject to license terms.
29 29 */
30 30
31 31 #include <sys/param.h>
32 32 #include <sys/types.h>
33 33 #include <sys/systm.h>
34 34 #include <sys/cred.h>
35 35 #include <sys/buf.h>
36 36 #include <sys/vfs.h>
37 37 #include <sys/vnode.h>
38 38 #include <sys/uio.h>
39 39 #include <sys/errno.h>
40 40 #include <sys/sysmacros.h>
41 41 #include <sys/statvfs.h>
42 42 #include <sys/kmem.h>
43 43 #include <sys/dirent.h>
44 44 #include <rpc/types.h>
45 45 #include <rpc/auth.h>
46 46 #include <rpc/rpcsec_gss.h>
47 47 #include <rpc/svc.h>
48 48 #include <sys/strsubr.h>
49 49 #include <sys/strsun.h>
50 50 #include <sys/sdt.h>
51 51
52 52 #include <nfs/nfs.h>
53 53 #include <nfs/export.h>
54 54 #include <nfs/nfs4.h>
55 55 #include <nfs/nfs_cmd.h>
56 56
57 57
58 58 /*
59 59 * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
60 60 * This is used to return NFS4ERR_TOOSMALL when clients specify
61 61 * maxcount that isn't large enough to hold the smallest possible
62 62 * XDR encoded dirent.
63 63 *
64 64 * sizeof cookie (8 bytes) +
65 65 * sizeof name_len (4 bytes) +
66 66 * sizeof smallest (padded) name (4 bytes) +
67 67 * sizeof bitmap4_len (12 bytes) + NOTE: we always encode len=2 bm4
68 68 * sizeof attrlist4_len (4 bytes) +
69 69 * sizeof next boolean (4 bytes)
70 70 *
71 71 * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
72 72 * the smallest possible entry4 (assumes no attrs requested).
73 73 * sizeof nfsstat4 (4 bytes) +
74 74 * sizeof verifier4 (8 bytes) +
75 75 * sizeof entsecond_to_ry4list bool (4 bytes) +
76 76 * sizeof entry4 (36 bytes) +
77 77 * sizeof eof bool (4 bytes)
78 78 *
79 79 * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
80 80 * VOP_READDIR. Its value is the size of the maximum possible dirent
81 81 * for solaris. The DIRENT64_RECLEN macro returns the size of dirent
82 82 * required for a given name length. MAXNAMELEN is the maximum
83 83 * filename length allowed in Solaris. The first two DIRENT64_RECLEN()
84 84 * macros are to allow for . and .. entries -- just a minor tweak to try
85 85 * and guarantee that buffer we give to VOP_READDIR will be large enough
86 86 * to hold ., .., and the largest possible solaris dirent64.
87 87 */
88 88 #define RFS4_MINLEN_ENTRY4 36
89 89 #define RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
90 90 #define RFS4_MINLEN_RDDIR_BUF \
91 91 (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
92 92
93 93
94 94 #ifdef nextdp
95 95 #undef nextdp
96 96 #endif
97 97 #define nextdp(dp) ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
98 98
99 99 verifier4 Readdir4verf = 0x0;
100 100
101 101 static nfs_ftype4 vt_to_nf4[] = {
102 102 0, NF4REG, NF4DIR, NF4BLK, NF4CHR, NF4LNK, NF4FIFO, 0, 0, NF4SOCK, 0
103 103 };
104 104
105 105 int
106 106 nfs4_readdir_getvp(vnode_t *dvp, char *d_name, vnode_t **vpp,
107 107 struct exportinfo **exi, struct svc_req *req,
108 108 struct compound_state *cs, int expseudo)
109 109 {
110 110 int error;
111 111 int ismntpt;
112 112 fid_t fid;
113 113 vnode_t *vp, *pre_tvp;
114 114 nfsstat4 status;
115 115 struct exportinfo *newexi, *saveexi;
116 116 cred_t *scr;
117 117
118 118 *vpp = vp = NULL;
119 119
120 120 if (error = VOP_LOOKUP(dvp, d_name, &vp, NULL, 0, NULL, cs->cr,
121 121 NULL, NULL, NULL))
122 122 return (error);
123 123
124 124 /*
125 125 * If the directory is a referral point, don't return the
126 126 * attrs, instead set rdattr_error to MOVED.
127 127 */
128 128 if (vn_is_nfs_reparse(vp, cs->cr) && !client_is_downrev(req)) {
129 129 VN_RELE(vp);
130 130 DTRACE_PROBE2(nfs4serv__func__referral__moved,
131 131 vnode_t *, vp, char *, "nfs4_readdir_getvp");
132 132 return (NFS4ERR_MOVED);
133 133 }
134 134
135 135 /* Is this object mounted upon? */
136 136 ismntpt = vn_ismntpt(vp);
137 137
138 138 /*
139 139 * Nothing more to do if object is not a mount point or
140 140 * a possible LOFS shadow of an LOFS mount (which won't
141 141 * have v_vfsmountedhere set)
142 142 */
143 143 if (ismntpt == 0 && dvp->v_vfsp == vp->v_vfsp && expseudo == 0) {
144 144 *vpp = vp;
145 145 return (0);
146 146 }
147 147
148 148 if (ismntpt) {
149 149 /*
150 150 * Something is mounted here. Traverse and manage the
151 151 * namespace
152 152 */
153 153 pre_tvp = vp;
154 154 VN_HOLD(pre_tvp);
155 155
156 156 if ((error = traverse(&vp)) != 0) {
157 157 VN_RELE(vp);
158 158 VN_RELE(pre_tvp);
159 159 return (error);
160 160 }
161 161 if (vn_is_nfs_reparse(vp, cs->cr)) {
162 162 VN_RELE(vp);
163 163 VN_RELE(pre_tvp);
164 164 DTRACE_PROBE2(nfs4serv__func__referral__moved,
165 165 vnode_t *, vp, char *, "nfs4_readdir_getvp");
166 166 return (NFS4ERR_MOVED);
167 167 }
168 168 }
169 169
170 170 bzero(&fid, sizeof (fid));
171 171 fid.fid_len = MAXFIDSZ;
172 172
173 173 /*
174 174 * If VOP_FID not supported by underlying fs (mntfs, procfs,
175 175 * etc.), then return attrs for stub instead of VROOT object.
176 176 * If it fails for any other reason, then return the error.
177 177 */
178 178 if (error = VOP_FID(vp, &fid, NULL)) {
179 179 if (ismntpt == 0) {
180 180 VN_RELE(vp);
181 181 return (error);
182 182 }
183 183
184 184 if (error != ENOSYS && error != ENOTSUP) {
185 185 VN_RELE(vp);
186 186 VN_RELE(pre_tvp);
187 187 return (error);
188 188 }
189 189 /* go back to vnode that is "under" mount */
190 190 VN_RELE(vp);
191 191 *vpp = pre_tvp;
192 192 return (0);
193 193 }
194 194
195 195 newexi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
196 196 if (newexi == NULL) {
197 197 if (ismntpt == 0) {
198 198 *vpp = vp;
199 199 } else {
200 200 VN_RELE(vp);
201 201 *vpp = pre_tvp;
202 202 }
203 203 return (0);
204 204 }
205 205
206 206 if (ismntpt)
207 207 VN_RELE(pre_tvp);
208 208
209 209 /* Save the exi and present the new one to checkauth4() */
210 210 saveexi = cs->exi;
211 211 cs->exi = newexi;
212 212
213 213 /* Get the right cred like lookup does */
214 214 scr = cs->cr;
215 215 cs->cr = crdup(cs->basecr);
216 216
217 217 status = call_checkauth4(cs, req);
218 218
219 219 crfree(cs->cr);
220 220 cs->cr = scr;
221 221 cs->exi = saveexi;
222 222
223 223 /* Reset what call_checkauth4() may have set */
224 224 *cs->statusp = NFS4_OK;
225 225
226 226 if (status != NFS4_OK) {
227 227 VN_RELE(vp);
228 228 if (status == NFS4ERR_DELAY)
229 229 status = NFS4ERR_ACCESS;
230 230 return (status);
231 231 }
232 232 *vpp = vp;
233 233 *exi = newexi;
234 234
235 235 return (0);
236 236 }
237 237
238 238 /* This is the set of pathconf data for vfs */
239 239 typedef struct {
240 240 uint64_t maxfilesize;
241 241 uint32_t maxlink;
242 242 uint32_t maxname;
243 243 } rfs4_pc_encode_t;
244 244
245 245
246 246 static int
247 247 rfs4_get_pc_encode(vnode_t *vp, rfs4_pc_encode_t *pce, bitmap4 ar, cred_t *cr)
248 248 {
249 249 int error;
250 250 ulong_t pc_val;
251 251
252 252 pce->maxfilesize = 0;
253 253 pce->maxlink = 0;
254 254 pce->maxname = 0;
255 255
256 256 if (ar & FATTR4_MAXFILESIZE_MASK) {
257 257 /* Maximum File Size */
258 258 error = VOP_PATHCONF(vp, _PC_FILESIZEBITS, &pc_val, cr, NULL);
259 259 if (error)
260 260 return (error);
261 261
262 262 /*
263 263 * If the underlying file system does not support
264 264 * _PC_FILESIZEBITS, return a reasonable default. Note that
265 265 * error code on VOP_PATHCONF will be 0, even if the underlying
266 266 * file system does not support _PC_FILESIZEBITS.
267 267 */
268 268 if (pc_val == (ulong_t)-1) {
269 269 pce->maxfilesize = MAXOFF32_T;
270 270 } else {
271 271 if (pc_val >= (sizeof (uint64_t) * 8))
272 272 pce->maxfilesize = INT64_MAX;
273 273 else
274 274 pce->maxfilesize = ((1LL << (pc_val - 1)) - 1);
275 275 }
276 276 }
277 277
278 278 if (ar & FATTR4_MAXLINK_MASK) {
279 279 /* Maximum Link Count */
280 280 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &pc_val, cr, NULL);
281 281 if (error)
282 282 return (error);
283 283
284 284 pce->maxlink = pc_val;
285 285 }
286 286
287 287 if (ar & FATTR4_MAXNAME_MASK) {
288 288 /* Maximum Name Length */
289 289 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &pc_val, cr, NULL);
290 290 if (error)
291 291 return (error);
292 292
293 293 pce->maxname = pc_val;
294 294 }
295 295
296 296 return (0);
297 297 }
298 298
299 299 /* This is the set of statvfs data that is ready for encoding */
300 300 typedef struct {
301 301 uint64_t space_avail;
302 302 uint64_t space_free;
303 303 uint64_t space_total;
304 304 u_longlong_t fa;
305 305 u_longlong_t ff;
306 306 u_longlong_t ft;
307 307 } rfs4_sb_encode_t;
308 308
309 309 static int
310 310 rfs4_get_sb_encode(vfs_t *vfsp, rfs4_sb_encode_t *psbe)
311 311 {
312 312 int error;
313 313 struct statvfs64 sb;
314 314
315 315 /* Grab the per filesystem info */
316 316 if (error = VFS_STATVFS(vfsp, &sb)) {
317 317 return (error);
318 318 }
319 319
320 320 /* Calculate space available */
321 321 if (sb.f_bavail != (fsblkcnt64_t)-1) {
322 322 psbe->space_avail =
323 323 (fattr4_space_avail) sb.f_frsize *
324 324 (fattr4_space_avail) sb.f_bavail;
325 325 } else {
326 326 psbe->space_avail =
327 327 (fattr4_space_avail) sb.f_bavail;
328 328 }
329 329
330 330 /* Calculate space free */
331 331 if (sb.f_bfree != (fsblkcnt64_t)-1) {
332 332 psbe->space_free =
333 333 (fattr4_space_free) sb.f_frsize *
334 334 (fattr4_space_free) sb.f_bfree;
335 335 } else {
336 336 psbe->space_free =
337 337 (fattr4_space_free) sb.f_bfree;
338 338 }
339 339
340 340 /* Calculate space total */
341 341 if (sb.f_blocks != (fsblkcnt64_t)-1) {
342 342 psbe->space_total =
343 343 (fattr4_space_total) sb.f_frsize *
344 344 (fattr4_space_total) sb.f_blocks;
345 345 } else {
346 346 psbe->space_total =
347 347 (fattr4_space_total) sb.f_blocks;
348 348 }
349 349
350 350 /* For use later on attr encode */
351 351 psbe->fa = sb.f_favail;
352 352 psbe->ff = sb.f_ffree;
353 353 psbe->ft = sb.f_files;
354 354
355 355 return (0);
356 356 }
357 357
358 358 /*
359 359 * Macros to handle if we have don't have enough space for the requested
360 360 * attributes and this is the first entry and the
361 361 * requested attributes are more than the minimal useful
362 362 * set, reset the attributes to the minimal set and
363 363 * retry the encoding. If the client has asked for both
364 364 * mounted_on_fileid and fileid, prefer mounted_on_fileid.
365 365 */
366 366 #define MINIMAL_RD_ATTRS \
367 367 (FATTR4_MOUNTED_ON_FILEID_MASK| \
368 368 FATTR4_FILEID_MASK| \
369 369 FATTR4_RDATTR_ERROR_MASK)
370 370
371 371 #define MINIMIZE_ATTR_MASK(m) { \
372 372 if ((m) & FATTR4_MOUNTED_ON_FILEID_MASK) \
373 373 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_MOUNTED_ON_FILEID_MASK;\
374 374 else \
375 375 (m) &= FATTR4_RDATTR_ERROR_MASK|FATTR4_FILEID_MASK; \
376 376 }
377 377
378 378 #define IS_MIN_ATTR_MASK(m) (((m) & ~MINIMAL_RD_ATTRS) == 0)
379 379 /*
380 380 * If readdir only needs to return FILEID, we can take it from the
381 381 * dirent struct and save doing the lookup.
382 382 */
383 383 /* ARGSUSED */
384 384 void
385 385 rfs4_op_readdir(nfs_argop4 *argop, nfs_resop4 *resop,
386 386 struct svc_req *req, struct compound_state *cs)
387 387 {
388 388 READDIR4args *args = &argop->nfs_argop4_u.opreaddir;
389 389 READDIR4res *resp = &resop->nfs_resop4_u.opreaddir;
390 390 struct exportinfo *newexi = NULL;
391 391 int error;
392 392 mblk_t *mp;
393 393 uint_t mpcount;
394 394 int alloc_err = 0;
395 395 vnode_t *dvp = cs->vp;
396 396 vnode_t *vp;
397 397 vattr_t va;
398 398 struct dirent64 *dp;
399 399 rfs4_sb_encode_t dsbe, sbe;
400 400 int vfs_different;
401 401 int rddir_data_len, rddir_result_size;
402 402 caddr_t rddir_data;
403 403 offset_t rddir_next_offset;
404 404 int dircount;
405 405 int no_space;
406 406 int iseofdir;
407 407 uint_t eof;
408 408 struct iovec iov;
409 409 struct uio uio;
410 410 int tsize;
411 411 int check_visible;
412 412 int expseudo = 0;
413 413
414 414 uint32_t *ptr, *ptr_redzone;
415 415 uint32_t *beginning_ptr;
416 416 uint32_t *lastentry_ptr;
417 417 uint32_t *attrmask_ptr;
418 418 uint32_t *attr_offset_ptr;
419 419 uint32_t attr_length;
420 420 uint32_t rndup;
421 421 uint32_t namelen;
422 422 uint32_t rddirattr_error = 0;
423 423 int nents;
424 424 bitmap4 ar = args->attr_request & NFS4_SRV_RDDIR_SUPPORTED_ATTRS;
425 425 bitmap4 ae;
426 426 rfs4_pc_encode_t dpce, pce;
427 427 ulong_t pc_val;
428 428 uint64_t maxread;
429 429 uint64_t maxwrite;
430 430 uint_t true = TRUE;
431 431 uint_t false = FALSE;
432 432 uid_t lastuid;
433 433 gid_t lastgid;
434 434 int lu_set, lg_set;
435 435 utf8string owner, group;
436 436 int owner_error, group_error;
437 437 struct sockaddr *ca;
438 438 char *name = NULL;
439 439
440 440 DTRACE_NFSV4_2(op__readdir__start, struct compound_state *, cs,
441 441 READDIR4args *, args);
442 442
443 443 lu_set = lg_set = 0;
444 444 owner.utf8string_len = group.utf8string_len = 0;
445 445 owner.utf8string_val = group.utf8string_val = NULL;
446 446
447 447 resp->mblk = NULL;
448 448
449 449 /* Maximum read and write size */
450 450 maxread = maxwrite = rfs4_tsize(req);
451 451
452 452 if (dvp == NULL) {
453 453 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
454 454 goto out;
455 455 }
456 456
457 457 /*
458 458 * If there is an unshared filesystem mounted on this vnode,
459 459 * do not allow readdir in this directory.
460 460 */
461 461 if (vn_ismntpt(dvp)) {
462 462 *cs->statusp = resp->status = NFS4ERR_ACCESS;
463 463 goto out;
464 464 }
465 465
466 466 if (dvp->v_type != VDIR) {
467 467 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
468 468 goto out;
469 469 }
470 470
471 471 if (args->maxcount <= RFS4_MINLEN_RDDIR4) {
472 472 *cs->statusp = resp->status = NFS4ERR_TOOSMALL;
473 473 goto out;
474 474 }
475 475
476 476 /*
477 477 * If write-only attrs are requested, then fail the readdir op
478 478 */
479 479 if (args->attr_request &
480 480 (FATTR4_TIME_MODIFY_SET_MASK | FATTR4_TIME_ACCESS_SET_MASK)) {
481 481 *cs->statusp = resp->status = NFS4ERR_INVAL;
482 482 goto out;
483 483 }
484 484
485 485 error = VOP_ACCESS(dvp, VREAD, 0, cs->cr, NULL);
486 486 if (error) {
487 487 *cs->statusp = resp->status = puterrno4(error);
488 488 goto out;
489 489 }
490 490
491 491 if (args->cookieverf != Readdir4verf) {
492 492 *cs->statusp = resp->status = NFS4ERR_NOT_SAME;
493 493 goto out;
494 494 }
495 495
496 496 /* Is there pseudo-fs work that is needed for this readdir? */
497 497 check_visible = PSEUDO(cs->exi) ||
498 498 ! is_exported_sec(cs->nfsflavor, cs->exi) ||
499 499 cs->access & CS_ACCESS_LIMITED;
500 500
501 501 /* Check the requested attributes and only do the work if needed */
502 502
503 503 if (ar & (FATTR4_MAXFILESIZE_MASK |
504 504 FATTR4_MAXLINK_MASK |
505 505 FATTR4_MAXNAME_MASK)) {
506 506 if (error = rfs4_get_pc_encode(cs->vp, &dpce, ar, cs->cr)) {
507 507 *cs->statusp = resp->status = puterrno4(error);
508 508 goto out;
509 509 }
510 510 pce = dpce;
511 511 }
512 512
513 513 /* If there is statvfs data requested, pick it up once */
514 514 if (ar &
515 515 (FATTR4_FILES_AVAIL_MASK |
516 516 FATTR4_FILES_FREE_MASK |
517 517 FATTR4_FILES_TOTAL_MASK |
518 518 FATTR4_FILES_AVAIL_MASK |
519 519 FATTR4_FILES_FREE_MASK |
520 520 FATTR4_FILES_TOTAL_MASK)) {
521 521 if (error = rfs4_get_sb_encode(dvp->v_vfsp, &dsbe)) {
522 522 *cs->statusp = resp->status = puterrno4(error);
523 523 goto out;
524 524 }
525 525 sbe = dsbe;
526 526 }
527 527
528 528 /*
529 529 * Max transfer size of the server is the absolute limite.
530 530 * If the client has decided to max out with something really
531 531 * tiny, then return toosmall. Otherwise, move forward and
532 532 * see if a single entry can be encoded.
533 533 */
534 534 tsize = rfs4_tsize(req);
535 535 if (args->maxcount > tsize)
536 536 args->maxcount = tsize;
537 537 else if (args->maxcount < RFS4_MINLEN_RDDIR_BUF) {
538 538 if (args->maxcount < RFS4_MINLEN_ENTRY4) {
539 539 *cs->statusp = resp->status = NFS4ERR_TOOSMALL;
540 540 goto out;
541 541 }
542 542 }
543 543
544 544 /*
545 545 * How large should the mblk be for outgoing encoding.
546 546 */
547 547 if (args->maxcount < MAXBSIZE)
548 548 mpcount = MAXBSIZE;
549 549 else
550 550 mpcount = args->maxcount;
551 551
552 552 /*
553 553 * mp will contain the data to be sent out in the readdir reply.
554 554 * It will be freed after the reply has been sent.
555 555 * Let's roundup the data to a BYTES_PER_XDR_UNIX multiple,
556 556 * so that the call to xdrmblk_putmblk() never fails.
557 557 */
558 558 mp = allocb(RNDUP(mpcount), BPRI_MED);
559 559
560 560 if (mp == NULL) {
561 561 /*
562 562 * The allocation of the client's requested size has
563 563 * failed. It may be that the size is too large for
564 564 * current system utilization; step down to a "common"
565 565 * size and wait for the allocation to occur.
566 566 */
567 567 if (mpcount > MAXBSIZE)
568 568 args->maxcount = mpcount = MAXBSIZE;
569 569 mp = allocb_wait(RNDUP(mpcount), BPRI_MED,
570 570 STR_NOSIG, &alloc_err);
571 571 }
572 572
573 573 ASSERT(mp != NULL);
574 574 ASSERT(alloc_err == 0);
575 575
576 576 resp->mblk = mp;
577 577
578 578 ptr = beginning_ptr = (uint32_t *)mp->b_datap->db_base;
579 579
580 580 /*
581 581 * The "redzone" at the end of the encoding buffer is used
582 582 * to deal with xdr encoding length. Instead of checking
583 583 * each encoding of an attribute value before it is done,
584 584 * make the assumption that it will fit into the buffer and
585 585 * check occasionally.
586 586 *
587 587 * The largest block of attributes that are encoded without
588 588 * checking the redzone is 18 * BYTES_PER_XDR_UNIT (72 bytes)
589 589 * "round" to 128 as the redzone size.
590 590 */
591 591 if (args->maxcount < (mpcount - 128))
592 592 ptr_redzone =
593 593 (uint32_t *)(((char *)ptr) + RNDUP(args->maxcount));
594 594 else
595 595 ptr_redzone =
596 596 (uint32_t *)((((char *)ptr) + RNDUP(mpcount)) - 128);
597 597
598 598 /*
599 599 * Set the dircount; this will be used as the size for the
600 600 * readdir of the underlying filesystem. First make sure
601 601 * that it is large enough to do a reasonable readdir (client
602 602 * may have short changed us - it is an advisory number);
603 603 * then make sure that it isn't too large.
604 604 * After all of that, if maxcount is "small" then just use
605 605 * that for the dircount number.
606 606 */
607 607 dircount = (args->dircount < MAXBSIZE) ? MAXBSIZE : args->dircount;
608 608 dircount = (dircount > tsize) ? tsize : dircount;
609 609 if (dircount > args->maxcount)
610 610 dircount = args->maxcount;
611 611 if (args->maxcount <= MAXBSIZE) {
612 612 if (args->maxcount < RFS4_MINLEN_RDDIR_BUF)
613 613 dircount = RFS4_MINLEN_RDDIR_BUF;
614 614 else
615 615 dircount = args->maxcount;
616 616 }
617 617
618 618 /* number of entries fully encoded in outgoing buffer */
619 619 nents = 0;
620 620
621 621 /* ENCODE READDIR4res.cookieverf */
622 622 IXDR_PUT_HYPER(ptr, Readdir4verf);
623 623
624 624 rddir_data_len = dircount;
625 625 rddir_data = kmem_alloc(rddir_data_len, KM_NOSLEEP);
626 626 if (rddir_data == NULL) {
627 627 /* The allocation failed; downsize and wait for it this time */
628 628 if (rddir_data_len > MAXBSIZE)
629 629 rddir_data_len = dircount = MAXBSIZE;
630 630 rddir_data = kmem_alloc(rddir_data_len, KM_SLEEP);
631 631 }
632 632
633 633 rddir_next_offset = (offset_t)args->cookie;
634 634
635 635 ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
636 636
637 637 readagain:
638 638
639 639 no_space = FALSE;
640 640 iseofdir = FALSE;
641 641
642 642 vp = NULL;
643 643
644 644 /* Move on to reading the directory contents */
645 645 iov.iov_base = rddir_data;
646 646 iov.iov_len = rddir_data_len;
647 647 uio.uio_iov = &iov;
648 648 uio.uio_iovcnt = 1;
649 649 uio.uio_segflg = UIO_SYSSPACE;
650 650 uio.uio_extflg = UIO_COPY_CACHED;
651 651 uio.uio_loffset = rddir_next_offset;
652 652 uio.uio_resid = rddir_data_len;
653 653
654 654 (void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
655 655
656 656 error = VOP_READDIR(dvp, &uio, cs->cr, &iseofdir, NULL, 0);
657 657
658 658 VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
659 659
660 660 if (error) {
661 661 kmem_free((caddr_t)rddir_data, rddir_data_len);
662 662 freeb(resp->mblk);
663 663 resp->mblk = NULL;
664 664 resp->data_len = 0;
665 665 *cs->statusp = resp->status = puterrno4(error);
666 666 goto out;
667 667 }
668 668
669 669
670 670 rddir_result_size = rddir_data_len - uio.uio_resid;
671 671
672 672 /* No data were read. Check if we reached the end of the directory. */
673 673 if (rddir_result_size == 0) {
674 674 /* encode the BOOLEAN marking no further entries */
675 675 IXDR_PUT_U_INT32(ptr, false);
676 676 /* encode the BOOLEAN signifying end of directory */
677 677 IXDR_PUT_U_INT32(ptr, iseofdir ? true : false);
678 678 resp->data_len = (char *)ptr - (char *)beginning_ptr;
679 679 resp->mblk->b_wptr += resp->data_len;
680 680 kmem_free((caddr_t)rddir_data, rddir_data_len);
681 681 *cs->statusp = resp->status = NFS4_OK;
682 682 goto out;
683 683 }
684 684
685 685 lastentry_ptr = ptr;
686 686 no_space = 0;
687 687 for (dp = (struct dirent64 *)rddir_data;
688 688 !no_space && rddir_result_size > 0; dp = nextdp(dp)) {
689 689
690 690 /* reset expseudo */
691 691 expseudo = 0;
692 692
693 693 if (vp) {
694 694 VN_RELE(vp);
695 695 vp = NULL;
696 696 }
697 697
698 698 if (newexi)
699 699 newexi = NULL;
700 700
701 701 rddir_result_size -= dp->d_reclen;
702 702
703 703 /* skip "." and ".." entries */
704 704 if (dp->d_ino == 0 || NFS_IS_DOTNAME(dp->d_name)) {
705 705 rddir_next_offset = dp->d_off;
706 706 continue;
707 707 }
708 708
709 709 if (check_visible &&
710 710 !nfs_visible_inode(cs->exi, dp->d_ino, &expseudo)) {
711 711 rddir_next_offset = dp->d_off;
712 712 continue;
713 713 }
714 714
715 715 /*
716 716 * Only if the client requested attributes...
717 717 * If the VOP_LOOKUP fails ENOENT, then skip this entry
718 718 * for the readdir response. If there was another error,
719 719 * then set the rddirattr_error and the error will be
720 720 * encoded later in the "attributes" section.
721 721 */
722 722 ae = ar;
723 723 if (ar == 0)
724 724 goto reencode_attrs;
725 725
726 726 error = nfs4_readdir_getvp(dvp, dp->d_name,
727 727 &vp, &newexi, req, cs, expseudo);
728 728 if (error == ENOENT) {
729 729 rddir_next_offset = dp->d_off;
730 730 continue;
731 731 }
732 732
733 733 rddirattr_error = error;
734 734
735 735 /*
736 736 * The vp obtained from above may be from a
737 737 * different filesystem mount and the vfs-like
738 738 * attributes should be obtained from that
739 739 * different vfs; only do this if appropriate.
740 740 */
741 741 if (vp &&
742 742 (vfs_different = (dvp->v_vfsp != vp->v_vfsp))) {
743 743 if (ar & (FATTR4_FILES_AVAIL_MASK |
744 744 FATTR4_FILES_FREE_MASK |
745 745 FATTR4_FILES_TOTAL_MASK |
746 746 FATTR4_FILES_AVAIL_MASK |
747 747 FATTR4_FILES_FREE_MASK |
748 748 FATTR4_FILES_TOTAL_MASK)) {
749 749 if (error =
750 750 rfs4_get_sb_encode(dvp->v_vfsp,
751 751 &sbe)) {
752 752 /* Remove attrs from encode */
753 753 ae &= ~(FATTR4_FILES_AVAIL_MASK |
754 754 FATTR4_FILES_FREE_MASK |
755 755 FATTR4_FILES_TOTAL_MASK |
756 756 FATTR4_FILES_AVAIL_MASK |
757 757 FATTR4_FILES_FREE_MASK |
758 758 FATTR4_FILES_TOTAL_MASK);
759 759 rddirattr_error = error;
760 760 }
761 761 }
762 762 if (ar & (FATTR4_MAXFILESIZE_MASK |
763 763 FATTR4_MAXLINK_MASK |
764 764 FATTR4_MAXNAME_MASK)) {
765 765 if (error = rfs4_get_pc_encode(cs->vp,
766 766 &pce, ar, cs->cr)) {
767 767 ar &= ~(FATTR4_MAXFILESIZE_MASK |
768 768 FATTR4_MAXLINK_MASK |
769 769 FATTR4_MAXNAME_MASK);
770 770 rddirattr_error = error;
771 771 }
772 772 }
773 773 }
774 774
775 775 reencode_attrs:
776 776 /* encode the BOOLEAN for the existence of the next entry */
777 777 IXDR_PUT_U_INT32(ptr, true);
778 778 /* encode the COOKIE for the entry */
779 779 IXDR_PUT_U_HYPER(ptr, dp->d_off);
780 780
781 781 name = nfscmd_convname(ca, cs->exi, dp->d_name,
782 782 NFSCMD_CONV_OUTBOUND, MAXPATHLEN + 1);
783 783
784 784 if (name == NULL) {
785 785 rddir_next_offset = dp->d_off;
786 786 continue;
787 787 }
788 788 /* Calculate the dirent name length */
789 789 namelen = strlen(name);
790 790
791 791 rndup = RNDUP(namelen) / BYTES_PER_XDR_UNIT;
792 792
793 793 /* room for LENGTH + string ? */
794 794 if ((ptr + (1 + rndup)) > ptr_redzone) {
795 795 no_space = TRUE;
796 796 continue;
797 797 }
798 798
799 799 /* encode the LENGTH of the name */
800 800 IXDR_PUT_U_INT32(ptr, namelen);
801 801 /* encode the RNDUP FILL first */
802 802 ptr[rndup - 1] = 0;
803 803 /* encode the NAME of the entry */
804 804 bcopy(name, (char *)ptr, namelen);
805 805 /* now bump the ptr after... */
806 806 ptr += rndup;
807 807
808 808 if (name != dp->d_name)
809 809 kmem_free(name, MAXPATHLEN + 1);
810 810
811 811 /*
812 812 * Keep checking on the dircount to see if we have
813 813 * reached the limit; from the RFC, dircount is to be
814 814 * the XDR encoded limit of the cookie plus name.
815 815 * So the count is the name, XDR_UNIT of length for
816 816 * that name and 2 * XDR_UNIT bytes of cookie;
817 817 * However, use the regular DIRENT64 to match most
818 818 * client's APIs.
819 819 */
820 820 dircount -= DIRENT64_RECLEN(namelen);
821 821 if (nents != 0 && dircount < 0) {
822 822 no_space = TRUE;
823 823 continue;
824 824 }
825 825
826 826 /*
827 827 * Attributes requested?
828 828 * Gather up the attribute info and the previous VOP_LOOKUP()
829 829 * succeeded; if an error occurs on the VOP_GETATTR() then
830 830 * return just the error (again if it is requested).
831 831 * Note that the previous VOP_LOOKUP() could have failed
832 832 * itself which leaves this code without anything for
833 833 * a VOP_GETATTR().
834 834 * Also note that the readdir_attr_error is left in the
835 835 * encoding mask if requested and so is the mounted_on_fileid.
836 836 */
837 837 if (ae != 0) {
838 838 if (!vp) {
839 839 ae = ar & (FATTR4_RDATTR_ERROR_MASK |
840 840 FATTR4_MOUNTED_ON_FILEID_MASK);
841 841 } else {
842 842 va.va_mask = AT_ALL;
843 843 rddirattr_error =
844 844 VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
845 845 if (rddirattr_error) {
846 846 ae = ar & (FATTR4_RDATTR_ERROR_MASK |
847 847 FATTR4_MOUNTED_ON_FILEID_MASK);
848 848 } else {
849 849 /*
850 850 * We may lie about the object
851 851 * type for a referral
852 852 */
853 853 if (vn_is_nfs_reparse(vp, cs->cr) &&
854 854 client_is_downrev(req))
855 855 va.va_type = VLNK;
856 856 }
857 857 }
858 858 }
859 859
860 860 /* START OF ATTRIBUTE ENCODING */
861 861
862 862 /* encode the LENGTH of the BITMAP4 array */
863 863 IXDR_PUT_U_INT32(ptr, 2);
864 864 /* encode the BITMAP4 */
865 865 attrmask_ptr = ptr;
866 866 IXDR_PUT_HYPER(ptr, ae);
867 867 attr_offset_ptr = ptr;
868 868 /* encode the default LENGTH of the attributes for entry */
869 869 IXDR_PUT_U_INT32(ptr, 0);
870 870
871 871 if (ptr > ptr_redzone) {
872 872 no_space = TRUE;
873 873 continue;
874 874 }
875 875
876 876 /* Check if any of the first 32 attributes are being encoded */
877 877 if (ae & 0xffffffff00000000) {
878 878 /*
879 879 * Redzone check is done at the end of this section.
880 880 * This particular section will encode a maximum of
881 881 * 18 * BYTES_PER_XDR_UNIT of data
882 882 */
883 883 if (ae &
884 884 (FATTR4_SUPPORTED_ATTRS_MASK |
885 885 FATTR4_TYPE_MASK |
886 886 FATTR4_FH_EXPIRE_TYPE_MASK |
887 887 FATTR4_CHANGE_MASK |
888 888 FATTR4_SIZE_MASK |
889 889 FATTR4_LINK_SUPPORT_MASK |
890 890 FATTR4_SYMLINK_SUPPORT_MASK |
891 891 FATTR4_NAMED_ATTR_MASK |
892 892 FATTR4_FSID_MASK |
893 893 FATTR4_UNIQUE_HANDLES_MASK |
894 894 FATTR4_LEASE_TIME_MASK |
895 895 FATTR4_RDATTR_ERROR_MASK)) {
896 896
897 897 if (ae & FATTR4_SUPPORTED_ATTRS_MASK) {
898 898 IXDR_PUT_INT32(ptr, 2);
899 899 IXDR_PUT_HYPER(ptr,
900 900 rfs4_supported_attrs);
901 901 }
902 902 if (ae & FATTR4_TYPE_MASK) {
903 903 uint_t ftype = vt_to_nf4[va.va_type];
904 904 if (dvp->v_flag & V_XATTRDIR) {
905 905 if (va.va_type == VDIR)
906 906 ftype = NF4ATTRDIR;
907 907 else
908 908 ftype = NF4NAMEDATTR;
909 909 }
910 910 IXDR_PUT_U_INT32(ptr, ftype);
911 911 }
912 912 if (ae & FATTR4_FH_EXPIRE_TYPE_MASK) {
913 913 uint_t expire_type = FH4_PERSISTENT;
914 914 IXDR_PUT_U_INT32(ptr, expire_type);
915 915 }
916 916 if (ae & FATTR4_CHANGE_MASK) {
917 917 u_longlong_t change;
918 918 NFS4_SET_FATTR4_CHANGE(change,
919 919 va.va_ctime);
920 920 IXDR_PUT_HYPER(ptr, change);
921 921 }
922 922 if (ae & FATTR4_SIZE_MASK) {
923 923 u_longlong_t size = va.va_size;
924 924 IXDR_PUT_HYPER(ptr, size);
925 925 }
926 926 if (ae & FATTR4_LINK_SUPPORT_MASK) {
927 927 IXDR_PUT_U_INT32(ptr, true);
928 928 }
929 929 if (ae & FATTR4_SYMLINK_SUPPORT_MASK) {
930 930 IXDR_PUT_U_INT32(ptr, true);
931 931 }
932 932 if (ae & FATTR4_NAMED_ATTR_MASK) {
933 933 uint_t isit;
934 934 pc_val = FALSE;
935 935 int sattr_error;
936 936
937 937 if (!(vp->v_vfsp->vfs_flag &
938 938 VFS_XATTR)) {
939 939 isit = FALSE;
940 940 } else {
941 941 sattr_error = VOP_PATHCONF(vp,
942 942 _PC_SATTR_EXISTS,
943 943 &pc_val, cs->cr, NULL);
944 944 if (sattr_error || pc_val == 0)
945 945 (void) VOP_PATHCONF(vp,
946 946 _PC_XATTR_EXISTS,
947 947 &pc_val,
948 948 cs->cr, NULL);
949 949 }
950 950 isit = (pc_val ? TRUE : FALSE);
951 951 IXDR_PUT_U_INT32(ptr, isit);
952 952 }
953 953 if (ae & FATTR4_FSID_MASK) {
954 954 u_longlong_t major, minor;
955 955 struct exportinfo *exi;
956 956
957 957 exi = newexi ? newexi : cs->exi;
958 958 if (exi->exi_volatile_dev) {
959 959 int *pmaj = (int *)&major;
960 960
961 961 pmaj[0] = exi->exi_fsid.val[0];
962 962 pmaj[1] = exi->exi_fsid.val[1];
963 963 minor = 0;
964 964 } else {
965 965 major = getmajor(va.va_fsid);
966 966 minor = getminor(va.va_fsid);
967 967 }
968 968 IXDR_PUT_HYPER(ptr, major);
969 969 IXDR_PUT_HYPER(ptr, minor);
970 970 }
971 971 if (ae & FATTR4_UNIQUE_HANDLES_MASK) {
972 972 IXDR_PUT_U_INT32(ptr, false);
973 973 }
974 974 if (ae & FATTR4_LEASE_TIME_MASK) {
975 975 uint_t lt = rfs4_lease_time;
976 976 IXDR_PUT_U_INT32(ptr, lt);
977 977 }
978 978 if (ae & FATTR4_RDATTR_ERROR_MASK) {
979 979 rddirattr_error =
980 980 (rddirattr_error == 0 ?
981 981 0 : puterrno4(rddirattr_error));
982 982 IXDR_PUT_U_INT32(ptr, rddirattr_error);
983 983 }
984 984
985 985 /* Check the redzone boundary */
986 986 if (ptr > ptr_redzone) {
987 987 if (nents || IS_MIN_ATTR_MASK(ar)) {
988 988 no_space = TRUE;
989 989 continue;
990 990 }
991 991 MINIMIZE_ATTR_MASK(ar);
992 992 ae = ar;
993 993 ptr = lastentry_ptr;
994 994 goto reencode_attrs;
995 995 }
996 996 }
997 997 /*
998 998 * Redzone check is done at the end of this section.
999 999 * This particular section will encode a maximum of
1000 1000 * 4 * BYTES_PER_XDR_UNIT of data.
1001 1001 * NOTE: that if ACLs are supported that the
1002 1002 * redzone calculations will need to change.
1003 1003 */
1004 1004 if (ae &
1005 1005 (FATTR4_ACL_MASK |
1006 1006 FATTR4_ACLSUPPORT_MASK |
1007 1007 FATTR4_ARCHIVE_MASK |
1008 1008 FATTR4_CANSETTIME_MASK |
1009 1009 FATTR4_CASE_INSENSITIVE_MASK |
1010 1010 FATTR4_CASE_PRESERVING_MASK |
1011 1011 FATTR4_CHOWN_RESTRICTED_MASK)) {
1012 1012
1013 1013 if (ae & FATTR4_ACL_MASK) {
1014 1014 ASSERT(0);
1015 1015 }
1016 1016 if (ae & FATTR4_ACLSUPPORT_MASK) {
1017 1017 ASSERT(0);
1018 1018 }
1019 1019 if (ae & FATTR4_ARCHIVE_MASK) {
1020 1020 ASSERT(0);
1021 1021 }
1022 1022 if (ae & FATTR4_CANSETTIME_MASK) {
1023 1023 IXDR_PUT_U_INT32(ptr, true);
1024 1024 }
1025 1025 if (ae & FATTR4_CASE_INSENSITIVE_MASK) {
1026 1026 IXDR_PUT_U_INT32(ptr, false);
1027 1027 }
1028 1028 if (ae & FATTR4_CASE_PRESERVING_MASK) {
1029 1029 IXDR_PUT_U_INT32(ptr, true);
1030 1030 }
1031 1031 if (ae & FATTR4_CHOWN_RESTRICTED_MASK) {
1032 1032 uint_t isit;
1033 1033 pc_val = FALSE;
1034 1034 (void) VOP_PATHCONF(vp,
1035 1035 _PC_CHOWN_RESTRICTED,
1036 1036 &pc_val, cs->cr, NULL);
1037 1037 isit = (pc_val ? TRUE : FALSE);
1038 1038 IXDR_PUT_U_INT32(ptr, isit);
1039 1039 }
1040 1040 /* Check the redzone boundary */
1041 1041 if (ptr > ptr_redzone) {
1042 1042 if (nents || IS_MIN_ATTR_MASK(ar)) {
1043 1043 no_space = TRUE;
1044 1044 continue;
1045 1045 }
1046 1046 MINIMIZE_ATTR_MASK(ar);
1047 1047 ae = ar;
1048 1048 ptr = lastentry_ptr;
1049 1049 goto reencode_attrs;
1050 1050 }
1051 1051 }
1052 1052 /*
1053 1053 * Redzone check is done before the filehandle
1054 1054 * is encoded.
1055 1055 */
1056 1056 if (ae &
1057 1057 (FATTR4_FILEHANDLE_MASK |
1058 1058 FATTR4_FILEID_MASK)) {
1059 1059
1060 1060 if (ae & FATTR4_FILEHANDLE_MASK) {
1061 1061 struct {
1062 1062 uint_t len;
1063 1063 char *val;
1064 1064 char fh[NFS_FH4_LEN];
1065 1065 } fh;
1066 1066 fh.len = 0;
1067 1067 fh.val = fh.fh;
1068 1068 (void) makefh4((nfs_fh4 *)&fh, vp,
1069 1069 (newexi ? newexi : cs->exi));
1070 1070
1071 1071 if (dvp->v_flag & V_XATTRDIR)
1072 1072 set_fh4_flag((nfs_fh4 *)&fh,
1073 1073 FH4_NAMEDATTR);
1074 1074
1075 1075 if (!xdr_inline_encode_nfs_fh4(
1076 1076 &ptr, ptr_redzone,
1077 1077 (nfs_fh4_fmt_t *)fh.val)) {
1078 1078 if (nents ||
1079 1079 IS_MIN_ATTR_MASK(ar)) {
1080 1080 no_space = TRUE;
1081 1081 continue;
1082 1082 }
1083 1083 MINIMIZE_ATTR_MASK(ar);
1084 1084 ae = ar;
1085 1085 ptr = lastentry_ptr;
1086 1086 goto reencode_attrs;
1087 1087 }
1088 1088 }
1089 1089 if (ae & FATTR4_FILEID_MASK) {
1090 1090 IXDR_PUT_HYPER(ptr, va.va_nodeid);
1091 1091 }
1092 1092 /* Check the redzone boundary */
1093 1093 if (ptr > ptr_redzone) {
1094 1094 if (nents || IS_MIN_ATTR_MASK(ar)) {
1095 1095 no_space = TRUE;
1096 1096 continue;
1097 1097 }
1098 1098 MINIMIZE_ATTR_MASK(ar);
1099 1099 ae = ar;
1100 1100 ptr = lastentry_ptr;
1101 1101 goto reencode_attrs;
1102 1102 }
1103 1103 }
1104 1104 /*
1105 1105 * Redzone check is done at the end of this section.
1106 1106 * This particular section will encode a maximum of
1107 1107 * 15 * BYTES_PER_XDR_UNIT of data.
1108 1108 */
1109 1109 if (ae &
1110 1110 (FATTR4_FILES_AVAIL_MASK |
1111 1111 FATTR4_FILES_FREE_MASK |
1112 1112 FATTR4_FILES_TOTAL_MASK |
1113 1113 FATTR4_FS_LOCATIONS_MASK |
1114 1114 FATTR4_HIDDEN_MASK |
1115 1115 FATTR4_HOMOGENEOUS_MASK |
1116 1116 FATTR4_MAXFILESIZE_MASK |
1117 1117 FATTR4_MAXLINK_MASK |
1118 1118 FATTR4_MAXNAME_MASK |
1119 1119 FATTR4_MAXREAD_MASK |
1120 1120 FATTR4_MAXWRITE_MASK)) {
1121 1121
1122 1122 if (ae & FATTR4_FILES_AVAIL_MASK) {
1123 1123 IXDR_PUT_HYPER(ptr, sbe.fa);
1124 1124 }
1125 1125 if (ae & FATTR4_FILES_FREE_MASK) {
1126 1126 IXDR_PUT_HYPER(ptr, sbe.ff);
1127 1127 }
1128 1128 if (ae & FATTR4_FILES_TOTAL_MASK) {
1129 1129 IXDR_PUT_HYPER(ptr, sbe.ft);
1130 1130 }
1131 1131 if (ae & FATTR4_FS_LOCATIONS_MASK) {
1132 1132 ASSERT(0);
1133 1133 }
1134 1134 if (ae & FATTR4_HIDDEN_MASK) {
1135 1135 ASSERT(0);
1136 1136 }
1137 1137 if (ae & FATTR4_HOMOGENEOUS_MASK) {
1138 1138 IXDR_PUT_U_INT32(ptr, true);
1139 1139 }
1140 1140 if (ae & FATTR4_MAXFILESIZE_MASK) {
1141 1141 IXDR_PUT_HYPER(ptr, pce.maxfilesize);
1142 1142 }
1143 1143 if (ae & FATTR4_MAXLINK_MASK) {
1144 1144 IXDR_PUT_U_INT32(ptr, pce.maxlink);
1145 1145 }
1146 1146 if (ae & FATTR4_MAXNAME_MASK) {
1147 1147 IXDR_PUT_U_INT32(ptr, pce.maxname);
1148 1148 }
1149 1149 if (ae & FATTR4_MAXREAD_MASK) {
1150 1150 IXDR_PUT_HYPER(ptr, maxread);
1151 1151 }
1152 1152 if (ae & FATTR4_MAXWRITE_MASK) {
1153 1153 IXDR_PUT_HYPER(ptr, maxwrite);
1154 1154 }
1155 1155 /* Check the redzone boundary */
1156 1156 if (ptr > ptr_redzone) {
1157 1157 if (nents || IS_MIN_ATTR_MASK(ar)) {
1158 1158 no_space = TRUE;
1159 1159 continue;
1160 1160 }
1161 1161 MINIMIZE_ATTR_MASK(ar);
1162 1162 ae = ar;
1163 1163 ptr = lastentry_ptr;
1164 1164 goto reencode_attrs;
1165 1165 }
1166 1166 }
1167 1167 }
1168 1168 if (ae & 0x00000000ffffffff) {
1169 1169 /*
1170 1170 * Redzone check is done at the end of this section.
1171 1171 * This particular section will encode a maximum of
1172 1172 * 3 * BYTES_PER_XDR_UNIT of data.
1173 1173 */
1174 1174 if (ae &
1175 1175 (FATTR4_MIMETYPE_MASK |
1176 1176 FATTR4_MODE_MASK |
1177 1177 FATTR4_NO_TRUNC_MASK |
1178 1178 FATTR4_NUMLINKS_MASK)) {
1179 1179
1180 1180 if (ae & FATTR4_MIMETYPE_MASK) {
1181 1181 ASSERT(0);
1182 1182 }
1183 1183 if (ae & FATTR4_MODE_MASK) {
1184 1184 uint_t m = va.va_mode;
1185 1185 IXDR_PUT_U_INT32(ptr, m);
1186 1186 }
1187 1187 if (ae & FATTR4_NO_TRUNC_MASK) {
1188 1188 IXDR_PUT_U_INT32(ptr, true);
1189 1189 }
1190 1190 if (ae & FATTR4_NUMLINKS_MASK) {
1191 1191 IXDR_PUT_U_INT32(ptr, va.va_nlink);
1192 1192 }
1193 1193 /* Check the redzone boundary */
1194 1194 if (ptr > ptr_redzone) {
1195 1195 if (nents || IS_MIN_ATTR_MASK(ar)) {
1196 1196 no_space = TRUE;
1197 1197 continue;
1198 1198 }
1199 1199 MINIMIZE_ATTR_MASK(ar);
1200 1200 ae = ar;
1201 1201 ptr = lastentry_ptr;
1202 1202 goto reencode_attrs;
1203 1203 }
1204 1204 }
1205 1205 /*
1206 1206 * Redzone check is done before the encoding of the
1207 1207 * owner string since the length is indeterminate.
1208 1208 */
1209 1209 if (ae & FATTR4_OWNER_MASK) {
1210 1210 if (!lu_set) {
1211 1211 owner_error = nfs_idmap_uid_str(
1212 1212 va.va_uid, &owner, TRUE);
1213 1213 if (!owner_error) {
1214 1214 lu_set = TRUE;
1215 1215 lastuid = va.va_uid;
1216 1216 }
1217 1217 } else if (va.va_uid != lastuid) {
1218 1218 if (owner.utf8string_len != 0) {
1219 1219 kmem_free(owner.utf8string_val,
1220 1220 owner.utf8string_len);
1221 1221 owner.utf8string_len = 0;
1222 1222 owner.utf8string_val = NULL;
1223 1223 }
1224 1224 owner_error = nfs_idmap_uid_str(
1225 1225 va.va_uid, &owner, TRUE);
1226 1226 if (!owner_error) {
1227 1227 lastuid = va.va_uid;
1228 1228 } else {
1229 1229 lu_set = FALSE;
1230 1230 }
1231 1231 }
1232 1232 if (!owner_error) {
1233 1233 if ((ptr +
1234 1234 (owner.utf8string_len /
1235 1235 BYTES_PER_XDR_UNIT)
1236 1236 + 2) > ptr_redzone) {
1237 1237 if (nents ||
1238 1238 IS_MIN_ATTR_MASK(ar)) {
1239 1239 no_space = TRUE;
1240 1240 continue;
1241 1241 }
1242 1242 MINIMIZE_ATTR_MASK(ar);
1243 1243 ae = ar;
1244 1244 ptr = lastentry_ptr;
1245 1245 goto reencode_attrs;
1246 1246 }
1247 1247 /* encode the LENGTH of owner string */
1248 1248 IXDR_PUT_U_INT32(ptr,
1249 1249 owner.utf8string_len);
1250 1250 /* encode the RNDUP FILL first */
1251 1251 rndup = RNDUP(owner.utf8string_len) /
1252 1252 BYTES_PER_XDR_UNIT;
1253 1253 ptr[rndup - 1] = 0;
1254 1254 /* encode the OWNER */
1255 1255 bcopy(owner.utf8string_val, ptr,
1256 1256 owner.utf8string_len);
1257 1257 ptr += rndup;
1258 1258 }
1259 1259 }
1260 1260 /*
1261 1261 * Redzone check is done before the encoding of the
1262 1262 * group string since the length is indeterminate.
1263 1263 */
1264 1264 if (ae & FATTR4_OWNER_GROUP_MASK) {
1265 1265 if (!lg_set) {
1266 1266 group_error =
1267 1267 nfs_idmap_gid_str(va.va_gid,
1268 1268 &group, TRUE);
1269 1269 if (!group_error) {
1270 1270 lg_set = TRUE;
1271 1271 lastgid = va.va_gid;
1272 1272 }
1273 1273 } else if (va.va_gid != lastgid) {
1274 1274 if (group.utf8string_len != 0) {
1275 1275 kmem_free(
1276 1276 group.utf8string_val,
1277 1277 group.utf8string_len);
1278 1278 group.utf8string_len = 0;
1279 1279 group.utf8string_val = NULL;
1280 1280 }
1281 1281 group_error =
1282 1282 nfs_idmap_gid_str(va.va_gid,
1283 1283 &group, TRUE);
1284 1284 if (!group_error)
1285 1285 lastgid = va.va_gid;
1286 1286 else
1287 1287 lg_set = FALSE;
1288 1288 }
1289 1289 if (!group_error) {
1290 1290 if ((ptr +
1291 1291 (group.utf8string_len /
1292 1292 BYTES_PER_XDR_UNIT)
1293 1293 + 2) > ptr_redzone) {
1294 1294 if (nents ||
1295 1295 IS_MIN_ATTR_MASK(ar)) {
1296 1296 no_space = TRUE;
1297 1297 continue;
1298 1298 }
1299 1299 MINIMIZE_ATTR_MASK(ar);
1300 1300 ae = ar;
1301 1301 ptr = lastentry_ptr;
1302 1302 goto reencode_attrs;
1303 1303 }
1304 1304 /* encode the LENGTH of owner string */
1305 1305 IXDR_PUT_U_INT32(ptr,
1306 1306 group.utf8string_len);
1307 1307 /* encode the RNDUP FILL first */
1308 1308 rndup = RNDUP(group.utf8string_len) /
1309 1309 BYTES_PER_XDR_UNIT;
1310 1310 ptr[rndup - 1] = 0;
1311 1311 /* encode the OWNER */
1312 1312 bcopy(group.utf8string_val, ptr,
1313 1313 group.utf8string_len);
1314 1314 ptr += rndup;
1315 1315 }
1316 1316 }
1317 1317 if (ae &
1318 1318 (FATTR4_QUOTA_AVAIL_HARD_MASK |
1319 1319 FATTR4_QUOTA_AVAIL_SOFT_MASK |
1320 1320 FATTR4_QUOTA_USED_MASK)) {
1321 1321 if (ae & FATTR4_QUOTA_AVAIL_HARD_MASK) {
1322 1322 ASSERT(0);
1323 1323 }
1324 1324 if (ae & FATTR4_QUOTA_AVAIL_SOFT_MASK) {
1325 1325 ASSERT(0);
1326 1326 }
1327 1327 if (ae & FATTR4_QUOTA_USED_MASK) {
1328 1328 ASSERT(0);
1329 1329 }
1330 1330 }
1331 1331 /*
1332 1332 * Redzone check is done at the end of this section.
1333 1333 * This particular section will encode a maximum of
1334 1334 * 10 * BYTES_PER_XDR_UNIT of data.
1335 1335 */
1336 1336 if (ae &
1337 1337 (FATTR4_RAWDEV_MASK |
1338 1338 FATTR4_SPACE_AVAIL_MASK |
1339 1339 FATTR4_SPACE_FREE_MASK |
1340 1340 FATTR4_SPACE_TOTAL_MASK |
1341 1341 FATTR4_SPACE_USED_MASK |
1342 1342 FATTR4_SYSTEM_MASK)) {
1343 1343
1344 1344 if (ae & FATTR4_RAWDEV_MASK) {
1345 1345 fattr4_rawdev rd;
1346 1346 rd.specdata1 =
1347 1347 (uint32)getmajor(va.va_rdev);
1348 1348 rd.specdata2 =
1349 1349 (uint32)getminor(va.va_rdev);
1350 1350 IXDR_PUT_U_INT32(ptr, rd.specdata1);
1351 1351 IXDR_PUT_U_INT32(ptr, rd.specdata2);
1352 1352 }
1353 1353 if (ae & FATTR4_SPACE_AVAIL_MASK) {
1354 1354 IXDR_PUT_HYPER(ptr, sbe.space_avail);
1355 1355 }
1356 1356 if (ae & FATTR4_SPACE_FREE_MASK) {
1357 1357 IXDR_PUT_HYPER(ptr, sbe.space_free);
1358 1358 }
1359 1359 if (ae & FATTR4_SPACE_TOTAL_MASK) {
1360 1360 IXDR_PUT_HYPER(ptr, sbe.space_total);
1361 1361 }
1362 1362 if (ae & FATTR4_SPACE_USED_MASK) {
1363 1363 u_longlong_t su;
1364 1364 su = (fattr4_space_used) DEV_BSIZE *
1365 1365 (fattr4_space_used) va.va_nblocks;
1366 1366 IXDR_PUT_HYPER(ptr, su);
1367 1367 }
1368 1368 if (ae & FATTR4_SYSTEM_MASK) {
1369 1369 ASSERT(0);
1370 1370 }
1371 1371 /* Check the redzone boundary */
1372 1372 if (ptr > ptr_redzone) {
1373 1373 if (nents || IS_MIN_ATTR_MASK(ar)) {
1374 1374 no_space = TRUE;
1375 1375 continue;
1376 1376 }
1377 1377 MINIMIZE_ATTR_MASK(ar);
1378 1378 ae = ar;
1379 1379 ptr = lastentry_ptr;
1380 1380 goto reencode_attrs;
1381 1381 }
1382 1382 }
1383 1383 /*
1384 1384 * Redzone check is done at the end of this section.
1385 1385 * This particular section will encode a maximum of
1386 1386 * 14 * BYTES_PER_XDR_UNIT of data.
1387 1387 */
1388 1388 if (ae &
1389 1389 (FATTR4_TIME_ACCESS_MASK |
↓ open down ↓ |
1389 lines elided |
↑ open up ↑ |
1390 1390 FATTR4_TIME_ACCESS_SET_MASK |
1391 1391 FATTR4_TIME_BACKUP_MASK |
1392 1392 FATTR4_TIME_CREATE_MASK |
1393 1393 FATTR4_TIME_DELTA_MASK |
1394 1394 FATTR4_TIME_METADATA_MASK |
1395 1395 FATTR4_TIME_MODIFY_MASK |
1396 1396 FATTR4_TIME_MODIFY_SET_MASK |
1397 1397 FATTR4_MOUNTED_ON_FILEID_MASK)) {
1398 1398
1399 1399 if (ae & FATTR4_TIME_ACCESS_MASK) {
1400 - u_longlong_t sec =
1401 - (u_longlong_t)va.va_atime.tv_sec;
1402 - uint_t nsec =
1403 - (uint_t)va.va_atime.tv_nsec;
1404 - IXDR_PUT_HYPER(ptr, sec);
1405 - IXDR_PUT_INT32(ptr, nsec);
1400 + nfstime4 atime;
1401 + (void) nfs4_time_vton(&va.va_atime,
1402 + &atime);
1403 + IXDR_PUT_HYPER(ptr, atime.seconds);
1404 + IXDR_PUT_INT32(ptr, atime.nseconds);
1406 1405 }
1407 1406 if (ae & FATTR4_TIME_ACCESS_SET_MASK) {
1408 1407 ASSERT(0);
1409 1408 }
1410 1409 if (ae & FATTR4_TIME_BACKUP_MASK) {
1411 1410 ASSERT(0);
1412 1411 }
1413 1412 if (ae & FATTR4_TIME_CREATE_MASK) {
1414 1413 ASSERT(0);
1415 1414 }
1416 1415 if (ae & FATTR4_TIME_DELTA_MASK) {
1417 1416 u_longlong_t sec = 0;
1418 1417 uint_t nsec = 1000;
1419 1418 IXDR_PUT_HYPER(ptr, sec);
1420 1419 IXDR_PUT_INT32(ptr, nsec);
1421 1420 }
1422 1421 if (ae & FATTR4_TIME_METADATA_MASK) {
1423 - u_longlong_t sec =
1424 - (u_longlong_t)va.va_ctime.tv_sec;
1425 - uint_t nsec =
1426 - (uint_t)va.va_ctime.tv_nsec;
1427 - IXDR_PUT_HYPER(ptr, sec);
1428 - IXDR_PUT_INT32(ptr, nsec);
1422 + nfstime4 ctime;
1423 + (void) nfs4_time_vton(&va.va_ctime,
1424 + &ctime);
1425 + IXDR_PUT_HYPER(ptr, ctime.seconds);
1426 + IXDR_PUT_INT32(ptr, ctime.nseconds);
1429 1427 }
1430 1428 if (ae & FATTR4_TIME_MODIFY_MASK) {
1431 - u_longlong_t sec =
1432 - (u_longlong_t)va.va_mtime.tv_sec;
1433 - uint_t nsec =
1434 - (uint_t)va.va_mtime.tv_nsec;
1435 - IXDR_PUT_HYPER(ptr, sec);
1436 - IXDR_PUT_INT32(ptr, nsec);
1429 + nfstime4 mtime;
1430 + (void) nfs4_time_vton(&va.va_mtime,
1431 + &mtime);
1432 + IXDR_PUT_HYPER(ptr, mtime.seconds);
1433 + IXDR_PUT_INT32(ptr, mtime.nseconds);
1437 1434 }
1438 1435 if (ae & FATTR4_TIME_MODIFY_SET_MASK) {
1439 1436 ASSERT(0);
1440 1437 }
1441 1438 if (ae & FATTR4_MOUNTED_ON_FILEID_MASK) {
1442 1439 IXDR_PUT_HYPER(ptr, dp->d_ino);
1443 1440 }
1444 1441 /* Check the redzone boundary */
1445 1442 if (ptr > ptr_redzone) {
1446 1443 if (nents || IS_MIN_ATTR_MASK(ar)) {
1447 1444 no_space = TRUE;
1448 1445 continue;
1449 1446 }
1450 1447 MINIMIZE_ATTR_MASK(ar);
1451 1448 ae = ar;
1452 1449 ptr = lastentry_ptr;
1453 1450 goto reencode_attrs;
1454 1451 }
1455 1452 }
1456 1453 }
1457 1454
1458 1455 /* Reset to directory's vfs info when encoding complete */
1459 1456 if (vfs_different) {
1460 1457 dsbe = sbe;
1461 1458 dpce = pce;
1462 1459 vfs_different = 0;
1463 1460 }
1464 1461
1465 1462 /* "go back" and encode the attributes' length */
1466 1463 attr_length =
1467 1464 (char *)ptr -
1468 1465 (char *)attr_offset_ptr -
1469 1466 BYTES_PER_XDR_UNIT;
1470 1467 IXDR_PUT_U_INT32(attr_offset_ptr, attr_length);
1471 1468
1472 1469 /*
1473 1470 * If there was trouble obtaining a mapping for either
1474 1471 * the owner or group attributes, then remove them from
1475 1472 * bitmap4 for this entry and reset the bitmap value
1476 1473 * in the data stream.
1477 1474 */
1478 1475 if (owner_error || group_error) {
1479 1476 if (owner_error)
1480 1477 ae &= ~FATTR4_OWNER_MASK;
1481 1478 if (group_error)
1482 1479 ae &= ~FATTR4_OWNER_GROUP_MASK;
1483 1480 IXDR_PUT_HYPER(attrmask_ptr, ae);
1484 1481 }
1485 1482
1486 1483 /* END OF ATTRIBUTE ENCODING */
1487 1484
1488 1485 lastentry_ptr = ptr;
1489 1486 nents++;
1490 1487 rddir_next_offset = dp->d_off;
1491 1488 }
1492 1489
1493 1490 /*
1494 1491 * Check for the case that another VOP_READDIR() has to be done.
1495 1492 * - no space encoding error
1496 1493 * - no entry successfully encoded
1497 1494 * - still more directory to read
1498 1495 */
1499 1496 if (!no_space && nents == 0 && !iseofdir)
1500 1497 goto readagain;
1501 1498
1502 1499 *cs->statusp = resp->status = NFS4_OK;
1503 1500
1504 1501 /*
1505 1502 * If no_space is set then we terminated prematurely,
1506 1503 * rewind to the last entry and this can never be EOF.
1507 1504 */
1508 1505 if (no_space) {
1509 1506 ptr = lastentry_ptr;
1510 1507 eof = FALSE; /* ended encoded prematurely */
1511 1508 } else {
1512 1509 eof = (iseofdir ? TRUE : FALSE);
1513 1510 }
1514 1511
1515 1512 /*
1516 1513 * If we have entries, always return them, otherwise only error
1517 1514 * if we ran out of space.
1518 1515 */
1519 1516 if (nents || !no_space) {
1520 1517 ASSERT(ptr != NULL);
1521 1518 /* encode the BOOLEAN marking no further entries */
1522 1519 IXDR_PUT_U_INT32(ptr, false);
1523 1520 /* encode the BOOLEAN signifying end of directory */
1524 1521 IXDR_PUT_U_INT32(ptr, eof);
1525 1522
1526 1523 resp->data_len = (char *)ptr - (char *)beginning_ptr;
1527 1524 resp->mblk->b_wptr += resp->data_len;
1528 1525 } else {
1529 1526 freeb(mp);
1530 1527 resp->mblk = NULL;
1531 1528 resp->data_len = 0;
1532 1529 *cs->statusp = resp->status = NFS4ERR_TOOSMALL;
1533 1530 }
1534 1531
1535 1532 kmem_free((caddr_t)rddir_data, rddir_data_len);
1536 1533 if (vp)
1537 1534 VN_RELE(vp);
1538 1535 if (owner.utf8string_len != 0)
1539 1536 kmem_free(owner.utf8string_val, owner.utf8string_len);
1540 1537 if (group.utf8string_len != 0)
1541 1538 kmem_free(group.utf8string_val, group.utf8string_len);
1542 1539
1543 1540 out:
1544 1541 DTRACE_NFSV4_2(op__readdir__done, struct compound_state *, cs,
1545 1542 READDIR4res *, resp);
1546 1543 }
↓ open down ↓ |
100 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX