cachefiles: Calculate the blockshift in terms of bytes, not pages
[linux-2.6-microblaze.git] / fs / cachefiles / cache.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Manage high-level VFS aspects of a cache.
3  *
4  * Copyright (C) 2007, 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/slab.h>
9 #include <linux/statfs.h>
10 #include <linux/namei.h>
11 #include "internal.h"
12
13 /*
14  * Bring a cache online.
15  */
16 int cachefiles_add_cache(struct cachefiles_cache *cache)
17 {
18         struct fscache_cache *cache_cookie;
19         struct path path;
20         struct kstatfs stats;
21         struct dentry *graveyard, *cachedir, *root;
22         const struct cred *saved_cred;
23         int ret;
24
25         _enter("");
26
27         cache_cookie = fscache_acquire_cache(cache->tag);
28         if (IS_ERR(cache_cookie))
29                 return PTR_ERR(cache_cookie);
30
31         /* we want to work under the module's security ID */
32         ret = cachefiles_get_security_ID(cache);
33         if (ret < 0)
34                 goto error_getsec;
35
36         cachefiles_begin_secure(cache, &saved_cred);
37
38         /* look up the directory at the root of the cache */
39         ret = kern_path(cache->rootdirname, LOOKUP_DIRECTORY, &path);
40         if (ret < 0)
41                 goto error_open_root;
42
43         cache->mnt = path.mnt;
44         root = path.dentry;
45
46         ret = -EINVAL;
47         if (is_idmapped_mnt(path.mnt)) {
48                 pr_warn("File cache on idmapped mounts not supported");
49                 goto error_unsupported;
50         }
51
52         /* check parameters */
53         ret = -EOPNOTSUPP;
54         if (d_is_negative(root) ||
55             !d_backing_inode(root)->i_op->lookup ||
56             !d_backing_inode(root)->i_op->mkdir ||
57             !(d_backing_inode(root)->i_opflags & IOP_XATTR) ||
58             !root->d_sb->s_op->statfs ||
59             !root->d_sb->s_op->sync_fs ||
60             root->d_sb->s_blocksize > PAGE_SIZE)
61                 goto error_unsupported;
62
63         ret = -EROFS;
64         if (sb_rdonly(root->d_sb))
65                 goto error_unsupported;
66
67         /* determine the security of the on-disk cache as this governs
68          * security ID of files we create */
69         ret = cachefiles_determine_cache_security(cache, root, &saved_cred);
70         if (ret < 0)
71                 goto error_unsupported;
72
73         /* get the cache size and blocksize */
74         ret = vfs_statfs(&path, &stats);
75         if (ret < 0)
76                 goto error_unsupported;
77
78         ret = -ERANGE;
79         if (stats.f_bsize <= 0)
80                 goto error_unsupported;
81
82         ret = -EOPNOTSUPP;
83         if (stats.f_bsize > PAGE_SIZE)
84                 goto error_unsupported;
85
86         cache->bsize = stats.f_bsize;
87         cache->bshift = ilog2(stats.f_bsize);
88
89         _debug("blksize %u (shift %u)",
90                cache->bsize, cache->bshift);
91
92         _debug("size %llu, avail %llu",
93                (unsigned long long) stats.f_blocks,
94                (unsigned long long) stats.f_bavail);
95
96         /* set up caching limits */
97         do_div(stats.f_files, 100);
98         cache->fstop = stats.f_files * cache->fstop_percent;
99         cache->fcull = stats.f_files * cache->fcull_percent;
100         cache->frun  = stats.f_files * cache->frun_percent;
101
102         _debug("limits {%llu,%llu,%llu} files",
103                (unsigned long long) cache->frun,
104                (unsigned long long) cache->fcull,
105                (unsigned long long) cache->fstop);
106
107         do_div(stats.f_blocks, 100);
108         cache->bstop = stats.f_blocks * cache->bstop_percent;
109         cache->bcull = stats.f_blocks * cache->bcull_percent;
110         cache->brun  = stats.f_blocks * cache->brun_percent;
111
112         _debug("limits {%llu,%llu,%llu} blocks",
113                (unsigned long long) cache->brun,
114                (unsigned long long) cache->bcull,
115                (unsigned long long) cache->bstop);
116
117         /* get the cache directory and check its type */
118         cachedir = cachefiles_get_directory(cache, root, "cache", NULL);
119         if (IS_ERR(cachedir)) {
120                 ret = PTR_ERR(cachedir);
121                 goto error_unsupported;
122         }
123
124         cache->store = cachedir;
125
126         /* get the graveyard directory */
127         graveyard = cachefiles_get_directory(cache, root, "graveyard", NULL);
128         if (IS_ERR(graveyard)) {
129                 ret = PTR_ERR(graveyard);
130                 goto error_unsupported;
131         }
132
133         cache->graveyard = graveyard;
134         cache->cache = cache_cookie;
135
136         ret = fscache_add_cache(cache_cookie, &cachefiles_cache_ops, cache);
137         if (ret < 0)
138                 goto error_add_cache;
139
140         /* done */
141         set_bit(CACHEFILES_READY, &cache->flags);
142         dput(root);
143
144         pr_info("File cache on %s registered\n", cache_cookie->name);
145
146         /* check how much space the cache has */
147         cachefiles_has_space(cache, 0, 0, cachefiles_has_space_check);
148         cachefiles_end_secure(cache, saved_cred);
149         _leave(" = 0 [%px]", cache->cache);
150         return 0;
151
152 error_add_cache:
153         cachefiles_put_directory(cache->graveyard);
154         cache->graveyard = NULL;
155 error_unsupported:
156         cachefiles_put_directory(cache->store);
157         cache->store = NULL;
158         mntput(cache->mnt);
159         cache->mnt = NULL;
160         dput(root);
161 error_open_root:
162         cachefiles_end_secure(cache, saved_cred);
163 error_getsec:
164         fscache_relinquish_cache(cache_cookie);
165         cache->cache = NULL;
166         pr_err("Failed to register: %d\n", ret);
167         return ret;
168 }
169
170 /*
171  * See if we have space for a number of pages and/or a number of files in the
172  * cache
173  */
174 int cachefiles_has_space(struct cachefiles_cache *cache,
175                          unsigned fnr, unsigned bnr,
176                          enum cachefiles_has_space_for reason)
177 {
178         struct kstatfs stats;
179         u64 b_avail, b_writing;
180         int ret;
181
182         struct path path = {
183                 .mnt    = cache->mnt,
184                 .dentry = cache->mnt->mnt_root,
185         };
186
187         //_enter("{%llu,%llu,%llu,%llu,%llu,%llu},%u,%u",
188         //       (unsigned long long) cache->frun,
189         //       (unsigned long long) cache->fcull,
190         //       (unsigned long long) cache->fstop,
191         //       (unsigned long long) cache->brun,
192         //       (unsigned long long) cache->bcull,
193         //       (unsigned long long) cache->bstop,
194         //       fnr, bnr);
195
196         /* find out how many pages of blockdev are available */
197         memset(&stats, 0, sizeof(stats));
198
199         ret = vfs_statfs(&path, &stats);
200         if (ret < 0) {
201                 trace_cachefiles_vfs_error(NULL, d_inode(path.dentry), ret,
202                                            cachefiles_trace_statfs_error);
203                 if (ret == -EIO)
204                         cachefiles_io_error(cache, "statfs failed");
205                 _leave(" = %d", ret);
206                 return ret;
207         }
208
209         b_avail = stats.f_bavail;
210         b_writing = atomic_long_read(&cache->b_writing);
211         if (b_avail > b_writing)
212                 b_avail -= b_writing;
213         else
214                 b_avail = 0;
215
216         //_debug("avail %llu,%llu",
217         //       (unsigned long long)stats.f_ffree,
218         //       (unsigned long long)b_avail);
219
220         /* see if there is sufficient space */
221         if (stats.f_ffree > fnr)
222                 stats.f_ffree -= fnr;
223         else
224                 stats.f_ffree = 0;
225
226         if (b_avail > bnr)
227                 b_avail -= bnr;
228         else
229                 b_avail = 0;
230
231         ret = -ENOBUFS;
232         if (stats.f_ffree < cache->fstop ||
233             b_avail < cache->bstop)
234                 goto stop_and_begin_cull;
235
236         ret = 0;
237         if (stats.f_ffree < cache->fcull ||
238             b_avail < cache->bcull)
239                 goto begin_cull;
240
241         if (test_bit(CACHEFILES_CULLING, &cache->flags) &&
242             stats.f_ffree >= cache->frun &&
243             b_avail >= cache->brun &&
244             test_and_clear_bit(CACHEFILES_CULLING, &cache->flags)
245             ) {
246                 _debug("cease culling");
247                 cachefiles_state_changed(cache);
248         }
249
250         //_leave(" = 0");
251         return 0;
252
253 stop_and_begin_cull:
254         switch (reason) {
255         case cachefiles_has_space_for_write:
256                 fscache_count_no_write_space();
257                 break;
258         case cachefiles_has_space_for_create:
259                 fscache_count_no_create_space();
260                 break;
261         default:
262                 break;
263         }
264 begin_cull:
265         if (!test_and_set_bit(CACHEFILES_CULLING, &cache->flags)) {
266                 _debug("### CULL CACHE ###");
267                 cachefiles_state_changed(cache);
268         }
269
270         _leave(" = %d", ret);
271         return ret;
272 }
273
274 /*
275  * Mark all the objects as being out of service and queue them all for cleanup.
276  */
277 static void cachefiles_withdraw_objects(struct cachefiles_cache *cache)
278 {
279         struct cachefiles_object *object;
280         unsigned int count = 0;
281
282         _enter("");
283
284         spin_lock(&cache->object_list_lock);
285
286         while (!list_empty(&cache->object_list)) {
287                 object = list_first_entry(&cache->object_list,
288                                           struct cachefiles_object, cache_link);
289                 cachefiles_see_object(object, cachefiles_obj_see_withdrawal);
290                 list_del_init(&object->cache_link);
291                 fscache_withdraw_cookie(object->cookie);
292                 count++;
293                 if ((count & 63) == 0) {
294                         spin_unlock(&cache->object_list_lock);
295                         cond_resched();
296                         spin_lock(&cache->object_list_lock);
297                 }
298         }
299
300         spin_unlock(&cache->object_list_lock);
301         _leave(" [%u objs]", count);
302 }
303
304 /*
305  * Withdraw volumes.
306  */
307 static void cachefiles_withdraw_volumes(struct cachefiles_cache *cache)
308 {
309         _enter("");
310
311         for (;;) {
312                 struct cachefiles_volume *volume = NULL;
313
314                 spin_lock(&cache->object_list_lock);
315                 if (!list_empty(&cache->volumes)) {
316                         volume = list_first_entry(&cache->volumes,
317                                                   struct cachefiles_volume, cache_link);
318                         list_del_init(&volume->cache_link);
319                 }
320                 spin_unlock(&cache->object_list_lock);
321                 if (!volume)
322                         break;
323
324                 cachefiles_withdraw_volume(volume);
325         }
326
327         _leave("");
328 }
329
330 /*
331  * Sync a cache to backing disk.
332  */
333 static void cachefiles_sync_cache(struct cachefiles_cache *cache)
334 {
335         const struct cred *saved_cred;
336         int ret;
337
338         _enter("%s", cache->cache->name);
339
340         /* make sure all pages pinned by operations on behalf of the netfs are
341          * written to disc */
342         cachefiles_begin_secure(cache, &saved_cred);
343         down_read(&cache->mnt->mnt_sb->s_umount);
344         ret = sync_filesystem(cache->mnt->mnt_sb);
345         up_read(&cache->mnt->mnt_sb->s_umount);
346         cachefiles_end_secure(cache, saved_cred);
347
348         if (ret == -EIO)
349                 cachefiles_io_error(cache,
350                                     "Attempt to sync backing fs superblock returned error %d",
351                                     ret);
352 }
353
354 /*
355  * Withdraw cache objects.
356  */
357 void cachefiles_withdraw_cache(struct cachefiles_cache *cache)
358 {
359         struct fscache_cache *fscache = cache->cache;
360
361         pr_info("File cache on %s unregistering\n", fscache->name);
362
363         fscache_withdraw_cache(fscache);
364
365         /* we now have to destroy all the active objects pertaining to this
366          * cache - which we do by passing them off to thread pool to be
367          * disposed of */
368         cachefiles_withdraw_objects(cache);
369         fscache_wait_for_objects(fscache);
370
371         cachefiles_withdraw_volumes(cache);
372         cachefiles_sync_cache(cache);
373         cache->cache = NULL;
374         fscache_relinquish_cache(fscache);
375 }