blob: 23ef56f55ce509d149923ae218d1f6dabd8f1da1 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302/*
Steve French099dd782021-09-13 14:51:10 -05003 * CIFS filesystem cache interface
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05304 *
5 * Copyright (c) 2010 Novell, Inc.
Suresh Jayaramanb81209d2010-11-24 17:49:06 +05306 * Author(s): Suresh Jayaraman <sjayaraman@suse.de>
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05307 *
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05308 */
9#include "fscache.h"
10#include "cifsglob.h"
11#include "cifs_debug.h"
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +053012#include "cifs_fs_sb.h"
Samuel Cabreroe73a42e2020-11-30 19:02:48 +010013#include "cifsproto.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053014
David Howells70431bf2020-11-17 15:56:59 +000015static void cifs_fscache_fill_volume_coherency(
16 struct cifs_tcon *tcon,
17 struct cifs_fscache_volume_coherency_data *cd)
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053018{
David Howells70431bf2020-11-17 15:56:59 +000019 memset(cd, 0, sizeof(*cd));
20 cd->resource_id = cpu_to_le64(tcon->resource_id);
21 cd->vol_create_time = tcon->vol_create_time;
22 cd->vol_serial_number = cpu_to_le32(tcon->vol_serial_number);
23}
David Howells402cb8d2018-04-04 13:41:28 +010024
David Howells70431bf2020-11-17 15:56:59 +000025int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
26{
27 struct cifs_fscache_volume_coherency_data cd;
28 struct TCP_Server_Info *server = tcon->ses->server;
29 struct fscache_volume *vcookie;
30 const struct sockaddr *sa = (struct sockaddr *)&server->dstaddr;
31 size_t slen, i;
32 char *sharename;
33 char *key;
34 int ret = -ENOMEM;
35
36 tcon->fscache = NULL;
37 switch (sa->sa_family) {
38 case AF_INET:
39 case AF_INET6:
40 break;
41 default:
42 cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
43 return -EINVAL;
44 }
Shyam Prasad N2adc8202021-12-02 07:30:00 +000045
David Howells402cb8d2018-04-04 13:41:28 +010046 memset(&key, 0, sizeof(key));
Steve French02102742021-11-10 03:15:29 -060047
David Howells402cb8d2018-04-04 13:41:28 +010048 sharename = extract_sharename(tcon->treeName);
49 if (IS_ERR(sharename)) {
50 cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
David Howells70431bf2020-11-17 15:56:59 +000051 return -EINVAL;
David Howells402cb8d2018-04-04 13:41:28 +010052 }
Suresh Jayaramand03382c2010-07-05 18:12:27 +053053
David Howells70431bf2020-11-17 15:56:59 +000054 slen = strlen(sharename);
55 for (i = 0; i < slen; i++)
56 if (sharename[i] == '/')
57 sharename[i] = ';';
Steve French58659852020-06-05 17:19:46 -050058
David Howells70431bf2020-11-17 15:56:59 +000059 key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
60 if (!key)
61 goto out;
62
63 cifs_fscache_fill_volume_coherency(tcon, &cd);
64 vcookie = fscache_acquire_volume(key,
65 NULL, /* preferred_cache */
66 &cd, sizeof(cd));
67 cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
68 if (IS_ERR(vcookie)) {
69 if (vcookie != ERR_PTR(-EBUSY)) {
70 ret = PTR_ERR(vcookie);
71 goto out_2;
72 }
73 pr_err("Cache volume key already in use (%s)\n", key);
74 vcookie = NULL;
75 }
76
77 tcon->fscache = vcookie;
78 ret = 0;
79out_2:
80 kfree(key);
81out:
David Howells402cb8d2018-04-04 13:41:28 +010082 kfree(sharename);
David Howells70431bf2020-11-17 15:56:59 +000083 return ret;
Suresh Jayaramand03382c2010-07-05 18:12:27 +053084}
85
Steve French96daf2b2011-05-27 04:34:02 +000086void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
Suresh Jayaramand03382c2010-07-05 18:12:27 +053087{
David Howells70431bf2020-11-17 15:56:59 +000088 struct cifs_fscache_volume_coherency_data cd;
Steve French58659852020-06-05 17:19:46 -050089
Joe Perchesf96637b2013-05-04 22:12:25 -050090 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
David Howells70431bf2020-11-17 15:56:59 +000091
92 cifs_fscache_fill_volume_coherency(tcon, &cd);
93 fscache_relinquish_volume(tcon->fscache, &cd, false);
Suresh Jayaramand03382c2010-07-05 18:12:27 +053094 tcon->fscache = NULL;
95}
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +053096
David Howells70431bf2020-11-17 15:56:59 +000097void cifs_fscache_get_inode_cookie(struct inode *inode)
David Howells402cb8d2018-04-04 13:41:28 +010098{
David Howells70431bf2020-11-17 15:56:59 +000099 struct cifs_fscache_inode_coherency_data cd;
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530100 struct cifsInodeInfo *cifsi = CIFS_I(inode);
101 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Steve French96daf2b2011-05-27 04:34:02 +0000102 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530103
David Howells874c8ca12022-06-09 21:46:04 +0100104 cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530105
David Howells874c8ca12022-06-09 21:46:04 +0100106 cifsi->netfs.cache =
David Howells70431bf2020-11-17 15:56:59 +0000107 fscache_acquire_cookie(tcon->fscache, 0,
108 &cifsi->uniqueid, sizeof(cifsi->uniqueid),
109 &cd, sizeof(cd),
David Howells874c8ca12022-06-09 21:46:04 +0100110 i_size_read(&cifsi->netfs.inode));
David Howells70431bf2020-11-17 15:56:59 +0000111}
David Howells402cb8d2018-04-04 13:41:28 +0100112
David Howells70431bf2020-11-17 15:56:59 +0000113void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
114{
115 if (update) {
116 struct cifs_fscache_inode_coherency_data cd;
117 loff_t i_size = i_size_read(inode);
David Howells402cb8d2018-04-04 13:41:28 +0100118
David Howells70431bf2020-11-17 15:56:59 +0000119 cifs_fscache_fill_coherency(inode, &cd);
120 fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
121 } else {
122 fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
123 }
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530124}
125
126void cifs_fscache_release_inode_cookie(struct inode *inode)
127{
128 struct cifsInodeInfo *cifsi = CIFS_I(inode);
David Howellsbc899ee2021-06-29 22:37:05 +0100129 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530130
David Howellsbc899ee2021-06-29 22:37:05 +0100131 if (cookie) {
132 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
133 fscache_relinquish_cookie(cookie, false);
David Howells874c8ca12022-06-09 21:46:04 +0100134 cifsi->netfs.cache = NULL;
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530135 }
136}
137
David Howells0174ee92022-01-27 16:02:58 +0000138/*
139 * Fallback page reading interface.
140 */
141static int fscache_fallback_read_page(struct inode *inode, struct page *page)
142{
143 struct netfs_cache_resources cres;
144 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
145 struct iov_iter iter;
146 struct bio_vec bvec[1];
147 int ret;
148
149 memset(&cres, 0, sizeof(cres));
150 bvec[0].bv_page = page;
151 bvec[0].bv_offset = 0;
152 bvec[0].bv_len = PAGE_SIZE;
153 iov_iter_bvec(&iter, READ, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
154
155 ret = fscache_begin_read_operation(&cres, cookie);
156 if (ret < 0)
157 return ret;
158
159 ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
160 NULL, NULL);
161 fscache_end_operation(&cres);
162 return ret;
163}
164
165/*
166 * Fallback page writing interface.
167 */
168static int fscache_fallback_write_page(struct inode *inode, struct page *page,
169 bool no_space_allocated_yet)
170{
171 struct netfs_cache_resources cres;
172 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
173 struct iov_iter iter;
174 struct bio_vec bvec[1];
175 loff_t start = page_offset(page);
176 size_t len = PAGE_SIZE;
177 int ret;
178
179 memset(&cres, 0, sizeof(cres));
180 bvec[0].bv_page = page;
181 bvec[0].bv_offset = 0;
182 bvec[0].bv_len = PAGE_SIZE;
183 iov_iter_bvec(&iter, WRITE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
184
185 ret = fscache_begin_write_operation(&cres, cookie);
186 if (ret < 0)
187 return ret;
188
189 ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
190 no_space_allocated_yet);
191 if (ret == 0)
192 ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
193 fscache_end_operation(&cres);
194 return ret;
195}
196
Suresh Jayaraman566982362010-07-05 18:13:25 +0530197/*
198 * Retrieve a page from FS-Cache
199 */
200int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
201{
David Howells0174ee92022-01-27 16:02:58 +0000202 int ret;
Suresh Jayaraman566982362010-07-05 18:13:25 +0530203
David Howells0174ee92022-01-27 16:02:58 +0000204 cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
205 __func__, cifs_inode_cookie(inode), page, inode);
206
207 ret = fscache_fallback_read_page(inode, page);
208 if (ret < 0)
209 return ret;
210
211 /* Read completed synchronously */
212 SetPageUptodate(page);
213 return 0;
Suresh Jayaraman566982362010-07-05 18:13:25 +0530214}
215
Suresh Jayaraman9dc06552010-07-05 18:13:11 +0530216void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
217{
Joe Perchesf96637b2013-05-04 22:12:25 -0500218 cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
David Howells0174ee92022-01-27 16:02:58 +0000219 __func__, cifs_inode_cookie(inode), page, inode);
Suresh Jayaraman9dc06552010-07-05 18:13:11 +0530220
David Howells0174ee92022-01-27 16:02:58 +0000221 fscache_fallback_write_page(inode, page, true);
222}
223
224/*
225 * Query the cache occupancy.
226 */
227int __cifs_fscache_query_occupancy(struct inode *inode,
228 pgoff_t first, unsigned int nr_pages,
229 pgoff_t *_data_first,
230 unsigned int *_data_nr_pages)
231{
232 struct netfs_cache_resources cres;
233 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
234 loff_t start, data_start;
235 size_t len, data_len;
236 int ret;
237
238 ret = fscache_begin_read_operation(&cres, cookie);
239 if (ret < 0)
240 return ret;
241
242 start = first * PAGE_SIZE;
243 len = nr_pages * PAGE_SIZE;
244 ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
245 &data_start, &data_len);
246 if (ret == 0) {
247 *_data_first = data_start / PAGE_SIZE;
248 *_data_nr_pages = len / PAGE_SIZE;
249 }
250
251 fscache_end_operation(&cres);
252 return ret;
Shyam Prasad N18d04062021-08-10 10:22:28 +0000253}