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 
find_or_create_cached_dir(struct cached_fids * cfids,const char * path,bool lookup_only)19 static struct cached_fid *find_or_create_cached_dir(struct cached_fids *cfids,
20 						    const char *path,
21 						    bool lookup_only)
22 {
23 	struct cached_fid *cfid;
24 
25 	spin_lock(&cfids->cfid_list_lock);
26 	list_for_each_entry(cfid, &cfids->entries, entry) {
27 		if (!strcmp(cfid->path, path)) {
28 			/*
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.
32 			 */
33 			if (!cfid->has_lease) {
34 				spin_unlock(&cfids->cfid_list_lock);
35 				return NULL;
36 			}
37 			kref_get(&cfid->refcount);
38 			spin_unlock(&cfids->cfid_list_lock);
39 			return cfid;
40 		}
41 	}
42 	if (lookup_only) {
43 		spin_unlock(&cfids->cfid_list_lock);
44 		return NULL;
45 	}
46 	if (cfids->num_entries >= MAX_CACHED_FIDS) {
47 		spin_unlock(&cfids->cfid_list_lock);
48 		return NULL;
49 	}
50 	cfid = init_cached_dir(path);
51 	if (cfid == NULL) {
52 		spin_unlock(&cfids->cfid_list_lock);
53 		return NULL;
54 	}
55 	cfid->cfids = cfids;
56 	cfids->num_entries++;
57 	list_add(&cfid->entry, &cfids->entries);
58 	cfid->on_list = true;
59 	kref_get(&cfid->refcount);
60 	spin_unlock(&cfids->cfid_list_lock);
61 	return cfid;
62 }
63 
64 static struct dentry *
path_to_dentry(struct cifs_sb_info * cifs_sb,const char * path)65 path_to_dentry(struct cifs_sb_info *cifs_sb, const char *path)
66 {
67 	struct dentry *dentry;
68 	const char *s, *p;
69 	char sep;
70 
71 	sep = CIFS_DIR_SEP(cifs_sb);
72 	dentry = dget(cifs_sb->root);
73 	s = path;
74 
75 	do {
76 		struct inode *dir = d_inode(dentry);
77 		struct dentry *child;
78 
79 		if (!S_ISDIR(dir->i_mode)) {
80 			dput(dentry);
81 			dentry = ERR_PTR(-ENOTDIR);
82 			break;
83 		}
84 
85 		/* skip separators */
86 		while (*s == sep)
87 			s++;
88 		if (!*s)
89 			break;
90 		p = s++;
91 		/* next separator */
92 		while (*s && *s != sep)
93 			s++;
94 
95 		child = lookup_positive_unlocked(p, dentry, s - p);
96 		dput(dentry);
97 		dentry = child;
98 	} while (!IS_ERR(dentry));
99 	return dentry;
100 }
101 
102 /*
103  * Open the and cache a directory handle.
104  * If error then *cfid is not initialized.
105  */
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)106 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
107 		    const char *path,
108 		    struct cifs_sb_info *cifs_sb,
109 		    bool lookup_only, struct cached_fid **ret_cfid)
110 {
111 	struct cifs_ses *ses;
112 	struct TCP_Server_Info *server;
113 	struct cifs_open_parms oparms;
114 	struct smb2_create_rsp *o_rsp = NULL;
115 	struct smb2_query_info_rsp *qi_rsp = NULL;
116 	int resp_buftype[2];
117 	struct smb_rqst rqst[2];
118 	struct kvec rsp_iov[2];
119 	struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
120 	struct kvec qi_iov[1];
121 	int rc, flags = 0;
122 	__le16 *utf16_path = NULL;
123 	u8 oplock = SMB2_OPLOCK_LEVEL_II;
124 	struct cifs_fid *pfid;
125 	struct dentry *dentry = NULL;
126 	struct cached_fid *cfid;
127 	struct cached_fids *cfids;
128 
129 	if (tcon == NULL || tcon->cfids == NULL || tcon->nohandlecache ||
130 	    is_smb1_server(tcon->ses->server))
131 		return -EOPNOTSUPP;
132 
133 	ses = tcon->ses;
134 	server = ses->server;
135 	cfids = tcon->cfids;
136 
137 	if (!server->ops->new_lease_key)
138 		return -EIO;
139 
140 	if (cifs_sb->root == NULL)
141 		return -ENOENT;
142 
143 	utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
144 	if (!utf16_path)
145 		return -ENOMEM;
146 
147 	cfid = find_or_create_cached_dir(cfids, path, lookup_only);
148 	if (cfid == NULL) {
149 		kfree(utf16_path);
150 		return -ENOENT;
151 	}
152 	/*
153 	 * At this point we either have a lease already and we can just
154 	 * return it. If not we are guaranteed to be the only thread accessing
155 	 * this cfid.
156 	 */
157 	if (cfid->has_lease) {
158 		*ret_cfid = cfid;
159 		kfree(utf16_path);
160 		return 0;
161 	}
162 
163 	/*
164 	 * We do not hold the lock for the open because in case
165 	 * SMB2_open needs to reconnect.
166 	 * This is safe because no other thread will be able to get a ref
167 	 * to the cfid until we have finished opening the file and (possibly)
168 	 * acquired a lease.
169 	 */
170 	if (smb3_encryption_required(tcon))
171 		flags |= CIFS_TRANSFORM_REQ;
172 
173 	pfid = &cfid->fid;
174 	server->ops->new_lease_key(pfid);
175 
176 	memset(rqst, 0, sizeof(rqst));
177 	resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
178 	memset(rsp_iov, 0, sizeof(rsp_iov));
179 
180 	/* Open */
181 	memset(&open_iov, 0, sizeof(open_iov));
182 	rqst[0].rq_iov = open_iov;
183 	rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
184 
185 	oparms = (struct cifs_open_parms) {
186 		.tcon = tcon,
187 		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_FILE),
188 		.desired_access = FILE_READ_ATTRIBUTES,
189 		.disposition = FILE_OPEN,
190 		.fid = pfid,
191 	};
192 
193 	rc = SMB2_open_init(tcon, server,
194 			    &rqst[0], &oplock, &oparms, utf16_path);
195 	if (rc)
196 		goto oshr_free;
197 	smb2_set_next_command(tcon, &rqst[0]);
198 
199 	memset(&qi_iov, 0, sizeof(qi_iov));
200 	rqst[1].rq_iov = qi_iov;
201 	rqst[1].rq_nvec = 1;
202 
203 	rc = SMB2_query_info_init(tcon, server,
204 				  &rqst[1], COMPOUND_FID,
205 				  COMPOUND_FID, FILE_ALL_INFORMATION,
206 				  SMB2_O_INFO_FILE, 0,
207 				  sizeof(struct smb2_file_all_info) +
208 				  PATH_MAX * 2, 0, NULL);
209 	if (rc)
210 		goto oshr_free;
211 
212 	smb2_set_related(&rqst[1]);
213 
214 	rc = compound_send_recv(xid, ses, server,
215 				flags, 2, rqst,
216 				resp_buftype, rsp_iov);
217 	if (rc) {
218 		if (rc == -EREMCHG) {
219 			tcon->need_reconnect = true;
220 			pr_warn_once("server share %s deleted\n",
221 				     tcon->tree_name);
222 		}
223 		goto oshr_free;
224 	}
225 	cfid->tcon = tcon;
226 	cfid->is_open = true;
227 
228 	o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
229 	oparms.fid->persistent_fid = o_rsp->PersistentFileId;
230 	oparms.fid->volatile_fid = o_rsp->VolatileFileId;
231 #ifdef CONFIG_CIFS_DEBUG2
232 	oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
233 #endif /* CIFS_DEBUG2 */
234 
235 	if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
236 		goto oshr_free;
237 
238 	smb2_parse_contexts(server, o_rsp,
239 			    &oparms.fid->epoch,
240 			    oparms.fid->lease_key, &oplock,
241 			    NULL, NULL);
242 	if (!(oplock & SMB2_LEASE_READ_CACHING_HE))
243 		goto oshr_free;
244 	qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
245 	if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
246 		goto oshr_free;
247 	if (!smb2_validate_and_copy_iov(
248 				le16_to_cpu(qi_rsp->OutputBufferOffset),
249 				sizeof(struct smb2_file_all_info),
250 				&rsp_iov[1], sizeof(struct smb2_file_all_info),
251 				(char *)&cfid->file_all_info))
252 		cfid->file_all_info_is_valid = true;
253 
254 	if (!path[0])
255 		dentry = dget(cifs_sb->root);
256 	else {
257 		dentry = path_to_dentry(cifs_sb, path);
258 		if (IS_ERR(dentry)) {
259 			rc = -ENOENT;
260 			goto oshr_free;
261 		}
262 	}
263 	cfid->dentry = dentry;
264 	cfid->time = jiffies;
265 	cfid->has_lease = true;
266 
267 oshr_free:
268 	kfree(utf16_path);
269 	SMB2_open_free(&rqst[0]);
270 	SMB2_query_info_free(&rqst[1]);
271 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
272 	free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
273 	spin_lock(&cfids->cfid_list_lock);
274 	if (rc && !cfid->has_lease) {
275 		if (cfid->on_list) {
276 			list_del(&cfid->entry);
277 			cfid->on_list = false;
278 			cfids->num_entries--;
279 		}
280 		rc = -ENOENT;
281 	}
282 	spin_unlock(&cfids->cfid_list_lock);
283 	if (!rc && !cfid->has_lease) {
284 		/*
285 		 * We are guaranteed to have two references at this point.
286 		 * One for the caller and one for a potential lease.
287 		 * Release the Lease-ref so that the directory will be closed
288 		 * when the caller closes the cached handle.
289 		 */
290 		kref_put(&cfid->refcount, smb2_close_cached_fid);
291 	}
292 	if (rc) {
293 		if (cfid->is_open)
294 			SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
295 				   cfid->fid.volatile_fid);
296 		free_cached_dir(cfid);
297 		cfid = NULL;
298 	}
299 
300 	if (rc == 0) {
301 		*ret_cfid = cfid;
302 		atomic_inc(&tcon->num_remote_opens);
303 	}
304 
305 	return rc;
306 }
307 
open_cached_dir_by_dentry(struct cifs_tcon * tcon,struct dentry * dentry,struct cached_fid ** ret_cfid)308 int open_cached_dir_by_dentry(struct cifs_tcon *tcon,
309 			      struct dentry *dentry,
310 			      struct cached_fid **ret_cfid)
311 {
312 	struct cached_fid *cfid;
313 	struct cached_fids *cfids = tcon->cfids;
314 
315 	if (cfids == NULL)
316 		return -ENOENT;
317 
318 	spin_lock(&cfids->cfid_list_lock);
319 	list_for_each_entry(cfid, &cfids->entries, entry) {
320 		if (dentry && cfid->dentry == dentry) {
321 			cifs_dbg(FYI, "found a cached root file handle by dentry\n");
322 			kref_get(&cfid->refcount);
323 			*ret_cfid = cfid;
324 			spin_unlock(&cfids->cfid_list_lock);
325 			return 0;
326 		}
327 	}
328 	spin_unlock(&cfids->cfid_list_lock);
329 	return -ENOENT;
330 }
331 
332 static void
smb2_close_cached_fid(struct kref * ref)333 smb2_close_cached_fid(struct kref *ref)
334 {
335 	struct cached_fid *cfid = container_of(ref, struct cached_fid,
336 					       refcount);
337 
338 	spin_lock(&cfid->cfids->cfid_list_lock);
339 	if (cfid->on_list) {
340 		list_del(&cfid->entry);
341 		cfid->on_list = false;
342 		cfid->cfids->num_entries--;
343 	}
344 	spin_unlock(&cfid->cfids->cfid_list_lock);
345 
346 	dput(cfid->dentry);
347 	cfid->dentry = NULL;
348 
349 	if (cfid->is_open) {
350 		SMB2_close(0, cfid->tcon, cfid->fid.persistent_fid,
351 			   cfid->fid.volatile_fid);
352 		atomic_dec(&cfid->tcon->num_remote_opens);
353 	}
354 
355 	free_cached_dir(cfid);
356 }
357 
drop_cached_dir_by_name(const unsigned int xid,struct cifs_tcon * tcon,const char * name,struct cifs_sb_info * cifs_sb)358 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
359 			     const char *name, struct cifs_sb_info *cifs_sb)
360 {
361 	struct cached_fid *cfid = NULL;
362 	int rc;
363 
364 	rc = open_cached_dir(xid, tcon, name, cifs_sb, true, &cfid);
365 	if (rc) {
366 		cifs_dbg(FYI, "no cached dir found for rmdir(%s)\n", name);
367 		return;
368 	}
369 	spin_lock(&cfid->cfids->cfid_list_lock);
370 	if (cfid->has_lease) {
371 		cfid->has_lease = false;
372 		kref_put(&cfid->refcount, smb2_close_cached_fid);
373 	}
374 	spin_unlock(&cfid->cfids->cfid_list_lock);
375 	close_cached_dir(cfid);
376 }
377 
378 
close_cached_dir(struct cached_fid * cfid)379 void close_cached_dir(struct cached_fid *cfid)
380 {
381 	kref_put(&cfid->refcount, smb2_close_cached_fid);
382 }
383 
384 /*
385  * Called from cifs_kill_sb when we unmount a share
386  */
close_all_cached_dirs(struct cifs_sb_info * cifs_sb)387 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
388 {
389 	struct rb_root *root = &cifs_sb->tlink_tree;
390 	struct rb_node *node;
391 	struct cached_fid *cfid;
392 	struct cifs_tcon *tcon;
393 	struct tcon_link *tlink;
394 	struct cached_fids *cfids;
395 
396 	for (node = rb_first(root); node; node = rb_next(node)) {
397 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
398 		tcon = tlink_tcon(tlink);
399 		if (IS_ERR(tcon))
400 			continue;
401 		cfids = tcon->cfids;
402 		if (cfids == NULL)
403 			continue;
404 		list_for_each_entry(cfid, &cfids->entries, entry) {
405 			dput(cfid->dentry);
406 			cfid->dentry = NULL;
407 		}
408 	}
409 }
410 
411 /*
412  * Invalidate all cached dirs when a TCON has been reset
413  * due to a session loss.
414  */
invalidate_all_cached_dirs(struct cifs_tcon * tcon)415 void invalidate_all_cached_dirs(struct cifs_tcon *tcon)
416 {
417 	struct cached_fids *cfids = tcon->cfids;
418 	struct cached_fid *cfid, *q;
419 	LIST_HEAD(entry);
420 
421 	spin_lock(&cfids->cfid_list_lock);
422 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
423 		list_move(&cfid->entry, &entry);
424 		cfids->num_entries--;
425 		cfid->is_open = false;
426 		cfid->on_list = false;
427 		/* To prevent race with smb2_cached_lease_break() */
428 		kref_get(&cfid->refcount);
429 	}
430 	spin_unlock(&cfids->cfid_list_lock);
431 
432 	list_for_each_entry_safe(cfid, q, &entry, entry) {
433 		list_del(&cfid->entry);
434 		cancel_work_sync(&cfid->lease_break);
435 		if (cfid->has_lease) {
436 			/*
437 			 * We lease was never cancelled from the server so we
438 			 * need to drop the reference.
439 			 */
440 			spin_lock(&cfids->cfid_list_lock);
441 			cfid->has_lease = false;
442 			spin_unlock(&cfids->cfid_list_lock);
443 			kref_put(&cfid->refcount, smb2_close_cached_fid);
444 		}
445 		/* Drop the extra reference opened above*/
446 		kref_put(&cfid->refcount, smb2_close_cached_fid);
447 	}
448 }
449 
450 static void
smb2_cached_lease_break(struct work_struct * work)451 smb2_cached_lease_break(struct work_struct *work)
452 {
453 	struct cached_fid *cfid = container_of(work,
454 				struct cached_fid, lease_break);
455 
456 	spin_lock(&cfid->cfids->cfid_list_lock);
457 	cfid->has_lease = false;
458 	spin_unlock(&cfid->cfids->cfid_list_lock);
459 	kref_put(&cfid->refcount, smb2_close_cached_fid);
460 }
461 
cached_dir_lease_break(struct cifs_tcon * tcon,__u8 lease_key[16])462 int cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
463 {
464 	struct cached_fids *cfids = tcon->cfids;
465 	struct cached_fid *cfid;
466 
467 	if (cfids == NULL)
468 		return false;
469 
470 	spin_lock(&cfids->cfid_list_lock);
471 	list_for_each_entry(cfid, &cfids->entries, entry) {
472 		if (cfid->has_lease &&
473 		    !memcmp(lease_key,
474 			    cfid->fid.lease_key,
475 			    SMB2_LEASE_KEY_SIZE)) {
476 			cfid->time = 0;
477 			/*
478 			 * We found a lease remove it from the list
479 			 * so no threads can access it.
480 			 */
481 			list_del(&cfid->entry);
482 			cfid->on_list = false;
483 			cfids->num_entries--;
484 
485 			queue_work(cifsiod_wq,
486 				   &cfid->lease_break);
487 			spin_unlock(&cfids->cfid_list_lock);
488 			return true;
489 		}
490 	}
491 	spin_unlock(&cfids->cfid_list_lock);
492 	return false;
493 }
494 
init_cached_dir(const char * path)495 static struct cached_fid *init_cached_dir(const char *path)
496 {
497 	struct cached_fid *cfid;
498 
499 	cfid = kzalloc(sizeof(*cfid), GFP_ATOMIC);
500 	if (!cfid)
501 		return NULL;
502 	cfid->path = kstrdup(path, GFP_ATOMIC);
503 	if (!cfid->path) {
504 		kfree(cfid);
505 		return NULL;
506 	}
507 
508 	INIT_WORK(&cfid->lease_break, smb2_cached_lease_break);
509 	INIT_LIST_HEAD(&cfid->entry);
510 	INIT_LIST_HEAD(&cfid->dirents.entries);
511 	mutex_init(&cfid->dirents.de_mutex);
512 	spin_lock_init(&cfid->fid_lock);
513 	kref_init(&cfid->refcount);
514 	return cfid;
515 }
516 
free_cached_dir(struct cached_fid * cfid)517 static void free_cached_dir(struct cached_fid *cfid)
518 {
519 	struct cached_dirent *dirent, *q;
520 
521 	dput(cfid->dentry);
522 	cfid->dentry = NULL;
523 
524 	/*
525 	 * Delete all cached dirent names
526 	 */
527 	list_for_each_entry_safe(dirent, q, &cfid->dirents.entries, entry) {
528 		list_del(&dirent->entry);
529 		kfree(dirent->name);
530 		kfree(dirent);
531 	}
532 
533 	kfree(cfid->path);
534 	cfid->path = NULL;
535 	kfree(cfid);
536 }
537 
init_cached_dirs(void)538 struct cached_fids *init_cached_dirs(void)
539 {
540 	struct cached_fids *cfids;
541 
542 	cfids = kzalloc(sizeof(*cfids), GFP_KERNEL);
543 	if (!cfids)
544 		return NULL;
545 	spin_lock_init(&cfids->cfid_list_lock);
546 	INIT_LIST_HEAD(&cfids->entries);
547 	return cfids;
548 }
549 
550 /*
551  * Called from tconInfoFree when we are tearing down the tcon.
552  * There are no active users or open files/directories at this point.
553  */
free_cached_dirs(struct cached_fids * cfids)554 void free_cached_dirs(struct cached_fids *cfids)
555 {
556 	struct cached_fid *cfid, *q;
557 	LIST_HEAD(entry);
558 
559 	spin_lock(&cfids->cfid_list_lock);
560 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
561 		cfid->on_list = false;
562 		cfid->is_open = false;
563 		list_move(&cfid->entry, &entry);
564 	}
565 	spin_unlock(&cfids->cfid_list_lock);
566 
567 	list_for_each_entry_safe(cfid, q, &entry, entry) {
568 		list_del(&cfid->entry);
569 		free_cached_dir(cfid);
570 	}
571 
572 	kfree(cfids);
573 }
574