1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * CIFS filesystem cache interface
4 *
5 * Copyright (c) 2010 Novell, Inc.
6 * Author(s): Suresh Jayaraman <sjayaraman@suse.de>
7 *
8 */
9 #include "fscache.h"
10 #include "cifsglob.h"
11 #include "cifs_debug.h"
12 #include "cifs_fs_sb.h"
13 #include "cifsproto.h"
14
15 /*
16 * Key for fscache inode. [!] Contents must match comparisons in cifs_find_inode().
17 */
18 struct cifs_fscache_inode_key {
19
20 __le64 uniqueid; /* server inode number */
21 __le64 createtime; /* creation time on server */
22 u8 type; /* S_IFMT file type */
23 } __packed;
24
cifs_fscache_fill_volume_coherency(struct cifs_tcon * tcon,struct cifs_fscache_volume_coherency_data * cd)25 static void cifs_fscache_fill_volume_coherency(
26 struct cifs_tcon *tcon,
27 struct cifs_fscache_volume_coherency_data *cd)
28 {
29 memset(cd, 0, sizeof(*cd));
30 cd->resource_id = cpu_to_le64(tcon->resource_id);
31 cd->vol_create_time = tcon->vol_create_time;
32 cd->vol_serial_number = cpu_to_le32(tcon->vol_serial_number);
33 }
34
cifs_fscache_get_super_cookie(struct cifs_tcon * tcon)35 int cifs_fscache_get_super_cookie(struct cifs_tcon *tcon)
36 {
37 struct cifs_fscache_volume_coherency_data cd;
38 struct TCP_Server_Info *server = tcon->ses->server;
39 struct fscache_volume *vcookie;
40 const struct sockaddr *sa = (struct sockaddr *)&server->dstaddr;
41 char *sharename;
42 char *key;
43 int ret = -ENOMEM;
44
45 if (tcon->fscache_acquired)
46 return 0;
47
48 mutex_lock(&tcon->fscache_lock);
49 if (tcon->fscache_acquired) {
50 mutex_unlock(&tcon->fscache_lock);
51 return 0;
52 }
53 tcon->fscache_acquired = true;
54
55 tcon->fscache = NULL;
56 switch (sa->sa_family) {
57 case AF_INET:
58 case AF_INET6:
59 break;
60 default:
61 mutex_unlock(&tcon->fscache_lock);
62 cifs_dbg(VFS, "Unknown network family '%d'\n", sa->sa_family);
63 return -EINVAL;
64 }
65
66 memset(&key, 0, sizeof(key));
67
68 sharename = extract_sharename(tcon->tree_name);
69 if (IS_ERR(sharename)) {
70 mutex_unlock(&tcon->fscache_lock);
71 cifs_dbg(FYI, "%s: couldn't extract sharename\n", __func__);
72 return PTR_ERR(sharename);
73 }
74
75 strreplace(sharename, '/', ';');
76
77 key = kasprintf(GFP_KERNEL, "cifs,%pISpc,%s", sa, sharename);
78 if (!key)
79 goto out;
80
81 cifs_fscache_fill_volume_coherency(tcon, &cd);
82 vcookie = fscache_acquire_volume(key,
83 NULL, /* preferred_cache */
84 &cd, sizeof(cd));
85 cifs_dbg(FYI, "%s: (%s/0x%p)\n", __func__, key, vcookie);
86 if (IS_ERR(vcookie)) {
87 if (vcookie != ERR_PTR(-EBUSY)) {
88 ret = PTR_ERR(vcookie);
89 goto out_2;
90 }
91 pr_err("Cache volume key already in use (%s)\n", key);
92 vcookie = NULL;
93 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
94 netfs_trace_tcon_ref_see_fscache_collision);
95 } else {
96 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
97 netfs_trace_tcon_ref_see_fscache_okay);
98 }
99
100 tcon->fscache = vcookie;
101 ret = 0;
102 out_2:
103 kfree(key);
104 out:
105 kfree(sharename);
106 mutex_unlock(&tcon->fscache_lock);
107 return ret;
108 }
109
cifs_fscache_release_super_cookie(struct cifs_tcon * tcon)110 void cifs_fscache_release_super_cookie(struct cifs_tcon *tcon)
111 {
112 struct cifs_fscache_volume_coherency_data cd;
113
114 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, tcon->fscache);
115
116 cifs_fscache_fill_volume_coherency(tcon, &cd);
117 fscache_relinquish_volume(tcon->fscache, &cd, false);
118 tcon->fscache = NULL;
119 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
120 netfs_trace_tcon_ref_see_fscache_relinq);
121 }
122
cifs_fscache_get_inode_cookie(struct inode * inode)123 void cifs_fscache_get_inode_cookie(struct inode *inode)
124 {
125 struct cifs_fscache_inode_coherency_data cd;
126 struct cifs_fscache_inode_key key;
127 struct cifsInodeInfo *cifsi = CIFS_I(inode);
128 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
129 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
130
131 key.uniqueid = cpu_to_le64(cifsi->uniqueid);
132 key.createtime = cpu_to_le64(cifsi->createtime);
133 key.type = (inode->i_mode & S_IFMT) >> 12;
134 cifs_fscache_fill_coherency(&cifsi->netfs.inode, &cd);
135
136 cifsi->netfs.cache =
137 fscache_acquire_cookie(tcon->fscache, 0,
138 &key, sizeof(key),
139 &cd, sizeof(cd),
140 i_size_read(&cifsi->netfs.inode));
141 if (cifsi->netfs.cache)
142 mapping_set_release_always(inode->i_mapping);
143 }
144
cifs_fscache_unuse_inode_cookie(struct inode * inode,bool update)145 void cifs_fscache_unuse_inode_cookie(struct inode *inode, bool update)
146 {
147 if (update) {
148 struct cifs_fscache_inode_coherency_data cd;
149 loff_t i_size = i_size_read(inode);
150
151 cifs_fscache_fill_coherency(inode, &cd);
152 fscache_unuse_cookie(cifs_inode_cookie(inode), &cd, &i_size);
153 } else {
154 fscache_unuse_cookie(cifs_inode_cookie(inode), NULL, NULL);
155 }
156 }
157
cifs_fscache_release_inode_cookie(struct inode * inode)158 void cifs_fscache_release_inode_cookie(struct inode *inode)
159 {
160 struct cifsInodeInfo *cifsi = CIFS_I(inode);
161 struct fscache_cookie *cookie = cifs_inode_cookie(inode);
162
163 if (cookie) {
164 cifs_dbg(FYI, "%s: (0x%p)\n", __func__, cookie);
165 fscache_relinquish_cookie(cookie, false);
166 cifsi->netfs.cache = NULL;
167 }
168 }
169