1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* cache.c
*
* Copyright (C) 1997 by Bill Hawes
*
* Routines to support directory cacheing using the page cache.
* Right now this only works for smbfs, but will be generalized
* for use with other filesystems.
*
* Please add a note about your changes to smbfs in the ChangeLog file.
*/
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/dirent.h>
#include <linux/smb_fs.h>
#include <linux/pagemap.h>
#include <asm/page.h>
#include "smb_debug.h"
static inline struct address_space *
get_cache_inode(struct cache_head *cachep)
{
return page_cache_entry((unsigned long) cachep)->mapping;
}
/*
* Try to reassemble the old dircache. If we fail - set ->valid to 0.
* In any case, get at least the page at offset 0 (with ->valid==0 if
* the old one didn't make it, indeed).
*/
struct cache_head *
smb_get_dircache(struct dentry * dentry)
{
struct address_space * mapping = &dentry->d_inode->i_data;
struct cache_head * cachep = NULL;
struct page *page;
page = find_lock_page(mapping, 0);
if (!page) {
/* Sorry, not even page 0 around */
page = grab_cache_page(mapping, 0);
if (!page)
goto out;
cachep = kmap(page);
memset((char*)cachep, 0, PAGE_SIZE);
goto out;
}
cachep = kmap(page);
if (cachep->valid) {
/*
* OK, at least the page 0 survived and seems to be promising.
* Let's try to reassemble the rest.
*/
struct cache_index * index = cachep->index;
unsigned long offset;
int i;
for (offset = 0, i = 0; i < cachep->pages; i++, index++) {
offset += PAGE_SIZE;
page = find_lock_page(mapping,offset>>PAGE_CACHE_SHIFT);
if (!page) {
/* Alas, poor Yorick */
cachep->valid = 0;
goto out;
}
index->block = kmap(page);
}
}
out:
return cachep;
}
/*
* Unlock and release the data blocks.
*/
static void
smb_free_cache_blocks(struct cache_head * cachep)
{
struct cache_index * index = cachep->index;
struct page * page;
int i;
VERBOSE("freeing %d blocks\n", cachep->pages);
for (i = 0; i < cachep->pages; i++, index++) {
if (!index->block)
continue;
page = page_cache_entry((unsigned long) index->block);
index->block = NULL;
kunmap(page);
UnlockPage(page);
page_cache_release(page);
}
}
/*
* Unlocks and releases the dircache.
*/
void
smb_free_dircache(struct cache_head * cachep)
{
struct page *page;
VERBOSE("freeing cache\n");
smb_free_cache_blocks(cachep);
page = page_cache_entry((unsigned long) cachep);
kunmap(page);
UnlockPage(page);
page_cache_release(page);
}
/*
* Initializes the dircache. We release any existing data blocks,
* and then clear the cache_head structure.
*/
void
smb_init_dircache(struct cache_head * cachep)
{
VERBOSE("initializing cache, %d blocks\n", cachep->pages);
smb_free_cache_blocks(cachep);
memset(cachep, 0, sizeof(struct cache_head));
}
/*
* Add a new entry to the cache. This assumes that the
* entries are coming in order and are added to the end.
*/
void
smb_add_to_cache(struct cache_head * cachep, struct cache_dirent *entry,
off_t fpos)
{
struct address_space * mapping = get_cache_inode(cachep);
struct cache_index * index;
struct cache_block * block;
struct page *page;
unsigned long page_off;
unsigned int nent, offset, len = entry->len;
unsigned int needed = len + sizeof(struct cache_entry);
VERBOSE("cache %p, status %d, adding %.*s at %ld\n",
mapping, cachep->status, entry->len, entry->name, fpos);
/*
* Don't do anything if we've had an error ...
*/
if (cachep->status)
goto out;
index = &cachep->index[cachep->idx];
if (!index->block)
goto get_block;
/* space available? */
if (needed < index->space) {
add_entry:
nent = index->num_entries;
index->num_entries++;
index->space -= needed;
offset = index->space +
index->num_entries * sizeof(struct cache_entry);
block = index->block;
memcpy(&block->cb_data.names[offset], entry->name, len);
block->cb_data.table[nent].namelen = len;
block->cb_data.table[nent].offset = offset;
block->cb_data.table[nent].ino = entry->ino;
cachep->entries++;
VERBOSE("added entry %.*s, len=%d, pos=%ld, entries=%d\n",
entry->len, entry->name, len, fpos, cachep->entries);
return;
}
/*
* This block is full ... advance the index.
*/
cachep->idx++;
if (cachep->idx > NINDEX) /* not likely */
goto out_full;
index++;
/*
* Get the next cache block. We don't care for its contents.
*/
get_block:
cachep->pages++;
page_off = PAGE_SIZE + (cachep->idx << PAGE_SHIFT);
page = grab_cache_page(mapping, page_off>>PAGE_CACHE_SHIFT);
if (page) {
block = kmap(page);
index->block = block;
index->space = PAGE_SIZE;
goto add_entry;
}
/*
* On failure, just set the return status ...
*/
out_full:
cachep->status = -ENOMEM;
out:
return;
}
int
smb_find_in_cache(struct cache_head * cachep, off_t pos,
struct cache_dirent *entry)
{
struct cache_index * index = cachep->index;
struct cache_block * block;
unsigned int i, nent, offset = 0;
off_t next_pos = 2;
VERBOSE("smb_find_in_cache: cache %p, looking for pos=%ld\n",
cachep, pos);
for (i = 0; i < cachep->pages; i++, index++)
{
if (pos < next_pos)
break;
nent = pos - next_pos;
next_pos += index->num_entries;
if (pos >= next_pos)
continue;
/*
* The entry is in this block. Note: we return
* then name as a reference with _no_ null byte.
*/
block = index->block;
entry->ino = block->cb_data.table[nent].ino;
entry->len = block->cb_data.table[nent].namelen;
offset = block->cb_data.table[nent].offset;
entry->name = &block->cb_data.names[offset];
VERBOSE("found %.*s, len=%d, pos=%ld\n",
entry->len, entry->name, entry->len, pos);
break;
}
return offset;
}
int
smb_refill_dircache(struct cache_head * cachep, struct dentry *dentry)
{
struct inode * inode = dentry->d_inode;
int result;
VERBOSE("smb_refill_dircache: cache %s/%s, blocks=%d\n",
DENTRY_PATH(dentry), cachep->pages);
/*
* Fill the cache, starting at position 2.
*/
retry:
inode->u.smbfs_i.cache_valid |= SMB_F_CACHEVALID;
result = smb_proc_readdir(dentry, 2, cachep);
if (result < 0)
{
PARANOIA("readdir failed, result=%d\n", result);
goto out;
}
/*
* Check whether the cache was invalidated while
* we were doing the scan ...
*/
if (!(inode->u.smbfs_i.cache_valid & SMB_F_CACHEVALID))
{
PARANOIA("cache invalidated, retrying\n");
goto retry;
}
result = cachep->status;
if (!result)
{
cachep->valid = 1;
}
VERBOSE("cache %s/%s status=%d, entries=%d\n",
DENTRY_PATH(dentry), cachep->status, cachep->entries);
out:
return result;
}
void
smb_invalid_dir_cache(struct inode * dir)
{
/*
* Get rid of any unlocked pages, and clear the
* 'valid' flag in case a scan is in progress.
*/
invalidate_inode_pages(dir);
dir->u.smbfs_i.cache_valid &= ~SMB_F_CACHEVALID;
dir->u.smbfs_i.oldmtime = 0;
}
|