xref: /linux/fs/smb/client/cached_dir.c (revision e91c37f1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Functions to handle the cached directory entries
4  *
5  *  Copyright (c) 2022, Ronnie Sahlberg <lsahlber@redhat.com>
6  */
7 
8 #include <linux/namei.h>
9 #include "cifsglob.h"
10 #include "cifsproto.h"
11 #include "cifs_debug.h"
12 #include "smb2proto.h"
13 #include "cached_dir.h"
14 
15 static struct cached_fid *init_cached_dir(const char *path);
16 static void free_cached_dir(struct cached_fid *cfid);
17 static void smb2_close_cached_fid(struct kref *ref);
18 static void cfids_laundromat_worker(struct work_struct *work);
19 
20 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
21 						    const char *path,
22 						    bool lookup_only,
23 						    __u32 max_cached_dirs)
24 {
25 	struct cached_fid *cfid;
26 
27 	spin_lock(&cfids->cfid_list_lock);
28 	list_for_each_entry(cfid, &cfids->entries, entry) {
29 		if (!strcmp(cfid->path, path)) {
30 			/*
31 			 * If it doesn't have a lease it is either not yet
32 			 * fully cached or it may be in the process of
33 			 * being deleted due to a lease break.
34 			 */
35 			if (!cfid->time || !cfid->has_lease) {
36 				spin_unlock(&cfids->cfid_list_lock);
37 				return NULL;
38 			}
39 			kref_get(&cfid->refcount);
40 			spin_unlock(&cfids->cfid_list_lock);
41 			return cfid;
42 		}
43 	}
44 	if (lookup_only) {
45 		spin_unlock(&cfids->cfid_list_lock);
46 		return NULL;
47 	}
48 	if (cfids->num_entries >= max_cached_dirs) {
49 		spin_unlock(&cfids->cfid_list_lock);
50 		return NULL;
51 	}
52 	cfid = init_cached_dir(path);
53 	if (cfid == NULL) {
54 		spin_unlock(&cfids->cfid_list_lock);
55 		return NULL;
56 	}
57 	cfid->cfids = cfids;
58 	cfids->num_entries++;
59 	list_add(&cfid->entry, &cfids->entries);
60 	cfid->on_list = true;
61 	kref_get(&cfid->refcount);
62 	spin_unlock(&cfids->cfid_list_lock);
63 	return cfid;
64 }
65 
66 static struct dentry *
67 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
68 {
69 	struct dentry *dentry;
70 	const char *s, *p;
71 	char sep;
72 
73 	sep = CIFS_DIR_SEP(cifs_sb);
74 	dentry = dget(cifs_sb->root);
75 	s = path;
76 
77 	do {
78 		struct inode *dir = d_inode(dentry);
79 		struct dentry *child;
80 
81 		if (!S_ISDIR(dir->i_mode)) {
82 			dput(dentry);
83 			dentry = ERR_PTR(-ENOTDIR);
84 			break;
85 		}
86 
87 		/* skip separators */
88 		while (*s == sep)
89 			s++;
90 		if (!*s)
91 			break;
92 		p = s++;
93 		/* next separator */
94 		while (*s && *s != sep)
95 			s++;
96 
97 		child = lookup_positive_unlocked(p, dentry, s - p);
98 		dput(dentry);
99 		dentry = child;
100 	} while (!IS_ERR(dentry));
101 	return dentry;
102 }
103 
104 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb,
105 				  const char *path)
106 {
107 	size_t len = 0;
108 
109 	if (!*path)
110 		return path;
111 
112 	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
113 	    cifs_sb->prepath) {
114 		len = strlen(cifs_sb->prepath) + 1;
115 		if (unlikely(len > strlen(path)))
116 			return ERR_PTR(-EINVAL);
117 	}
118 	return path + len;
119 }
120 
121 /*
122  * Open the and cache a directory handle.
123  * If error then *cfid is not initialized.
124  */
125 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
126 		    const char *path,
127 		    struct cifs_sb_info *cifs_sb,
128 		    bool lookup_only, struct cached_fid **ret_cfid)
129 {
130 	struct cifs_ses *ses;
131 	struct TCP_Server_Info *server;
132 	struct cifs_open_parms oparms;
133 	struct smb2_create_rsp *o_rsp = NULL;
134 	struct smb2_query_info_rsp *qi_rsp = NULL;
135 	int resp_buftype[2];
136 	struct smb_rqst rqst[2];
137 	struct kvec rsp_iov[2];
138 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
139 	struct kvec qi_iov[1];
140 	int rc, flags = 0;
141 	__le16 *utf16_path = NULL;
142 	u8 oplock = SMB2_OPLOCK_LEVEL_II;
143 	struct cifs_fid *pfid;
144 	struct dentry *dentry = NULL;
145 	struct cached_fid *cfid;
146 	struct cached_fids *cfids;
147 	const char *npath;
148 	int retries = 0, cur_sleep = 1;
149 
150 	if (tcon == NULL || tcon->cfids == NULL || tcon->nohandlecache ||
151 	    is_smb1_server(tcon->ses->server) || (dir_cache_timeout == 0))
152 		return -EOPNOTSUPP;
153 
154 	ses = tcon->ses;
155 	cfids = tcon->cfids;
156 
157 	if (cifs_sb->root == NULL)
158 		return -ENOENT;
159 
160 replay_again:
161 	/* reinitialize for possible replay */
162 	flags = 0;
163 	oplock = SMB2_OPLOCK_LEVEL_II;
164 	server = cifs_pick_channel(ses);
165 
166 	if (!server->ops->new_lease_key)
167 		return -EIO;
168 
169 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
170 	if (!utf16_path)
171 		return -ENOMEM;
172 
173 	cfid = find_or_create_cached_dir(cfids, path, lookup_only, tcon->max_cached_dirs);
174 	if (cfid == NULL) {
175 		kfree(utf16_path);
176 		return -ENOENT;
177 	}
178 	/*
179 	 * Return cached fid if it has a lease.  Otherwise, it is either a new
180 	 * entry or laundromat worker removed it from @cfids->entries.  Caller
181 	 * will put last reference if the latter.
182 	 */
183 	spin_lock(&cfids->cfid_list_lock);
184 	if (cfid->has_lease) {
185 		spin_unlock(&cfids->cfid_list_lock);
186 		*ret_cfid = cfid;
187 		kfree(utf16_path);
188 		return 0;
189 	}
190 	spin_unlock(&cfids->cfid_list_lock);
191 
192 	/*
193 	 * Skip any prefix paths in @path as lookup_positive_unlocked() ends up
194 	 * calling ->lookup() which already adds those through
195 	 * build_path_from_dentry().  Also, do it earlier as we might reconnect
196 	 * below when trying to send compounded request and then potentially
197 	 * having a different prefix path (e.g. after DFS failover).
198 	 */
199 	npath = path_no_prefix(cifs_sb, path);
200 	if (IS_ERR(npath)) {
201 		rc = PTR_ERR(npath);
202 		goto out;
203 	}
204 
205 	if (!npath[0]) {
206 		dentry = dget(cifs_sb->root);
207 	} else {
208 		dentry = path_to_dentry(cifs_sb, npath);
209 		if (IS_ERR(dentry)) {
210 			rc = -ENOENT;
211 			goto out;
212 		}
213 	}
214 	cfid->dentry = dentry;
215 
216 	/*
217 	 * We do not hold the lock for the open because in case
218 	 * SMB2_open needs to reconnect.
219 	 * This is safe because no other thread will be able to get a ref
220 	 * to the cfid until we have finished opening the file and (possibly)
221 	 * acquired a lease.
222 	 */
223 	if (smb3_encryption_required(tcon))
224 		flags |= CIFS_TRANSFORM_REQ;
225 
226 	pfid = &cfid->fid;
227 	server->ops->new_lease_key(pfid);
228 
229 	memset(rqst, 0, sizeof(rqst));
230 	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
231 	memset(rsp_iov, 0, sizeof(rsp_iov));
232 
233 	/* Open */
234 	memset(&open_iov, 0, sizeof(open_iov));
235 	rqst[0].rq_iov = open_iov;
236 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
237 
238 	oparms = (struct cifs_open_parms) {
239 		.tcon = tcon,
240 		.path = path,
241 		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
242 		.desired_access =  FILE_READ_DATA | FILE_READ_ATTRIBUTES,
243 		.disposition = FILE_OPEN,
244 		.fid = pfid,
245 	};
246 
247 	rc = SMB2_open_init(tcon, server,
248 			    &rqst[0], &oplock, &oparms, utf16_path);
249 	if (rc)
250 		goto oshr_free;
251 	smb2_set_next_command(tcon, &rqst[0]);
252 
253 	memset(&qi_iov, 0, sizeof(qi_iov));
254 	rqst[1].rq_iov = qi_iov;
255 	rqst[1].rq_nvec = 1;
256 
257 	rc = SMB2_query_info_init(tcon, server,
258 				  &rqst[1], COMPOUND_FID,
259 				  COMPOUND_FID, FILE_ALL_INFORMATION,
260 				  SMB2_O_INFO_FILE, 0,
261 				  sizeof(struct smb2_file_all_info) +
262 				  PATH_MAX * 2, 0, NULL);
263 	if (rc)
264 		goto oshr_free;
265 
266 	smb2_set_related(&rqst[1]);
267 
268 	/*
269 	 * Set @cfid->has_lease to true before sending out compounded request so
270 	 * its lease reference can be put in cached_dir_lease_break() due to a
271 	 * potential lease break right after the request is sent or while @cfid
272 	 * is still being cached.  Concurrent processes won't be to use it yet
273 	 * due to @cfid->time being zero.
274 	 */
275 	cfid->has_lease = true;
276 
277 	if (retries) {
278 		smb2_set_replay(server, &rqst[0]);
279 		smb2_set_replay(server, &rqst[1]);
280 	}
281 
282 	rc = compound_send_recv(xid, ses, server,
283 				flags, 2, rqst,
284 				resp_buftype, rsp_iov);
285 	if (rc) {
286 		if (rc == -EREMCHG) {
287 			tcon->need_reconnect = true;
288 			pr_warn_once("server share %s deleted\n",
289 				     tcon->tree_name);
290 		}
291 		goto oshr_free;
292 	}
293 	cfid->tcon = tcon;
294 	cfid->is_open = true;
295 
296 	spin_lock(&cfids->cfid_list_lock);
297 
298 	o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
299 	oparms.fid->persistent_fid = o_rsp->PersistentFileId;
300 	oparms.fid->volatile_fid = o_rsp->VolatileFileId;
301 #ifdef CONFIG_CIFS_DEBUG2
302 	oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
303 #endif /* CIFS_DEBUG2 */
304 
305 
306 	if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) {
307 		spin_unlock(&cfids->cfid_list_lock);
308 		rc = -EINVAL;
309 		goto oshr_free;
310 	}
311 
312 	rc = smb2_parse_contexts(server, rsp_iov,
313 				 &oparms.fid->epoch,
314 				 oparms.fid->lease_key,
315 				 &oplock, NULL, NULL);
316 	if (rc) {
317 		spin_unlock(&cfids->cfid_list_lock);
318 		goto oshr_free;
319 	}
320 
321 	rc = -EINVAL;
322 	if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) {
323 		spin_unlock(&cfids->cfid_list_lock);
324 		goto oshr_free;
325 	}
326 	qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
327 	if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info)) {
328 		spin_unlock(&cfids->cfid_list_lock);
329 		goto oshr_free;
330 	}
331 	if (!smb2_validate_and_copy_iov(
332 				le16_to_cpu(qi_rsp->OutputBufferOffset),
333 				sizeof(struct smb2_file_all_info),
334 				&rsp_iov[1], sizeof(struct smb2_file_all_info),
335 				(char *)&cfid->file_all_info))
336 		cfid->file_all_info_is_valid = true;
337 
338 	cfid->time = jiffies;
339 	spin_unlock(&cfids->cfid_list_lock);
340 	/* At this point the directory handle is fully cached */
341 	rc = 0;
342 
343 oshr_free:
344 	SMB2_open_free(&rqst[0]);
345 	SMB2_query_info_free(&rqst[1]);
346 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
347 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
348 	if (rc) {
349 		spin_lock(&cfids->cfid_list_lock);
350 		if (cfid->on_list) {
351 			list_del(&cfid->entry);
352 			cfid->on_list = false;
353 			cfids->num_entries--;
354 		}
355 		if (cfid->has_lease) {
356 			/*
357 			 * We are guaranteed to have two references at this
358 			 * point. One for the caller and one for a potential
359 			 * lease. Release the Lease-ref so that the directory
360 			 * will be closed when the caller closes the cached
361 			 * handle.
362 			 */
363 			cfid->has_lease = false;
364 			spin_unlock(&cfids->cfid_list_lock);
365 			kref_put(&cfid->refcount, smb2_close_cached_fid);
366 			goto out;
367 		}
368 		spin_unlock(&cfids->cfid_list_lock);
369 	}
370 out:
371 	if (rc) {
372 		if (cfid->is_open)
373 			SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
374 				   cfid->fid.volatile_fid);
375 		free_cached_dir(cfid);
376 	} else {
377 		*ret_cfid = cfid;
378 		atomic_inc(&tcon->num_remote_opens);
379 	}
380 	kfree(utf16_path);
381 
382 	if (is_replayable_error(rc) &&
383 	    smb2_should_replay(tcon, &retries, &cur_sleep))
384 		goto replay_again;
385 
386 	return rc;
387 }
388 
389 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
390 			      struct dentry *dentry,
391 			      struct cached_fid **ret_cfid)
392 {
393 	struct cached_fid *cfid;
394 	struct cached_fids *cfids = tcon->cfids;
395 
396 	if (cfids == NULL)
397 		return -ENOENT;
398 
399 	spin_lock(&cfids->cfid_list_lock);
400 	list_for_each_entry(cfid, &cfids->entries, entry) {
401 		if (dentry && cfid->dentry == dentry) {
402 			cifs_dbg(FYI, "found a cached root file handle by dentry\n");
403 			kref_get(&cfid->refcount);
404 			*ret_cfid = cfid;
405 			spin_unlock(&cfids->cfid_list_lock);
406 			return 0;
407 		}
408 	}
409 	spin_unlock(&cfids->cfid_list_lock);
410 	return -ENOENT;
411 }
412 
413 static void
414 smb2_close_cached_fid(struct kref *ref)
415 {
416 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
417 					       refcount);
418 
419 	spin_lock(&cfid->cfids->cfid_list_lock);
420 	if (cfid->on_list) {
421 		list_del(&cfid->entry);
422 		cfid->on_list = false;
423 		cfid->cfids->num_entries--;
424 	}
425 	spin_unlock(&cfid->cfids->cfid_list_lock);
426 
427 	dput(cfid->dentry);
428 	cfid->dentry = NULL;
429 
430 	if (cfid->is_open) {
431 		SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
432 			   cfid->fid.volatile_fid);
433 		atomic_dec(&cfid->tcon->num_remote_opens);
434 	}
435 
436 	free_cached_dir(cfid);
437 }
438 
439 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
440 			     const char *name, struct cifs_sb_info *cifs_sb)
441 {
442 	struct cached_fid *cfid = NULL;
443 	int rc;
444 
445 	rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid);
446 	if (rc) {
447 		cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name);
448 		return;
449 	}
450 	spin_lock(&cfid->cfids->cfid_list_lock);
451 	if (cfid->has_lease) {
452 		cfid->has_lease = false;
453 		kref_put(&cfid->refcount, smb2_close_cached_fid);
454 	}
455 	spin_unlock(&cfid->cfids->cfid_list_lock);
456 	close_cached_dir(cfid);
457 }
458 
459 
460 void close_cached_dir(struct cached_fid *cfid)
461 {
462 	kref_put(&cfid->refcount, smb2_close_cached_fid);
463 }
464 
465 /*
466  * Called from cifs_kill_sb when we unmount a share
467  */
468 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
469 {
470 	struct rb_root *root = &cifs_sb->tlink_tree;
471 	struct rb_node *node;
472 	struct cached_fid *cfid;
473 	struct cifs_tcon *tcon;
474 	struct tcon_link *tlink;
475 	struct cached_fids *cfids;
476 
477 	for (node = rb_first(root); node; node = rb_next(node)) {
478 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
479 		tcon = tlink_tcon(tlink);
480 		if (IS_ERR(tcon))
481 			continue;
482 		cfids = tcon->cfids;
483 		if (cfids == NULL)
484 			continue;
485 		list_for_each_entry(cfid, &cfids->entries, entry) {
486 			dput(cfid->dentry);
487 			cfid->dentry = NULL;
488 		}
489 	}
490 }
491 
492 /*
493  * Invalidate all cached dirs when a TCON has been reset
494  * due to a session loss.
495  */
496 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
497 {
498 	struct cached_fids *cfids = tcon->cfids;
499 	struct cached_fid *cfid, *q;
500 	LIST_HEAD(entry);
501 
502 	if (cfids == NULL)
503 		return;
504 
505 	spin_lock(&cfids->cfid_list_lock);
506 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
507 		list_move(&cfid->entry, &entry);
508 		cfids->num_entries--;
509 		cfid->is_open = false;
510 		cfid->on_list = false;
511 		/* To prevent race with smb2_cached_lease_break() */
512 		kref_get(&cfid->refcount);
513 	}
514 	spin_unlock(&cfids->cfid_list_lock);
515 
516 	list_for_each_entry_safe(cfid, q, &entry, entry) {
517 		list_del(&cfid->entry);
518 		cancel_work_sync(&cfid->lease_break);
519 		if (cfid->has_lease) {
520 			/*
521 			 * We lease was never cancelled from the server so we
522 			 * need to drop the reference.
523 			 */
524 			spin_lock(&cfids->cfid_list_lock);
525 			cfid->has_lease = false;
526 			spin_unlock(&cfids->cfid_list_lock);
527 			kref_put(&cfid->refcount, smb2_close_cached_fid);
528 		}
529 		/* Drop the extra reference opened above*/
530 		kref_put(&cfid->refcount, smb2_close_cached_fid);
531 	}
532 }
533 
534 static void
535 smb2_cached_lease_break(struct work_struct *work)
536 {
537 	struct cached_fid *cfid = container_of(work,
538 				struct cached_fid, lease_break);
539 
540 	spin_lock(&cfid->cfids->cfid_list_lock);
541 	cfid->has_lease = false;
542 	spin_unlock(&cfid->cfids->cfid_list_lock);
543 	kref_put(&cfid->refcount, smb2_close_cached_fid);
544 }
545 
546 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
547 {
548 	struct cached_fids *cfids = tcon->cfids;
549 	struct cached_fid *cfid;
550 
551 	if (cfids == NULL)
552 		return false;
553 
554 	spin_lock(&cfids->cfid_list_lock);
555 	list_for_each_entry(cfid, &cfids->entries, entry) {
556 		if (cfid->has_lease &&
557 		    !memcmp(lease_key,
558 			    cfid->fid.lease_key,
559 			    SMB2_LEASE_KEY_SIZE)) {
560 			cfid->time = 0;
561 			/*
562 			 * We found a lease remove it from the list
563 			 * so no threads can access it.
564 			 */
565 			list_del(&cfid->entry);
566 			cfid->on_list = false;
567 			cfids->num_entries--;
568 
569 			queue_work(cifsiod_wq,
570 				   &cfid->lease_break);
571 			spin_unlock(&cfids->cfid_list_lock);
572 			return true;
573 		}
574 	}
575 	spin_unlock(&cfids->cfid_list_lock);
576 	return false;
577 }
578 
579 static struct cached_fid *init_cached_dir(const char *path)
580 {
581 	struct cached_fid *cfid;
582 
583 	cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC);
584 	if (!cfid)
585 		return NULL;
586 	cfid->path = kstrdup(path, GFP_ATOMIC);
587 	if (!cfid->path) {
588 		kfree(cfid);
589 		return NULL;
590 	}
591 
592 	INIT_WORK(&cfid->lease_break, smb2_cached_lease_break);
593 	INIT_LIST_HEAD(&cfid->entry);
594 	INIT_LIST_HEAD(&cfid->dirents.entries);
595 	mutex_init(&cfid->dirents.de_mutex);
596 	spin_lock_init(&cfid->fid_lock);
597 	kref_init(&cfid->refcount);
598 	return cfid;
599 }
600 
601 static void free_cached_dir(struct cached_fid *cfid)
602 {
603 	struct cached_dirent *dirent, *q;
604 
605 	dput(cfid->dentry);
606 	cfid->dentry = NULL;
607 
608 	/*
609 	 * Delete all cached dirent names
610 	 */
611 	list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
612 		list_del(&dirent->entry);
613 		kfree(dirent->name);
614 		kfree(dirent);
615 	}
616 
617 	kfree(cfid->path);
618 	cfid->path = NULL;
619 	kfree(cfid);
620 }
621 
622 static void cfids_laundromat_worker(struct work_struct *work)
623 {
624 	struct cached_fids *cfids;
625 	struct cached_fid *cfid, *q;
626 	LIST_HEAD(entry);
627 
628 	cfids = container_of(work, struct cached_fids, laundromat_work.work);
629 
630 	spin_lock(&cfids->cfid_list_lock);
631 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
632 		if (cfid->time &&
633 		    time_after(jiffies, cfid->time + HZ * dir_cache_timeout)) {
634 			cfid->on_list = false;
635 			list_move(&cfid->entry, &entry);
636 			cfids->num_entries--;
637 			/* To prevent race with smb2_cached_lease_break() */
638 			kref_get(&cfid->refcount);
639 		}
640 	}
641 	spin_unlock(&cfids->cfid_list_lock);
642 
643 	list_for_each_entry_safe(cfid, q, &entry, entry) {
644 		list_del(&cfid->entry);
645 		/*
646 		 * Cancel and wait for the work to finish in case we are racing
647 		 * with it.
648 		 */
649 		cancel_work_sync(&cfid->lease_break);
650 		if (cfid->has_lease) {
651 			/*
652 			 * Our lease has not yet been cancelled from the server
653 			 * so we need to drop the reference.
654 			 */
655 			spin_lock(&cfids->cfid_list_lock);
656 			cfid->has_lease = false;
657 			spin_unlock(&cfids->cfid_list_lock);
658 			kref_put(&cfid->refcount, smb2_close_cached_fid);
659 		}
660 		/* Drop the extra reference opened above */
661 		kref_put(&cfid->refcount, smb2_close_cached_fid);
662 	}
663 	queue_delayed_work(cifsiod_wq, &cfids->laundromat_work,
664 			   dir_cache_timeout * HZ);
665 }
666 
667 struct cached_fids *init_cached_dirs(void)
668 {
669 	struct cached_fids *cfids;
670 
671 	cfids = kzalloc(sizeof(*cfids), GFP_KERNEL);
672 	if (!cfids)
673 		return NULL;
674 	spin_lock_init(&cfids->cfid_list_lock);
675 	INIT_LIST_HEAD(&cfids->entries);
676 
677 	INIT_DELAYED_WORK(&cfids->laundromat_work, cfids_laundromat_worker);
678 	queue_delayed_work(cifsiod_wq, &cfids->laundromat_work,
679 			   dir_cache_timeout * HZ);
680 
681 	return cfids;
682 }
683 
684 /*
685  * Called from tconInfoFree when we are tearing down the tcon.
686  * There are no active users or open files/directories at this point.
687  */
688 void free_cached_dirs(struct cached_fids *cfids)
689 {
690 	struct cached_fid *cfid, *q;
691 	LIST_HEAD(entry);
692 
693 	if (cfids == NULL)
694 		return;
695 
696 	cancel_delayed_work_sync(&cfids->laundromat_work);
697 
698 	spin_lock(&cfids->cfid_list_lock);
699 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
700 		cfid->on_list = false;
701 		cfid->is_open = false;
702 		list_move(&cfid->entry, &entry);
703 	}
704 	spin_unlock(&cfids->cfid_list_lock);
705 
706 	list_for_each_entry_safe(cfid, q, &entry, entry) {
707 		list_del(&cfid->entry);
708 		free_cached_dir(cfid);
709 	}
710 
711 	kfree(cfids);
712 }
713