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