1 // SPDX-License-Identifier: LGPL-2.1
3 * CIFS filesystem cache interface
5 * Copyright (c) 2010 Novell, Inc.
11 #include "cifs_debug.h"
12 #include "cifs_fs_sb.h"
13 #include "cifsproto.h"
15 static void cifs_fscache_fill_volume_coherency(
16 struct cifs_tcon *tcon,
17 struct cifs_fscache_volume_coherency_data *cd)
19 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);
25 int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
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;
37 switch (sa->sa_family) {
42 cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
46 memset(&key, 0, sizeof(key));
48 sharename = extract_sharename(tcon->treeName);
49 if (IS_ERR(sharename)) {
50 cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
54 slen = strlen(sharename);
55 for (i = 0; i < slen; i++)
56 if (sharename[i] == '/')
59 key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
63 cifs_fscache_fill_volume_coherency(tcon, &cd);
64 vcookie = fscache_acquire_volume(key,
65 NULL, /* preferred_cache */
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);
73 pr_err("Cache volume key already in use (%s)\n", key);
77 tcon->fscache = vcookie;
86 void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
88 struct cifs_fscache_volume_coherency_data cd;
90 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
92 cifs_fscache_fill_volume_coherency(tcon, &cd);
93 fscache_relinquish_volume(tcon->fscache, &cd, false);
97 void cifs_fscache_get_inode_cookie(struct inode *inode)
99 struct cifs_fscache_inode_coherency_data cd;
100 struct cifsInodeInfo *cifsi = CIFS_I(inode);
101 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
102 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
104 cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
107 fscache_acquire_cookie(tcon->fscache, 0,
108 &cifsi->uniqueid, sizeof(cifsi->uniqueid),
110 i_size_read(&cifsi->netfs.inode));
113 void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
116 struct cifs_fscache_inode_coherency_data cd;
117 loff_t i_size = i_size_read(inode);
119 cifs_fscache_fill_coherency(inode, &cd);
120 fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
122 fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
126 void cifs_fscache_release_inode_cookie(struct inode *inode)
128 struct cifsInodeInfo *cifsi = CIFS_I(inode);
129 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
132 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
133 fscache_relinquish_cookie(cookie, false);
134 cifsi->netfs.cache = NULL;
139 * Fallback page reading interface.
141 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
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];
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);
155 ret = fscache_begin_read_operation(&cres, cookie);
159 ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
161 fscache_end_operation(&cres);
166 * Fallback page writing interface.
168 static int fscache_fallback_write_page(struct inode *inode, struct page *page,
169 bool no_space_allocated_yet)
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;
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);
185 ret = fscache_begin_write_operation(&cres, cookie);
189 ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
190 no_space_allocated_yet);
192 ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
193 fscache_end_operation(&cres);
198 * Retrieve a page from FS-Cache
200 int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
204 cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
205 __func__, cifs_inode_cookie(inode), page, inode);
207 ret = fscache_fallback_read_page(inode, page);
211 /* Read completed synchronously */
212 SetPageUptodate(page);
216 void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
218 cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
219 __func__, cifs_inode_cookie(inode), page, inode);
221 fscache_fallback_write_page(inode, page, true);
225 * Query the cache occupancy.
227 int __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)
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;
238 ret = fscache_begin_read_operation(&cres, cookie);
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);
247 *_data_first = data_start / PAGE_SIZE;
248 *_data_nr_pages = len / PAGE_SIZE;
251 fscache_end_operation(&cres);