1 // SPDX-License-Identifier: LGPL-2.1
3 * CIFS filesystem cache interface
5 * Copyright (c) 2010 Novell, Inc.
6 * Author(s): Suresh Jayaraman <sjayaraman@suse.de>
11 #include "cifs_debug.h"
12 #include "cifs_fs_sb.h"
13 #include "cifsproto.h"
16 * Key for fscache inode. [!] Contents must match comparisons in cifs_find_inode().
18 struct cifs_fscache_inode_key {
20 __le64 uniqueid; /* server inode number */
21 __le64 createtime; /* creation time on server */
22 u8 type; /* S_IFMT file type */
25 static void cifs_fscache_fill_volume_coherency(
26 struct cifs_tcon *tcon,
27 struct cifs_fscache_volume_coherency_data *cd)
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);
35 int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
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;
46 if (tcon->fscache_acquired)
49 mutex_lock(&tcon->fscache_lock);
50 if (tcon->fscache_acquired) {
51 mutex_unlock(&tcon->fscache_lock);
54 tcon->fscache_acquired = true;
57 switch (sa->sa_family) {
62 mutex_unlock(&tcon->fscache_lock);
63 cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
67 memset(&key, 0, sizeof(key));
69 sharename = extract_sharename(tcon->tree_name);
70 if (IS_ERR(sharename)) {
71 mutex_unlock(&tcon->fscache_lock);
72 cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
73 return PTR_ERR(sharename);
76 slen = strlen(sharename);
77 for (i = 0; i < slen; i++)
78 if (sharename[i] == '/')
81 key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
85 cifs_fscache_fill_volume_coherency(tcon, &cd);
86 vcookie = fscache_acquire_volume(key,
87 NULL, /* preferred_cache */
89 cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
90 if (IS_ERR(vcookie)) {
91 if (vcookie != ERR_PTR(-EBUSY)) {
92 ret = PTR_ERR(vcookie);
95 pr_err("Cache volume key already in use (%s)\n", key);
99 tcon->fscache = vcookie;
105 mutex_unlock(&tcon->fscache_lock);
109 void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
111 struct cifs_fscache_volume_coherency_data cd;
113 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
115 cifs_fscache_fill_volume_coherency(tcon, &cd);
116 fscache_relinquish_volume(tcon->fscache, &cd, false);
117 tcon->fscache = NULL;
120 void cifs_fscache_get_inode_cookie(struct inode *inode)
122 struct cifs_fscache_inode_coherency_data cd;
123 struct cifs_fscache_inode_key key;
124 struct cifsInodeInfo *cifsi = CIFS_I(inode);
125 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
126 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
128 key.uniqueid = cpu_to_le64(cifsi->uniqueid);
129 key.createtime = cpu_to_le64(cifsi->createtime);
130 key.type = (inode->i_mode & S_IFMT) >> 12;
131 cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
134 fscache_acquire_cookie(tcon->fscache, 0,
137 i_size_read(&cifsi->netfs.inode));
138 if (cifsi->netfs.cache)
139 mapping_set_release_always(inode->i_mapping);
142 void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
145 struct cifs_fscache_inode_coherency_data cd;
146 loff_t i_size = i_size_read(inode);
148 cifs_fscache_fill_coherency(inode, &cd);
149 fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
151 fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
155 void cifs_fscache_release_inode_cookie(struct inode *inode)
157 struct cifsInodeInfo *cifsi = CIFS_I(inode);
158 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
161 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
162 fscache_relinquish_cookie(cookie, false);
163 cifsi->netfs.cache = NULL;
168 * Fallback page reading interface.
170 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
172 struct netfs_cache_resources cres;
173 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
174 struct iov_iter iter;
178 memset(&cres, 0, sizeof(cres));
179 bvec_set_page(&bvec, page, PAGE_SIZE, 0);
180 iov_iter_bvec(&iter, ITER_DEST, &bvec, 1, PAGE_SIZE);
182 ret = fscache_begin_read_operation(&cres, cookie);
186 ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
188 fscache_end_operation(&cres);
193 * Fallback page writing interface.
195 static int fscache_fallback_write_pages(struct inode *inode, loff_t start, size_t len,
196 bool no_space_allocated_yet)
198 struct netfs_cache_resources cres;
199 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
200 struct iov_iter iter;
203 memset(&cres, 0, sizeof(cres));
204 iov_iter_xarray(&iter, ITER_SOURCE, &inode->i_mapping->i_pages, start, len);
206 ret = fscache_begin_write_operation(&cres, cookie);
210 ret = cres.ops->prepare_write(&cres, &start, &len, len, i_size_read(inode),
211 no_space_allocated_yet);
213 ret = fscache_write(&cres, start, &iter, NULL, NULL);
214 fscache_end_operation(&cres);
219 * Retrieve a page from FS-Cache
221 int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
225 cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
226 __func__, cifs_inode_cookie(inode), page, inode);
228 ret = fscache_fallback_read_page(inode, page);
232 /* Read completed synchronously */
233 SetPageUptodate(page);
237 void __cifs_readahead_to_fscache(struct inode *inode, loff_t pos, size_t len)
239 cifs_dbg(FYI, "%s: (fsc: %p, p: %llx, l: %zx, i: %p)\n",
240 __func__, cifs_inode_cookie(inode), pos, len, inode);
242 fscache_fallback_write_pages(inode, pos, len, true);
246 * Query the cache occupancy.
248 int __cifs_fscache_query_occupancy(struct inode *inode,
249 pgoff_t first, unsigned int nr_pages,
250 pgoff_t *_data_first,
251 unsigned int *_data_nr_pages)
253 struct netfs_cache_resources cres;
254 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
255 loff_t start, data_start;
256 size_t len, data_len;
259 ret = fscache_begin_read_operation(&cres, cookie);
263 start = first * PAGE_SIZE;
264 len = nr_pages * PAGE_SIZE;
265 ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
266 &data_start, &data_len);
268 *_data_first = data_start / PAGE_SIZE;
269 *_data_nr_pages = len / PAGE_SIZE;
272 fscache_end_operation(&cres);