blob: cac601498925febed61febf6aa574843c942e55c [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
Martin Brandenburg382f4582017-04-25 15:37:59 -04002 * Copyright 2017 Omnibond Systems, L.L.C.
Mike Marshall5db11c22015-07-17 10:38:12 -04003 */
4
5#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05006#include "orangefs-kernel.h"
7#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -04008
Martin Brandenburg480e3e52017-04-25 15:38:01 -04009struct 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
Mike Marshall5db11c22015-07-17 10:38:12 -040025/*
Martin Brandenburg382f4582017-04-25 15:37:59 -040026 * 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
Martin Brandenburg382f4582017-04-25 15:37:59 -040034 *
35 * The trailer_buf starts with a struct orangefs_readdir_response_s
36 * which must be skipped to get to the directory data.
Martin Brandenburg480e3e52017-04-25 15:38:01 -040037 *
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.
Mike Marshall5db11c22015-07-17 10:38:12 -040058 */
Martin Brandenburg382f4582017-04-25 15:37:59 -040059
Martin Brandenburg480e3e52017-04-25 15:38:01 -040060static int do_readdir(struct orangefs_inode_s *oi,
61 struct orangefs_dir *od, struct dentry *dentry,
62 struct orangefs_kernel_op_s *op)
Mike Marshall5db11c22015-07-17 10:38:12 -040063{
Martin Brandenburg382f4582017-04-25 15:37:59 -040064 struct orangefs_readdir_response_s *resp;
Martin Brandenburg382f4582017-04-25 15:37:59 -040065 int bufi, r;
Mike Marshall5db11c22015-07-17 10:38:12 -040066
Martin Brandenburgee3b8d32016-02-17 12:55:42 -050067 /*
Martin Brandenburg382f4582017-04-25 15:37:59 -040068 * 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.
Martin Brandenburgee3b8d32016-02-17 12:55:42 -050072 */
Martin Brandenburg382f4582017-04-25 15:37:59 -040073 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 =
Martin Brandenburg7d221482016-01-04 15:05:28 -050077 ORANGEFS_MAX_DIRENT_COUNT_READDIR;
Mike Marshall5db11c22015-07-17 10:38:12 -040078
Martin Brandenburg382f4582017-04-25 15:37:59 -040079again:
80 bufi = orangefs_readdir_index_get();
81 if (bufi < 0) {
Martin Brandenburg382f4582017-04-25 15:37:59 -040082 od->error = bufi;
83 return bufi;
Mike Marshall5db11c22015-07-17 10:38:12 -040084 }
85
Martin Brandenburg382f4582017-04-25 15:37:59 -040086 op->upcall.req.readdir.buf_index = bufi;
87
88 r = service_operation(op, "orangefs_readdir",
89 get_interruptible_flag(dentry->d_inode));
90
91 orangefs_readdir_index_put(bufi);
92
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);
Martin Brandenburg382f4582017-04-25 15:37:59 -040099 od->error = r;
100 return r;
101 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400102 }
103
Martin Brandenburg382f4582017-04-25 15:37:59 -0400104 if (r < 0) {
105 vfree(op->downcall.trailer_buf);
Martin Brandenburg382f4582017-04-25 15:37:59 -0400106 od->error = r;
107 return r;
108 } else if (op->downcall.status) {
109 vfree(op->downcall.trailer_buf);
Martin Brandenburg382f4582017-04-25 15:37:59 -0400110 od->error = op->downcall.status;
111 return op->downcall.status;
Mike Marshall5db11c22015-07-17 10:38:12 -0400112 }
113
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400114 /*
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
Martin Brandenburg382f4582017-04-25 15:37:59 -0400124 resp = (struct orangefs_readdir_response_s *)
125 op->downcall.trailer_buf;
126 od->token = resp->token;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400127 return 0;
128}
Martin Brandenburg382f4582017-04-25 15:37:59 -0400129
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400130static 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++;
Al Viro9f5e2f72016-02-16 19:54:13 -0500141 }
142
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400143 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
Martin Brandenburg382f4582017-04-25 15:37:59 -0400154 return 0;
155}
Mike Marshall5db11c22015-07-17 10:38:12 -0400156
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400157static 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,
186 struct dir_context *ctx)
187{
188 const int offset = sizeof(struct orangefs_readdir_response_s);
189 struct orangefs_khandle *khandle;
190 __u32 *len, padlen;
191 loff_t i;
192 char *s;
193 i = ctx->pos & ~PART_MASK;
194
195 /* The file offset from userspace is too large. */
196 if (i > part->len)
Martin Brandenburgbf15ba72017-05-02 12:15:10 -0400197 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;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400205
206 while (i < part->len) {
207 if (part->len < i + sizeof *len)
Martin Brandenburgbf15ba72017-05-02 12:15:10 -0400208 break;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400209 len = (void *)part + offset + i;
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) +
215 (8 - (sizeof *len + *len + 1)%8)%8;
216 if (part->len < i + padlen + sizeof *khandle)
Martin Brandenburgbf15ba72017-05-02 12:15:10 -0400217 goto next;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400218 s = (void *)part + offset + i + sizeof *len;
219 if (s[*len] != 0)
Martin Brandenburgbf15ba72017-05-02 12:15:10 -0400220 goto next;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400221 khandle = (void *)part + offset + i + padlen;
222 if (!dir_emit(ctx, s, *len,
223 orangefs_khandle_to_ino(khandle),
224 DT_UNKNOWN))
225 return 0;
226 i += padlen + sizeof *khandle;
227 i = i + (8 - i%8)%8;
228 BUG_ON(i > part->len);
229 ctx->pos = (ctx->pos & PART_MASK) | i;
Martin Brandenburgbf15ba72017-05-02 12:15:10 -0400230 continue;
231next:
232 i += 8;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400233 }
234 return 1;
235}
236
Martin Brandenburg382f4582017-04-25 15:37:59 -0400237static int orangefs_dir_fill(struct orangefs_inode_s *oi,
238 struct orangefs_dir *od, struct dentry *dentry,
239 struct dir_context *ctx)
240{
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400241 struct orangefs_dir_part *part;
242 size_t count;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400243
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400244 count = ((ctx->pos & PART_MASK) >> PART_SHIFT) - 1;
245
246 part = od->part;
247 while (part->next && count) {
248 count--;
249 part = part->next;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400250 }
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400251 /* 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 }
274 }
Martin Brandenburg382f4582017-04-25 15:37:59 -0400275 return 0;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400276}
277
Martin Brandenburg942835d2017-05-02 12:15:11 -0400278static 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
Martin Brandenburg382f4582017-04-25 15:37:59 -0400300static 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;
307
308 dentry = file->f_path.dentry;
309 oi = ORANGEFS_I(dentry->d_inode);
310 od = file->private_data;
311
312 if (od->error)
313 return od->error;
314
315 if (ctx->pos == 0) {
316 if (!dir_emit_dot(file, ctx))
317 return 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400318 ctx->pos++;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400319 }
320 if (ctx->pos == 1) {
321 if (!dir_emit_dotdot(file, ctx))
322 return 0;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400323 ctx->pos = 1 << PART_SHIFT;
Mike Marshall5db11c22015-07-17 10:38:12 -0400324 }
325
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400326 /*
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
Martin Brandenburg382f4582017-04-25 15:37:59 -0400333 r = 0;
334
Martin Brandenburg72f66b82017-04-25 15:38:00 -0400335 /*
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 */
Martin Brandenburg7b796ae2017-04-25 15:38:02 -0400339 while (od->token != ORANGEFS_ITERATE_END &&
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400340 ctx->pos > od->end) {
Martin Brandenburg72f66b82017-04-25 15:38:00 -0400341 r = orangefs_dir_more(oi, od, dentry);
342 if (r)
343 return r;
344 }
Martin Brandenburg7b796ae2017-04-25 15:38:02 -0400345 if (od->token == ORANGEFS_ITERATE_END && ctx->pos > od->end)
Martin Brandenburg72f66b82017-04-25 15:38:00 -0400346 return -EIO;
Martin Brandenburg72f66b82017-04-25 15:38:00 -0400347
348 /* Then try to fill if there's any left in the buffer. */
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400349 if (ctx->pos < od->end) {
Martin Brandenburg382f4582017-04-25 15:37:59 -0400350 r = orangefs_dir_fill(oi, od, dentry, ctx);
351 if (r)
352 return r;
Mike Marshall5db11c22015-07-17 10:38:12 -0400353 }
354
Martin Brandenburg72f66b82017-04-25 15:38:00 -0400355 /* Finally get some more and try to fill. */
Martin Brandenburg7b796ae2017-04-25 15:38:02 -0400356 if (od->token != ORANGEFS_ITERATE_END) {
Martin Brandenburg382f4582017-04-25 15:37:59 -0400357 r = orangefs_dir_more(oi, od, dentry);
358 if (r)
359 return r;
360 r = orangefs_dir_fill(oi, od, dentry, ctx);
Mike Marshall5db11c22015-07-17 10:38:12 -0400361 }
362
Martin Brandenburg382f4582017-04-25 15:37:59 -0400363 return r;
Mike Marshall5db11c22015-07-17 10:38:12 -0400364}
365
Yi Liu8bb8aef2015-11-24 15:12:14 -0500366static int orangefs_dir_open(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400367{
Martin Brandenburg382f4582017-04-25 15:37:59 -0400368 struct orangefs_dir *od;
369 file->private_data = kmalloc(sizeof(struct orangefs_dir),
370 GFP_KERNEL);
Mike Marshall5db11c22015-07-17 10:38:12 -0400371 if (!file->private_data)
372 return -ENOMEM;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400373 od = file->private_data;
Martin Brandenburg7b796ae2017-04-25 15:38:02 -0400374 od->token = ORANGEFS_ITERATE_START;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400375 od->part = NULL;
376 od->end = 1 << PART_SHIFT;
Martin Brandenburg382f4582017-04-25 15:37:59 -0400377 od->error = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400378 return 0;
379}
380
Yi Liu8bb8aef2015-11-24 15:12:14 -0500381static int orangefs_dir_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400382{
Martin Brandenburg382f4582017-04-25 15:37:59 -0400383 struct orangefs_dir *od = file->private_data;
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400384 struct orangefs_dir_part *part = od->part;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500385 orangefs_flush_inode(inode);
Martin Brandenburg480e3e52017-04-25 15:38:01 -0400386 while (part) {
387 struct orangefs_dir_part *next = part->next;
388 vfree(part);
389 part = next;
390 }
Martin Brandenburg382f4582017-04-25 15:37:59 -0400391 kfree(od);
Mike Marshall5db11c22015-07-17 10:38:12 -0400392 return 0;
393}
394
Yi Liu8bb8aef2015-11-24 15:12:14 -0500395const struct file_operations orangefs_dir_operations = {
Martin Brandenburg942835d2017-05-02 12:15:11 -0400396 .llseek = orangefs_dir_llseek,
Mike Marshall5db11c22015-07-17 10:38:12 -0400397 .read = generic_read_dir,
Martin Brandenburg382f4582017-04-25 15:37:59 -0400398 .iterate = orangefs_dir_iterate,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500399 .open = orangefs_dir_open,
Martin Brandenburg382f4582017-04-25 15:37:59 -0400400 .release = orangefs_dir_release
Mike Marshall5db11c22015-07-17 10:38:12 -0400401};