xref: /linux/fs/smb/client/fscache.c (revision 173b0b5b0e865348684c02bd9cb1d22b5d46e458)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *   CIFS filesystem cache interface
4  *
5  *   Copyright (c) 2010 Novell, Inc.
6  *   Author(s): Suresh Jayaraman <sjayaraman@suse.de>
7  *
8  */
9 #include "fscache.h"
10 #include "cifsglob.h"
11 #include "cifs_debug.h"
12 #include "cifs_fs_sb.h"
13 #include "cifsproto.h"
14 
15 /*
16  * Key for fscache inode.  [!] Contents must match comparisons in cifs_find_inode().
17  */
18 struct cifs_fscache_inode_key {
19 
20 	__le64  uniqueid;	/* server inode number */
21 	__le64  createtime;	/* creation time on server */
22 	u8	type;		/* S_IFMT file type */
23 } __packed;
24 
25 static void cifs_fscache_fill_volume_coherency(
26 	struct cifs_tcon *tcon,
27 	struct cifs_fscache_volume_coherency_data *cd)
28 {
29 	memset(cd, 0, sizeof(*cd));
30 	cd->resource_id		= cpu_to_le64(tcon->resource_id);
31 	cd->vol_create_time	= tcon->vol_create_time;
32 	cd->vol_serial_number	= cpu_to_le32(tcon->vol_serial_number);
33 }
34 
35 int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
36 {
37 	struct cifs_fscache_volume_coherency_data cd;
38 	struct TCP_Server_Info *server = tcon->ses->server;
39 	struct fscache_volume *vcookie;
40 	const struct sockaddr *sa = (struct sockaddr *)&server->dstaddr;
41 	size_t slen, i;
42 	char *sharename;
43 	char *key;
44 	int ret = -ENOMEM;
45 
46 	tcon->fscache = NULL;
47 	switch (sa->sa_family) {
48 	case AF_INET:
49 	case AF_INET6:
50 		break;
51 	default:
52 		cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
53 		return -EINVAL;
54 	}
55 
56 	memset(&key, 0, sizeof(key));
57 
58 	sharename = extract_sharename(tcon->tree_name);
59 	if (IS_ERR(sharename)) {
60 		cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
61 		return PTR_ERR(sharename);
62 	}
63 
64 	slen = strlen(sharename);
65 	for (i = 0; i < slen; i++)
66 		if (sharename[i] == '/')
67 			sharename[i] = ';';
68 
69 	key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
70 	if (!key)
71 		goto out;
72 
73 	cifs_fscache_fill_volume_coherency(tcon, &cd);
74 	vcookie = fscache_acquire_volume(key,
75 					 NULL, /* preferred_cache */
76 					 &cd, sizeof(cd));
77 	cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
78 	if (IS_ERR(vcookie)) {
79 		if (vcookie != ERR_PTR(-EBUSY)) {
80 			ret = PTR_ERR(vcookie);
81 			goto out_2;
82 		}
83 		pr_err("Cache volume key already in use (%s)\n", key);
84 		vcookie = NULL;
85 	}
86 
87 	tcon->fscache = vcookie;
88 	ret = 0;
89 out_2:
90 	kfree(key);
91 out:
92 	kfree(sharename);
93 	return ret;
94 }
95 
96 void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
97 {
98 	struct cifs_fscache_volume_coherency_data cd;
99 
100 	cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
101 
102 	cifs_fscache_fill_volume_coherency(tcon, &cd);
103 	fscache_relinquish_volume(tcon->fscache, &cd, false);
104 	tcon->fscache = NULL;
105 }
106 
107 void cifs_fscache_get_inode_cookie(struct inode *inode)
108 {
109 	struct cifs_fscache_inode_coherency_data cd;
110 	struct cifs_fscache_inode_key key;
111 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
112 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
113 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
114 
115 	key.uniqueid	= cpu_to_le64(cifsi->uniqueid);
116 	key.createtime	= cpu_to_le64(cifsi->createtime);
117 	key.type	= (inode->i_mode & S_IFMT) >> 12;
118 	cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
119 
120 	cifsi->netfs.cache =
121 		fscache_acquire_cookie(tcon->fscache, 0,
122 				       &key, sizeof(key),
123 				       &cd, sizeof(cd),
124 				       i_size_read(&cifsi->netfs.inode));
125 	if (cifsi->netfs.cache)
126 		mapping_set_release_always(inode->i_mapping);
127 }
128 
129 void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
130 {
131 	if (update) {
132 		struct cifs_fscache_inode_coherency_data cd;
133 		loff_t i_size = i_size_read(inode);
134 
135 		cifs_fscache_fill_coherency(inode, &cd);
136 		fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
137 	} else {
138 		fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
139 	}
140 }
141 
142 void cifs_fscache_release_inode_cookie(struct inode *inode)
143 {
144 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
145 	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
146 
147 	if (cookie) {
148 		cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
149 		fscache_relinquish_cookie(cookie, false);
150 		cifsi->netfs.cache = NULL;
151 	}
152 }
153 
154 /*
155  * Fallback page reading interface.
156  */
157 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
158 {
159 	struct netfs_cache_resources cres;
160 	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
161 	struct iov_iter iter;
162 	struct bio_vec bvec;
163 	int ret;
164 
165 	memset(&cres, 0, sizeof(cres));
166 	bvec_set_page(&bvec, page, PAGE_SIZE, 0);
167 	iov_iter_bvec(&iter, ITER_DEST, &bvec, 1, PAGE_SIZE);
168 
169 	ret = fscache_begin_read_operation(&cres, cookie);
170 	if (ret < 0)
171 		return ret;
172 
173 	ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
174 			   NULL, NULL);
175 	fscache_end_operation(&cres);
176 	return ret;
177 }
178 
179 /*
180  * Fallback page writing interface.
181  */
182 static int fscache_fallback_write_pages(struct inode *inode, loff_t start, size_t len,
183 					bool no_space_allocated_yet)
184 {
185 	struct netfs_cache_resources cres;
186 	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
187 	struct iov_iter iter;
188 	int ret;
189 
190 	memset(&cres, 0, sizeof(cres));
191 	iov_iter_xarray(&iter, ITER_SOURCE, &inode->i_mapping->i_pages, start, len);
192 
193 	ret = fscache_begin_write_operation(&cres, cookie);
194 	if (ret < 0)
195 		return ret;
196 
197 	ret = cres.ops->prepare_write(&cres, &start, &len, len, i_size_read(inode),
198 				      no_space_allocated_yet);
199 	if (ret == 0)
200 		ret = fscache_write(&cres, start, &iter, NULL, NULL);
201 	fscache_end_operation(&cres);
202 	return ret;
203 }
204 
205 /*
206  * Retrieve a page from FS-Cache
207  */
208 int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
209 {
210 	int ret;
211 
212 	cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
213 		 __func__, cifs_inode_cookie(inode), page, inode);
214 
215 	ret = fscache_fallback_read_page(inode, page);
216 	if (ret < 0)
217 		return ret;
218 
219 	/* Read completed synchronously */
220 	SetPageUptodate(page);
221 	return 0;
222 }
223 
224 void __cifs_readahead_to_fscache(struct inode *inode, loff_t pos, size_t len)
225 {
226 	cifs_dbg(FYI, "%s: (fsc: %p, p: %llx, l: %zx, i: %p)\n",
227 		 __func__, cifs_inode_cookie(inode), pos, len, inode);
228 
229 	fscache_fallback_write_pages(inode, pos, len, true);
230 }
231 
232 /*
233  * Query the cache occupancy.
234  */
235 int __cifs_fscache_query_occupancy(struct inode *inode,
236 				   pgoff_t first, unsigned int nr_pages,
237 				   pgoff_t *_data_first,
238 				   unsigned int *_data_nr_pages)
239 {
240 	struct netfs_cache_resources cres;
241 	struct fscache_cookie *cookie = cifs_inode_cookie(inode);
242 	loff_t start, data_start;
243 	size_t len, data_len;
244 	int ret;
245 
246 	ret = fscache_begin_read_operation(&cres, cookie);
247 	if (ret < 0)
248 		return ret;
249 
250 	start = first * PAGE_SIZE;
251 	len = nr_pages * PAGE_SIZE;
252 	ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
253 					&data_start, &data_len);
254 	if (ret == 0) {
255 		*_data_first = data_start / PAGE_SIZE;
256 		*_data_nr_pages = len / PAGE_SIZE;
257 	}
258 
259 	fscache_end_operation(&cres);
260 	return ret;
261 }
262