Commit | Line | Data |
---|---|---|
488f1d2d SJ |
1 | /* |
2 | * fs/cifs/fscache.c - CIFS filesystem cache interface | |
3 | * | |
4 | * Copyright (c) 2010 Novell, Inc. | |
b81209de | 5 | * Author(s): Suresh Jayaraman <sjayaraman@suse.de> |
488f1d2d SJ |
6 | * |
7 | * This library is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Lesser General Public License as published | |
9 | * by the Free Software Foundation; either version 2.1 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
15 | * the GNU Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | #include "fscache.h" | |
22 | #include "cifsglob.h" | |
23 | #include "cifs_debug.h" | |
9451a9a5 | 24 | #include "cifs_fs_sb.h" |
488f1d2d | 25 | |
402cb8dd DH |
26 | /* |
27 | * Key layout of CIFS server cache index object | |
28 | */ | |
29 | struct cifs_server_key { | |
30 | struct { | |
31 | uint16_t family; /* address family */ | |
32 | __be16 port; /* IP port */ | |
33 | } hdr; | |
34 | union { | |
35 | struct in_addr ipv4_addr; | |
36 | struct in6_addr ipv6_addr; | |
37 | }; | |
38 | } __packed; | |
39 | ||
40 | /* | |
41 | * Get a cookie for a server object keyed by {IPaddress,port,family} tuple | |
42 | */ | |
488f1d2d SJ |
43 | void cifs_fscache_get_client_cookie(struct TCP_Server_Info *server) |
44 | { | |
402cb8dd DH |
45 | const struct sockaddr *sa = (struct sockaddr *) &server->dstaddr; |
46 | const struct sockaddr_in *addr = (struct sockaddr_in *) sa; | |
47 | const struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) sa; | |
48 | struct cifs_server_key key; | |
49 | uint16_t key_len = sizeof(key.hdr); | |
50 | ||
51 | memset(&key, 0, sizeof(key)); | |
52 | ||
53 | /* | |
54 | * Should not be a problem as sin_family/sin6_family overlays | |
55 | * sa_family field | |
56 | */ | |
57 | key.hdr.family = sa->sa_family; | |
58 | switch (sa->sa_family) { | |
59 | case AF_INET: | |
60 | key.hdr.port = addr->sin_port; | |
61 | key.ipv4_addr = addr->sin_addr; | |
62 | key_len += sizeof(key.ipv4_addr); | |
63 | break; | |
64 | ||
65 | case AF_INET6: | |
66 | key.hdr.port = addr6->sin6_port; | |
67 | key.ipv6_addr = addr6->sin6_addr; | |
68 | key_len += sizeof(key.ipv6_addr); | |
69 | break; | |
70 | ||
71 | default: | |
72 | cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family); | |
73 | server->fscache = NULL; | |
74 | return; | |
75 | } | |
76 | ||
488f1d2d SJ |
77 | server->fscache = |
78 | fscache_acquire_cookie(cifs_fscache_netfs.primary_index, | |
402cb8dd DH |
79 | &cifs_fscache_server_index_def, |
80 | &key, key_len, | |
81 | NULL, 0, | |
ee1235a9 | 82 | server, 0, true); |
f96637be JP |
83 | cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", |
84 | __func__, server, server->fscache); | |
488f1d2d SJ |
85 | } |
86 | ||
87 | void cifs_fscache_release_client_cookie(struct TCP_Server_Info *server) | |
88 | { | |
f96637be JP |
89 | cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", |
90 | __func__, server, server->fscache); | |
402cb8dd | 91 | fscache_relinquish_cookie(server->fscache, NULL, false); |
488f1d2d SJ |
92 | server->fscache = NULL; |
93 | } | |
94 | ||
96daf2b0 | 95 | void cifs_fscache_get_super_cookie(struct cifs_tcon *tcon) |
d03382ce SJ |
96 | { |
97 | struct TCP_Server_Info *server = tcon->ses->server; | |
402cb8dd DH |
98 | char *sharename; |
99 | ||
100 | sharename = extract_sharename(tcon->treeName); | |
101 | if (IS_ERR(sharename)) { | |
102 | cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__); | |
103 | tcon->fscache = NULL; | |
104 | return; | |
105 | } | |
d03382ce SJ |
106 | |
107 | tcon->fscache = | |
108 | fscache_acquire_cookie(server->fscache, | |
402cb8dd DH |
109 | &cifs_fscache_super_index_def, |
110 | sharename, strlen(sharename), | |
111 | &tcon->resource_id, sizeof(tcon->resource_id), | |
ee1235a9 | 112 | tcon, 0, true); |
402cb8dd | 113 | kfree(sharename); |
f96637be JP |
114 | cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", |
115 | __func__, server->fscache, tcon->fscache); | |
d03382ce SJ |
116 | } |
117 | ||
96daf2b0 | 118 | void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon) |
d03382ce | 119 | { |
f96637be | 120 | cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache); |
402cb8dd | 121 | fscache_relinquish_cookie(tcon->fscache, &tcon->resource_id, false); |
d03382ce SJ |
122 | tcon->fscache = NULL; |
123 | } | |
9451a9a5 | 124 | |
402cb8dd DH |
125 | static void cifs_fscache_acquire_inode_cookie(struct cifsInodeInfo *cifsi, |
126 | struct cifs_tcon *tcon) | |
127 | { | |
128 | struct cifs_fscache_inode_auxdata auxdata; | |
129 | ||
130 | memset(&auxdata, 0, sizeof(auxdata)); | |
131 | auxdata.eof = cifsi->server_eof; | |
132 | auxdata.last_write_time = cifsi->vfs_inode.i_mtime; | |
133 | auxdata.last_change_time = cifsi->vfs_inode.i_ctime; | |
134 | ||
135 | cifsi->fscache = | |
136 | fscache_acquire_cookie(tcon->fscache, | |
137 | &cifs_fscache_inode_object_def, | |
138 | &cifsi->uniqueid, sizeof(cifsi->uniqueid), | |
139 | &auxdata, sizeof(auxdata), | |
ee1235a9 | 140 | cifsi, cifsi->vfs_inode.i_size, true); |
402cb8dd DH |
141 | } |
142 | ||
9451a9a5 SJ |
143 | static void cifs_fscache_enable_inode_cookie(struct inode *inode) |
144 | { | |
145 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
146 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
96daf2b0 | 147 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
9451a9a5 SJ |
148 | |
149 | if (cifsi->fscache) | |
150 | return; | |
151 | ||
402cb8dd DH |
152 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)) |
153 | return; | |
154 | ||
155 | cifs_fscache_acquire_inode_cookie(cifsi, tcon); | |
156 | ||
157 | cifs_dbg(FYI, "%s: got FH cookie (0x%p/0x%p)\n", | |
158 | __func__, tcon->fscache, cifsi->fscache); | |
9451a9a5 SJ |
159 | } |
160 | ||
161 | void cifs_fscache_release_inode_cookie(struct inode *inode) | |
162 | { | |
402cb8dd | 163 | struct cifs_fscache_inode_auxdata auxdata; |
9451a9a5 SJ |
164 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
165 | ||
166 | if (cifsi->fscache) { | |
402cb8dd DH |
167 | memset(&auxdata, 0, sizeof(auxdata)); |
168 | auxdata.eof = cifsi->server_eof; | |
169 | auxdata.last_write_time = cifsi->vfs_inode.i_mtime; | |
170 | auxdata.last_change_time = cifsi->vfs_inode.i_ctime; | |
171 | ||
f96637be | 172 | cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache); |
402cb8dd | 173 | fscache_relinquish_cookie(cifsi->fscache, &auxdata, false); |
9451a9a5 SJ |
174 | cifsi->fscache = NULL; |
175 | } | |
176 | } | |
177 | ||
178 | static void cifs_fscache_disable_inode_cookie(struct inode *inode) | |
179 | { | |
180 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
181 | ||
182 | if (cifsi->fscache) { | |
f96637be | 183 | cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cifsi->fscache); |
c902ce1b | 184 | fscache_uncache_all_inode_pages(cifsi->fscache, inode); |
402cb8dd | 185 | fscache_relinquish_cookie(cifsi->fscache, NULL, true); |
9451a9a5 SJ |
186 | cifsi->fscache = NULL; |
187 | } | |
188 | } | |
189 | ||
190 | void cifs_fscache_set_inode_cookie(struct inode *inode, struct file *filp) | |
191 | { | |
192 | if ((filp->f_flags & O_ACCMODE) != O_RDONLY) | |
193 | cifs_fscache_disable_inode_cookie(inode); | |
b81209de | 194 | else |
9451a9a5 | 195 | cifs_fscache_enable_inode_cookie(inode); |
9451a9a5 SJ |
196 | } |
197 | ||
198 | void cifs_fscache_reset_inode_cookie(struct inode *inode) | |
199 | { | |
200 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
201 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
402cb8dd | 202 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
9451a9a5 SJ |
203 | struct fscache_cookie *old = cifsi->fscache; |
204 | ||
205 | if (cifsi->fscache) { | |
206 | /* retire the current fscache cache and get a new one */ | |
402cb8dd | 207 | fscache_relinquish_cookie(cifsi->fscache, NULL, true); |
9451a9a5 | 208 | |
402cb8dd | 209 | cifs_fscache_acquire_inode_cookie(cifsi, tcon); |
f96637be JP |
210 | cifs_dbg(FYI, "%s: new cookie 0x%p oldcookie 0x%p\n", |
211 | __func__, cifsi->fscache, old); | |
9451a9a5 SJ |
212 | } |
213 | } | |
85f2d6b4 SJ |
214 | |
215 | int cifs_fscache_release_page(struct page *page, gfp_t gfp) | |
216 | { | |
217 | if (PageFsCache(page)) { | |
218 | struct inode *inode = page->mapping->host; | |
219 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
220 | ||
f96637be JP |
221 | cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", |
222 | __func__, page, cifsi->fscache); | |
85f2d6b4 SJ |
223 | if (!fscache_maybe_release_page(cifsi->fscache, page, gfp)) |
224 | return 0; | |
225 | } | |
226 | ||
227 | return 1; | |
228 | } | |
229 | ||
56698236 SJ |
230 | static void cifs_readpage_from_fscache_complete(struct page *page, void *ctx, |
231 | int error) | |
232 | { | |
f96637be | 233 | cifs_dbg(FYI, "%s: (0x%p/%d)\n", __func__, page, error); |
56698236 SJ |
234 | if (!error) |
235 | SetPageUptodate(page); | |
236 | unlock_page(page); | |
237 | } | |
238 | ||
239 | /* | |
240 | * Retrieve a page from FS-Cache | |
241 | */ | |
242 | int __cifs_readpage_from_fscache(struct inode *inode, struct page *page) | |
243 | { | |
244 | int ret; | |
245 | ||
f96637be JP |
246 | cifs_dbg(FYI, "%s: (fsc:%p, p:%p, i:0x%p\n", |
247 | __func__, CIFS_I(inode)->fscache, page, inode); | |
56698236 SJ |
248 | ret = fscache_read_or_alloc_page(CIFS_I(inode)->fscache, page, |
249 | cifs_readpage_from_fscache_complete, | |
250 | NULL, | |
251 | GFP_KERNEL); | |
252 | switch (ret) { | |
253 | ||
254 | case 0: /* page found in fscache, read submitted */ | |
f96637be | 255 | cifs_dbg(FYI, "%s: submitted\n", __func__); |
56698236 SJ |
256 | return ret; |
257 | case -ENOBUFS: /* page won't be cached */ | |
258 | case -ENODATA: /* page not in cache */ | |
f96637be | 259 | cifs_dbg(FYI, "%s: %d\n", __func__, ret); |
56698236 SJ |
260 | return 1; |
261 | ||
262 | default: | |
f96637be | 263 | cifs_dbg(VFS, "unknown error ret = %d\n", ret); |
56698236 SJ |
264 | } |
265 | return ret; | |
266 | } | |
267 | ||
268 | /* | |
269 | * Retrieve a set of pages from FS-Cache | |
270 | */ | |
271 | int __cifs_readpages_from_fscache(struct inode *inode, | |
272 | struct address_space *mapping, | |
273 | struct list_head *pages, | |
274 | unsigned *nr_pages) | |
275 | { | |
276 | int ret; | |
277 | ||
f96637be JP |
278 | cifs_dbg(FYI, "%s: (0x%p/%u/0x%p)\n", |
279 | __func__, CIFS_I(inode)->fscache, *nr_pages, inode); | |
56698236 SJ |
280 | ret = fscache_read_or_alloc_pages(CIFS_I(inode)->fscache, mapping, |
281 | pages, nr_pages, | |
282 | cifs_readpage_from_fscache_complete, | |
283 | NULL, | |
284 | mapping_gfp_mask(mapping)); | |
285 | switch (ret) { | |
286 | case 0: /* read submitted to the cache for all pages */ | |
f96637be | 287 | cifs_dbg(FYI, "%s: submitted\n", __func__); |
56698236 SJ |
288 | return ret; |
289 | ||
290 | case -ENOBUFS: /* some pages are not cached and can't be */ | |
291 | case -ENODATA: /* some pages are not cached */ | |
f96637be | 292 | cifs_dbg(FYI, "%s: no page\n", __func__); |
56698236 SJ |
293 | return 1; |
294 | ||
295 | default: | |
f96637be | 296 | cifs_dbg(FYI, "unknown error ret = %d\n", ret); |
56698236 SJ |
297 | } |
298 | ||
299 | return ret; | |
300 | } | |
301 | ||
9dc06558 SJ |
302 | void __cifs_readpage_to_fscache(struct inode *inode, struct page *page) |
303 | { | |
ee1235a9 | 304 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
9dc06558 SJ |
305 | int ret; |
306 | ||
f96637be | 307 | cifs_dbg(FYI, "%s: (fsc: %p, p: %p, i: %p)\n", |
ee1235a9 DH |
308 | __func__, cifsi->fscache, page, inode); |
309 | ret = fscache_write_page(cifsi->fscache, page, | |
310 | cifsi->vfs_inode.i_size, GFP_KERNEL); | |
9dc06558 | 311 | if (ret != 0) |
ee1235a9 | 312 | fscache_uncache_page(cifsi->fscache, page); |
9dc06558 SJ |
313 | } |
314 | ||
54afa990 DH |
315 | void __cifs_fscache_readpages_cancel(struct inode *inode, struct list_head *pages) |
316 | { | |
317 | cifs_dbg(FYI, "%s: (fsc: %p, i: %p)\n", | |
318 | __func__, CIFS_I(inode)->fscache, inode); | |
319 | fscache_readpages_cancel(CIFS_I(inode)->fscache, pages); | |
320 | } | |
321 | ||
85f2d6b4 SJ |
322 | void __cifs_fscache_invalidate_page(struct page *page, struct inode *inode) |
323 | { | |
324 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
325 | struct fscache_cookie *cookie = cifsi->fscache; | |
326 | ||
f96637be | 327 | cifs_dbg(FYI, "%s: (0x%p/0x%p)\n", __func__, page, cookie); |
85f2d6b4 SJ |
328 | fscache_wait_on_page_write(cookie, page); |
329 | fscache_uncache_page(cookie, page); | |
330 | } |