~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ceph/cache.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Ceph cache definitions.
  3  *
  4  *  Copyright (C) 2013 by Adfin Solutions, Inc. All Rights Reserved.
  5  *  Written by Milosz Tanski (milosz@adfin.com)
  6  *
  7  *  This program is free software; you can redistribute it and/or modify
  8  *  it under the terms of the GNU General Public License version 2
  9  *  as published by the Free Software Foundation.
 10  *
 11  *  This program is distributed in the hope that it will be useful,
 12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  *  GNU General Public License for more details.
 15  *
 16  *  You should have received a copy of the GNU General Public License
 17  *  along with this program; if not, write to:
 18  *  Free Software Foundation
 19  *  51 Franklin Street, Fifth Floor
 20  *  Boston, MA  02111-1301  USA
 21  *
 22  */
 23 
 24 #include "super.h"
 25 #include "cache.h"
 26 
 27 struct ceph_aux_inode {
 28         struct timespec mtime;
 29         loff_t          size;
 30 };
 31 
 32 struct fscache_netfs ceph_cache_netfs = {
 33         .name           = "ceph",
 34         .version        = 0,
 35 };
 36 
 37 static uint16_t ceph_fscache_session_get_key(const void *cookie_netfs_data,
 38                                              void *buffer, uint16_t maxbuf)
 39 {
 40         const struct ceph_fs_client* fsc = cookie_netfs_data;
 41         uint16_t klen;
 42 
 43         klen = sizeof(fsc->client->fsid);
 44         if (klen > maxbuf)
 45                 return 0;
 46 
 47         memcpy(buffer, &fsc->client->fsid, klen);
 48         return klen;
 49 }
 50 
 51 static const struct fscache_cookie_def ceph_fscache_fsid_object_def = {
 52         .name           = "CEPH.fsid",
 53         .type           = FSCACHE_COOKIE_TYPE_INDEX,
 54         .get_key        = ceph_fscache_session_get_key,
 55 };
 56 
 57 int ceph_fscache_register(void)
 58 {
 59         return fscache_register_netfs(&ceph_cache_netfs);
 60 }
 61 
 62 void ceph_fscache_unregister(void)
 63 {
 64         fscache_unregister_netfs(&ceph_cache_netfs);
 65 }
 66 
 67 int ceph_fscache_register_fs(struct ceph_fs_client* fsc)
 68 {
 69         fsc->fscache = fscache_acquire_cookie(ceph_cache_netfs.primary_index,
 70                                               &ceph_fscache_fsid_object_def,
 71                                               fsc, true);
 72 
 73         if (fsc->fscache == NULL) {
 74                 pr_err("Unable to resgister fsid: %p fscache cookie", fsc);
 75                 return 0;
 76         }
 77 
 78         fsc->revalidate_wq = alloc_workqueue("ceph-revalidate", 0, 1);
 79         if (fsc->revalidate_wq == NULL)
 80                 return -ENOMEM;
 81 
 82         return 0;
 83 }
 84 
 85 static uint16_t ceph_fscache_inode_get_key(const void *cookie_netfs_data,
 86                                            void *buffer, uint16_t maxbuf)
 87 {
 88         const struct ceph_inode_info* ci = cookie_netfs_data;
 89         uint16_t klen;
 90 
 91         /* use ceph virtual inode (id + snapshot) */
 92         klen = sizeof(ci->i_vino);
 93         if (klen > maxbuf)
 94                 return 0;
 95 
 96         memcpy(buffer, &ci->i_vino, klen);
 97         return klen;
 98 }
 99 
100 static uint16_t ceph_fscache_inode_get_aux(const void *cookie_netfs_data,
101                                            void *buffer, uint16_t bufmax)
102 {
103         struct ceph_aux_inode aux;
104         const struct ceph_inode_info* ci = cookie_netfs_data;
105         const struct inode* inode = &ci->vfs_inode;
106 
107         memset(&aux, 0, sizeof(aux));
108         aux.mtime = inode->i_mtime;
109         aux.size = inode->i_size;
110 
111         memcpy(buffer, &aux, sizeof(aux));
112 
113         return sizeof(aux);
114 }
115 
116 static void ceph_fscache_inode_get_attr(const void *cookie_netfs_data,
117                                         uint64_t *size)
118 {
119         const struct ceph_inode_info* ci = cookie_netfs_data;
120         const struct inode* inode = &ci->vfs_inode;
121 
122         *size = inode->i_size;
123 }
124 
125 static enum fscache_checkaux ceph_fscache_inode_check_aux(
126         void *cookie_netfs_data, const void *data, uint16_t dlen)
127 {
128         struct ceph_aux_inode aux;
129         struct ceph_inode_info* ci = cookie_netfs_data;
130         struct inode* inode = &ci->vfs_inode;
131 
132         if (dlen != sizeof(aux))
133                 return FSCACHE_CHECKAUX_OBSOLETE;
134 
135         memset(&aux, 0, sizeof(aux));
136         aux.mtime = inode->i_mtime;
137         aux.size = inode->i_size;
138 
139         if (memcmp(data, &aux, sizeof(aux)) != 0)
140                 return FSCACHE_CHECKAUX_OBSOLETE;
141 
142         dout("ceph inode 0x%p cached okay", ci);
143         return FSCACHE_CHECKAUX_OKAY;
144 }
145 
146 static void ceph_fscache_inode_now_uncached(void* cookie_netfs_data)
147 {
148         struct ceph_inode_info* ci = cookie_netfs_data;
149         struct pagevec pvec;
150         pgoff_t first;
151         int loop, nr_pages;
152 
153         pagevec_init(&pvec, 0);
154         first = 0;
155 
156         dout("ceph inode 0x%p now uncached", ci);
157 
158         while (1) {
159                 nr_pages = pagevec_lookup(&pvec, ci->vfs_inode.i_mapping, first,
160                                           PAGEVEC_SIZE - pagevec_count(&pvec));
161 
162                 if (!nr_pages)
163                         break;
164 
165                 for (loop = 0; loop < nr_pages; loop++)
166                         ClearPageFsCache(pvec.pages[loop]);
167 
168                 first = pvec.pages[nr_pages - 1]->index + 1;
169 
170                 pvec.nr = nr_pages;
171                 pagevec_release(&pvec);
172                 cond_resched();
173         }
174 }
175 
176 static const struct fscache_cookie_def ceph_fscache_inode_object_def = {
177         .name           = "CEPH.inode",
178         .type           = FSCACHE_COOKIE_TYPE_DATAFILE,
179         .get_key        = ceph_fscache_inode_get_key,
180         .get_attr       = ceph_fscache_inode_get_attr,
181         .get_aux        = ceph_fscache_inode_get_aux,
182         .check_aux      = ceph_fscache_inode_check_aux,
183         .now_uncached   = ceph_fscache_inode_now_uncached,
184 };
185 
186 void ceph_fscache_register_inode_cookie(struct ceph_fs_client* fsc,
187                                         struct ceph_inode_info* ci)
188 {
189         struct inode* inode = &ci->vfs_inode;
190 
191         /* No caching for filesystem */
192         if (fsc->fscache == NULL)
193                 return;
194 
195         /* Only cache for regular files that are read only */
196         if ((ci->vfs_inode.i_mode & S_IFREG) == 0)
197                 return;
198 
199         /* Avoid multiple racing open requests */
200         mutex_lock(&inode->i_mutex);
201 
202         if (ci->fscache)
203                 goto done;
204 
205         ci->fscache = fscache_acquire_cookie(fsc->fscache,
206                                              &ceph_fscache_inode_object_def,
207                                              ci, true);
208         fscache_check_consistency(ci->fscache);
209 done:
210         mutex_unlock(&inode->i_mutex);
211 
212 }
213 
214 void ceph_fscache_unregister_inode_cookie(struct ceph_inode_info* ci)
215 {
216         struct fscache_cookie* cookie;
217 
218         if ((cookie = ci->fscache) == NULL)
219                 return;
220 
221         ci->fscache = NULL;
222 
223         fscache_uncache_all_inode_pages(cookie, &ci->vfs_inode);
224         fscache_relinquish_cookie(cookie, 0);
225 }
226 
227 static void ceph_readpage_from_fscache_complete(struct page *page, void *data, int error)
228 {
229         if (!error)
230                 SetPageUptodate(page);
231 
232         unlock_page(page);
233 }
234 
235 static inline int cache_valid(struct ceph_inode_info *ci)
236 {
237         return ((ceph_caps_issued(ci) & CEPH_CAP_FILE_CACHE) &&
238                 (ci->i_fscache_gen == ci->i_rdcache_gen));
239 }
240 
241 
242 /* Atempt to read from the fscache,
243  *
244  * This function is called from the readpage_nounlock context. DO NOT attempt to
245  * unlock the page here (or in the callback).
246  */
247 int ceph_readpage_from_fscache(struct inode *inode, struct page *page)
248 {
249         struct ceph_inode_info *ci = ceph_inode(inode);
250         int ret;
251 
252         if (!cache_valid(ci))
253                 return -ENOBUFS;
254 
255         ret = fscache_read_or_alloc_page(ci->fscache, page,
256                                          ceph_readpage_from_fscache_complete, NULL,
257                                          GFP_KERNEL);
258 
259         switch (ret) {
260                 case 0: /* Page found */
261                         dout("page read submitted\n");
262                         return 0;
263                 case -ENOBUFS: /* Pages were not found, and can't be */
264                 case -ENODATA: /* Pages were not found */
265                         dout("page/inode not in cache\n");
266                         return ret;
267                 default:
268                         dout("%s: unknown error ret = %i\n", __func__, ret);
269                         return ret;
270         }
271 }
272 
273 int ceph_readpages_from_fscache(struct inode *inode,
274                                   struct address_space *mapping,
275                                   struct list_head *pages,
276                                   unsigned *nr_pages)
277 {
278         struct ceph_inode_info *ci = ceph_inode(inode);
279         int ret;
280 
281         if (!cache_valid(ci))
282                 return -ENOBUFS;
283 
284         ret = fscache_read_or_alloc_pages(ci->fscache, mapping, pages, nr_pages,
285                                           ceph_readpage_from_fscache_complete,
286                                           NULL, mapping_gfp_mask(mapping));
287 
288         switch (ret) {
289                 case 0: /* All pages found */
290                         dout("all-page read submitted\n");
291                         return 0;
292                 case -ENOBUFS: /* Some pages were not found, and can't be */
293                 case -ENODATA: /* some pages were not found */
294                         dout("page/inode not in cache\n");
295                         return ret;
296                 default:
297                         dout("%s: unknown error ret = %i\n", __func__, ret);
298                         return ret;
299         }
300 }
301 
302 void ceph_readpage_to_fscache(struct inode *inode, struct page *page)
303 {
304         struct ceph_inode_info *ci = ceph_inode(inode);
305         int ret;
306 
307         if (!PageFsCache(page))
308                 return;
309 
310         if (!cache_valid(ci))
311                 return;
312 
313         ret = fscache_write_page(ci->fscache, page, GFP_KERNEL);
314         if (ret)
315                  fscache_uncache_page(ci->fscache, page);
316 }
317 
318 void ceph_invalidate_fscache_page(struct inode* inode, struct page *page)
319 {
320         struct ceph_inode_info *ci = ceph_inode(inode);
321 
322         if (!PageFsCache(page))
323                 return;
324 
325         fscache_wait_on_page_write(ci->fscache, page);
326         fscache_uncache_page(ci->fscache, page);
327 }
328 
329 void ceph_fscache_unregister_fs(struct ceph_fs_client* fsc)
330 {
331         if (fsc->revalidate_wq)
332                 destroy_workqueue(fsc->revalidate_wq);
333 
334         fscache_relinquish_cookie(fsc->fscache, 0);
335         fsc->fscache = NULL;
336 }
337 
338 static void ceph_revalidate_work(struct work_struct *work)
339 {
340         int issued;
341         u32 orig_gen;
342         struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
343                                                   i_revalidate_work);
344         struct inode *inode = &ci->vfs_inode;
345 
346         spin_lock(&ci->i_ceph_lock);
347         issued = __ceph_caps_issued(ci, NULL);
348         orig_gen = ci->i_rdcache_gen;
349         spin_unlock(&ci->i_ceph_lock);
350 
351         if (!(issued & CEPH_CAP_FILE_CACHE)) {
352                 dout("revalidate_work lost cache before validation %p\n",
353                      inode);
354                 goto out;
355         }
356 
357         if (!fscache_check_consistency(ci->fscache))
358                 fscache_invalidate(ci->fscache);
359 
360         spin_lock(&ci->i_ceph_lock);
361         /* Update the new valid generation (backwards sanity check too) */
362         if (orig_gen > ci->i_fscache_gen) {
363                 ci->i_fscache_gen = orig_gen;
364         }
365         spin_unlock(&ci->i_ceph_lock);
366 
367 out:
368         iput(&ci->vfs_inode);
369 }
370 
371 void ceph_queue_revalidate(struct inode *inode)
372 {
373         struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
374         struct ceph_inode_info *ci = ceph_inode(inode);
375 
376         if (fsc->revalidate_wq == NULL || ci->fscache == NULL)
377                 return;
378 
379         ihold(inode);
380 
381         if (queue_work(ceph_sb_to_client(inode->i_sb)->revalidate_wq,
382                        &ci->i_revalidate_work)) {
383                 dout("ceph_queue_revalidate %p\n", inode);
384         } else {
385                 dout("ceph_queue_revalidate %p failed\n)", inode);
386                 iput(inode);
387         }
388 }
389 
390 void ceph_fscache_inode_init(struct ceph_inode_info *ci)
391 {
392         ci->fscache = NULL;
393         /* The first load is verifed cookie open time */
394         ci->i_fscache_gen = 1;
395         INIT_WORK(&ci->i_revalidate_work, ceph_revalidate_work);
396 }
397 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp