0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/init.h>
0009 #include <linux/kernel.h>
0010 #include <linux/sched.h>
0011 #include <linux/mm.h>
0012 #include <linux/nfs_fs.h>
0013 #include <linux/nfs_fs_sb.h>
0014 #include <linux/in6.h>
0015 #include <linux/seq_file.h>
0016 #include <linux/slab.h>
0017 #include <linux/iversion.h>
0018
0019 #include "internal.h"
0020 #include "iostat.h"
0021 #include "fscache.h"
0022 #include "nfstrace.h"
0023
0024 #define NFS_MAX_KEY_LEN 1000
0025
0026 static bool nfs_append_int(char *key, int *_len, unsigned long long x)
0027 {
0028 if (*_len > NFS_MAX_KEY_LEN)
0029 return false;
0030 if (x == 0)
0031 key[(*_len)++] = ',';
0032 else
0033 *_len += sprintf(key + *_len, ",%llx", x);
0034 return true;
0035 }
0036
0037
0038
0039
0040
0041
0042
0043 static bool nfs_fscache_get_client_key(struct nfs_client *clp,
0044 char *key, int *_len)
0045 {
0046 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
0047 const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
0048
0049 *_len += snprintf(key + *_len, NFS_MAX_KEY_LEN - *_len,
0050 ",%u.%u,%x",
0051 clp->rpc_ops->version,
0052 clp->cl_minorversion,
0053 clp->cl_addr.ss_family);
0054
0055 switch (clp->cl_addr.ss_family) {
0056 case AF_INET:
0057 if (!nfs_append_int(key, _len, sin->sin_port) ||
0058 !nfs_append_int(key, _len, sin->sin_addr.s_addr))
0059 return false;
0060 return true;
0061
0062 case AF_INET6:
0063 if (!nfs_append_int(key, _len, sin6->sin6_port) ||
0064 !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[0]) ||
0065 !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[1]) ||
0066 !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[2]) ||
0067 !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[3]))
0068 return false;
0069 return true;
0070
0071 default:
0072 printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
0073 clp->cl_addr.ss_family);
0074 return false;
0075 }
0076 }
0077
0078
0079
0080
0081
0082
0083
0084
0085 int nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
0086 {
0087 struct fscache_volume *vcookie;
0088 struct nfs_server *nfss = NFS_SB(sb);
0089 unsigned int len = 3;
0090 char *key;
0091
0092 if (uniq) {
0093 nfss->fscache_uniq = kmemdup_nul(uniq, ulen, GFP_KERNEL);
0094 if (!nfss->fscache_uniq)
0095 return -ENOMEM;
0096 }
0097
0098 key = kmalloc(NFS_MAX_KEY_LEN + 24, GFP_KERNEL);
0099 if (!key)
0100 return -ENOMEM;
0101
0102 memcpy(key, "nfs", 3);
0103 if (!nfs_fscache_get_client_key(nfss->nfs_client, key, &len) ||
0104 !nfs_append_int(key, &len, nfss->fsid.major) ||
0105 !nfs_append_int(key, &len, nfss->fsid.minor) ||
0106 !nfs_append_int(key, &len, sb->s_flags & NFS_SB_MASK) ||
0107 !nfs_append_int(key, &len, nfss->flags) ||
0108 !nfs_append_int(key, &len, nfss->rsize) ||
0109 !nfs_append_int(key, &len, nfss->wsize) ||
0110 !nfs_append_int(key, &len, nfss->acregmin) ||
0111 !nfs_append_int(key, &len, nfss->acregmax) ||
0112 !nfs_append_int(key, &len, nfss->acdirmin) ||
0113 !nfs_append_int(key, &len, nfss->acdirmax) ||
0114 !nfs_append_int(key, &len, nfss->client->cl_auth->au_flavor))
0115 goto out;
0116
0117 if (ulen > 0) {
0118 if (ulen > NFS_MAX_KEY_LEN - len)
0119 goto out;
0120 key[len++] = ',';
0121 memcpy(key + len, uniq, ulen);
0122 len += ulen;
0123 }
0124 key[len] = 0;
0125
0126
0127 vcookie = fscache_acquire_volume(key,
0128 NULL,
0129 NULL, 0 );
0130 if (IS_ERR(vcookie)) {
0131 if (vcookie != ERR_PTR(-EBUSY)) {
0132 kfree(key);
0133 return PTR_ERR(vcookie);
0134 }
0135 pr_err("NFS: Cache volume key already in use (%s)\n", key);
0136 vcookie = NULL;
0137 }
0138 nfss->fscache = vcookie;
0139
0140 out:
0141 kfree(key);
0142 return 0;
0143 }
0144
0145
0146
0147
0148 void nfs_fscache_release_super_cookie(struct super_block *sb)
0149 {
0150 struct nfs_server *nfss = NFS_SB(sb);
0151
0152 fscache_relinquish_volume(nfss->fscache, NULL, false);
0153 nfss->fscache = NULL;
0154 kfree(nfss->fscache_uniq);
0155 }
0156
0157
0158
0159
0160 void nfs_fscache_init_inode(struct inode *inode)
0161 {
0162 struct nfs_fscache_inode_auxdata auxdata;
0163 struct nfs_server *nfss = NFS_SERVER(inode);
0164 struct nfs_inode *nfsi = NFS_I(inode);
0165
0166 nfsi->fscache = NULL;
0167 if (!(nfss->fscache && S_ISREG(inode->i_mode)))
0168 return;
0169
0170 nfs_fscache_update_auxdata(&auxdata, inode);
0171
0172 nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
0173 0,
0174 nfsi->fh.data,
0175 nfsi->fh.size,
0176 &auxdata,
0177 sizeof(auxdata),
0178 i_size_read(inode));
0179 }
0180
0181
0182
0183
0184 void nfs_fscache_clear_inode(struct inode *inode)
0185 {
0186 struct nfs_inode *nfsi = NFS_I(inode);
0187 struct fscache_cookie *cookie = nfs_i_fscache(inode);
0188
0189 fscache_relinquish_cookie(cookie, false);
0190 nfsi->fscache = NULL;
0191 }
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 void nfs_fscache_open_file(struct inode *inode, struct file *filp)
0213 {
0214 struct nfs_fscache_inode_auxdata auxdata;
0215 struct fscache_cookie *cookie = nfs_i_fscache(inode);
0216 bool open_for_write = inode_is_open_for_write(inode);
0217
0218 if (!fscache_cookie_valid(cookie))
0219 return;
0220
0221 fscache_use_cookie(cookie, open_for_write);
0222 if (open_for_write) {
0223 nfs_fscache_update_auxdata(&auxdata, inode);
0224 fscache_invalidate(cookie, &auxdata, i_size_read(inode),
0225 FSCACHE_INVAL_DIO_WRITE);
0226 }
0227 }
0228 EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
0229
0230 void nfs_fscache_release_file(struct inode *inode, struct file *filp)
0231 {
0232 struct nfs_fscache_inode_auxdata auxdata;
0233 struct fscache_cookie *cookie = nfs_i_fscache(inode);
0234 loff_t i_size = i_size_read(inode);
0235
0236 nfs_fscache_update_auxdata(&auxdata, inode);
0237 fscache_unuse_cookie(cookie, &auxdata, &i_size);
0238 }
0239
0240
0241
0242
0243 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
0244 {
0245 struct netfs_cache_resources cres;
0246 struct fscache_cookie *cookie = nfs_i_fscache(inode);
0247 struct iov_iter iter;
0248 struct bio_vec bvec[1];
0249 int ret;
0250
0251 memset(&cres, 0, sizeof(cres));
0252 bvec[0].bv_page = page;
0253 bvec[0].bv_offset = 0;
0254 bvec[0].bv_len = PAGE_SIZE;
0255 iov_iter_bvec(&iter, READ, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
0256
0257 ret = fscache_begin_read_operation(&cres, cookie);
0258 if (ret < 0)
0259 return ret;
0260
0261 ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
0262 NULL, NULL);
0263 fscache_end_operation(&cres);
0264 return ret;
0265 }
0266
0267
0268
0269
0270 static int fscache_fallback_write_page(struct inode *inode, struct page *page,
0271 bool no_space_allocated_yet)
0272 {
0273 struct netfs_cache_resources cres;
0274 struct fscache_cookie *cookie = nfs_i_fscache(inode);
0275 struct iov_iter iter;
0276 struct bio_vec bvec[1];
0277 loff_t start = page_offset(page);
0278 size_t len = PAGE_SIZE;
0279 int ret;
0280
0281 memset(&cres, 0, sizeof(cres));
0282 bvec[0].bv_page = page;
0283 bvec[0].bv_offset = 0;
0284 bvec[0].bv_len = PAGE_SIZE;
0285 iov_iter_bvec(&iter, WRITE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
0286
0287 ret = fscache_begin_write_operation(&cres, cookie);
0288 if (ret < 0)
0289 return ret;
0290
0291 ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
0292 no_space_allocated_yet);
0293 if (ret == 0)
0294 ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
0295 fscache_end_operation(&cres);
0296 return ret;
0297 }
0298
0299
0300
0301
0302 int __nfs_fscache_read_page(struct inode *inode, struct page *page)
0303 {
0304 int ret;
0305
0306 trace_nfs_fscache_read_page(inode, page);
0307 if (PageChecked(page)) {
0308 ClearPageChecked(page);
0309 ret = 1;
0310 goto out;
0311 }
0312
0313 ret = fscache_fallback_read_page(inode, page);
0314 if (ret < 0) {
0315 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
0316 SetPageChecked(page);
0317 goto out;
0318 }
0319
0320
0321 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
0322 SetPageUptodate(page);
0323 ret = 0;
0324 out:
0325 trace_nfs_fscache_read_page_exit(inode, page, ret);
0326 return ret;
0327 }
0328
0329
0330
0331
0332
0333 void __nfs_fscache_write_page(struct inode *inode, struct page *page)
0334 {
0335 int ret;
0336
0337 trace_nfs_fscache_write_page(inode, page);
0338
0339 ret = fscache_fallback_write_page(inode, page, true);
0340
0341 if (ret != 0) {
0342 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
0343 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
0344 } else {
0345 nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
0346 }
0347 trace_nfs_fscache_write_page_exit(inode, page, ret);
0348 }