1 // SPDX-License-Identifier: GPL-2.0
3 * Functions to handle the cached directory entries
5 * Copyright (c) 2022, Ronnie Sahlberg <lsahlber@redhat.com>
8 #include <linux/namei.h>
10 #include "cifsproto.h"
11 #include "cifs_debug.h"
12 #include "smb2proto.h"
13 #include "cached_dir.h"
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);
19 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
23 struct cached_fid *cfid;
25 spin_lock(&cfids->cfid_list_lock);
26 list_for_each_entry(cfid, &cfids->entries, entry) {
27 if (!strcmp(cfid->path, path)) {
29 * If it doesn't have a lease it is either not yet
30 * fully cached or it may be in the process of
31 * being deleted due to a lease break.
33 if (!cfid->has_lease) {
34 spin_unlock(&cfids->cfid_list_lock);
37 kref_get(&cfid->refcount);
38 spin_unlock(&cfids->cfid_list_lock);
43 spin_unlock(&cfids->cfid_list_lock);
46 if (cfids->num_entries >= MAX_CACHED_FIDS) {
47 spin_unlock(&cfids->cfid_list_lock);
50 cfid = init_cached_dir(path);
52 spin_unlock(&cfids->cfid_list_lock);
57 list_add(&cfid->entry, &cfids->entries);
59 kref_get(&cfid->refcount);
60 spin_unlock(&cfids->cfid_list_lock);
64 static struct dentry *
65 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
67 struct dentry *dentry;
71 sep = CIFS_DIR_SEP(cifs_sb);
72 dentry = dget(cifs_sb->root);
76 struct inode *dir = d_inode(dentry);
79 if (!S_ISDIR(dir->i_mode)) {
81 dentry = ERR_PTR(-ENOTDIR);
92 while (*s && *s != sep)
95 child = lookup_positive_unlocked(p, dentry, s - p);
98 } while (!IS_ERR(dentry));
102 static const char *path_no_prefix(struct cifs_sb_info *cifs_sb,
110 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
112 len = strlen(cifs_sb->prepath) + 1;
113 if (unlikely(len > strlen(path)))
114 return ERR_PTR(-EINVAL);
120 * Open the and cache a directory handle.
121 * If error then *cfid is not initialized.
123 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
125 struct cifs_sb_info *cifs_sb,
126 bool lookup_only, struct cached_fid **ret_cfid)
128 struct cifs_ses *ses;
129 struct TCP_Server_Info *server;
130 struct cifs_open_parms oparms;
131 struct smb2_create_rsp *o_rsp = NULL;
132 struct smb2_query_info_rsp *qi_rsp = NULL;
134 struct smb_rqst rqst[2];
135 struct kvec rsp_iov[2];
136 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
137 struct kvec qi_iov[1];
139 __le16 *utf16_path = NULL;
140 u8 oplock = SMB2_OPLOCK_LEVEL_II;
141 struct cifs_fid *pfid;
142 struct dentry *dentry = NULL;
143 struct cached_fid *cfid;
144 struct cached_fids *cfids;
147 if (tcon == NULL || tcon->cfids == NULL || tcon->nohandlecache ||
148 is_smb1_server(tcon->ses->server))
152 server = ses->server;
155 if (!server->ops->new_lease_key)
158 if (cifs_sb->root == NULL)
161 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
165 cfid = find_or_create_cached_dir(cfids, path, lookup_only);
171 * At this point we either have a lease already and we can just
172 * return it. If not we are guaranteed to be the only thread accessing
175 if (cfid->has_lease) {
182 * Skip any prefix paths in @path as lookup_positive_unlocked() ends up
183 * calling ->lookup() which already adds those through
184 * build_path_from_dentry(). Also, do it earlier as we might reconnect
185 * below when trying to send compounded request and then potentially
186 * having a different prefix path (e.g. after DFS failover).
188 npath = path_no_prefix(cifs_sb, path);
196 * We do not hold the lock for the open because in case
197 * SMB2_open needs to reconnect.
198 * This is safe because no other thread will be able to get a ref
199 * to the cfid until we have finished opening the file and (possibly)
202 if (smb3_encryption_required(tcon))
203 flags |= CIFS_TRANSFORM_REQ;
206 server->ops->new_lease_key(pfid);
208 memset(rqst, 0, sizeof(rqst));
209 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
210 memset(rsp_iov, 0, sizeof(rsp_iov));
213 memset(&open_iov, 0, sizeof(open_iov));
214 rqst[0].rq_iov = open_iov;
215 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
217 oparms = (struct cifs_open_parms) {
220 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
221 .desired_access = FILE_READ_ATTRIBUTES,
222 .disposition = FILE_OPEN,
226 rc = SMB2_open_init(tcon, server,
227 &rqst[0], &oplock, &oparms, utf16_path);
230 smb2_set_next_command(tcon, &rqst[0]);
232 memset(&qi_iov, 0, sizeof(qi_iov));
233 rqst[1].rq_iov = qi_iov;
236 rc = SMB2_query_info_init(tcon, server,
237 &rqst[1], COMPOUND_FID,
238 COMPOUND_FID, FILE_ALL_INFORMATION,
240 sizeof(struct smb2_file_all_info) +
241 PATH_MAX * 2, 0, NULL);
245 smb2_set_related(&rqst[1]);
247 rc = compound_send_recv(xid, ses, server,
249 resp_buftype, rsp_iov);
251 if (rc == -EREMCHG) {
252 tcon->need_reconnect = true;
253 pr_warn_once("server share %s deleted\n",
259 cfid->is_open = true;
261 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
262 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
263 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
264 #ifdef CONFIG_CIFS_DEBUG2
265 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
266 #endif /* CIFS_DEBUG2 */
268 if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
271 smb2_parse_contexts(server, o_rsp,
273 oparms.fid->lease_key, &oplock,
275 if (!(oplock & SMB2_LEASE_READ_CACHING_HE))
277 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
278 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
280 if (!smb2_validate_and_copy_iov(
281 le16_to_cpu(qi_rsp->OutputBufferOffset),
282 sizeof(struct smb2_file_all_info),
283 &rsp_iov[1], sizeof(struct smb2_file_all_info),
284 (char *)&cfid->file_all_info))
285 cfid->file_all_info_is_valid = true;
288 dentry = dget(cifs_sb->root);
290 dentry = path_to_dentry(cifs_sb, npath);
291 if (IS_ERR(dentry)) {
296 cfid->dentry = dentry;
297 cfid->time = jiffies;
298 cfid->has_lease = true;
302 SMB2_open_free(&rqst[0]);
303 SMB2_query_info_free(&rqst[1]);
304 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
305 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
306 spin_lock(&cfids->cfid_list_lock);
307 if (rc && !cfid->has_lease) {
309 list_del(&cfid->entry);
310 cfid->on_list = false;
311 cfids->num_entries--;
315 spin_unlock(&cfids->cfid_list_lock);
316 if (!rc && !cfid->has_lease) {
318 * We are guaranteed to have two references at this point.
319 * One for the caller and one for a potential lease.
320 * Release the Lease-ref so that the directory will be closed
321 * when the caller closes the cached handle.
323 kref_put(&cfid->refcount, smb2_close_cached_fid);
327 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
328 cfid->fid.volatile_fid);
329 free_cached_dir(cfid);
335 atomic_inc(&tcon->num_remote_opens);
341 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
342 struct dentry *dentry,
343 struct cached_fid **ret_cfid)
345 struct cached_fid *cfid;
346 struct cached_fids *cfids = tcon->cfids;
351 spin_lock(&cfids->cfid_list_lock);
352 list_for_each_entry(cfid, &cfids->entries, entry) {
353 if (dentry && cfid->dentry == dentry) {
354 cifs_dbg(FYI, "found a cached root file handle by dentry\n");
355 kref_get(&cfid->refcount);
357 spin_unlock(&cfids->cfid_list_lock);
361 spin_unlock(&cfids->cfid_list_lock);
366 smb2_close_cached_fid(struct kref *ref)
368 struct cached_fid *cfid = container_of(ref, struct cached_fid,
371 spin_lock(&cfid->cfids->cfid_list_lock);
373 list_del(&cfid->entry);
374 cfid->on_list = false;
375 cfid->cfids->num_entries--;
377 spin_unlock(&cfid->cfids->cfid_list_lock);
383 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
384 cfid->fid.volatile_fid);
385 atomic_dec(&cfid->tcon->num_remote_opens);
388 free_cached_dir(cfid);
391 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
392 const char *name, struct cifs_sb_info *cifs_sb)
394 struct cached_fid *cfid = NULL;
397 rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid);
399 cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name);
402 spin_lock(&cfid->cfids->cfid_list_lock);
403 if (cfid->has_lease) {
404 cfid->has_lease = false;
405 kref_put(&cfid->refcount, smb2_close_cached_fid);
407 spin_unlock(&cfid->cfids->cfid_list_lock);
408 close_cached_dir(cfid);
412 void close_cached_dir(struct cached_fid *cfid)
414 kref_put(&cfid->refcount, smb2_close_cached_fid);
418 * Called from cifs_kill_sb when we unmount a share
420 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
422 struct rb_root *root = &cifs_sb->tlink_tree;
423 struct rb_node *node;
424 struct cached_fid *cfid;
425 struct cifs_tcon *tcon;
426 struct tcon_link *tlink;
427 struct cached_fids *cfids;
429 for (node = rb_first(root); node; node = rb_next(node)) {
430 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
431 tcon = tlink_tcon(tlink);
437 list_for_each_entry(cfid, &cfids->entries, entry) {
445 * Invalidate all cached dirs when a TCON has been reset
446 * due to a session loss.
448 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
450 struct cached_fids *cfids = tcon->cfids;
451 struct cached_fid *cfid, *q;
454 spin_lock(&cfids->cfid_list_lock);
455 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
456 list_move(&cfid->entry, &entry);
457 cfids->num_entries--;
458 cfid->is_open = false;
459 cfid->on_list = false;
460 /* To prevent race with smb2_cached_lease_break() */
461 kref_get(&cfid->refcount);
463 spin_unlock(&cfids->cfid_list_lock);
465 list_for_each_entry_safe(cfid, q, &entry, entry) {
466 list_del(&cfid->entry);
467 cancel_work_sync(&cfid->lease_break);
468 if (cfid->has_lease) {
470 * We lease was never cancelled from the server so we
471 * need to drop the reference.
473 spin_lock(&cfids->cfid_list_lock);
474 cfid->has_lease = false;
475 spin_unlock(&cfids->cfid_list_lock);
476 kref_put(&cfid->refcount, smb2_close_cached_fid);
478 /* Drop the extra reference opened above*/
479 kref_put(&cfid->refcount, smb2_close_cached_fid);
484 smb2_cached_lease_break(struct work_struct *work)
486 struct cached_fid *cfid = container_of(work,
487 struct cached_fid, lease_break);
489 spin_lock(&cfid->cfids->cfid_list_lock);
490 cfid->has_lease = false;
491 spin_unlock(&cfid->cfids->cfid_list_lock);
492 kref_put(&cfid->refcount, smb2_close_cached_fid);
495 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
497 struct cached_fids *cfids = tcon->cfids;
498 struct cached_fid *cfid;
503 spin_lock(&cfids->cfid_list_lock);
504 list_for_each_entry(cfid, &cfids->entries, entry) {
505 if (cfid->has_lease &&
508 SMB2_LEASE_KEY_SIZE)) {
511 * We found a lease remove it from the list
512 * so no threads can access it.
514 list_del(&cfid->entry);
515 cfid->on_list = false;
516 cfids->num_entries--;
518 queue_work(cifsiod_wq,
520 spin_unlock(&cfids->cfid_list_lock);
524 spin_unlock(&cfids->cfid_list_lock);
528 static struct cached_fid *init_cached_dir(const char *path)
530 struct cached_fid *cfid;
532 cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC);
535 cfid->path = kstrdup(path, GFP_ATOMIC);
541 INIT_WORK(&cfid->lease_break, smb2_cached_lease_break);
542 INIT_LIST_HEAD(&cfid->entry);
543 INIT_LIST_HEAD(&cfid->dirents.entries);
544 mutex_init(&cfid->dirents.de_mutex);
545 spin_lock_init(&cfid->fid_lock);
546 kref_init(&cfid->refcount);
550 static void free_cached_dir(struct cached_fid *cfid)
552 struct cached_dirent *dirent, *q;
558 * Delete all cached dirent names
560 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
561 list_del(&dirent->entry);
571 struct cached_fids *init_cached_dirs(void)
573 struct cached_fids *cfids;
575 cfids = kzalloc(sizeof(*cfids), GFP_KERNEL);
578 spin_lock_init(&cfids->cfid_list_lock);
579 INIT_LIST_HEAD(&cfids->entries);
584 * Called from tconInfoFree when we are tearing down the tcon.
585 * There are no active users or open files/directories at this point.
587 void free_cached_dirs(struct cached_fids *cfids)
589 struct cached_fid *cfid, *q;
592 spin_lock(&cfids->cfid_list_lock);
593 list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
594 cfid->on_list = false;
595 cfid->is_open = false;
596 list_move(&cfid->entry, &entry);
598 spin_unlock(&cfids->cfid_list_lock);
600 list_for_each_entry_safe(cfid, q, &entry, entry) {
601 list_del(&cfid->entry);
602 free_cached_dir(cfid);