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