dir.c 9.13 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * Copyright 2017 Omnibond Systems, L.L.C.
4 5 6
 */

#include "protocol.h"
7 8
#include "orangefs-kernel.h"
#include "orangefs-bufmap.h"
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
struct orangefs_dir_part {
	struct orangefs_dir_part *next;
	size_t len;
};

struct orangefs_dir {
	__u64 token;
	struct orangefs_dir_part *part;
	loff_t end;
	int error;
};

#define PART_SHIFT (24)
#define PART_SIZE (1<<24)
#define PART_MASK (~(PART_SIZE - 1))

26
/*
27 28 29 30 31 32 33 34 35 36 37
 * There can be up to 512 directory entries.  Each entry is encoded as
 * follows:
 * 4 bytes: string size (n)
 * n bytes: string
 * 1 byte: trailing zero
 * padding to 8 bytes
 * 16 bytes: khandle
 * padding to 8 bytes
 *
 * The trailer_buf starts with a struct orangefs_readdir_response_s
 * which must be skipped to get to the directory data.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 *
 * The data which is received from the userspace daemon is termed a
 * part and is stored in a linked list in case more than one part is
 * needed for a large directory.
 *
 * The position pointer (ctx->pos) encodes the part and offset on which
 * to begin reading at.  Bits above PART_SHIFT encode the part and bits
 * below PART_SHIFT encode the offset.  Parts are stored in a linked
 * list which grows as data is received from the server.  The overhead
 * associated with managing the list is presumed to be small compared to
 * the overhead of communicating with the server.
 *
 * As data is received from the server, it is placed at the end of the
 * part list.  Data is parsed from the current position as it is needed.
 * When data is determined to be corrupt, it is either because the
 * userspace component has sent back corrupt data or because the file
 * pointer has been moved to an invalid location.  Since the two cannot
 * be differentiated, return EIO.
 *
 * Part zero is synthesized to contains `.' and `..'.  Part one is the
 * first part of the part list.
59 60
 */

61 62 63
static int do_readdir(struct orangefs_inode_s *oi,
    struct orangefs_dir *od, struct dentry *dentry,
    struct orangefs_kernel_op_s *op)
64 65 66 67
{
	struct orangefs_readdir_response_s *resp;
	int bufi, r;

68
	/*
69 70 71 72
	 * Despite the badly named field, readdir does not use shared
	 * memory.  However, there are a limited number of readdir
	 * slots, which must be allocated here.  This flag simply tells
	 * the op scheduler to return the op here for retry.
73
	 */
74 75 76 77
	op->uses_shared_memory = 1;
	op->upcall.req.readdir.refn = oi->refn;
	op->upcall.req.readdir.token = od->token;
	op->upcall.req.readdir.max_dirent_count =
78
	    ORANGEFS_MAX_DIRENT_COUNT_READDIR;
79

80 81 82 83 84
again:
	bufi = orangefs_readdir_index_get();
	if (bufi < 0) {
		od->error = bufi;
		return bufi;
85 86
	}

87
	op->upcall.req.readdir.buf_index = bufi;
88

89 90
	r = service_operation(op, "orangefs_readdir",
	    get_interruptible_flag(dentry->d_inode));
91

92
	orangefs_readdir_index_put(bufi);
93

94 95 96 97 98 99 100 101 102
	if (op_state_purged(op)) {
		if (r == -EAGAIN) {
			vfree(op->downcall.trailer_buf);
			goto again;
		} else if (r == -EIO) {
			vfree(op->downcall.trailer_buf);
			od->error = r;
			return r;
		}
103 104
	}

105 106 107 108 109 110 111 112 113 114
	if (r < 0) {
		vfree(op->downcall.trailer_buf);
		od->error = r;
		return r;
	} else if (op->downcall.status) {
		vfree(op->downcall.trailer_buf);
		od->error = op->downcall.status;
		return op->downcall.status;
	}

115 116 117 118 119 120 121 122 123 124
	/*
	 * The maximum size is size per entry times the 512 entries plus
	 * the header.  This is well under the limit.
	 */
	if (op->downcall.trailer_size > PART_SIZE) {
		vfree(op->downcall.trailer_buf);
		od->error = -EIO;
		return -EIO;
	}

125 126 127
	resp = (struct orangefs_readdir_response_s *)
	    op->downcall.trailer_buf;
	od->token = resp->token;
128 129
	return 0;
}
130

131 132 133 134 135 136 137 138
static int parse_readdir(struct orangefs_dir *od,
    struct orangefs_kernel_op_s *op)
{
	struct orangefs_dir_part *part, *new;
	size_t count;

	count = 1;
	part = od->part;
139
	while (part) {
140
		count++;
141 142 143 144
		if (part->next)
			part = part->next;
		else
			break;
145 146
	}

147 148 149 150 151 152 153 154 155 156 157
	new = (void *)op->downcall.trailer_buf;
	new->next = NULL;
	new->len = op->downcall.trailer_size -
	    sizeof(struct orangefs_readdir_response_s);
	if (!od->part)
		od->part = new;
	else
		part->next = new;
	count++;
	od->end = count << PART_SHIFT;

158 159
	return 0;
}
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
static int orangefs_dir_more(struct orangefs_inode_s *oi,
    struct orangefs_dir *od, struct dentry *dentry)
{
	struct orangefs_kernel_op_s *op;
	int r;

	op = op_alloc(ORANGEFS_VFS_OP_READDIR);
	if (!op) {
		od->error = -ENOMEM;
		return -ENOMEM;
	}
	r = do_readdir(oi, od, dentry, op);
	if (r) {
		od->error = r;
		goto out;
	}
	r = parse_readdir(od, op);
	if (r) {
		od->error = r;
		goto out;
	}

	od->error = 0;
out:
	op_release(op);
	return od->error;
}

static int fill_from_part(struct orangefs_dir_part *part,
190 191
    struct dir_context *ctx)
{
192
	const int offset = sizeof(struct orangefs_readdir_response_s);
193 194
	struct orangefs_khandle *khandle;
	__u32 *len, padlen;
195
	loff_t i;
196
	char *s;
197 198 199 200
	i = ctx->pos & ~PART_MASK;

	/* The file offset from userspace is too large. */
	if (i > part->len)
201 202 203 204 205 206 207 208
		return 1;

	/*
	 * If the seek pointer is positioned just before an entry it
	 * should find the next entry.
	 */
	if (i % 8)
		i = i + (8 - i%8)%8;
209 210 211

	while (i < part->len) {
		if (part->len < i + sizeof *len)
212
			break;
213
		len = (void *)part + offset + i;
214 215 216 217 218
		/*
		 * len is the size of the string itself.  padlen is the
		 * total size of the encoded string.
		 */
		padlen = (sizeof *len + *len + 1) +
219 220
		    (8 - (sizeof *len + *len + 1)%8)%8;
		if (part->len < i + padlen + sizeof *khandle)
221
			goto next;
222
		s = (void *)part + offset + i + sizeof *len;
223
		if (s[*len] != 0)
224
			goto next;
225
		khandle = (void *)part + offset + i + padlen;
226
		if (!dir_emit(ctx, s, *len,
227 228
		    orangefs_khandle_to_ino(khandle),
		    DT_UNKNOWN))
229
			return 0;
230 231
		i += padlen + sizeof *khandle;
		i = i + (8 - i%8)%8;
232 233
		BUG_ON(i > part->len);
		ctx->pos = (ctx->pos & PART_MASK) | i;
234 235 236
		continue;
next:
		i += 8;
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
	}
	return 1;
}

static int orangefs_dir_fill(struct orangefs_inode_s *oi,
    struct orangefs_dir *od, struct dentry *dentry,
    struct dir_context *ctx)
{
	struct orangefs_dir_part *part;
	size_t count;

	count = ((ctx->pos & PART_MASK) >> PART_SHIFT) - 1;

	part = od->part;
	while (part->next && count) {
		count--;
		part = part->next;
	}
	/* This means the userspace file offset is invalid. */
	if (count) {
		od->error = -EIO;
		return -EIO;
	}

	while (part && part->len) {
		int r;
		r = fill_from_part(part, ctx);
		if (r < 0) {
			od->error = r;
			return r;
		} else if (r == 0) {
			/* Userspace buffer is full. */
			break;
		} else {
			/*
			 * The part ran out of data.  Move to the next
			 * part. */
			ctx->pos = (ctx->pos & PART_MASK) +
			    (1 << PART_SHIFT);
			part = part->next;
		}
278 279 280
	}
	return 0;
}
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static loff_t orangefs_dir_llseek(struct file *file, loff_t offset,
    int whence)
{
	struct orangefs_dir *od = file->private_data;
	/*
	 * Delete the stored data so userspace sees new directory
	 * entries.
	 */
	if (!whence && offset < od->end) {
		struct orangefs_dir_part *part = od->part;
		while (part) {
			struct orangefs_dir_part *next = part->next;
			vfree(part);
			part = next;
		}
		od->token = ORANGEFS_ITERATE_START;
		od->part = NULL;
		od->end = 1 << PART_SHIFT;
	}
	return default_llseek(file, offset, whence);
}

304 305 306 307 308 309 310
static int orangefs_dir_iterate(struct file *file,
    struct dir_context *ctx)
{
	struct orangefs_inode_s *oi;
	struct orangefs_dir *od;
	struct dentry *dentry;
	int r;
311

312 313 314
	dentry = file->f_path.dentry;
	oi = ORANGEFS_I(dentry->d_inode);
	od = file->private_data;
315

316 317
	if (od->error)
		return od->error;
318

319 320 321 322
	if (ctx->pos == 0) {
		if (!dir_emit_dot(file, ctx))
			return 0;
		ctx->pos++;
323
	}
324 325 326
	if (ctx->pos == 1) {
		if (!dir_emit_dotdot(file, ctx))
			return 0;
327
		ctx->pos = 1 << PART_SHIFT;
328 329
	}

330 331 332 333 334 335 336
	/*
	 * The seek position is in the first synthesized part but is not
	 * valid.
	 */
	if ((ctx->pos & PART_MASK) == 0)
		return -EIO;

337 338
	r = 0;

339 340 341 342
	/*
	 * Must read more if the user has sought past what has been read
	 * so far.  Stop a user who has sought past the end.
	 */
343
	while (od->token != ORANGEFS_ITERATE_END &&
344
	    ctx->pos > od->end) {
345 346 347 348
		r = orangefs_dir_more(oi, od, dentry);
		if (r)
			return r;
	}
349
	if (od->token == ORANGEFS_ITERATE_END && ctx->pos > od->end)
350 351 352
		return -EIO;

	/* Then try to fill if there's any left in the buffer. */
353
	if (ctx->pos < od->end) {
354 355 356
		r = orangefs_dir_fill(oi, od, dentry, ctx);
		if (r)
			return r;
357 358
	}

359
	/* Finally get some more and try to fill. */
360
	if (od->token != ORANGEFS_ITERATE_END) {
361 362 363 364
		r = orangefs_dir_more(oi, od, dentry);
		if (r)
			return r;
		r = orangefs_dir_fill(oi, od, dentry, ctx);
365 366
	}

367
	return r;
368 369
}

370
static int orangefs_dir_open(struct inode *inode, struct file *file)
371
{
372 373 374
	struct orangefs_dir *od;
	file->private_data = kmalloc(sizeof(struct orangefs_dir),
	    GFP_KERNEL);
375 376
	if (!file->private_data)
		return -ENOMEM;
377
	od = file->private_data;
378
	od->token = ORANGEFS_ITERATE_START;
379 380
	od->part = NULL;
	od->end = 1 << PART_SHIFT;
381
	od->error = 0;
382 383 384
	return 0;
}

385
static int orangefs_dir_release(struct inode *inode, struct file *file)
386
{
387
	struct orangefs_dir *od = file->private_data;
388 389 390 391 392 393
	struct orangefs_dir_part *part = od->part;
	while (part) {
		struct orangefs_dir_part *next = part->next;
		vfree(part);
		part = next;
	}
394
	kfree(od);
395 396 397
	return 0;
}

398
const struct file_operations orangefs_dir_operations = {
399
	.llseek = orangefs_dir_llseek,
400
	.read = generic_read_dir,
401
	.iterate = orangefs_dir_iterate,
402
	.open = orangefs_dir_open,
403
	.release = orangefs_dir_release
404
};