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;
47 switch (sa->sa_family) {
52 cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
56 memset(&key, 0, sizeof(key));
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);
64 slen = strlen(sharename);
65 for (i = 0; i < slen; i++)
66 if (sharename[i] == '/')
69 key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
73 cifs_fscache_fill_volume_coherency(tcon, &cd);
74 vcookie = fscache_acquire_volume(key,
75 NULL, /* preferred_cache */
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);
83 pr_err("Cache volume key already in use (%s)\n", key);
87 tcon->fscache = vcookie;
96 void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
98 struct cifs_fscache_volume_coherency_data cd;
100 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
102 cifs_fscache_fill_volume_coherency(tcon, &cd);
103 fscache_relinquish_volume(tcon->fscache, &cd, false);
104 tcon->fscache = NULL;
107 void cifs_fscache_get_inode_cookie(struct inode *inode)
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);
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);
121 fscache_acquire_cookie(tcon->fscache, 0,
124 i_size_read(&cifsi->netfs.inode));
125 if (cifsi->netfs.cache)
126 mapping_set_release_always(inode->i_mapping);
129 void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
132 struct cifs_fscache_inode_coherency_data cd;
133 loff_t i_size = i_size_read(inode);
135 cifs_fscache_fill_coherency(inode, &cd);
136 fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
138 fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
142 void cifs_fscache_release_inode_cookie(struct inode *inode)
144 struct cifsInodeInfo *cifsi = CIFS_I(inode);
145 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
148 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
149 fscache_relinquish_cookie(cookie, false);
150 cifsi->netfs.cache = NULL;
155 * Fallback page reading interface.
157 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
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[1];
165 memset(&cres, 0, sizeof(cres));
166 bvec[0].bv_page = page;
167 bvec[0].bv_offset = 0;
168 bvec[0].bv_len = PAGE_SIZE;
169 iov_iter_bvec(&iter, ITER_DEST, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
171 ret = fscache_begin_read_operation(&cres, cookie);
175 ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
177 fscache_end_operation(&cres);
182 * Fallback page writing interface.
184 static int fscache_fallback_write_page(struct inode *inode, struct page *page,
185 bool no_space_allocated_yet)
187 struct netfs_cache_resources cres;
188 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
189 struct iov_iter iter;
190 struct bio_vec bvec[1];
191 loff_t start = page_offset(page);
192 size_t len = PAGE_SIZE;
195 memset(&cres, 0, sizeof(cres));
196 bvec[0].bv_page = page;
197 bvec[0].bv_offset = 0;
198 bvec[0].bv_len = PAGE_SIZE;
199 iov_iter_bvec(&iter, ITER_SOURCE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
201 ret = fscache_begin_write_operation(&cres, cookie);
205 ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
206 no_space_allocated_yet);
208 ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
209 fscache_end_operation(&cres);
214 * Retrieve a page from FS-Cache
216 int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
220 cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
221 __func__, cifs_inode_cookie(inode), page, inode);
223 ret = fscache_fallback_read_page(inode, page);
227 /* Read completed synchronously */
228 SetPageUptodate(page);
232 void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
234 cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
235 __func__, cifs_inode_cookie(inode), page, inode);
237 fscache_fallback_write_page(inode, page, true);
241 * Query the cache occupancy.
243 int __cifs_fscache_query_occupancy(struct inode *inode,
244 pgoff_t first, unsigned int nr_pages,
245 pgoff_t *_data_first,
246 unsigned int *_data_nr_pages)
248 struct netfs_cache_resources cres;
249 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
250 loff_t start, data_start;
251 size_t len, data_len;
254 ret = fscache_begin_read_operation(&cres, cookie);
258 start = first * PAGE_SIZE;
259 len = nr_pages * PAGE_SIZE;
260 ret = cres.ops->query_occupancy(&cres, start, len, PAGE_SIZE,
261 &data_start, &data_len);
263 *_data_first = data_start / PAGE_SIZE;
264 *_data_nr_pages = len / PAGE_SIZE;
267 fscache_end_operation(&cres);