xref: /titanic_50/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision af79d7d9cd4d7b7b3b52261fc6f4be7426335485)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * General Structures Layout
28  * -------------------------
29  *
30  * This is a simplified diagram showing the relationship between most of the
31  * main structures.
32  *
33  * +-------------------+
34  * |     SMB_INFO      |
35  * +-------------------+
36  *          |
37  *          |
38  *          v
39  * +-------------------+       +-------------------+      +-------------------+
40  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
41  * +-------------------+       +-------------------+      +-------------------+
42  *          |
43  *          |
44  *          v
45  * +-------------------+       +-------------------+      +-------------------+
46  * |       USER        |<----->|       USER        |......|       USER        |
47  * +-------------------+       +-------------------+      +-------------------+
48  *          |
49  *          |
50  *          v
51  * +-------------------+       +-------------------+      +-------------------+
52  * |       TREE        |<----->|       TREE        |......|       TREE        |
53  * +-------------------+       +-------------------+      +-------------------+
54  *      |         |
55  *      |         |
56  *      |         v
57  *      |     +-------+       +-------+      +-------+
58  *      |     | OFILE |<----->| OFILE |......| OFILE |
59  *      |     +-------+       +-------+      +-------+
60  *      |
61  *      |
62  *      v
63  *  +-------+       +------+      +------+
64  *  | ODIR  |<----->| ODIR |......| ODIR |
65  *  +-------+       +------+      +------+
66  *
67  *
68  * Odir State Machine
69  * ------------------
70  *
71  *    +-------------------------+
72  *    |  SMB_ODIR_STATE_OPEN    |<----------- open / creation
73  *    +-------------------------+
74  *		    |
75  *		    | close
76  *		    |
77  *		    v
78  *    +-------------------------+
79  *    | SMB_ODIR_STATE_CLOSING  |
80  *    +-------------------------+
81  *		    |
82  *		    | last release
83  *		    |
84  *		    v
85  *    +-------------------------+
86  *    | SMB_ODIR_STATE_CLOSED   |----------> deletion
87  *    +-------------------------+
88  *
89  *
90  * SMB_ODIR_STATE_OPEN
91  * - the odir exists in the list of odirs of its tree.
92  * - references will be given out if the odir is looked up
93  * - if a close is received the odir will transition to
94  *   SMB_ODIR_STATE_CLOSING.
95  *
96  * SMB_ODIR_STATE_CLOSING
97  * - the odir exists in the list of odirs of its tree.
98  * - references will NOT be given out if the odir is looked up.
99  * - when the last reference is released (refcnt == 0) the
100  *   odir will transition to SMB_ODIR_STATE_CLOSED.
101  *
102  * SMB_ODIR_STATE_CLOSED
103  * - the odir exists in the list of odirs of its tree.
104  * - there are no users of the odir (refcnt == 0)
105  * - references will NOT be given out if the odir is looked up.
106  * - the odir is being removed from the tree's list and deleted.
107  *
108  * Comments
109  * --------
110  *    The state machine of the odir structures is controlled by 3 elements:
111  *      - The list of odirs of the tree it belongs to.
112  *      - The mutex embedded in the structure itself.
113  *      - The reference count.
114  *
115  *    There's a mutex embedded in the odir structure used to protect its fields
116  *    and there's a lock embedded in the list of odirs of a tree. To
117  *    increment or to decrement the reference count the mutex must be entered.
118  *    To insert the odir into the list of odirs of the tree and to remove
119  *    the odir from it, the lock must be entered in RW_WRITER mode.
120  *
121  *    In order to avoid deadlocks, when both (mutex and lock of the odir
122  *    list) have to be entered, the lock must be entered first.
123  *
124  *
125  * Odir Interface
126  * ---------------
127  * odid = smb_odir_open(pathname)
128  *	Create an odir representing the directory specified in pathname and
129  *	add it into the tree's list of odirs.
130  *	Return an identifier (odid) uniquely identifying the created odir.
131  *
132  * smb_odir_openat(smb_node_t *unode)
133  *	Create an odir representing the extended attribute directory
134  *	associated with the file (or directory) represented by unode
135  *	and add it into the tree's list of odirs.
136  *	Return an identifier (odid) uniquely identifying the created odir.
137  *
138  * smb_odir_t *odir = smb_tree_lookup_odir(odid)
139  *	Find the odir corresponding to the specified odid in the tree's
140  *	list of odirs.
141  *
142  * smb_odir_read(..., smb_odirent_t *odirent)
143  *	Find the next directory entry in the odir and return it in odirent.
144  *
145  * smb_odir_read_fileinfo(..., smb_fileinfo_t *)
146  *	Find the next directory entry in the odir. Return the details of
147  *	the directory entry in smb_fileinfo_t. (See odir internals below)
148  *
149  * smb_odir_read_stream_info(..., smb_streaminfo_t *)
150  *	Find the next named stream entry in the odir. Return the details of
151  *	the named stream in smb_streaminfo_t.
152  *
153  * smb_odir_release(smb_odir_t *odir)
154  *	Release the hold on the odir, obtained by lookup.
155  *
156  * smb_odir_close(smb_odir_t *odir)
157  *	Close the odir and remove it from the tree's list of odirs.
158  *
159  *
160  * Odir Internals
161  * --------------
162  * The odir object represent an open directory search. Each read operation
163  * provides the caller with a structure containing information  pertaining
164  * to the next directory entry that matches the search criteria, namely
165  * the filename or match pattern and, in the case of smb_odir_read_fileinfo(),
166  * the search attributes.
167  *
168  * The odir maintains a buffer (d_buf) of directory entries read from
169  * the filesystem via a vop_readdir. The buffer is populated when a read
170  * request (smb_odir_next_odirent) finds that the buffer is empty or that
171  * the end of the buffer has been reached, and also when a new client request
172  * (find next) begins.
173  *
174  * The data in d_buf (that which is returned from the file system) can
175  * be in one of two formats. If the file system supports extended directory
176  * entries we request that the data be returned as edirent_t structures. If
177  * it does not the data will be returned as dirent64_t structures. For
178  * convenience, when the next directory entry is read from d_buf by
179  * smb_odir_next_odirent it is translated into an smb_odirent_t.
180  *
181  * smb_odir_read_fileinfo
182  * The processing required to obtain the information to populate the caller's
183  * smb_fileinfo_t differs depending upon whether the directory search is for a
184  * single specified filename or for multiple files matching a search pattern.
185  * Thus smb_odir_read_fileinfo uses two static functions:
186  * smb_odir_single_fileinfo - obtains the smb_fileinfo_t info for the single
187  * filename as specified in smb_odir_open request.
188  * smb_odir_wildcard_fileinfo - obtains the smb_fileinfo_t info for the filename
189  * returned from the smb_odir_next_odirent. This is called in a loop until
190  * an entry matching the search criteria is found or no more entries exist.
191  *
192  * If a directory entry is a VLNK, the name returned in the smb_fileinfo_t
193  * is the name of the directory entry but the attributes are the attribites
194  * of the file that is the target of the link. If the link target cannot
195  * be found the attributes returned are the attributes of the link itself.
196  *
197  * smb_odir_read_stream_info
198  * In order for an odir to provide information about stream files it
199  * must be opened with smb_odir_openat(). smb_odir_read_streaminfo() can
200  * then be used to obtain the name and size of named stream files.
201  *
202  * Resuming a Search
203  * -----------------
204  * A directory search often consists of multiple client requests: an initial
205  * find_first request followed by zero or more find_next requests and a
206  * find_close request.
207  * The find_first request will open and lookup the odir, read its desired
208  * number of entries from the odir, then release the odir and return.
209  * A find_next request will lookup the odir and read its desired number of
210  * entries from the odir, then release the odir and return.
211  * At the end of the search the find_close request will close the odir.
212  *
213  * In order to be able to resume a directory search (find_next) the odir
214  * provides the capability for the caller to save one or more resume points
215  * (cookies) at the end of a request, and to specify which resume point
216  * (cookie) to restart from at the beginning of the next search.
217  *	smb_odir_save_cookie(..., cookie)
218  *	smb_odir_resume_at(smb_odir_resume_t *resume)
219  * A search can be resumed at a specified resume point (cookie), the resume
220  * point (cookie) stored at a specified index in the d_cookies array, or
221  * a specified filename. The latter (specified filename) is not yet supported.
222  *
223  * See smb_search, smb_find, smb_find_unique, and smb_trans2_find for details
224  */
225 
226 #include <smbsrv/smb_incl.h>
227 #include <smbsrv/smb_kproto.h>
228 #include <smbsrv/smb_fsops.h>
229 #include <sys/extdirent.h>
230 
231 /* static functions */
232 static smb_odir_t *smb_odir_create(smb_request_t *, smb_node_t *,
233     char *, uint16_t);
234 static void smb_odir_delete(smb_odir_t *);
235 static int smb_odir_single_fileinfo(smb_request_t *, smb_odir_t *,
236     smb_fileinfo_t *);
237 static int smb_odir_wildcard_fileinfo(smb_request_t *, smb_odir_t *,
238     smb_odirent_t *, smb_fileinfo_t *);
239 static int smb_odir_next_odirent(smb_odir_t *, smb_odirent_t *);
240 static boolean_t smb_odir_lookup_link(smb_request_t *, smb_odir_t *, char *,
241     smb_node_t **, smb_attr_t *);
242 
243 
244 /*
245  * smb_odir_open
246  *
247  * Create an odir representing the directory specified in pathname.
248  *
249  * Returns:
250  * odid - Unique identifier of newly created odir.
251  *    0 - error, error details set in sr.
252  */
253 uint16_t
254 smb_odir_open(smb_request_t *sr, char *path, uint16_t sattr)
255 {
256 	int		rc;
257 	smb_tree_t	*tree;
258 	smb_node_t	*dnode;
259 	char		pattern[MAXNAMELEN];
260 	smb_odir_t 	*od;
261 
262 	ASSERT(sr);
263 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
264 	ASSERT(sr->tid_tree);
265 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
266 
267 	tree = sr->tid_tree;
268 
269 	rc = smb_pathname_reduce(sr, sr->user_cr, path,
270 	    tree->t_snode, tree->t_snode, &dnode, pattern);
271 	if (rc != 0) {
272 		smbsr_errno(sr, rc);
273 		return (0);
274 	}
275 
276 	if (dnode->vp->v_type != VDIR) {
277 		smbsr_error(sr, NT_STATUS_OBJECT_PATH_NOT_FOUND,
278 		    ERRDOS, ERROR_PATH_NOT_FOUND);
279 		smb_node_release(dnode);
280 		return (0);
281 	}
282 
283 	if (smb_fsop_access(sr, sr->user_cr, dnode, FILE_LIST_DIRECTORY) != 0) {
284 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
285 		    ERRDOS, ERROR_ACCESS_DENIED);
286 		smb_node_release(dnode);
287 		return (0);
288 	}
289 
290 	od = smb_odir_create(sr, dnode, pattern, sattr);
291 	smb_node_release(dnode);
292 	return (od ? od->d_odid : 0);
293 }
294 
295 /*
296  * smb_odir_openat
297  *
298  * Create an odir representing the extended attribute directory
299  * associated with the file (or directory) represented by unode.
300  *
301  * Returns:
302  * odid - Unique identifier of newly created odir.
303  *    0 - error, error details set in sr.
304  */
305 uint16_t
306 smb_odir_openat(smb_request_t *sr, smb_node_t *unode)
307 {
308 	int		rc;
309 	vnode_t		*xattr_dvp;
310 	smb_odir_t	*od;
311 	cred_t		*cr;
312 	char		pattern[SMB_STREAM_PREFIX_LEN + 2];
313 
314 	smb_node_t	*xattr_dnode;
315 	smb_attr_t	tmp_attr;
316 
317 	ASSERT(sr);
318 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
319 	ASSERT(unode);
320 	ASSERT(unode->n_magic == SMB_NODE_MAGIC);
321 
322 	if (SMB_TREE_CONTAINS_NODE(sr, unode) == 0 ||
323 	    SMB_TREE_HAS_ACCESS(sr, ACE_LIST_DIRECTORY) == 0) {
324 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
325 		    ERRDOS, ERROR_ACCESS_DENIED);
326 		return (0);
327 	}
328 	cr = sr->user_cr;
329 
330 	/* find the xattrdir vnode */
331 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
332 	if (rc != 0) {
333 		smbsr_errno(sr, rc);
334 		return (0);
335 	}
336 
337 	/* lookup the xattrdir's smb_node */
338 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
339 	    unode, NULL, &tmp_attr);
340 	VN_RELE(xattr_dvp);
341 	if (xattr_dnode == NULL) {
342 		smbsr_error(sr, NT_STATUS_NO_MEMORY,
343 		    ERRDOS, ERROR_NOT_ENOUGH_MEMORY);
344 		return (0);
345 	}
346 
347 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
348 	od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES);
349 	smb_node_release(xattr_dnode);
350 	if (od == NULL)
351 		return (0);
352 
353 	od->d_xat = B_TRUE;
354 	return (od->d_odid);
355 }
356 
357 /*
358  * smb_odir_hold
359  */
360 boolean_t
361 smb_odir_hold(smb_odir_t *od)
362 {
363 	ASSERT(od);
364 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
365 
366 	mutex_enter(&od->d_mutex);
367 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
368 		mutex_exit(&od->d_mutex);
369 		return (B_FALSE);
370 	}
371 
372 	od->d_refcnt++;
373 	mutex_exit(&od->d_mutex);
374 	return (B_TRUE);
375 }
376 
377 /*
378  * smb_odir_release
379  *
380  * If the odir is in SMB_ODIR_STATE_CLOSING and this release
381  * results in a refcnt of 0, the odir may be removed from
382  * the tree's list of odirs and deleted.  The odir's state is
383  * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and
384  * deleting it. This ensure that nobody else can ontain a reference
385  * to it while we are deleting it.
386  */
387 void
388 smb_odir_release(smb_odir_t *od)
389 {
390 	ASSERT(od);
391 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
392 
393 	mutex_enter(&od->d_mutex);
394 	ASSERT(od->d_refcnt);
395 
396 	switch (od->d_state) {
397 	case SMB_ODIR_STATE_OPEN:
398 		od->d_refcnt--;
399 		break;
400 	case SMB_ODIR_STATE_CLOSING:
401 		od->d_refcnt--;
402 		if (od->d_refcnt == 0) {
403 			od->d_state = SMB_ODIR_STATE_CLOSED;
404 			mutex_exit(&od->d_mutex);
405 			smb_odir_delete(od);
406 			return;
407 		}
408 		break;
409 	case SMB_ODIR_STATE_CLOSED:
410 		break;
411 	default:
412 		ASSERT(0);
413 		break;
414 	}
415 
416 	mutex_exit(&od->d_mutex);
417 }
418 
419 /*
420  * smb_odir_close
421  */
422 void
423 smb_odir_close(smb_odir_t *od)
424 {
425 	ASSERT(od);
426 	ASSERT(od->d_refcnt);
427 
428 	mutex_enter(&od->d_mutex);
429 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
430 		mutex_exit(&od->d_mutex);
431 		return;
432 	}
433 	od->d_state = SMB_ODIR_STATE_CLOSING;
434 	mutex_exit(&od->d_mutex);
435 
436 	smb_odir_release(od);
437 }
438 
439 /*
440  * smb_odir_read
441  *
442  * Find the next directory entry matching the search pattern.
443  * No search attribute matching is performed.
444  *
445  * Returns:
446  *  0 - success.
447  *      - If a matching entry was found eof will be B_FALSE and
448  *        odirent will be populated.
449  *      - If there are no matching entries eof will be B_TRUE.
450  * -1 - error, error details set in sr.
451  */
452 int
453 smb_odir_read(smb_request_t *sr, smb_odir_t *od,
454     smb_odirent_t *odirent, boolean_t *eof)
455 {
456 	int rc;
457 
458 	ASSERT(sr);
459 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
460 	ASSERT(od);
461 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
462 	ASSERT(odirent);
463 
464 	mutex_enter(&od->d_mutex);
465 
466 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
467 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
468 		mutex_exit(&od->d_mutex);
469 		return (-1);
470 	}
471 
472 	for (;;) {
473 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
474 			break;
475 		if (smb_match_name(odirent->od_ino, odirent->od_name,
476 		    od->d_pattern, od->d_ignore_case))
477 			break;
478 	}
479 
480 	mutex_exit(&od->d_mutex);
481 
482 	switch (rc) {
483 	case 0:
484 		*eof = B_FALSE;
485 		return (0);
486 	case ENOENT:
487 		*eof = B_TRUE;
488 		return (0);
489 	default:
490 		smbsr_errno(sr, rc);
491 		return (-1);
492 	}
493 }
494 
495 /*
496  * smb_odir_read_fileinfo
497  *
498  * Find the next directory entry matching the search pattern
499  * and attributes: od->d_pattern and od->d_sattr.
500  *
501  * If the search pattern specifies a single filename call
502  * smb_odir_single_fileinfo to get the file attributes and
503  * populate the caller's smb_fileinfo_t.
504  *
505  * If the search pattern contains wildcards call smb_odir_next_odirent
506  * to get the next directory entry then. Repeat until a matching
507  * filename is found. Call smb_odir_wildcard_fileinfo to get the
508  * file attributes and populate the caller's smb_fileinfo_t.
509  * This is repeated until a file matching the search criteria is found.
510  *
511  * Returns:
512  *  0 - success.
513  *      - If a matching entry was found eof will be B_FALSE and
514  *        fileinfo will be populated.
515  *      - If there are no matching entries eof will be B_TRUE.
516  * -1 - error, error details set in sr.
517  */
518 int
519 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
520     smb_fileinfo_t *fileinfo, boolean_t *eof)
521 {
522 	int		rc;
523 	smb_odirent_t	*odirent;
524 
525 	ASSERT(sr);
526 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
527 	ASSERT(od);
528 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
529 	ASSERT(fileinfo);
530 
531 	mutex_enter(&od->d_mutex);
532 
533 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
534 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
535 		mutex_exit(&od->d_mutex);
536 		return (-1);
537 	}
538 
539 	if (!od->d_wildcards) {
540 		if (od->d_eof)
541 			rc = ENOENT;
542 		else
543 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
544 		od->d_eof = B_TRUE;
545 	} else {
546 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
547 		for (;;) {
548 			bzero(fileinfo, sizeof (smb_fileinfo_t));
549 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
550 				break;
551 
552 			if (!smb_match_name(odirent->od_ino, odirent->od_name,
553 			    od->d_pattern, od->d_ignore_case))
554 				continue;
555 
556 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
557 			    fileinfo);
558 			if (rc == 0)
559 				break;
560 		}
561 		kmem_free(odirent, sizeof (smb_odirent_t));
562 	}
563 	mutex_exit(&od->d_mutex);
564 
565 	switch (rc) {
566 	case 0:
567 		*eof = B_FALSE;
568 		return (0);
569 	case ENOENT:
570 		*eof = B_TRUE;
571 		return (0);
572 	default:
573 		smbsr_errno(sr, rc);
574 		return (-1);
575 	}
576 }
577 
578 
579 /*
580  * smb_odir_read_streaminfo
581  *
582  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
583  * and thus represents an NTFS named stream.
584  * No search attribute matching is performed.
585  *
586  * Returns:
587  *  0 - success.
588  *      - If a matching entry was found eof will be B_FALSE and
589  *        sinfo will be populated.
590  *      - If there are no matching entries eof will be B_TRUE.
591  * -1 - error, error details set in sr.
592  */
593 int
594 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
595     smb_streaminfo_t *sinfo, boolean_t *eof)
596 {
597 	int		rc;
598 	smb_odirent_t	*odirent;
599 	vnode_t		*vp;
600 	smb_attr_t	attr;
601 
602 	ASSERT(sr);
603 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
604 	ASSERT(od);
605 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
606 	ASSERT(sinfo);
607 
608 	mutex_enter(&od->d_mutex);
609 
610 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
611 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
612 		mutex_exit(&od->d_mutex);
613 		return (-1);
614 	}
615 
616 	/* Check that odir represents an xattr directory */
617 	if (!od->d_xat) {
618 		*eof = B_TRUE;
619 		mutex_exit(&od->d_mutex);
620 		return (0);
621 	}
622 
623 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
624 
625 	for (;;) {
626 		bzero(sinfo, sizeof (smb_streaminfo_t));
627 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
628 			break;
629 
630 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
631 		    SMB_STREAM_PREFIX_LEN)) {
632 			continue;
633 		}
634 
635 		/*
636 		 * since we only care about the size attribute we don't need to
637 		 * pass the vp of the unnamed stream file to smb_vop_getattr
638 		 */
639 		rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp,
640 		    NULL, 0, od->d_tree->t_snode->vp, od->d_user->u_cred);
641 		if (rc == 0) {
642 			rc = smb_vop_getattr(vp, NULL, &attr, 0,
643 			    od->d_user->u_cred);
644 			VN_RELE(vp);
645 		}
646 
647 		if (rc == 0) {
648 			(void) strlcpy(sinfo->si_name,
649 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
650 			    sizeof (sinfo->si_name));
651 			sinfo->si_size = attr.sa_vattr.va_size;
652 			break;
653 		}
654 	}
655 	mutex_exit(&od->d_mutex);
656 
657 	kmem_free(odirent, sizeof (smb_odirent_t));
658 
659 	switch (rc) {
660 	case 0:
661 		*eof = B_FALSE;
662 		return (0);
663 	case ENOENT:
664 		*eof = B_TRUE;
665 		return (0);
666 	default:
667 		smbsr_errno(sr, rc);
668 		return (-1);
669 	}
670 }
671 
672 /*
673  * smb_odir_save_cookie
674  *
675  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
676  * to be used as resume points for a 'find next' request.
677  */
678 void
679 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
680 {
681 	ASSERT(od);
682 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
683 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
684 
685 	mutex_enter(&od->d_mutex);
686 	od->d_cookies[idx] = cookie;
687 	mutex_exit(&od->d_mutex);
688 }
689 
690 /*
691  * smb_odir_resume_at
692  *
693  * Searching can be resumed from:
694  * - the cookie saved at a specified index (SMBsearch, SMBfind).
695  * - a specified cookie (SMB_trans2_find)
696  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
697  *   Defaults to continuing from where the last search ended.
698  *
699  * Continuation from where the last search ended (SMB_trans2_find)
700  * is implemented by saving the last cookie at a specific index (0)
701  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
702  * and d_eof to force a vop_readdir.
703  */
704 void
705 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
706 {
707 	ASSERT(od);
708 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
709 	ASSERT(resume);
710 
711 	mutex_enter(&od->d_mutex);
712 
713 	switch (resume->or_type) {
714 		case SMB_ODIR_RESUME_IDX:
715 			ASSERT(resume->or_idx >= 0);
716 			ASSERT(resume->or_idx < SMB_MAX_SEARCH);
717 
718 			if ((resume->or_idx < 0) ||
719 			    (resume->or_idx >= SMB_MAX_SEARCH)) {
720 				resume->or_idx = 0;
721 			}
722 			od->d_offset = od->d_cookies[resume->or_idx];
723 			break;
724 		case SMB_ODIR_RESUME_COOKIE:
725 			od->d_offset = resume->or_cookie;
726 			break;
727 		case SMB_ODIR_RESUME_FNAME:
728 		default:
729 			od->d_offset = od->d_cookies[0];
730 			break;
731 	}
732 
733 	/* Force a vop_readdir to refresh d_buf */
734 	od->d_bufptr = NULL;
735 	od->d_eof = B_FALSE;
736 
737 	mutex_exit(&od->d_mutex);
738 }
739 
740 
741 /* *** static functions *** */
742 
743 /*
744  * smb_odir_create
745  * Allocate and populate an odir obect and add it to the tree's list.
746  */
747 static smb_odir_t *
748 smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
749     char *pattern, uint16_t sattr)
750 {
751 	smb_odir_t	*od;
752 	smb_tree_t	*tree;
753 	uint16_t	odid;
754 
755 	ASSERT(sr);
756 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
757 	ASSERT(sr->tid_tree);
758 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
759 	ASSERT(dnode);
760 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
761 
762 	tree = sr->tid_tree;
763 
764 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
765 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
766 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
767 		return (NULL);
768 	}
769 
770 	od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP);
771 	bzero(od, sizeof (smb_odir_t));
772 
773 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
774 	od->d_refcnt = 1;
775 	od->d_state = SMB_ODIR_STATE_OPEN;
776 	od->d_magic = SMB_ODIR_MAGIC;
777 	od->d_opened_by_pid = sr->smb_pid;
778 	od->d_session = tree->t_session;
779 	od->d_user = tree->t_user;
780 	od->d_tree = tree;
781 	od->d_dnode = dnode;
782 	smb_node_ref(dnode);
783 	od->d_odid = odid;
784 	od->d_sattr = sattr;
785 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
786 	od->d_wildcards = (smb_convert_wildcards(od->d_pattern) != 0);
787 	od->d_is_edp = vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS);
788 	od->d_ignore_case =
789 	    smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE);
790 	od->d_eof = B_FALSE;
791 
792 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
793 	smb_llist_insert_tail(&tree->t_odir_list, od);
794 	smb_llist_exit(&tree->t_odir_list);
795 
796 	atomic_inc_32(&tree->t_session->s_dir_cnt);
797 	return (od);
798 }
799 
800 /*
801  * smb_odir_delete
802  *
803  * Removal of the odir from the tree's list of odirs must be
804  * done before any resources associated with the odir are
805  * released.
806  */
807 static void
808 smb_odir_delete(smb_odir_t *od)
809 {
810 	ASSERT(od);
811 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
812 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
813 	ASSERT(od->d_refcnt == 0);
814 
815 	smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER);
816 	smb_llist_remove(&od->d_tree->t_odir_list, od);
817 	smb_llist_exit(&od->d_tree->t_odir_list);
818 
819 	od->d_magic = 0;
820 	atomic_dec_32(&od->d_tree->t_session->s_dir_cnt);
821 	smb_node_release(od->d_dnode);
822 	smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid);
823 	mutex_destroy(&od->d_mutex);
824 	kmem_cache_free(od->d_tree->t_server->si_cache_odir, od);
825 }
826 
827 /*
828  * smb_odir_next_odirent
829  *
830  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
831  * is empty or we've reached the end of it), read the next set of
832  * entries from the file system (vop_readdir).
833  *
834  * File systems which support VFSFT_EDIRENT_FLAGS will return the
835  * directory entries as a buffer of edirent_t structure. Others will
836  * return a buffer of dirent64_t structures.  For simplicity translate
837  * the data into an smb_odirent_t structure.
838  * The ed_name/d_name in d_buf is NULL terminated by the file system.
839  *
840  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
841  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT.
842  *
843  * Returns:
844  *      0 - success. odirent is populated with the next directory entry
845  * ENOENT - no more directory entries
846  *  errno - error
847  */
848 static int
849 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
850 {
851 	int		rc;
852 	int		reclen;
853 	int		eof;
854 	dirent64_t	*dp;
855 	edirent_t	*edp;
856 
857 	ASSERT(MUTEX_HELD(&od->d_mutex));
858 
859 	if (od->d_bufptr != NULL) {
860 		reclen = od->d_is_edp ?
861 		    od->d_edp->ed_reclen : od->d_dp->d_reclen;
862 
863 		if (reclen == 0) {
864 			od->d_bufptr = NULL;
865 		} else {
866 			od->d_bufptr += reclen;
867 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
868 				od->d_bufptr = NULL;
869 		}
870 	}
871 
872 	if (od->d_bufptr == NULL) {
873 		if (od->d_eof)
874 			return (ENOENT);
875 
876 		od->d_bufsize = sizeof (od->d_buf);
877 
878 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
879 		    od->d_buf, &od->d_bufsize, &eof, od->d_user->u_cred);
880 
881 		if ((rc == 0) && (od->d_bufsize == 0))
882 			rc = ENOENT;
883 
884 		if (rc != 0) {
885 			od->d_bufptr = NULL;
886 			od->d_bufsize = 0;
887 			return (rc);
888 		}
889 
890 		od->d_eof = (eof != 0);
891 		od->d_bufptr = od->d_buf;
892 	}
893 
894 	od->d_offset = (od->d_is_edp) ? od->d_edp->ed_off : od->d_dp->d_off;
895 	if (od->d_offset >= SMB_MAXDIRSIZE) {
896 		od->d_bufptr = NULL;
897 		od->d_bufsize = 0;
898 		return (ENOENT);
899 	}
900 
901 	if (od->d_is_edp) {
902 		edp = od->d_edp;
903 		odirent->od_ino = edp->ed_ino;
904 		odirent->od_eflags = edp->ed_eflags;
905 		(void) strlcpy(odirent->od_name, edp->ed_name,
906 		    sizeof (odirent->od_name));
907 	} else {
908 		dp = od->d_dp;
909 		odirent->od_ino = dp->d_ino;
910 		odirent->od_eflags = 0;
911 		(void) strlcpy(odirent->od_name, dp->d_name,
912 		    sizeof (odirent->od_name));
913 	}
914 
915 	return (0);
916 }
917 
918 /*
919  * smb_odir_single_fileinfo
920  *
921  * Lookup the file identified by od->d_pattern.
922  *
923  * If the looked up file is a link, we attempt to lookup the link target
924  * to use its attributes in place of those of the files's.
925  * If we fail to lookup the target of the link we use the original
926  * file's attributes.
927  * Check if the attributes match the search attributes.
928  *
929  * Returns: 0 - success
930  *     ENOENT - no match
931  *      errno - error
932  */
933 static int
934 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
935     smb_fileinfo_t *fileinfo)
936 {
937 	int		rc;
938 	smb_node_t	*fnode, *tgt_node;
939 	smb_attr_t	attr, tgt_attr, *fattr;
940 	ino64_t		ino;
941 	char		*name;
942 	uint32_t	dosattr;
943 
944 	ASSERT(sr);
945 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
946 	ASSERT(od);
947 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
948 
949 	ASSERT(MUTEX_HELD(&od->d_mutex));
950 	bzero(fileinfo, sizeof (smb_fileinfo_t));
951 
952 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, 0, od->d_tree->t_snode,
953 	    od->d_dnode, od->d_pattern, &fnode, &attr, 0, 0);
954 	if (rc != 0)
955 		return (rc);
956 
957 	name = fnode->od_name;
958 
959 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
960 	ino = attr.sa_vattr.va_nodeid;
961 	(void) smb_mangle_name(ino, name,
962 	    fileinfo->fi_shortname, fileinfo->fi_name83, 0);
963 
964 	/* follow link to get target node & attr */
965 	if ((fnode->vp->v_type == VLNK) &&
966 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
967 		smb_node_release(fnode);
968 		fnode = tgt_node;
969 		fattr = &tgt_attr;
970 	} else {
971 		fattr = &attr;
972 	}
973 
974 	/* check search attributes */
975 	dosattr = smb_node_get_dosattr(fnode);
976 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
977 		smb_node_release(fnode);
978 		return (ENOENT);
979 	}
980 
981 	fileinfo->fi_dosattr = dosattr;
982 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
983 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
984 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
985 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
986 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
987 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
988 	if (fattr->sa_crtime.tv_sec)
989 		fileinfo->fi_crtime = fattr->sa_crtime;
990 	else
991 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
992 
993 	smb_node_release(fnode);
994 	return (0);
995 }
996 
997 /*
998  * smb_odir_wildcard_fileinfo
999  *
1000  * odirent contains a directory entry, obtained from a vop_readdir.
1001  * If a case conflict is identified the filename is mangled and the
1002  * shortname is used as 'name', in place of odirent->od_name. This
1003  * name will be used in the smb_fsop_lookup because smb_fsop_lookup
1004  * performs a case insensitive lookup if the tree is case insesitive,
1005  * so the mangled name is required in the case conflict scenario to
1006  * ensure the correct match.
1007  *
1008  * If the looked up file is a link, we attempt to lookup the link target
1009  * to use its attributes in place of those of the files's.
1010  * If we fail to lookup the target of the link we use the original
1011  * file's attributes.
1012  * Check if the attributes match the search attributes.
1013  *
1014  * Although some file systems can have directories larger than
1015  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
1016  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
1017  * offset as the cookie (uint32_t).
1018  *
1019  * Returns: 0 - success
1020  *     ENOENT - no match, proceed to next entry
1021  *      errno - error
1022  */
1023 static int
1024 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
1025     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
1026 {
1027 	int		rc;
1028 	smb_node_t	*fnode, *tgt_node;
1029 	smb_attr_t	attr, tgt_attr, *fattr;
1030 	char		*name;
1031 	uint32_t	dosattr;
1032 	boolean_t	case_conflict;
1033 
1034 	ASSERT(sr);
1035 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1036 	ASSERT(od);
1037 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1038 
1039 	ASSERT(MUTEX_HELD(&od->d_mutex));
1040 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1041 
1042 	case_conflict = ((od->d_ignore_case) &&
1043 	    (odirent->od_eflags & ED_CASE_CONFLICT));
1044 	(void) smb_mangle_name(odirent->od_ino, odirent->od_name,
1045 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
1046 	name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name;
1047 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1048 
1049 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, 0, od->d_tree->t_snode,
1050 	    od->d_dnode, name, &fnode, &attr, 0, 0);
1051 	if (rc != 0)
1052 		return (rc);
1053 
1054 	/* follow link to get target node & attr */
1055 	if ((fnode->vp->v_type == VLNK) &&
1056 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
1057 		smb_node_release(fnode);
1058 		fnode = tgt_node;
1059 		fattr = &tgt_attr;
1060 	} else {
1061 		fattr = &attr;
1062 	}
1063 
1064 	/* check search attributes */
1065 	dosattr = smb_node_get_dosattr(fnode);
1066 	if (!smb_sattr_check(dosattr, od->d_sattr)) {
1067 		smb_node_release(fnode);
1068 		return (ENOENT);
1069 	}
1070 
1071 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1072 	fileinfo->fi_dosattr = dosattr;
1073 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
1074 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
1075 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
1076 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
1077 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
1078 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
1079 	if (fattr->sa_crtime.tv_sec)
1080 		fileinfo->fi_crtime = fattr->sa_crtime;
1081 	else
1082 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
1083 
1084 	smb_node_release(fnode);
1085 	return (0);
1086 }
1087 
1088 /*
1089  * smb_odir_lookup_link
1090  *
1091  * If the file is a symlink we lookup the object to which the
1092  * symlink refers so that we can return its attributes.
1093  * This can cause a problem if a symlink in a sub-directory
1094  * points to a parent directory (some UNIX GUI's create a symlink
1095  * in $HOME/.desktop that points to the user's home directory).
1096  * Some Windows applications (e.g. virus scanning) loop/hang
1097  * trying to follow this recursive path and there is little
1098  * we can do because the path is constructed on the client.
1099  * smb_dirsymlink_enable allows an end-user to disable
1100  * symlinks to directories. Symlinks to other object types
1101  * should be unaffected.
1102  *
1103  * Returns:  B_TRUE - followed link. tgt_node and tgt_attr set
1104  *          B_FALSE - link not followed
1105  */
1106 static boolean_t
1107 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1108     char *fname, smb_node_t **tgt_node, smb_attr_t *tgt_attr)
1109 {
1110 	int rc;
1111 
1112 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, SMB_FOLLOW_LINKS,
1113 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node, tgt_attr, 0, 0);
1114 	if (rc != 0) {
1115 		*tgt_node = NULL;
1116 		return (B_FALSE);
1117 	}
1118 
1119 	if ((tgt_attr->sa_vattr.va_type == VDIR) && (!smb_dirsymlink_enable)) {
1120 		smb_node_release(*tgt_node);
1121 		*tgt_node = NULL;
1122 		return (B_FALSE);
1123 	}
1124 
1125 	return (B_TRUE);
1126 }
1127