GNU Linux-libre 6.1.86-gnu
[releases.git] / fs / smb / client / fscache.c
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[1];
163         int ret;
164
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);
170
171         ret = fscache_begin_read_operation(&cres, cookie);
172         if (ret < 0)
173                 return ret;
174
175         ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
176                            NULL, NULL);
177         fscache_end_operation(&cres);
178         return ret;
179 }
180
181 /*
182  * Fallback page writing interface.
183  */
184 static int fscache_fallback_write_page(struct inode *inode, struct page *page,
185                                        bool no_space_allocated_yet)
186 {
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;
193         int ret;
194
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);
200
201         ret = fscache_begin_write_operation(&cres, cookie);
202         if (ret < 0)
203                 return ret;
204
205         ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
206                                       no_space_allocated_yet);
207         if (ret == 0)
208                 ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
209         fscache_end_operation(&cres);
210         return ret;
211 }
212
213 /*
214  * Retrieve a page from FS-Cache
215  */
216 int __cifs_readpage_from_fscache(struct inode *inode, struct page *page)
217 {
218         int ret;
219
220         cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n",
221                  __func__, cifs_inode_cookie(inode), page, inode);
222
223         ret = fscache_fallback_read_page(inode, page);
224         if (ret < 0)
225                 return ret;
226
227         /* Read completed synchronously */
228         SetPageUptodate(page);
229         return 0;
230 }
231
232 void __cifs_readpage_to_fscache(struct inode *inode, struct page *page)
233 {
234         cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n",
235                  __func__, cifs_inode_cookie(inode), page, inode);
236
237         fscache_fallback_write_page(inode, page, true);
238 }
239
240 /*
241  * Query the cache occupancy.
242  */
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)
247 {
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;
252         int ret;
253
254         ret = fscache_begin_read_operation(&cres, cookie);
255         if (ret < 0)
256                 return ret;
257
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);
262         if (ret == 0) {
263                 *_data_first = data_start / PAGE_SIZE;
264                 *_data_nr_pages = len / PAGE_SIZE;
265         }
266
267         fscache_end_operation(&cres);
268         return ret;
269 }