orangefs: invalidate stored directory on seek
[linux-block.git] / fs / orangefs / dir.c
CommitLineData
5db11c21 1/*
382f4581 2 * Copyright 2017 Omnibond Systems, L.L.C.
5db11c21
MM
3 */
4
5#include "protocol.h"
575e9461
MM
6#include "orangefs-kernel.h"
7#include "orangefs-bufmap.h"
5db11c21 8
480e3e53
MB
9struct orangefs_dir_part {
10 struct orangefs_dir_part *next;
11 size_t len;
12};
13
14struct orangefs_dir {
15 __u64 token;
16 struct orangefs_dir_part *part;
17 loff_t end;
18 int error;
19};
20
21#define PART_SHIFT (24)
22#define PART_SIZE (1<<24)
23#define PART_MASK (~(PART_SIZE - 1))
24
5db11c21 25/*
382f4581
MB
26 * There can be up to 512 directory entries. Each entry is encoded as
27 * follows:
28 * 4 bytes: string size (n)
29 * n bytes: string
30 * 1 byte: trailing zero
31 * padding to 8 bytes
32 * 16 bytes: khandle
33 * padding to 8 bytes
382f4581
MB
34 *
35 * The trailer_buf starts with a struct orangefs_readdir_response_s
36 * which must be skipped to get to the directory data.
480e3e53
MB
37 *
38 * The data which is received from the userspace daemon is termed a
39 * part and is stored in a linked list in case more than one part is
40 * needed for a large directory.
41 *
42 * The position pointer (ctx->pos) encodes the part and offset on which
43 * to begin reading at. Bits above PART_SHIFT encode the part and bits
44 * below PART_SHIFT encode the offset. Parts are stored in a linked
45 * list which grows as data is received from the server. The overhead
46 * associated with managing the list is presumed to be small compared to
47 * the overhead of communicating with the server.
48 *
49 * As data is received from the server, it is placed at the end of the
50 * part list. Data is parsed from the current position as it is needed.
51 * When data is determined to be corrupt, it is either because the
52 * userspace component has sent back corrupt data or because the file
53 * pointer has been moved to an invalid location. Since the two cannot
54 * be differentiated, return EIO.
55 *
56 * Part zero is synthesized to contains `.' and `..'. Part one is the
57 * first part of the part list.
5db11c21 58 */
5db11c21 59
480e3e53
MB
60static int do_readdir(struct orangefs_inode_s *oi,
61 struct orangefs_dir *od, struct dentry *dentry,
62 struct orangefs_kernel_op_s *op)
382f4581 63{
382f4581 64 struct orangefs_readdir_response_s *resp;
382f4581
MB
65 int bufi, r;
66
ee3b8d37 67 /*
382f4581
MB
68 * Despite the badly named field, readdir does not use shared
69 * memory. However, there are a limited number of readdir
70 * slots, which must be allocated here. This flag simply tells
71 * the op scheduler to return the op here for retry.
ee3b8d37 72 */
382f4581
MB
73 op->uses_shared_memory = 1;
74 op->upcall.req.readdir.refn = oi->refn;
75 op->upcall.req.readdir.token = od->token;
76 op->upcall.req.readdir.max_dirent_count =
7d221485 77 ORANGEFS_MAX_DIRENT_COUNT_READDIR;
5db11c21 78
382f4581
MB
79again:
80 bufi = orangefs_readdir_index_get();
81 if (bufi < 0) {
382f4581
MB
82 od->error = bufi;
83 return bufi;
5db11c21 84 }
5db11c21 85
382f4581 86 op->upcall.req.readdir.buf_index = bufi;
5db11c21 87
382f4581
MB
88 r = service_operation(op, "orangefs_readdir",
89 get_interruptible_flag(dentry->d_inode));
5db11c21 90
382f4581 91 orangefs_readdir_index_put(bufi);
ee3b8d37 92
382f4581
MB
93 if (op_state_purged(op)) {
94 if (r == -EAGAIN) {
95 vfree(op->downcall.trailer_buf);
96 goto again;
97 } else if (r == -EIO) {
98 vfree(op->downcall.trailer_buf);
382f4581
MB
99 od->error = r;
100 return r;
101 }
5db11c21
MM
102 }
103
382f4581
MB
104 if (r < 0) {
105 vfree(op->downcall.trailer_buf);
382f4581
MB
106 od->error = r;
107 return r;
108 } else if (op->downcall.status) {
109 vfree(op->downcall.trailer_buf);
382f4581
MB
110 od->error = op->downcall.status;
111 return op->downcall.status;
112 }
113
480e3e53
MB
114 /*
115 * The maximum size is size per entry times the 512 entries plus
116 * the header. This is well under the limit.
117 */
118 if (op->downcall.trailer_size > PART_SIZE) {
119 vfree(op->downcall.trailer_buf);
120 od->error = -EIO;
121 return -EIO;
122 }
123
382f4581
MB
124 resp = (struct orangefs_readdir_response_s *)
125 op->downcall.trailer_buf;
126 od->token = resp->token;
480e3e53
MB
127 return 0;
128}
382f4581 129
480e3e53
MB
130static int parse_readdir(struct orangefs_dir *od,
131 struct orangefs_kernel_op_s *op)
132{
133 struct orangefs_dir_part *part, *new;
134 size_t count;
135
136 count = 1;
137 part = od->part;
138 while (part && part->next) {
139 part = part->next;
140 count++;
382f4581
MB
141 }
142
480e3e53
MB
143 new = (void *)op->downcall.trailer_buf;
144 new->next = NULL;
145 new->len = op->downcall.trailer_size -
146 sizeof(struct orangefs_readdir_response_s);
147 if (!od->part)
148 od->part = new;
149 else
150 part->next = new;
151 count++;
152 od->end = count << PART_SHIFT;
153
382f4581
MB
154 return 0;
155}
9f5e2f7f 156
480e3e53
MB
157static int orangefs_dir_more(struct orangefs_inode_s *oi,
158 struct orangefs_dir *od, struct dentry *dentry)
159{
160 struct orangefs_kernel_op_s *op;
161 int r;
162
163 op = op_alloc(ORANGEFS_VFS_OP_READDIR);
164 if (!op) {
165 od->error = -ENOMEM;
166 return -ENOMEM;
167 }
168 r = do_readdir(oi, od, dentry, op);
169 if (r) {
170 od->error = r;
171 goto out;
172 }
173 r = parse_readdir(od, op);
174 if (r) {
175 od->error = r;
176 goto out;
177 }
178
179 od->error = 0;
180out:
181 op_release(op);
182 return od->error;
183}
184
185static int fill_from_part(struct orangefs_dir_part *part,
382f4581
MB
186 struct dir_context *ctx)
187{
480e3e53 188 const int offset = sizeof(struct orangefs_readdir_response_s);
382f4581
MB
189 struct orangefs_khandle *khandle;
190 __u32 *len, padlen;
72f66b83 191 loff_t i;
382f4581 192 char *s;
480e3e53
MB
193 i = ctx->pos & ~PART_MASK;
194
195 /* The file offset from userspace is too large. */
196 if (i > part->len)
bf15ba7c
MB
197 return 1;
198
199 /*
200 * If the seek pointer is positioned just before an entry it
201 * should find the next entry.
202 */
203 if (i % 8)
204 i = i + (8 - i%8)%8;
480e3e53
MB
205
206 while (i < part->len) {
207 if (part->len < i + sizeof *len)
bf15ba7c 208 break;
480e3e53 209 len = (void *)part + offset + i;
382f4581
MB
210 /*
211 * len is the size of the string itself. padlen is the
212 * total size of the encoded string.
213 */
214 padlen = (sizeof *len + *len + 1) +
480e3e53
MB
215 (8 - (sizeof *len + *len + 1)%8)%8;
216 if (part->len < i + padlen + sizeof *khandle)
bf15ba7c 217 goto next;
480e3e53 218 s = (void *)part + offset + i + sizeof *len;
382f4581 219 if (s[*len] != 0)
bf15ba7c 220 goto next;
480e3e53 221 khandle = (void *)part + offset + i + padlen;
382f4581 222 if (!dir_emit(ctx, s, *len,
480e3e53
MB
223 orangefs_khandle_to_ino(khandle),
224 DT_UNKNOWN))
382f4581 225 return 0;
72f66b83
MB
226 i += padlen + sizeof *khandle;
227 i = i + (8 - i%8)%8;
480e3e53
MB
228 BUG_ON(i > part->len);
229 ctx->pos = (ctx->pos & PART_MASK) | i;
bf15ba7c
MB
230 continue;
231next:
232 i += 8;
480e3e53
MB
233 }
234 return 1;
235}
236
237static int orangefs_dir_fill(struct orangefs_inode_s *oi,
238 struct orangefs_dir *od, struct dentry *dentry,
239 struct dir_context *ctx)
240{
241 struct orangefs_dir_part *part;
242 size_t count;
243
244 count = ((ctx->pos & PART_MASK) >> PART_SHIFT) - 1;
245
246 part = od->part;
247 while (part->next && count) {
248 count--;
249 part = part->next;
250 }
251 /* This means the userspace file offset is invalid. */
252 if (count) {
253 od->error = -EIO;
254 return -EIO;
255 }
256
257 while (part && part->len) {
258 int r;
259 r = fill_from_part(part, ctx);
260 if (r < 0) {
261 od->error = r;
262 return r;
263 } else if (r == 0) {
264 /* Userspace buffer is full. */
265 break;
266 } else {
267 /*
268 * The part ran out of data. Move to the next
269 * part. */
270 ctx->pos = (ctx->pos & PART_MASK) +
271 (1 << PART_SHIFT);
272 part = part->next;
273 }
382f4581 274 }
382f4581 275 return 0;
382f4581 276}
5db11c21 277
942835d6
MB
278static loff_t orangefs_dir_llseek(struct file *file, loff_t offset,
279 int whence)
280{
281 struct orangefs_dir *od = file->private_data;
282 /*
283 * Delete the stored data so userspace sees new directory
284 * entries.
285 */
286 if (!whence && offset < od->end) {
287 struct orangefs_dir_part *part = od->part;
288 while (part) {
289 struct orangefs_dir_part *next = part->next;
290 vfree(part);
291 part = next;
292 }
293 od->token = ORANGEFS_ITERATE_START;
294 od->part = NULL;
295 od->end = 1 << PART_SHIFT;
296 }
297 return default_llseek(file, offset, whence);
298}
299
382f4581
MB
300static int orangefs_dir_iterate(struct file *file,
301 struct dir_context *ctx)
302{
303 struct orangefs_inode_s *oi;
304 struct orangefs_dir *od;
305 struct dentry *dentry;
306 int r;
5db11c21 307
382f4581
MB
308 dentry = file->f_path.dentry;
309 oi = ORANGEFS_I(dentry->d_inode);
310 od = file->private_data;
5db11c21 311
382f4581
MB
312 if (od->error)
313 return od->error;
5db11c21 314
382f4581
MB
315 if (ctx->pos == 0) {
316 if (!dir_emit_dot(file, ctx))
317 return 0;
318 ctx->pos++;
5db11c21 319 }
382f4581
MB
320 if (ctx->pos == 1) {
321 if (!dir_emit_dotdot(file, ctx))
322 return 0;
480e3e53 323 ctx->pos = 1 << PART_SHIFT;
5db11c21
MM
324 }
325
480e3e53
MB
326 /*
327 * The seek position is in the first synthesized part but is not
328 * valid.
329 */
330 if ((ctx->pos & PART_MASK) == 0)
331 return -EIO;
332
382f4581
MB
333 r = 0;
334
72f66b83
MB
335 /*
336 * Must read more if the user has sought past what has been read
337 * so far. Stop a user who has sought past the end.
338 */
7b796ae3 339 while (od->token != ORANGEFS_ITERATE_END &&
480e3e53 340 ctx->pos > od->end) {
72f66b83
MB
341 r = orangefs_dir_more(oi, od, dentry);
342 if (r)
343 return r;
344 }
7b796ae3 345 if (od->token == ORANGEFS_ITERATE_END && ctx->pos > od->end)
72f66b83 346 return -EIO;
72f66b83
MB
347
348 /* Then try to fill if there's any left in the buffer. */
480e3e53 349 if (ctx->pos < od->end) {
382f4581
MB
350 r = orangefs_dir_fill(oi, od, dentry, ctx);
351 if (r)
352 return r;
5db11c21
MM
353 }
354
72f66b83 355 /* Finally get some more and try to fill. */
7b796ae3 356 if (od->token != ORANGEFS_ITERATE_END) {
382f4581
MB
357 r = orangefs_dir_more(oi, od, dentry);
358 if (r)
359 return r;
360 r = orangefs_dir_fill(oi, od, dentry, ctx);
5db11c21
MM
361 }
362
382f4581 363 return r;
5db11c21
MM
364}
365
8bb8aefd 366static int orangefs_dir_open(struct inode *inode, struct file *file)
5db11c21 367{
382f4581
MB
368 struct orangefs_dir *od;
369 file->private_data = kmalloc(sizeof(struct orangefs_dir),
370 GFP_KERNEL);
5db11c21
MM
371 if (!file->private_data)
372 return -ENOMEM;
382f4581 373 od = file->private_data;
7b796ae3 374 od->token = ORANGEFS_ITERATE_START;
480e3e53
MB
375 od->part = NULL;
376 od->end = 1 << PART_SHIFT;
382f4581 377 od->error = 0;
5db11c21
MM
378 return 0;
379}
380
8bb8aefd 381static int orangefs_dir_release(struct inode *inode, struct file *file)
5db11c21 382{
382f4581 383 struct orangefs_dir *od = file->private_data;
480e3e53 384 struct orangefs_dir_part *part = od->part;
8bb8aefd 385 orangefs_flush_inode(inode);
480e3e53
MB
386 while (part) {
387 struct orangefs_dir_part *next = part->next;
388 vfree(part);
389 part = next;
390 }
382f4581 391 kfree(od);
5db11c21
MM
392 return 0;
393}
394
8bb8aefd 395const struct file_operations orangefs_dir_operations = {
942835d6 396 .llseek = orangefs_dir_llseek,
5db11c21 397 .read = generic_read_dir,
382f4581 398 .iterate = orangefs_dir_iterate,
8bb8aefd 399 .open = orangefs_dir_open,
382f4581 400 .release = orangefs_dir_release
5db11c21 401};