xref: /linux/fs/smb/client/cached_dir.h (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
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 #ifndef _CACHED_DIR_H
9 #define _CACHED_DIR_H
10 
11 
12 struct cached_dirent {
13 	struct list_head entry;
14 	char *name;
15 	int namelen;
16 	loff_t pos;
17 	struct cifs_fattr fattr;
18 };
19 
20 struct cached_dirents {
21 	bool is_valid:1;
22 	bool is_failed:1;
23 	struct file *file; /*
24 			    * Used to associate the cache with a single
25 			    * open file instance.
26 			    */
27 	struct mutex de_mutex;
28 	loff_t pos;		 /* Expected ctx->pos */
29 	struct list_head entries;
30 	/* accounting for cached entries in this directory */
31 	unsigned long entries_count;
32 	unsigned long bytes_used;
33 };
34 
35 struct cached_fid {
36 	struct list_head entry;
37 	struct cached_fids *cfids;
38 	const char *path;
39 	bool has_lease;
40 	bool is_open;
41 	bool on_list;
42 	bool file_all_info_is_valid;
43 	unsigned long time; /* jiffies of when lease was taken */
44 	unsigned long last_access_time; /* jiffies of when last accessed */
45 	struct kref refcount;
46 	struct cifs_fid fid;
47 	struct cifs_tcon *tcon;
48 	struct dentry *dentry;
49 	struct work_struct put_work;
50 	struct work_struct close_work;
51 	struct cached_dirents dirents;
52 
53 	/* Must be last as it ends in a flexible-array member. */
54 	struct smb2_file_all_info file_all_info;
55 };
56 
57 /* default MAX_CACHED_FIDS is 16 */
58 struct cached_fids {
59 	/* Must be held when:
60 	 * - accessing the cfids->entries list
61 	 * - accessing the cfids->dying list
62 	 */
63 	spinlock_t cfid_list_lock;
64 	int num_entries;
65 	struct list_head entries;
66 	struct list_head dying;
67 	struct delayed_work laundromat_work;
68 	/* aggregate accounting for all cached dirents under this tcon */
69 	atomic_long_t total_dirents_entries;
70 	atomic64_t total_dirents_bytes;
71 };
72 
73 /* Module-wide directory cache accounting (defined in cifsfs.c) */
74 extern atomic64_t cifs_dircache_bytes_used; /* bytes across all mounts */
75 
76 static inline bool
77 is_valid_cached_dir(struct cached_fid *cfid)
78 {
79 	return cfid->time && cfid->has_lease;
80 }
81 
82 struct cached_fids *init_cached_dirs(void);
83 void free_cached_dirs(struct cached_fids *cfids);
84 int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, const char *path,
85 		    struct cifs_sb_info *cifs_sb, bool lookup_only,
86 		    struct cached_fid **ret_cfid);
87 int open_cached_dir_by_dentry(struct cifs_tcon *tcon, struct dentry *dentry,
88 			      struct cached_fid **ret_cfid);
89 void close_cached_dir(struct cached_fid *cfid);
90 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
91 			     const char *name, struct cifs_sb_info *cifs_sb);
92 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb);
93 void invalidate_all_cached_dirs(struct cifs_tcon *tcon);
94 bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]);
95 
96 #endif			/* _CACHED_DIR_H */
97