^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * V9FS cache definitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 by Abhishek Kulkarni <adkulkar@umail.iu.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/9p/9p.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "v9fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define CACHETAG_LEN 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct fscache_netfs v9fs_cache_netfs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .name = "9p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .version = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * v9fs_random_cachetag - Generate a random tag to be associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * with a new cache session.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * The value of jiffies is used for a fairly randomly cache tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int v9fs_random_cachetag(struct v9fs_session_info *v9ses)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!v9ses->cachetag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const struct fscache_cookie_def v9fs_cache_session_index_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .name = "9P.session",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .type = FSCACHE_COOKIE_TYPE_INDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* If no cache session tag was specified, we generate a random one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!v9ses->cachetag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (v9fs_random_cachetag(v9ses) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) v9ses->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) kfree(v9ses->cachetag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) v9ses->cachetag = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) &v9fs_cache_session_index_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) v9ses->cachetag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) strlen(v9ses->cachetag),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) v9ses, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) v9ses, v9ses->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) v9ses, v9ses->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fscache_relinquish_cookie(v9ses->fscache, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) v9ses->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static enum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) const void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) uint16_t buflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) loff_t object_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) const struct v9fs_inode *v9inode = cookie_netfs_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (buflen != sizeof(v9inode->qid.version))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (memcmp(buffer, &v9inode->qid.version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sizeof(v9inode->qid.version)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return FSCACHE_CHECKAUX_OBSOLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return FSCACHE_CHECKAUX_OKAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) const struct fscache_cookie_def v9fs_cache_inode_index_def = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .name = "9p.inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .type = FSCACHE_COOKIE_TYPE_DATAFILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .check_aux = v9fs_cache_inode_check_aux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void v9fs_cache_inode_get_cookie(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct v9fs_inode *v9inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct v9fs_session_info *v9ses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) v9ses = v9fs_inode2v9ses(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) &v9fs_cache_inode_index_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) &v9inode->qid.path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) sizeof(v9inode->qid.path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) &v9inode->qid.version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) sizeof(v9inode->qid.version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) v9inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) i_size_read(&v9inode->vfs_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) inode, v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void v9fs_cache_inode_put_cookie(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) inode, v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) v9inode->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void v9fs_cache_inode_flush_cookie(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) inode, v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fscache_relinquish_cookie(v9inode->fscache, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) v9inode->fscache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) mutex_lock(&v9inode->fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) v9fs_cache_inode_flush_cookie(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) v9fs_cache_inode_get_cookie(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_unlock(&v9inode->fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void v9fs_cache_inode_reset_cookie(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct v9fs_session_info *v9ses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct fscache_cookie *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) old = v9inode->fscache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) mutex_lock(&v9inode->fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) fscache_relinquish_cookie(v9inode->fscache, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) v9ses = v9fs_inode2v9ses(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) v9inode->fscache = fscache_acquire_cookie(v9ses->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) &v9fs_cache_inode_index_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) &v9inode->qid.path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) sizeof(v9inode->qid.path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) &v9inode->qid.version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sizeof(v9inode->qid.version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) v9inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) i_size_read(&v9inode->vfs_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) inode, old, v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) mutex_unlock(&v9inode->fscache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int __v9fs_fscache_release_page(struct page *page, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) BUG_ON(!v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return fscache_maybe_release_page(v9inode->fscache, page, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) void __v9fs_fscache_invalidate_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) BUG_ON(!v9inode->fscache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (PageFsCache(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) fscache_wait_on_page_write(v9inode->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) fscache_uncache_page(v9inode->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void v9fs_vfs_readpage_complete(struct page *page, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * __v9fs_readpage_from_fscache - read a page from cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Returns 0 if the pages are in cache and a BIO is submitted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * 1 if the pages are not in cache and -error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int __v9fs_readpage_from_fscache(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = fscache_read_or_alloc_page(v9inode->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) v9fs_vfs_readpage_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case -ENOBUFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case -ENODATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) p9_debug(P9_DEBUG_FSC, "page/inode not in cache %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * __v9fs_readpages_from_fscache - read multiple pages from cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * Returns 0 if the pages are in cache and a BIO is submitted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * 1 if the pages are not in cache and -error otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int __v9fs_readpages_from_fscache(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct list_head *pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned *nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) const struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) p9_debug(P9_DEBUG_FSC, "inode %p pages %u\n", inode, *nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!v9inode->fscache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) ret = fscache_read_or_alloc_pages(v9inode->fscache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mapping, pages, nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) v9fs_vfs_readpage_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) mapping_gfp_mask(mapping));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) case -ENOBUFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) case -ENODATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) p9_debug(P9_DEBUG_FSC, "pages/inodes not in cache %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) BUG_ON(!list_empty(pages));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) BUG_ON(*nr_pages != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) p9_debug(P9_DEBUG_FSC, "BIO submitted\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) p9_debug(P9_DEBUG_FSC, "ret %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * __v9fs_readpage_to_fscache - write a page to the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) void __v9fs_readpage_to_fscache(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) const struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ret = fscache_write_page(v9inode->fscache, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) i_size_read(&v9inode->vfs_inode), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) p9_debug(P9_DEBUG_FSC, "ret = %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) v9fs_uncache_page(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * wait for a page to complete writing to the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) void __v9fs_fscache_wait_on_page_write(struct inode *inode, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) const struct v9fs_inode *v9inode = V9FS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) p9_debug(P9_DEBUG_FSC, "inode %p page %p\n", inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (PageFsCache(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) fscache_wait_on_page_write(v9inode->fscache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }