xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_odir.c (revision 8159a906dbf7d5466ef599875f165fe944ed07e7)
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 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
324 		    ERRDOS, ERROR_ACCESS_DENIED);
325 		return (0);
326 	}
327 	cr = sr->user_cr;
328 
329 	/* find the xattrdir vnode */
330 	rc = smb_vop_lookup_xattrdir(unode->vp, &xattr_dvp, LOOKUP_XATTR, cr);
331 	if (rc != 0) {
332 		smbsr_errno(sr, rc);
333 		return (0);
334 	}
335 
336 	/* lookup the xattrdir's smb_node */
337 	xattr_dnode = smb_node_lookup(sr, NULL, cr, xattr_dvp, XATTR_DIR,
338 	    unode, NULL, &tmp_attr);
339 	VN_RELE(xattr_dvp);
340 	if (xattr_dnode == NULL) {
341 		smbsr_error(sr, NT_STATUS_NO_MEMORY,
342 		    ERRDOS, ERROR_NOT_ENOUGH_MEMORY);
343 		return (0);
344 	}
345 
346 	(void) snprintf(pattern, sizeof (pattern), "%s*", SMB_STREAM_PREFIX);
347 	od = smb_odir_create(sr, xattr_dnode, pattern, SMB_SEARCH_ATTRIBUTES);
348 	smb_node_release(xattr_dnode);
349 	if (od == NULL)
350 		return (0);
351 
352 	od->d_xat = B_TRUE;
353 	return (od->d_odid);
354 }
355 
356 /*
357  * smb_odir_hold
358  */
359 boolean_t
360 smb_odir_hold(smb_odir_t *od)
361 {
362 	ASSERT(od);
363 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
364 
365 	mutex_enter(&od->d_mutex);
366 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
367 		mutex_exit(&od->d_mutex);
368 		return (B_FALSE);
369 	}
370 
371 	od->d_refcnt++;
372 	mutex_exit(&od->d_mutex);
373 	return (B_TRUE);
374 }
375 
376 /*
377  * smb_odir_release
378  *
379  * If the odir is in SMB_ODIR_STATE_CLOSING and this release
380  * results in a refcnt of 0, the odir may be removed from
381  * the tree's list of odirs and deleted.  The odir's state is
382  * set to SMB_ODIR_STATE_CLOSED prior to exiting the mutex and
383  * deleting it. This ensure that nobody else can ontain a reference
384  * to it while we are deleting it.
385  */
386 void
387 smb_odir_release(smb_odir_t *od)
388 {
389 	ASSERT(od);
390 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
391 
392 	mutex_enter(&od->d_mutex);
393 	ASSERT(od->d_refcnt);
394 
395 	switch (od->d_state) {
396 	case SMB_ODIR_STATE_OPEN:
397 		od->d_refcnt--;
398 		break;
399 	case SMB_ODIR_STATE_CLOSING:
400 		od->d_refcnt--;
401 		if (od->d_refcnt == 0) {
402 			od->d_state = SMB_ODIR_STATE_CLOSED;
403 			mutex_exit(&od->d_mutex);
404 			smb_odir_delete(od);
405 			return;
406 		}
407 		break;
408 	case SMB_ODIR_STATE_CLOSED:
409 		break;
410 	default:
411 		ASSERT(0);
412 		break;
413 	}
414 
415 	mutex_exit(&od->d_mutex);
416 }
417 
418 /*
419  * smb_odir_close
420  */
421 void
422 smb_odir_close(smb_odir_t *od)
423 {
424 	ASSERT(od);
425 	ASSERT(od->d_refcnt);
426 
427 	mutex_enter(&od->d_mutex);
428 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
429 		mutex_exit(&od->d_mutex);
430 		return;
431 	}
432 	od->d_state = SMB_ODIR_STATE_CLOSING;
433 	mutex_exit(&od->d_mutex);
434 
435 	smb_odir_release(od);
436 }
437 
438 /*
439  * smb_odir_read
440  *
441  * Find the next directory entry matching the search pattern.
442  * No search attribute matching is performed.
443  *
444  * Returns:
445  *  0 - success.
446  *      - If a matching entry was found eof will be B_FALSE and
447  *        odirent will be populated.
448  *      - If there are no matching entries eof will be B_TRUE.
449  * -1 - error, error details set in sr.
450  */
451 int
452 smb_odir_read(smb_request_t *sr, smb_odir_t *od,
453     smb_odirent_t *odirent, boolean_t *eof)
454 {
455 	int rc;
456 
457 	ASSERT(sr);
458 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
459 	ASSERT(od);
460 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
461 	ASSERT(odirent);
462 
463 	mutex_enter(&od->d_mutex);
464 
465 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
466 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
467 		mutex_exit(&od->d_mutex);
468 		return (-1);
469 	}
470 
471 	for (;;) {
472 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
473 			break;
474 		if (smb_match_name(odirent->od_ino, odirent->od_name,
475 		    od->d_pattern, od->d_ignore_case))
476 			break;
477 	}
478 
479 	mutex_exit(&od->d_mutex);
480 
481 	switch (rc) {
482 	case 0:
483 		*eof = B_FALSE;
484 		return (0);
485 	case ENOENT:
486 		*eof = B_TRUE;
487 		return (0);
488 	default:
489 		smbsr_errno(sr, rc);
490 		return (-1);
491 	}
492 }
493 
494 /*
495  * smb_odir_read_fileinfo
496  *
497  * Find the next directory entry matching the search pattern
498  * and attributes: od->d_pattern and od->d_sattr.
499  *
500  * If the search pattern specifies a single filename call
501  * smb_odir_single_fileinfo to get the file attributes and
502  * populate the caller's smb_fileinfo_t.
503  *
504  * If the search pattern contains wildcards call smb_odir_next_odirent
505  * to get the next directory entry then. Repeat until a matching
506  * filename is found. Call smb_odir_wildcard_fileinfo to get the
507  * file attributes and populate the caller's smb_fileinfo_t.
508  * This is repeated until a file matching the search criteria is found.
509  *
510  * Returns:
511  *  0 - success.
512  *      - If a matching entry was found eof will be B_FALSE and
513  *        fileinfo will be populated.
514  *      - If there are no matching entries eof will be B_TRUE.
515  * -1 - error, error details set in sr.
516  */
517 int
518 smb_odir_read_fileinfo(smb_request_t *sr, smb_odir_t *od,
519     smb_fileinfo_t *fileinfo, boolean_t *eof)
520 {
521 	int		rc;
522 	smb_odirent_t	*odirent;
523 
524 	ASSERT(sr);
525 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
526 	ASSERT(od);
527 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
528 	ASSERT(fileinfo);
529 
530 	mutex_enter(&od->d_mutex);
531 
532 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
533 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
534 		mutex_exit(&od->d_mutex);
535 		return (-1);
536 	}
537 
538 	if (!od->d_wildcards) {
539 		if (od->d_eof)
540 			rc = ENOENT;
541 		else
542 			rc = smb_odir_single_fileinfo(sr, od, fileinfo);
543 		od->d_eof = B_TRUE;
544 	} else {
545 		odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
546 		for (;;) {
547 			bzero(fileinfo, sizeof (smb_fileinfo_t));
548 			if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
549 				break;
550 
551 			if (!smb_match_name(odirent->od_ino, odirent->od_name,
552 			    od->d_pattern, od->d_ignore_case))
553 				continue;
554 
555 			rc = smb_odir_wildcard_fileinfo(sr, od, odirent,
556 			    fileinfo);
557 			if (rc == 0)
558 				break;
559 		}
560 		kmem_free(odirent, sizeof (smb_odirent_t));
561 	}
562 	mutex_exit(&od->d_mutex);
563 
564 	switch (rc) {
565 	case 0:
566 		*eof = B_FALSE;
567 		return (0);
568 	case ENOENT:
569 		*eof = B_TRUE;
570 		return (0);
571 	default:
572 		smbsr_errno(sr, rc);
573 		return (-1);
574 	}
575 }
576 
577 
578 /*
579  * smb_odir_read_streaminfo
580  *
581  * Find the next directory entry whose name begins with SMB_STREAM_PREFIX,
582  * and thus represents an NTFS named stream.
583  * No search attribute matching is performed.
584  *
585  * Returns:
586  *  0 - success.
587  *      - If a matching entry was found eof will be B_FALSE and
588  *        sinfo will be populated.
589  *      - If there are no matching entries eof will be B_TRUE.
590  * -1 - error, error details set in sr.
591  */
592 int
593 smb_odir_read_streaminfo(smb_request_t *sr, smb_odir_t *od,
594     smb_streaminfo_t *sinfo, boolean_t *eof)
595 {
596 	int		rc;
597 	smb_odirent_t	*odirent;
598 	vnode_t		*vp;
599 	smb_attr_t	attr;
600 
601 	ASSERT(sr);
602 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
603 	ASSERT(od);
604 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
605 	ASSERT(sinfo);
606 
607 	mutex_enter(&od->d_mutex);
608 
609 	ASSERT(od->d_state == SMB_ODIR_STATE_OPEN);
610 	if (od->d_state != SMB_ODIR_STATE_OPEN) {
611 		mutex_exit(&od->d_mutex);
612 		return (-1);
613 	}
614 
615 	/* Check that odir represents an xattr directory */
616 	if (!od->d_xat) {
617 		*eof = B_TRUE;
618 		mutex_exit(&od->d_mutex);
619 		return (0);
620 	}
621 
622 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
623 
624 	for (;;) {
625 		bzero(sinfo, sizeof (smb_streaminfo_t));
626 		if ((rc = smb_odir_next_odirent(od, odirent)) != 0)
627 			break;
628 
629 		if (strncmp(odirent->od_name, SMB_STREAM_PREFIX,
630 		    SMB_STREAM_PREFIX_LEN)) {
631 			continue;
632 		}
633 
634 		/*
635 		 * since we only care about the size attribute we don't need to
636 		 * pass the vp of the unnamed stream file to smb_vop_getattr
637 		 */
638 		rc = smb_vop_lookup(od->d_dnode->vp, odirent->od_name, &vp,
639 		    NULL, 0, od->d_tree->t_snode->vp, od->d_user->u_cred);
640 		if (rc == 0) {
641 			rc = smb_vop_getattr(vp, NULL, &attr, 0,
642 			    od->d_user->u_cred);
643 			VN_RELE(vp);
644 		}
645 
646 		if (rc == 0) {
647 			(void) strlcpy(sinfo->si_name,
648 			    odirent->od_name + SMB_STREAM_PREFIX_LEN,
649 			    sizeof (sinfo->si_name));
650 			sinfo->si_size = attr.sa_vattr.va_size;
651 			break;
652 		}
653 	}
654 	mutex_exit(&od->d_mutex);
655 
656 	kmem_free(odirent, sizeof (smb_odirent_t));
657 
658 	switch (rc) {
659 	case 0:
660 		*eof = B_FALSE;
661 		return (0);
662 	case ENOENT:
663 		*eof = B_TRUE;
664 		return (0);
665 	default:
666 		smbsr_errno(sr, rc);
667 		return (-1);
668 	}
669 }
670 
671 /*
672  * smb_odir_save_cookie
673  *
674  * Callers can save up to SMB_MAX_SEARCH cookies in the odir
675  * to be used as resume points for a 'find next' request.
676  */
677 void
678 smb_odir_save_cookie(smb_odir_t *od, int idx, uint32_t cookie)
679 {
680 	ASSERT(od);
681 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
682 	ASSERT(idx >= 0 && idx < SMB_MAX_SEARCH);
683 
684 	mutex_enter(&od->d_mutex);
685 	od->d_cookies[idx] = cookie;
686 	mutex_exit(&od->d_mutex);
687 }
688 
689 /*
690  * smb_odir_resume_at
691  *
692  * Searching can be resumed from:
693  * - the cookie saved at a specified index (SMBsearch, SMBfind).
694  * - a specified cookie (SMB_trans2_find)
695  * - a specified filename (SMB_trans2_find) - NOT SUPPORTED.
696  *   Defaults to continuing from where the last search ended.
697  *
698  * Continuation from where the last search ended (SMB_trans2_find)
699  * is implemented by saving the last cookie at a specific index (0)
700  * smb_odir_resume_at indicates a new request, so reset od->d_bufptr
701  * and d_eof to force a vop_readdir.
702  */
703 void
704 smb_odir_resume_at(smb_odir_t *od, smb_odir_resume_t *resume)
705 {
706 	ASSERT(od);
707 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
708 	ASSERT(resume);
709 
710 	mutex_enter(&od->d_mutex);
711 
712 	switch (resume->or_type) {
713 		case SMB_ODIR_RESUME_IDX:
714 			ASSERT(resume->or_idx >= 0);
715 			ASSERT(resume->or_idx < SMB_MAX_SEARCH);
716 
717 			if ((resume->or_idx < 0) ||
718 			    (resume->or_idx >= SMB_MAX_SEARCH)) {
719 				resume->or_idx = 0;
720 			}
721 			od->d_offset = od->d_cookies[resume->or_idx];
722 			break;
723 		case SMB_ODIR_RESUME_COOKIE:
724 			od->d_offset = resume->or_cookie;
725 			break;
726 		case SMB_ODIR_RESUME_FNAME:
727 		default:
728 			od->d_offset = od->d_cookies[0];
729 			break;
730 	}
731 
732 	/* Force a vop_readdir to refresh d_buf */
733 	od->d_bufptr = NULL;
734 	od->d_eof = B_FALSE;
735 
736 	mutex_exit(&od->d_mutex);
737 }
738 
739 
740 /* *** static functions *** */
741 
742 /*
743  * smb_odir_create
744  * Allocate and populate an odir obect and add it to the tree's list.
745  */
746 static smb_odir_t *
747 smb_odir_create(smb_request_t *sr, smb_node_t *dnode,
748     char *pattern, uint16_t sattr)
749 {
750 	smb_odir_t	*od;
751 	smb_tree_t	*tree;
752 	uint16_t	odid;
753 
754 	ASSERT(sr);
755 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
756 	ASSERT(sr->tid_tree);
757 	ASSERT(sr->tid_tree->t_magic == SMB_TREE_MAGIC);
758 	ASSERT(dnode);
759 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
760 
761 	tree = sr->tid_tree;
762 
763 	if (smb_idpool_alloc(&tree->t_odid_pool, &odid)) {
764 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
765 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
766 		return (NULL);
767 	}
768 
769 	od = kmem_cache_alloc(tree->t_server->si_cache_odir, KM_SLEEP);
770 	bzero(od, sizeof (smb_odir_t));
771 
772 	mutex_init(&od->d_mutex, NULL, MUTEX_DEFAULT, NULL);
773 	od->d_refcnt = 1;
774 	od->d_state = SMB_ODIR_STATE_OPEN;
775 	od->d_magic = SMB_ODIR_MAGIC;
776 	od->d_opened_by_pid = sr->smb_pid;
777 	od->d_session = tree->t_session;
778 	od->d_user = tree->t_user;
779 	od->d_tree = tree;
780 	od->d_dnode = dnode;
781 	smb_node_ref(dnode);
782 	od->d_odid = odid;
783 	od->d_sattr = sattr;
784 	(void) strlcpy(od->d_pattern, pattern, sizeof (od->d_pattern));
785 	od->d_wildcards = (smb_convert_unicode_wildcards(od->d_pattern) != 0);
786 	od->d_is_edp = vfs_has_feature(dnode->vp->v_vfsp, VFSFT_DIRENTFLAGS);
787 	od->d_ignore_case =
788 	    smb_tree_has_feature(tree, SMB_TREE_CASEINSENSITIVE);
789 	od->d_eof = B_FALSE;
790 
791 	smb_llist_enter(&tree->t_odir_list, RW_WRITER);
792 	smb_llist_insert_tail(&tree->t_odir_list, od);
793 	smb_llist_exit(&tree->t_odir_list);
794 
795 	atomic_inc_32(&tree->t_session->s_dir_cnt);
796 	return (od);
797 }
798 
799 /*
800  * smb_odir_delete
801  *
802  * Removal of the odir from the tree's list of odirs must be
803  * done before any resources associated with the odir are
804  * released.
805  */
806 static void
807 smb_odir_delete(smb_odir_t *od)
808 {
809 	ASSERT(od);
810 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
811 	ASSERT(od->d_state == SMB_ODIR_STATE_CLOSED);
812 	ASSERT(od->d_refcnt == 0);
813 
814 	smb_llist_enter(&od->d_tree->t_odir_list, RW_WRITER);
815 	smb_llist_remove(&od->d_tree->t_odir_list, od);
816 	smb_llist_exit(&od->d_tree->t_odir_list);
817 
818 	od->d_magic = 0;
819 	atomic_dec_32(&od->d_tree->t_session->s_dir_cnt);
820 	smb_node_release(od->d_dnode);
821 	smb_idpool_free(&od->d_tree->t_odid_pool, od->d_odid);
822 	mutex_destroy(&od->d_mutex);
823 	kmem_cache_free(od->d_tree->t_server->si_cache_odir, od);
824 }
825 
826 /*
827  * smb_odir_next_odirent
828  *
829  * Find the next directory entry in d_buf. If d_bufptr is NULL (buffer
830  * is empty or we've reached the end of it), read the next set of
831  * entries from the file system (vop_readdir).
832  *
833  * File systems which support VFSFT_EDIRENT_FLAGS will return the
834  * directory entries as a buffer of edirent_t structure. Others will
835  * return a buffer of dirent64_t structures.  For simplicity translate
836  * the data into an smb_odirent_t structure.
837  * The ed_name/d_name in d_buf is NULL terminated by the file system.
838  *
839  * Some file systems can have directories larger than SMB_MAXDIRSIZE.
840  * If the odirent offset >= SMB_MAXDIRSIZE return ENOENT.
841  *
842  * Returns:
843  *      0 - success. odirent is populated with the next directory entry
844  * ENOENT - no more directory entries
845  *  errno - error
846  */
847 static int
848 smb_odir_next_odirent(smb_odir_t *od, smb_odirent_t *odirent)
849 {
850 	int		rc;
851 	int		reclen;
852 	int		eof;
853 	dirent64_t	*dp;
854 	edirent_t	*edp;
855 
856 	ASSERT(MUTEX_HELD(&od->d_mutex));
857 
858 	if (od->d_bufptr != NULL) {
859 		reclen = od->d_is_edp ?
860 		    od->d_edp->ed_reclen : od->d_dp->d_reclen;
861 
862 		if (reclen == 0) {
863 			od->d_bufptr = NULL;
864 		} else {
865 			od->d_bufptr += reclen;
866 			if (od->d_bufptr >= od->d_buf + od->d_bufsize)
867 				od->d_bufptr = NULL;
868 		}
869 	}
870 
871 	if (od->d_bufptr == NULL) {
872 		if (od->d_eof)
873 			return (ENOENT);
874 
875 		od->d_bufsize = sizeof (od->d_buf);
876 
877 		rc = smb_vop_readdir(od->d_dnode->vp, od->d_offset,
878 		    od->d_buf, &od->d_bufsize, &eof, od->d_user->u_cred);
879 
880 		if ((rc == 0) && (od->d_bufsize == 0))
881 			rc = ENOENT;
882 
883 		if (rc != 0) {
884 			od->d_bufptr = NULL;
885 			od->d_bufsize = 0;
886 			return (rc);
887 		}
888 
889 		od->d_eof = (eof != 0);
890 		od->d_bufptr = od->d_buf;
891 	}
892 
893 	od->d_offset = (od->d_is_edp) ? od->d_edp->ed_off : od->d_dp->d_off;
894 	if (od->d_offset >= SMB_MAXDIRSIZE) {
895 		od->d_bufptr = NULL;
896 		od->d_bufsize = 0;
897 		return (ENOENT);
898 	}
899 
900 	if (od->d_is_edp) {
901 		edp = od->d_edp;
902 		odirent->od_ino = edp->ed_ino;
903 		odirent->od_eflags = edp->ed_eflags;
904 		(void) strlcpy(odirent->od_name, edp->ed_name,
905 		    sizeof (odirent->od_name));
906 	} else {
907 		dp = od->d_dp;
908 		odirent->od_ino = dp->d_ino;
909 		odirent->od_eflags = 0;
910 		(void) strlcpy(odirent->od_name, dp->d_name,
911 		    sizeof (odirent->od_name));
912 	}
913 
914 	return (0);
915 }
916 
917 /*
918  * smb_odir_single_fileinfo
919  *
920  * Lookup the file identified by od->d_pattern.
921  *
922  * If the looked up file is a link, we attempt to lookup the link target
923  * to use its attributes in place of those of the files's.
924  * If we fail to lookup the target of the link we use the original
925  * file's attributes.
926  * Check if the attributes match the search attributes.
927  *
928  * Returns: 0 - success
929  *     ENOENT - no match
930  *      errno - error
931  */
932 static int
933 smb_odir_single_fileinfo(smb_request_t *sr, smb_odir_t *od,
934     smb_fileinfo_t *fileinfo)
935 {
936 	int		rc;
937 	smb_node_t	*fnode, *tgt_node;
938 	smb_attr_t	attr, tgt_attr, *fattr;
939 	ino64_t		ino;
940 	char		*name;
941 	uint32_t	dosattr;
942 
943 	ASSERT(sr);
944 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
945 	ASSERT(od);
946 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
947 
948 	ASSERT(MUTEX_HELD(&od->d_mutex));
949 	bzero(fileinfo, sizeof (smb_fileinfo_t));
950 
951 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, 0, od->d_tree->t_snode,
952 	    od->d_dnode, od->d_pattern, &fnode, &attr, 0, 0);
953 	if (rc != 0)
954 		return (rc);
955 
956 	name = fnode->od_name;
957 
958 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
959 	ino = attr.sa_vattr.va_nodeid;
960 	(void) smb_mangle_name(ino, name,
961 	    fileinfo->fi_shortname, fileinfo->fi_name83, 0);
962 
963 	/* follow link to get target node & attr */
964 	if ((fnode->vp->v_type == VLNK) &&
965 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
966 		smb_node_release(fnode);
967 		fnode = tgt_node;
968 		fattr = &tgt_attr;
969 	} else {
970 		fattr = &attr;
971 	}
972 
973 	/* check search attributes */
974 	dosattr = smb_node_get_dosattr(fnode);
975 	if (!smb_sattr_check(dosattr, od->d_sattr, fileinfo->fi_name)) {
976 		smb_node_release(fnode);
977 		return (ENOENT);
978 	}
979 
980 	fileinfo->fi_dosattr = dosattr;
981 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
982 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
983 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
984 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
985 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
986 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
987 	if (fattr->sa_crtime.tv_sec)
988 		fileinfo->fi_crtime = fattr->sa_crtime;
989 	else
990 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
991 
992 	smb_node_release(fnode);
993 	return (0);
994 }
995 
996 /*
997  * smb_odir_wildcard_fileinfo
998  *
999  * odirent contains a directory entry, obtained from a vop_readdir.
1000  * If a case conflict is identified the filename is mangled and the
1001  * shortname is used as 'name', in place of odirent->od_name. This
1002  * name will be used in the smb_fsop_lookup because smb_fsop_lookup
1003  * performs a case insensitive lookup if the tree is case insesitive,
1004  * so the mangled name is required in the case conflict scenario to
1005  * ensure the correct match.
1006  *
1007  * If the looked up file is a link, we attempt to lookup the link target
1008  * to use its attributes in place of those of the files's.
1009  * If we fail to lookup the target of the link we use the original
1010  * file's attributes.
1011  * Check if the attributes match the search attributes.
1012  *
1013  * Although some file systems can have directories larger than
1014  * SMB_MAXDIRSIZE smb_odir_next_odirent ensures that no offset larger
1015  * than SMB_MAXDIRSIZE is returned.  It is therefore safe to use the
1016  * offset as the cookie (uint32_t).
1017  *
1018  * Returns: 0 - success
1019  *     ENOENT - no match, proceed to next entry
1020  *      errno - error
1021  */
1022 static int
1023 smb_odir_wildcard_fileinfo(smb_request_t *sr, smb_odir_t *od,
1024     smb_odirent_t *odirent, smb_fileinfo_t *fileinfo)
1025 {
1026 	int		rc;
1027 	smb_node_t	*fnode, *tgt_node;
1028 	smb_attr_t	attr, tgt_attr, *fattr;
1029 	char		*name;
1030 	uint32_t	dosattr;
1031 	boolean_t	case_conflict;
1032 
1033 	ASSERT(sr);
1034 	ASSERT(sr->sr_magic == SMB_REQ_MAGIC);
1035 	ASSERT(od);
1036 	ASSERT(od->d_magic == SMB_ODIR_MAGIC);
1037 
1038 	ASSERT(MUTEX_HELD(&od->d_mutex));
1039 	bzero(fileinfo, sizeof (smb_fileinfo_t));
1040 
1041 	case_conflict = ((od->d_ignore_case) &&
1042 	    (odirent->od_eflags & ED_CASE_CONFLICT));
1043 	(void) smb_mangle_name(odirent->od_ino, odirent->od_name,
1044 	    fileinfo->fi_shortname, fileinfo->fi_name83, case_conflict);
1045 	name = (case_conflict) ? fileinfo->fi_shortname : odirent->od_name;
1046 	(void) strlcpy(fileinfo->fi_name, name, sizeof (fileinfo->fi_name));
1047 
1048 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, 0, od->d_tree->t_snode,
1049 	    od->d_dnode, name, &fnode, &attr, 0, 0);
1050 	if (rc != 0)
1051 		return (rc);
1052 
1053 	/* follow link to get target node & attr */
1054 	if ((fnode->vp->v_type == VLNK) &&
1055 	    (smb_odir_lookup_link(sr, od, name, &tgt_node, &tgt_attr))) {
1056 		smb_node_release(fnode);
1057 		fnode = tgt_node;
1058 		fattr = &tgt_attr;
1059 	} else {
1060 		fattr = &attr;
1061 	}
1062 
1063 	/* check search attributes */
1064 	dosattr = smb_node_get_dosattr(fnode);
1065 	if (!smb_sattr_check(dosattr, od->d_sattr, fileinfo->fi_name)) {
1066 		smb_node_release(fnode);
1067 		return (ENOENT);
1068 	}
1069 
1070 	fileinfo->fi_cookie = (uint32_t)od->d_offset;
1071 	fileinfo->fi_dosattr = dosattr;
1072 	fileinfo->fi_nodeid = fattr->sa_vattr.va_nodeid;
1073 	fileinfo->fi_size = smb_node_get_size(fnode, fattr);
1074 	fileinfo->fi_alloc_size = fattr->sa_vattr.va_nblocks * DEV_BSIZE;
1075 	fileinfo->fi_atime = fattr->sa_vattr.va_atime;
1076 	fileinfo->fi_mtime = fattr->sa_vattr.va_mtime;
1077 	fileinfo->fi_ctime = fattr->sa_vattr.va_ctime;
1078 	if (fattr->sa_crtime.tv_sec)
1079 		fileinfo->fi_crtime = fattr->sa_crtime;
1080 	else
1081 		fileinfo->fi_crtime = fattr->sa_vattr.va_mtime;
1082 
1083 	smb_node_release(fnode);
1084 	return (0);
1085 }
1086 
1087 /*
1088  * smb_odir_lookup_link
1089  *
1090  * If the file is a symlink we lookup the object to which the
1091  * symlink refers so that we can return its attributes.
1092  * This can cause a problem if a symlink in a sub-directory
1093  * points to a parent directory (some UNIX GUI's create a symlink
1094  * in $HOME/.desktop that points to the user's home directory).
1095  * Some Windows applications (e.g. virus scanning) loop/hang
1096  * trying to follow this recursive path and there is little
1097  * we can do because the path is constructed on the client.
1098  * smb_dirsymlink_enable allows an end-user to disable
1099  * symlinks to directories. Symlinks to other object types
1100  * should be unaffected.
1101  *
1102  * Returns:  B_TRUE - followed link. tgt_node and tgt_attr set
1103  *          B_FALSE - link not followed
1104  */
1105 static boolean_t
1106 smb_odir_lookup_link(smb_request_t *sr, smb_odir_t *od,
1107     char *fname, smb_node_t **tgt_node, smb_attr_t *tgt_attr)
1108 {
1109 	int rc;
1110 
1111 	rc = smb_fsop_lookup(sr, od->d_user->u_cred, SMB_FOLLOW_LINKS,
1112 	    od->d_tree->t_snode, od->d_dnode, fname, tgt_node, tgt_attr, 0, 0);
1113 	if (rc != 0) {
1114 		*tgt_node = NULL;
1115 		return (B_FALSE);
1116 	}
1117 
1118 	if ((tgt_attr->sa_vattr.va_type == VDIR) && (!smb_dirsymlink_enable)) {
1119 		smb_node_release(*tgt_node);
1120 		*tgt_node = NULL;
1121 		return (B_FALSE);
1122 	}
1123 
1124 	return (B_TRUE);
1125 }
1126