Linux Audio

Check our new training course

Yocto distribution development and maintenance

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