|
16 | 16 | #include "v9fs.h" |
17 | 17 | #include "cache.h" |
18 | 18 |
|
19 | | -#define CACHETAG_LEN 11 |
20 | | - |
21 | | -struct fscache_netfs v9fs_cache_netfs = { |
22 | | - .name = "9p", |
23 | | - .version = 0, |
24 | | -}; |
25 | | - |
26 | | -/* |
27 | | - * v9fs_random_cachetag - Generate a random tag to be associated |
28 | | - * with a new cache session. |
29 | | - * |
30 | | - * The value of jiffies is used for a fairly randomly cache tag. |
31 | | - */ |
32 | | - |
33 | | -static |
34 | | -int v9fs_random_cachetag(struct v9fs_session_info *v9ses) |
| 19 | +int v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses, |
| 20 | + const char *dev_name) |
35 | 21 | { |
36 | | - v9ses->cachetag = kmalloc(CACHETAG_LEN, GFP_KERNEL); |
37 | | - if (!v9ses->cachetag) |
38 | | - return -ENOMEM; |
| 22 | + struct fscache_volume *vcookie; |
| 23 | + char *name, *p; |
39 | 24 |
|
40 | | - return scnprintf(v9ses->cachetag, CACHETAG_LEN, "%lu", jiffies); |
41 | | -} |
42 | | - |
43 | | -const struct fscache_cookie_def v9fs_cache_session_index_def = { |
44 | | - .name = "9P.session", |
45 | | - .type = FSCACHE_COOKIE_TYPE_INDEX, |
46 | | -}; |
| 25 | + name = kasprintf(GFP_KERNEL, "9p,%s,%s", |
| 26 | + dev_name, v9ses->cachetag ?: v9ses->aname); |
| 27 | + if (!name) |
| 28 | + return -ENOMEM; |
47 | 29 |
|
48 | | -void v9fs_cache_session_get_cookie(struct v9fs_session_info *v9ses) |
49 | | -{ |
50 | | - /* If no cache session tag was specified, we generate a random one. */ |
51 | | - if (!v9ses->cachetag) { |
52 | | - if (v9fs_random_cachetag(v9ses) < 0) { |
53 | | - v9ses->fscache = NULL; |
54 | | - kfree(v9ses->cachetag); |
55 | | - v9ses->cachetag = NULL; |
56 | | - return; |
| 30 | + for (p = name; *p; p++) |
| 31 | + if (*p == '/') |
| 32 | + *p = ';'; |
| 33 | + |
| 34 | + vcookie = fscache_acquire_volume(name, NULL, NULL, 0); |
| 35 | + p9_debug(P9_DEBUG_FSC, "session %p get volume %p (%s)\n", |
| 36 | + v9ses, vcookie, name); |
| 37 | + if (IS_ERR(vcookie)) { |
| 38 | + if (vcookie != ERR_PTR(-EBUSY)) { |
| 39 | + kfree(name); |
| 40 | + return PTR_ERR(vcookie); |
57 | 41 | } |
| 42 | + pr_err("Cache volume key already in use (%s)\n", name); |
| 43 | + vcookie = NULL; |
58 | 44 | } |
59 | | - |
60 | | - v9ses->fscache = fscache_acquire_cookie(v9fs_cache_netfs.primary_index, |
61 | | - &v9fs_cache_session_index_def, |
62 | | - v9ses->cachetag, |
63 | | - strlen(v9ses->cachetag), |
64 | | - NULL, 0, |
65 | | - v9ses, 0, true); |
66 | | - p9_debug(P9_DEBUG_FSC, "session %p get cookie %p\n", |
67 | | - v9ses, v9ses->fscache); |
68 | | -} |
69 | | - |
70 | | -void v9fs_cache_session_put_cookie(struct v9fs_session_info *v9ses) |
71 | | -{ |
72 | | - p9_debug(P9_DEBUG_FSC, "session %p put cookie %p\n", |
73 | | - v9ses, v9ses->fscache); |
74 | | - fscache_relinquish_cookie(v9ses->fscache, NULL, false); |
75 | | - v9ses->fscache = NULL; |
76 | | -} |
77 | | - |
78 | | -static enum |
79 | | -fscache_checkaux v9fs_cache_inode_check_aux(void *cookie_netfs_data, |
80 | | - const void *buffer, |
81 | | - uint16_t buflen, |
82 | | - loff_t object_size) |
83 | | -{ |
84 | | - const struct v9fs_inode *v9inode = cookie_netfs_data; |
85 | | - |
86 | | - if (buflen != sizeof(v9inode->qid.version)) |
87 | | - return FSCACHE_CHECKAUX_OBSOLETE; |
88 | | - |
89 | | - if (memcmp(buffer, &v9inode->qid.version, |
90 | | - sizeof(v9inode->qid.version))) |
91 | | - return FSCACHE_CHECKAUX_OBSOLETE; |
92 | | - |
93 | | - return FSCACHE_CHECKAUX_OKAY; |
| 45 | + v9ses->fscache = vcookie; |
| 46 | + kfree(name); |
| 47 | + return 0; |
94 | 48 | } |
95 | 49 |
|
96 | | -const struct fscache_cookie_def v9fs_cache_inode_index_def = { |
97 | | - .name = "9p.inode", |
98 | | - .type = FSCACHE_COOKIE_TYPE_DATAFILE, |
99 | | - .check_aux = v9fs_cache_inode_check_aux, |
100 | | -}; |
101 | | - |
102 | 50 | void v9fs_cache_inode_get_cookie(struct inode *inode) |
103 | 51 | { |
104 | 52 | struct v9fs_inode *v9inode; |
105 | 53 | struct v9fs_session_info *v9ses; |
| 54 | + __le32 version; |
| 55 | + __le64 path; |
106 | 56 |
|
107 | 57 | if (!S_ISREG(inode->i_mode)) |
108 | 58 | return; |
109 | 59 |
|
110 | 60 | v9inode = V9FS_I(inode); |
111 | | - if (v9inode->fscache) |
| 61 | + if (WARN_ON(v9inode->fscache)) |
112 | 62 | return; |
113 | 63 |
|
| 64 | + version = cpu_to_le32(v9inode->qid.version); |
| 65 | + path = cpu_to_le64(v9inode->qid.path); |
114 | 66 | v9ses = v9fs_inode2v9ses(inode); |
115 | | - v9inode->fscache = fscache_acquire_cookie(v9ses->fscache, |
116 | | - &v9fs_cache_inode_index_def, |
117 | | - &v9inode->qid.path, |
118 | | - sizeof(v9inode->qid.path), |
119 | | - &v9inode->qid.version, |
120 | | - sizeof(v9inode->qid.version), |
121 | | - v9inode, |
122 | | - i_size_read(&v9inode->vfs_inode), |
123 | | - true); |
| 67 | + v9inode->fscache = |
| 68 | + fscache_acquire_cookie(v9fs_session_cache(v9ses), |
| 69 | + 0, |
| 70 | + &path, sizeof(path), |
| 71 | + &version, sizeof(version), |
| 72 | + i_size_read(&v9inode->vfs_inode)); |
124 | 73 |
|
125 | 74 | p9_debug(P9_DEBUG_FSC, "inode %p get cookie %p\n", |
126 | 75 | inode, v9inode->fscache); |
127 | 76 | } |
128 | | - |
129 | | -void v9fs_cache_inode_put_cookie(struct inode *inode) |
130 | | -{ |
131 | | - struct v9fs_inode *v9inode = V9FS_I(inode); |
132 | | - |
133 | | - if (!v9inode->fscache) |
134 | | - return; |
135 | | - p9_debug(P9_DEBUG_FSC, "inode %p put cookie %p\n", |
136 | | - inode, v9inode->fscache); |
137 | | - |
138 | | - fscache_relinquish_cookie(v9inode->fscache, &v9inode->qid.version, |
139 | | - false); |
140 | | - v9inode->fscache = NULL; |
141 | | -} |
142 | | - |
143 | | -void v9fs_cache_inode_flush_cookie(struct inode *inode) |
144 | | -{ |
145 | | - struct v9fs_inode *v9inode = V9FS_I(inode); |
146 | | - |
147 | | - if (!v9inode->fscache) |
148 | | - return; |
149 | | - p9_debug(P9_DEBUG_FSC, "inode %p flush cookie %p\n", |
150 | | - inode, v9inode->fscache); |
151 | | - |
152 | | - fscache_relinquish_cookie(v9inode->fscache, NULL, true); |
153 | | - v9inode->fscache = NULL; |
154 | | -} |
155 | | - |
156 | | -void v9fs_cache_inode_set_cookie(struct inode *inode, struct file *filp) |
157 | | -{ |
158 | | - struct v9fs_inode *v9inode = V9FS_I(inode); |
159 | | - |
160 | | - if (!v9inode->fscache) |
161 | | - return; |
162 | | - |
163 | | - mutex_lock(&v9inode->fscache_lock); |
164 | | - |
165 | | - if ((filp->f_flags & O_ACCMODE) != O_RDONLY) |
166 | | - v9fs_cache_inode_flush_cookie(inode); |
167 | | - else |
168 | | - v9fs_cache_inode_get_cookie(inode); |
169 | | - |
170 | | - mutex_unlock(&v9inode->fscache_lock); |
171 | | -} |
172 | | - |
173 | | -void v9fs_cache_inode_reset_cookie(struct inode *inode) |
174 | | -{ |
175 | | - struct v9fs_inode *v9inode = V9FS_I(inode); |
176 | | - struct v9fs_session_info *v9ses; |
177 | | - struct fscache_cookie *old; |
178 | | - |
179 | | - if (!v9inode->fscache) |
180 | | - return; |
181 | | - |
182 | | - old = v9inode->fscache; |
183 | | - |
184 | | - mutex_lock(&v9inode->fscache_lock); |
185 | | - fscache_relinquish_cookie(v9inode->fscache, NULL, true); |
186 | | - |
187 | | - v9ses = v9fs_inode2v9ses(inode); |
188 | | - v9inode->fscache = fscache_acquire_cookie(v9ses->fscache, |
189 | | - &v9fs_cache_inode_index_def, |
190 | | - &v9inode->qid.path, |
191 | | - sizeof(v9inode->qid.path), |
192 | | - &v9inode->qid.version, |
193 | | - sizeof(v9inode->qid.version), |
194 | | - v9inode, |
195 | | - i_size_read(&v9inode->vfs_inode), |
196 | | - true); |
197 | | - p9_debug(P9_DEBUG_FSC, "inode %p revalidating cookie old %p new %p\n", |
198 | | - inode, old, v9inode->fscache); |
199 | | - |
200 | | - mutex_unlock(&v9inode->fscache_lock); |
201 | | -} |
0 commit comments