1 // SPDX-License-Identifier: GPL-2.0
3 * Functions to handle the cached directory entries
10 #include "cifs_debug.h"
11 #include "smb2proto.h"
12 #include "cached_dir.h"
15 * Open the and cache a directory handle.
16 * If error then *cfid is not initialized.
18 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
20 struct cifs_sb_info *cifs_sb,
21 bool lookup_only, struct cached_fid **ret_cfid)
24 struct TCP_Server_Info *server;
25 struct cifs_open_parms oparms;
26 struct smb2_create_rsp *o_rsp = NULL;
27 struct smb2_query_info_rsp *qi_rsp = NULL;
29 struct smb_rqst rqst[2];
30 struct kvec rsp_iov[2];
31 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
32 struct kvec qi_iov[1];
34 __le16 utf16_path = 0; /* Null - since an open of top of share */
35 u8 oplock = SMB2_OPLOCK_LEVEL_II;
36 struct cifs_fid *pfid;
37 struct dentry *dentry;
38 struct cached_fid *cfid;
40 if (tcon == NULL || tcon->nohandlecache ||
41 is_smb1_server(tcon->ses->server))
47 if (cifs_sb->root == NULL)
53 dentry = cifs_sb->root;
56 mutex_lock(&cfid->fid_mutex);
58 cifs_dbg(FYI, "found a cached root file handle\n");
60 kref_get(&cfid->refcount);
61 mutex_unlock(&cfid->fid_mutex);
66 * We do not hold the lock for the open because in case
67 * SMB2_open needs to reconnect, it will end up calling
68 * cifs_mark_open_files_invalid() which takes the lock again
69 * thus causing a deadlock
71 mutex_unlock(&cfid->fid_mutex);
76 if (smb3_encryption_required(tcon))
77 flags |= CIFS_TRANSFORM_REQ;
79 if (!server->ops->new_lease_key)
83 server->ops->new_lease_key(pfid);
85 memset(rqst, 0, sizeof(rqst));
86 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
87 memset(rsp_iov, 0, sizeof(rsp_iov));
90 memset(&open_iov, 0, sizeof(open_iov));
91 rqst[0].rq_iov = open_iov;
92 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
95 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE);
96 oparms.desired_access = FILE_READ_ATTRIBUTES;
97 oparms.disposition = FILE_OPEN;
99 oparms.reconnect = false;
101 rc = SMB2_open_init(tcon, server,
102 &rqst[0], &oplock, &oparms, &utf16_path);
105 smb2_set_next_command(tcon, &rqst[0]);
107 memset(&qi_iov, 0, sizeof(qi_iov));
108 rqst[1].rq_iov = qi_iov;
111 rc = SMB2_query_info_init(tcon, server,
112 &rqst[1], COMPOUND_FID,
113 COMPOUND_FID, FILE_ALL_INFORMATION,
115 sizeof(struct smb2_file_all_info) +
116 PATH_MAX * 2, 0, NULL);
120 smb2_set_related(&rqst[1]);
122 rc = compound_send_recv(xid, ses, server,
124 resp_buftype, rsp_iov);
125 mutex_lock(&cfid->fid_mutex);
128 * Now we need to check again as the cached root might have
129 * been successfully re-opened from a concurrent process
132 if (cfid->is_valid) {
133 /* work was already done */
135 /* stash fids for close() later */
136 struct cifs_fid fid = {
137 .persistent_fid = pfid->persistent_fid,
138 .volatile_fid = pfid->volatile_fid,
142 * caller expects this func to set the fid in cfid to valid
143 * cached root, so increment the refcount.
145 kref_get(&cfid->refcount);
147 mutex_unlock(&cfid->fid_mutex);
150 /* close extra handle outside of crit sec */
151 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
157 /* Cached root is still invalid, continue normaly */
160 if (rc == -EREMCHG) {
161 tcon->need_reconnect = true;
162 pr_warn_once("server share %s deleted\n",
168 atomic_inc(&tcon->num_remote_opens);
170 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
171 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
172 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
173 #ifdef CONFIG_CIFS_DEBUG2
174 oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
175 #endif /* CIFS_DEBUG2 */
178 cfid->is_valid = true;
179 cfid->dentry = dentry;
181 kref_init(&cfid->refcount);
183 /* BB TBD check to see if oplock level check can be removed below */
184 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
186 * See commit 2f94a3125b87. Increment the refcount when we
187 * get a lease for root, release it if lease break occurs
189 kref_get(&cfid->refcount);
190 cfid->has_lease = true;
191 smb2_parse_contexts(server, o_rsp,
193 oparms.fid->lease_key, &oplock,
198 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
199 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
201 if (!smb2_validate_and_copy_iov(
202 le16_to_cpu(qi_rsp->OutputBufferOffset),
203 sizeof(struct smb2_file_all_info),
204 &rsp_iov[1], sizeof(struct smb2_file_all_info),
205 (char *)&cfid->file_all_info))
206 cfid->file_all_info_is_valid = true;
208 cfid->time = jiffies;
211 mutex_unlock(&cfid->fid_mutex);
213 SMB2_open_free(&rqst[0]);
214 SMB2_query_info_free(&rqst[1]);
215 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
216 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
223 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
224 struct dentry *dentry,
225 struct cached_fid **ret_cfid)
227 struct cached_fid *cfid;
231 mutex_lock(&cfid->fid_mutex);
232 if (cfid->dentry == dentry) {
233 cifs_dbg(FYI, "found a cached root file handle by dentry\n");
235 kref_get(&cfid->refcount);
236 mutex_unlock(&cfid->fid_mutex);
239 mutex_unlock(&cfid->fid_mutex);
244 smb2_close_cached_fid(struct kref *ref)
246 struct cached_fid *cfid = container_of(ref, struct cached_fid,
248 struct cached_dirent *dirent, *q;
250 if (cfid->is_valid) {
251 cifs_dbg(FYI, "clear cached root file handle\n");
252 SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
253 cfid->fid.volatile_fid);
257 * We only check validity above to send SMB2_close,
258 * but we still need to invalidate these entries
259 * when this function is called
261 cfid->is_valid = false;
262 cfid->file_all_info_is_valid = false;
263 cfid->has_lease = false;
269 * Delete all cached dirent names
271 mutex_lock(&cfid->dirents.de_mutex);
272 list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
273 list_del(&dirent->entry);
277 cfid->dirents.is_valid = 0;
278 cfid->dirents.is_failed = 0;
279 cfid->dirents.ctx = NULL;
280 cfid->dirents.pos = 0;
281 mutex_unlock(&cfid->dirents.de_mutex);
285 void close_cached_dir(struct cached_fid *cfid)
287 mutex_lock(&cfid->fid_mutex);
288 kref_put(&cfid->refcount, smb2_close_cached_fid);
289 mutex_unlock(&cfid->fid_mutex);
292 void close_cached_dir_lease_locked(struct cached_fid *cfid)
294 if (cfid->has_lease) {
295 cfid->has_lease = false;
296 kref_put(&cfid->refcount, smb2_close_cached_fid);
300 void close_cached_dir_lease(struct cached_fid *cfid)
302 mutex_lock(&cfid->fid_mutex);
303 close_cached_dir_lease_locked(cfid);
304 mutex_unlock(&cfid->fid_mutex);
308 * Called from cifs_kill_sb when we unmount a share
310 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
312 struct rb_root *root = &cifs_sb->tlink_tree;
313 struct rb_node *node;
314 struct cached_fid *cfid;
315 struct cifs_tcon *tcon;
316 struct tcon_link *tlink;
318 for (node = rb_first(root); node; node = rb_next(node)) {
319 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
320 tcon = tlink_tcon(tlink);
324 mutex_lock(&cfid->fid_mutex);
329 mutex_unlock(&cfid->fid_mutex);
334 * Invalidate and close all cached dirs when a TCON has been reset
335 * due to a session loss.
337 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
339 mutex_lock(&tcon->cfid->fid_mutex);
340 tcon->cfid->is_valid = false;
341 /* cached handle is not valid, so SMB2_CLOSE won't be sent below */
342 close_cached_dir_lease_locked(tcon->cfid);
343 memset(&tcon->cfid->fid, 0, sizeof(struct cifs_fid));
344 mutex_unlock(&tcon->cfid->fid_mutex);
348 smb2_cached_lease_break(struct work_struct *work)
350 struct cached_fid *cfid = container_of(work,
351 struct cached_fid, lease_break);
353 close_cached_dir_lease(cfid);
356 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
358 if (tcon->cfid->is_valid &&
360 tcon->cfid->fid.lease_key,
361 SMB2_LEASE_KEY_SIZE)) {
362 tcon->cfid->time = 0;
363 INIT_WORK(&tcon->cfid->lease_break,
364 smb2_cached_lease_break);
365 queue_work(cifsiod_wq,
366 &tcon->cfid->lease_break);
372 struct cached_fid *init_cached_dir(void)
374 struct cached_fid *cfid;
376 cfid = kzalloc(sizeof(*cfid), GFP_KERNEL);
379 INIT_LIST_HEAD(&cfid->dirents.entries);
380 mutex_init(&cfid->dirents.de_mutex);
381 mutex_init(&cfid->fid_mutex);
385 void free_cached_dir(struct cifs_tcon *tcon)