Subversion Repositories HelenOS

Rev

Rev 4593 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2689 jermar 1
/*
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup fs
30
 * @{
4010 decky 31
 */
2689 jermar 32
 
33
/**
4010 decky 34
 * @file vfs_ops.c
35
 * @brief Operations that VFS offers to its clients.
2689 jermar 36
 */
37
 
2763 jermar 38
#include "vfs.h"
2689 jermar 39
#include <ipc/ipc.h>
40
#include <async.h>
41
#include <errno.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <bool.h>
4518 jermar 46
#include <fibril_sync.h>
4509 decky 47
#include <adt/list.h>
2689 jermar 48
#include <unistd.h>
49
#include <ctype.h>
2708 jermar 50
#include <fcntl.h>
2689 jermar 51
#include <assert.h>
2763 jermar 52
#include <vfs/canonify.h>
2689 jermar 53
 
2748 jermar 54
/* Forward declarations of static functions. */
2770 jermar 55
static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
2748 jermar 56
 
2689 jermar 57
/**
58
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
59
 * concurrent VFS operation which modifies the file system namespace.
60
 */
4518 jermar 61
FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
2689 jermar 62
 
3109 jermar 63
vfs_pair_t rootfs = {
2689 jermar 64
	.fs_handle = 0,
3109 jermar 65
	.dev_handle = 0
2689 jermar 66
};
67
 
4010 decky 68
static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
4305 jermar 69
    fs_handle_t fs_handle, char *mp, char *opts)
2689 jermar 70
{
4010 decky 71
	vfs_lookup_res_t mp_res;
4409 jermar 72
	vfs_lookup_res_t mr_res;
2689 jermar 73
	vfs_node_t *mp_node = NULL;
4409 jermar 74
	vfs_node_t *mr_node;
75
	fs_index_t rindex;
76
	size_t rsize;
77
	unsigned rlnkcnt;
4305 jermar 78
	ipcarg_t rc;
2958 jermar 79
	int phone;
4305 jermar 80
	aid_t msg;
81
	ipc_call_t answer;
4463 decky 82
 
4305 jermar 83
	/* Resolve the path to the mountpoint. */
4518 jermar 84
	fibril_rwlock_write_lock(&namespace_rwlock);
2689 jermar 85
	if (rootfs.fs_handle) {
2707 jermar 86
		/* We already have the root FS. */
4264 svoboda 87
		if (str_cmp(mp, "/") == 0) {
2788 jermar 88
			/* Trying to mount root FS over root FS */
4518 jermar 89
			fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 90
			ipc_answer_0(rid, EBUSY);
2788 jermar 91
			return;
92
		}
4010 decky 93
 
94
		rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
2689 jermar 95
		if (rc != EOK) {
2707 jermar 96
			/* The lookup failed for some reason. */
4518 jermar 97
			fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 98
			ipc_answer_0(rid, rc);
2689 jermar 99
			return;
100
		}
4010 decky 101
 
2691 jermar 102
		mp_node = vfs_node_get(&mp_res);
2689 jermar 103
		if (!mp_node) {
4518 jermar 104
			fibril_rwlock_write_unlock(&namespace_rwlock);
4431 jermar 105
			ipc_answer_0(rid, ENOMEM);
2689 jermar 106
			return;
107
		}
4010 decky 108
 
2689 jermar 109
		/*
110
		 * Now we hold a reference to mp_node.
4584 jermar 111
		 * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
2689 jermar 112
		 * This prevents the mount point from being deleted.
113
		 */
114
	} else {
2707 jermar 115
		/* We still don't have the root file system mounted. */
4264 svoboda 116
		if (str_cmp(mp, "/") == 0) {
2958 jermar 117
			/*
118
			 * For this simple, but important case,
119
			 * we are almost done.
120
			 */
121
 
3109 jermar 122
			/* Tell the mountee that it is being mounted. */
123
			phone = vfs_grab_phone(fs_handle);
4584 jermar 124
			msg = async_send_1(phone, VFS_OUT_MOUNTED,
4305 jermar 125
			    (ipcarg_t) dev_handle, &answer);
126
			/* send the mount options */
127
			rc = ipc_data_write_start(phone, (void *)opts,
128
			    str_size(opts));
129
			if (rc != EOK) {
4551 jermar 130
				async_wait_for(msg, NULL);
4518 jermar 131
				vfs_release_phone(phone);
132
				fibril_rwlock_write_unlock(&namespace_rwlock);
4305 jermar 133
				ipc_answer_0(rid, rc);
134
				return;
135
			}
4551 jermar 136
			async_wait_for(msg, &rc);
4518 jermar 137
			vfs_release_phone(phone);
3352 jermar 138
 
139
			if (rc != EOK) {
4518 jermar 140
				fibril_rwlock_write_unlock(&namespace_rwlock);
3352 jermar 141
				ipc_answer_0(rid, rc);
142
				return;
3109 jermar 143
			}
4305 jermar 144
 
145
			rindex = (fs_index_t) IPC_GET_ARG1(answer);
146
			rsize = (size_t) IPC_GET_ARG2(answer);
147
			rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
4010 decky 148
 
3352 jermar 149
			mr_res.triplet.fs_handle = fs_handle;
150
			mr_res.triplet.dev_handle = dev_handle;
4305 jermar 151
			mr_res.triplet.index = rindex;
152
			mr_res.size = rsize;
153
			mr_res.lnkcnt = rlnkcnt;
3653 jermar 154
			mr_res.type = VFS_NODE_DIRECTORY;
4010 decky 155
 
3352 jermar 156
			rootfs.fs_handle = fs_handle;
157
			rootfs.dev_handle = dev_handle;
4010 decky 158
 
3352 jermar 159
			/* Add reference to the mounted root. */
160
			mr_node = vfs_node_get(&mr_res); 
161
			assert(mr_node);
4010 decky 162
 
4518 jermar 163
			fibril_rwlock_write_unlock(&namespace_rwlock);
2958 jermar 164
			ipc_answer_0(rid, rc);
2689 jermar 165
			return;
166
		} else {
167
			/*
168
			 * We can't resolve this without the root filesystem
169
			 * being mounted first.
170
			 */
4518 jermar 171
			fibril_rwlock_write_unlock(&namespace_rwlock);
2689 jermar 172
			ipc_answer_0(rid, ENOENT);
173
			return;
174
		}
175
	}
176
 
177
	/*
178
	 * At this point, we have all necessary pieces: file system and device
3109 jermar 179
	 * handles, and we know the mount point VFS node.
2689 jermar 180
	 */
4010 decky 181
 
4409 jermar 182
	int mountee_phone = vfs_grab_phone(fs_handle);
183
	assert(mountee_phone >= 0);
184
 
2958 jermar 185
	phone = vfs_grab_phone(mp_res.triplet.fs_handle);
4584 jermar 186
	msg = async_send_4(phone, VFS_OUT_MOUNT,
2691 jermar 187
	    (ipcarg_t) mp_res.triplet.dev_handle,
2957 jermar 188
	    (ipcarg_t) mp_res.triplet.index,
3109 jermar 189
	    (ipcarg_t) fs_handle,
4305 jermar 190
	    (ipcarg_t) dev_handle, &answer);
4409 jermar 191
 
192
	/* send connection */
193
	rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
194
	if (rc != EOK) {
4551 jermar 195
		async_wait_for(msg, NULL);
196
		vfs_release_phone(mountee_phone);
4518 jermar 197
		vfs_release_phone(phone);
4409 jermar 198
		/* Mount failed, drop reference to mp_node. */
199
		if (mp_node)
200
			vfs_node_put(mp_node);
201
		ipc_answer_0(rid, rc);
4518 jermar 202
		fibril_rwlock_write_unlock(&namespace_rwlock);
4409 jermar 203
		return;
204
	}
4551 jermar 205
 
206
	vfs_release_phone(mountee_phone);
4409 jermar 207
 
4305 jermar 208
	/* send the mount options */
209
	rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
210
	if (rc != EOK) {
4551 jermar 211
		async_wait_for(msg, NULL);
4518 jermar 212
		vfs_release_phone(phone);
4305 jermar 213
		/* Mount failed, drop reference to mp_node. */
214
		if (mp_node)
215
			vfs_node_put(mp_node);
4518 jermar 216
		fibril_rwlock_write_unlock(&namespace_rwlock);
4305 jermar 217
		ipc_answer_0(rid, rc);
218
		return;
219
	}
4551 jermar 220
	async_wait_for(msg, &rc);
4518 jermar 221
	vfs_release_phone(phone);
4010 decky 222
 
4431 jermar 223
	if (rc == EOK) {
224
		rindex = (fs_index_t) IPC_GET_ARG1(answer);
225
		rsize = (size_t) IPC_GET_ARG2(answer);
226
		rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
227
 
228
		mr_res.triplet.fs_handle = fs_handle;
229
		mr_res.triplet.dev_handle = dev_handle;
230
		mr_res.triplet.index = rindex;
231
		mr_res.size = rsize;
232
		mr_res.lnkcnt = rlnkcnt;
233
		mr_res.type = VFS_NODE_DIRECTORY;
234
 
235
		/* Add reference to the mounted root. */
236
		mr_node = vfs_node_get(&mr_res); 
237
		assert(mr_node);
238
	} else {
3109 jermar 239
		/* Mount failed, drop reference to mp_node. */
2689 jermar 240
		if (mp_node)
241
			vfs_node_put(mp_node);
242
	}
4409 jermar 243
 
2957 jermar 244
	ipc_answer_0(rid, rc);
4518 jermar 245
	fibril_rwlock_write_unlock(&namespace_rwlock);
2689 jermar 246
}
247
 
4010 decky 248
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
249
{
250
	/*
251
	 * We expect the library to do the device-name to device-handle
252
	 * translation for us, thus the device handle will arrive as ARG1
253
	 * in the request.
254
	 */
255
	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
256
 
257
	/*
258
	 * Mount flags are passed as ARG2.
259
	 */
260
	unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
261
 
262
	/*
263
	 * For now, don't make use of ARG3, but it can be used to
264
	 * carry mount options in the future.
265
	 */
266
 
267
	/* We want the client to send us the mount point. */
268
	ipc_callid_t callid;
269
	size_t size;
270
	if (!ipc_data_write_receive(&callid, &size)) {
271
		ipc_answer_0(callid, EINVAL);
272
		ipc_answer_0(rid, EINVAL);
273
		return;
274
	}
275
 
276
	/* Check whether size is reasonable wrt. the mount point. */
277
	if ((size < 1) || (size > MAX_PATH_LEN)) {
278
		ipc_answer_0(callid, EINVAL);
279
		ipc_answer_0(rid, EINVAL);
280
		return;
281
	}
282
 
283
	/* Allocate buffer for the mount point data being received. */
284
	char *mp = malloc(size + 1);
285
	if (!mp) {
286
		ipc_answer_0(callid, ENOMEM);
287
		ipc_answer_0(rid, ENOMEM);
288
		return;
289
	}
290
 
291
	/* Deliver the mount point. */
292
	ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
293
	if (retval != EOK) {
4301 jermar 294
		ipc_answer_0(rid, retval);
4010 decky 295
		free(mp);
296
		return;
297
	}
298
	mp[size] = '\0';
299
 
4305 jermar 300
	/* Now we expect to receive the mount options. */
301
	if (!ipc_data_write_receive(&callid, &size)) {
302
		ipc_answer_0(callid, EINVAL);
303
		ipc_answer_0(rid, EINVAL);
304
		free(mp);
305
		return;
306
	}
307
 
308
	/* Check the offered options size. */
4684 svoboda 309
	if (size > MAX_MNTOPTS_LEN) {
4305 jermar 310
		ipc_answer_0(callid, EINVAL);
311
		ipc_answer_0(rid, EINVAL);
312
		free(mp);
313
		return;
314
	}
315
 
316
	/* Allocate buffer for the mount options. */
317
	char *opts = (char *) malloc(size + 1);
318
	if (!opts) {
319
		ipc_answer_0(callid, ENOMEM);
320
		ipc_answer_0(rid, ENOMEM);
321
		free(mp);
322
		return;
323
	}
324
 
325
	/* Deliver the mount options. */
326
	retval = ipc_data_write_finalize(callid, opts, size);
327
	if (retval != EOK) {
328
		ipc_answer_0(rid, retval);
329
		free(mp);
330
		free(opts);
331
		return;
332
	}
333
	opts[size] = '\0';
334
 
4010 decky 335
	/*
336
	 * Now, we expect the client to send us data with the name of the file
337
	 * system.
338
	 */
339
	if (!ipc_data_write_receive(&callid, &size)) {
340
		ipc_answer_0(callid, EINVAL);
341
		ipc_answer_0(rid, EINVAL);
342
		free(mp);
4305 jermar 343
		free(opts);
4010 decky 344
		return;
345
	}
346
 
347
	/*
348
	 * Don't receive more than is necessary for storing a full file system
349
	 * name.
350
	 */
351
	if ((size < 1) || (size > FS_NAME_MAXLEN)) {
352
		ipc_answer_0(callid, EINVAL);
353
		ipc_answer_0(rid, EINVAL);
354
		free(mp);
4305 jermar 355
		free(opts);
4010 decky 356
		return;
357
	}
358
 
359
	/*
360
	 * Allocate buffer for file system name.
361
	 */
362
	char *fs_name = (char *) malloc(size + 1);
363
	if (fs_name == NULL) {
364
		ipc_answer_0(callid, ENOMEM);
4301 jermar 365
		ipc_answer_0(rid, ENOMEM);
4010 decky 366
		free(mp);
4305 jermar 367
		free(opts);
4010 decky 368
		return;
369
	}
370
 
371
	/* Deliver the file system name. */
372
	retval = ipc_data_write_finalize(callid, fs_name, size);
373
	if (retval != EOK) {
4301 jermar 374
		ipc_answer_0(rid, retval);
4010 decky 375
		free(mp);
4305 jermar 376
		free(opts);
4010 decky 377
		free(fs_name);
378
		return;
379
	}
380
	fs_name[size] = '\0';
4302 jermar 381
 
4010 decky 382
	/*
4302 jermar 383
	 * Wait for IPC_M_PING so that we can return an error if we don't know
384
	 * fs_name.
385
	 */
386
	ipc_call_t data;
387
	callid = async_get_call(&data);
388
	if (IPC_GET_METHOD(data) != IPC_M_PING) {
389
		ipc_answer_0(callid, ENOTSUP);
390
		ipc_answer_0(rid, ENOTSUP);
391
		free(mp);
4305 jermar 392
		free(opts);
4302 jermar 393
		free(fs_name);
394
		return;
395
	}
396
 
397
	/*
4010 decky 398
	 * Check if we know a file system with the same name as is in fs_name.
399
	 * This will also give us its file system handle.
400
	 */
4539 jermar 401
	fibril_mutex_lock(&fs_head_lock);
4566 jermar 402
	fs_handle_t fs_handle;
403
recheck:
404
	fs_handle = fs_name_to_handle(fs_name, false);
4010 decky 405
	if (!fs_handle) {
406
		if (flags & IPC_FLAG_BLOCKING) {
4566 jermar 407
			fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
408
			goto recheck;
4010 decky 409
		}
410
 
4539 jermar 411
		fibril_mutex_unlock(&fs_head_lock);
4010 decky 412
		ipc_answer_0(callid, ENOENT);
413
		ipc_answer_0(rid, ENOENT);
414
		free(mp);
415
		free(fs_name);
4305 jermar 416
		free(opts);
4010 decky 417
		return;
418
	}
4539 jermar 419
	fibril_mutex_unlock(&fs_head_lock);
4010 decky 420
 
421
	/* Acknowledge that we know fs_name. */
422
	ipc_answer_0(callid, EOK);
423
 
424
	/* Do the mount */
4305 jermar 425
	vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
4010 decky 426
	free(mp);
427
	free(fs_name);
4305 jermar 428
	free(opts);
4010 decky 429
}
430
 
2689 jermar 431
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
432
{
433
	if (!vfs_files_init()) {
434
		ipc_answer_0(rid, ENOMEM);
435
		return;
436
	}
4463 decky 437
 
2689 jermar 438
	/*
2700 jermar 439
	 * The POSIX interface is open(path, oflag, mode).
4584 jermar 440
	 * We can receive oflags and mode along with the VFS_IN_OPEN call;
441
	 * the path will need to arrive in another call.
2700 jermar 442
	 *
443
	 * We also receive one private, non-POSIX set of flags called lflag
444
	 * used to pass information to vfs_lookup_internal().
2689 jermar 445
	 */
2700 jermar 446
	int lflag = IPC_GET_ARG1(*request);
447
	int oflag = IPC_GET_ARG2(*request);
448
	int mode = IPC_GET_ARG3(*request);
2689 jermar 449
	size_t len;
4684 svoboda 450
 
451
	/* Ignore mode for now. */
452
	(void) mode;
4463 decky 453
 
3653 jermar 454
	/*
455
	 * Make sure that we are called with exactly one of L_FILE and
4463 decky 456
	 * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
3653 jermar 457
	 */
4518 jermar 458
	if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
459
	    ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
460
	    ((lflag & L_OPEN) != 0)) {
3653 jermar 461
		ipc_answer_0(rid, EINVAL);
462
		return;
463
	}
4463 decky 464
 
2708 jermar 465
	if (oflag & O_CREAT)
466
		lflag |= L_CREATE;
467
	if (oflag & O_EXCL)
468
		lflag |= L_EXCLUSIVE;
4463 decky 469
 
2689 jermar 470
	ipc_callid_t callid;
471
	if (!ipc_data_write_receive(&callid, &len)) {
472
		ipc_answer_0(callid, EINVAL);
473
		ipc_answer_0(rid, EINVAL);
474
		return;
475
	}
4463 decky 476
 
2752 jermar 477
	char *path = malloc(len + 1);
2689 jermar 478
	if (!path) {
479
		ipc_answer_0(callid, ENOMEM);
480
		ipc_answer_0(rid, ENOMEM);
481
		return;
482
	}
4463 decky 483
 
2689 jermar 484
	int rc;
485
	if ((rc = ipc_data_write_finalize(callid, path, len))) {
486
		ipc_answer_0(rid, rc);
487
		free(path);
488
		return;
489
	}
2752 jermar 490
	path[len] = '\0';
2689 jermar 491
 
492
	/*
493
	 * Avoid the race condition in which the file can be deleted before we
494
	 * find/create-and-lock the VFS node corresponding to the looked-up
495
	 * triplet.
496
	 */
2708 jermar 497
	if (lflag & L_CREATE)
4518 jermar 498
		fibril_rwlock_write_lock(&namespace_rwlock);
2708 jermar 499
	else
4518 jermar 500
		fibril_rwlock_read_lock(&namespace_rwlock);
4463 decky 501
 
2707 jermar 502
	/* The path is now populated and we can call vfs_lookup_internal(). */
2691 jermar 503
	vfs_lookup_res_t lr;
4463 decky 504
	rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
505
	if (rc != EOK) {
2708 jermar 506
		if (lflag & L_CREATE)
4518 jermar 507
			fibril_rwlock_write_unlock(&namespace_rwlock);
2708 jermar 508
		else
4518 jermar 509
			fibril_rwlock_read_unlock(&namespace_rwlock);
2689 jermar 510
		ipc_answer_0(rid, rc);
511
		free(path);
512
		return;
513
	}
4463 decky 514
 
2748 jermar 515
	/* Path is no longer needed. */
2689 jermar 516
	free(path);
4463 decky 517
 
2691 jermar 518
	vfs_node_t *node = vfs_node_get(&lr);
2708 jermar 519
	if (lflag & L_CREATE)
4518 jermar 520
		fibril_rwlock_write_unlock(&namespace_rwlock);
2708 jermar 521
	else
4518 jermar 522
		fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 523
 
2748 jermar 524
	/* Truncate the file if requested and if necessary. */
525
	if (oflag & O_TRUNC) {
4518 jermar 526
		fibril_rwlock_write_lock(&node->contents_rwlock);
2748 jermar 527
		if (node->size) {
528
			rc = vfs_truncate_internal(node->fs_handle,
529
			    node->dev_handle, node->index, 0);
530
			if (rc) {
4518 jermar 531
				fibril_rwlock_write_unlock(&node->contents_rwlock);
2748 jermar 532
				vfs_node_put(node);
533
				ipc_answer_0(rid, rc);
534
				return;
535
			}
536
			node->size = 0;
537
		}
4518 jermar 538
		fibril_rwlock_write_unlock(&node->contents_rwlock);
2748 jermar 539
	}
4463 decky 540
 
2689 jermar 541
	/*
542
	 * Get ourselves a file descriptor and the corresponding vfs_file_t
543
	 * structure.
544
	 */
545
	int fd = vfs_fd_alloc();
546
	if (fd < 0) {
547
		vfs_node_put(node);
548
		ipc_answer_0(rid, fd);
549
		return;
550
	}
551
	vfs_file_t *file = vfs_file_get(fd);
552
	file->node = node;
4463 decky 553
	if (oflag & O_APPEND)
2709 jermar 554
		file->append = true;
4463 decky 555
 
556
	/*
557
	 * The following increase in reference count is for the fact that the
558
	 * file is being opened and that a file structure is pointing to it.
559
	 * It is necessary so that the file will not disappear when
560
	 * vfs_node_put() is called. The reference will be dropped by the
4584 jermar 561
	 * respective VFS_IN_CLOSE.
4463 decky 562
	 */
563
	vfs_node_addref(node);
564
	vfs_node_put(node);
565
 
566
	/* Success! Return the new file descriptor to the client. */
567
	ipc_answer_1(rid, EOK, fd);
568
}
2689 jermar 569
 
4463 decky 570
void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
571
{
572
	// FIXME: check for sanity of the supplied fs, dev and index
573
 
574
	if (!vfs_files_init()) {
575
		ipc_answer_0(rid, ENOMEM);
576
		return;
577
	}
578
 
2689 jermar 579
	/*
4463 decky 580
	 * The interface is open_node(fs, dev, index, oflag).
581
	 */
582
	vfs_lookup_res_t lr;
583
 
584
	lr.triplet.fs_handle = IPC_GET_ARG1(*request);
585
	lr.triplet.dev_handle = IPC_GET_ARG2(*request);
586
	lr.triplet.index = IPC_GET_ARG3(*request);
587
	int oflag = IPC_GET_ARG4(*request);
588
 
4518 jermar 589
	fibril_rwlock_read_lock(&namespace_rwlock);
4463 decky 590
 
591
	int rc = vfs_open_node_internal(&lr);
592
	if (rc != EOK) {
4518 jermar 593
		fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 594
		ipc_answer_0(rid, rc);
595
		return;
596
	}
597
 
598
	vfs_node_t *node = vfs_node_get(&lr);
4518 jermar 599
	fibril_rwlock_read_unlock(&namespace_rwlock);
4463 decky 600
 
601
	/* Truncate the file if requested and if necessary. */
602
	if (oflag & O_TRUNC) {
4518 jermar 603
		fibril_rwlock_write_lock(&node->contents_rwlock);
4463 decky 604
		if (node->size) {
605
			rc = vfs_truncate_internal(node->fs_handle,
606
			    node->dev_handle, node->index, 0);
607
			if (rc) {
4518 jermar 608
				fibril_rwlock_write_unlock(&node->contents_rwlock);
4463 decky 609
				vfs_node_put(node);
610
				ipc_answer_0(rid, rc);
611
				return;
612
			}
613
			node->size = 0;
614
		}
4518 jermar 615
		fibril_rwlock_write_unlock(&node->contents_rwlock);
4463 decky 616
	}
617
 
618
	/*
619
	 * Get ourselves a file descriptor and the corresponding vfs_file_t
620
	 * structure.
621
	 */
622
	int fd = vfs_fd_alloc();
623
	if (fd < 0) {
624
		vfs_node_put(node);
625
		ipc_answer_0(rid, fd);
626
		return;
627
	}
628
	vfs_file_t *file = vfs_file_get(fd);
629
	file->node = node;
630
	if (oflag & O_APPEND)
631
		file->append = true;
632
 
633
	/*
2689 jermar 634
	 * The following increase in reference count is for the fact that the
635
	 * file is being opened and that a file structure is pointing to it.
636
	 * It is necessary so that the file will not disappear when
637
	 * vfs_node_put() is called. The reference will be dropped by the
4584 jermar 638
	 * respective VFS_IN_CLOSE.
2689 jermar 639
	 */
640
	vfs_node_addref(node);
641
	vfs_node_put(node);
4463 decky 642
 
2707 jermar 643
	/* Success! Return the new file descriptor to the client. */
2689 jermar 644
	ipc_answer_1(rid, EOK, fd);
645
}
646
 
4463 decky 647
void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
648
{
649
	int fd = IPC_GET_ARG1(*request);
650
 
651
	/* Lookup the file structure corresponding to the file descriptor. */
652
	vfs_file_t *file = vfs_file_get(fd);
653
	if (!file) {
654
		ipc_answer_0(rid, ENOENT);
655
		return;
656
	}
657
 
658
	/*
659
	 * Lock the open file structure so that no other thread can manipulate
660
	 * the same open file at a time.
661
	 */
4518 jermar 662
	fibril_mutex_lock(&file->lock);
4463 decky 663
	int fs_phone = vfs_grab_phone(file->node->fs_handle);
664
 
4584 jermar 665
	/* Make a VFS_OUT_SYMC request at the destination FS server. */
4463 decky 666
	aid_t msg;
667
	ipc_call_t answer;
4584 jermar 668
	msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->dev_handle,
669
	    file->node->index, &answer);
4518 jermar 670
 
4463 decky 671
	/* Wait for reply from the FS server. */
672
	ipcarg_t rc;
673
	async_wait_for(msg, &rc);
674
 
4551 jermar 675
	vfs_release_phone(fs_phone);
4518 jermar 676
	fibril_mutex_unlock(&file->lock);
4463 decky 677
 
3215 jermar 678
	ipc_answer_0(rid, rc);
2734 jermar 679
}
680
 
4463 decky 681
void vfs_close(ipc_callid_t rid, ipc_call_t *request)
682
{
683
	int fd = IPC_GET_ARG1(*request);
684
 
685
	/* Lookup the file structure corresponding to the file descriptor. */
686
	vfs_file_t *file = vfs_file_get(fd);
687
	if (!file) {
688
		ipc_answer_0(rid, ENOENT);
689
		return;
690
	}
691
 
692
	/*
693
	 * Lock the open file structure so that no other thread can manipulate
694
	 * the same open file at a time.
695
	 */
4518 jermar 696
	fibril_mutex_lock(&file->lock);
4463 decky 697
	int fs_phone = vfs_grab_phone(file->node->fs_handle);
698
 
4584 jermar 699
	/* Make a VFS_OUT_CLOSE request at the destination FS server. */
4463 decky 700
	aid_t msg;
701
	ipc_call_t answer;
4584 jermar 702
	msg = async_send_2(fs_phone, VFS_OUT_CLOSE, file->node->dev_handle,
703
	    file->node->index, &answer);
4518 jermar 704
 
4463 decky 705
	/* Wait for reply from the FS server. */
706
	ipcarg_t rc;
707
	async_wait_for(msg, &rc);
4551 jermar 708
 
709
	vfs_release_phone(fs_phone);
4518 jermar 710
	fibril_mutex_unlock(&file->lock);
4463 decky 711
 
712
	int retval = IPC_GET_ARG1(answer);
713
	if (retval != EOK)
714
		ipc_answer_0(rid, retval);
715
 
716
	retval = vfs_fd_free(fd);
717
	ipc_answer_0(rid, retval);
718
}
719
 
2689 jermar 720
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
721
{
722
 
723
	/*
724
	 * The following code strongly depends on the fact that the files data
725
	 * structure can be only accessed by a single fibril and all file
726
	 * operations are serialized (i.e. the reads and writes cannot
727
	 * interleave and a file cannot be closed while it is being read).
728
	 *
729
	 * Additional synchronization needs to be added once the table of
730
	 * open files supports parallel access!
731
	 */
732
 
733
	int fd = IPC_GET_ARG1(*request);
3079 decky 734
 
2707 jermar 735
	/* Lookup the file structure corresponding to the file descriptor. */
2689 jermar 736
	vfs_file_t *file = vfs_file_get(fd);
737
	if (!file) {
738
		ipc_answer_0(rid, ENOENT);
739
		return;
740
	}
3079 decky 741
 
2689 jermar 742
	/*
743
	 * Now we need to receive a call with client's
744
	 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
745
	 */
746
	ipc_callid_t callid;
747
	int res;
748
	if (read)
749
		res = ipc_data_read_receive(&callid, NULL);
750
	else 
751
		res = ipc_data_write_receive(&callid, NULL);
752
	if (!res) {
753
		ipc_answer_0(callid, EINVAL);
754
		ipc_answer_0(rid, EINVAL);
755
		return;
756
	}
3079 decky 757
 
2689 jermar 758
	/*
759
	 * Lock the open file structure so that no other thread can manipulate
760
	 * the same open file at a time.
761
	 */
4518 jermar 762
	fibril_mutex_lock(&file->lock);
3653 jermar 763
 
2689 jermar 764
	/*
765
	 * Lock the file's node so that no other client can read/write to it at
766
	 * the same time.
767
	 */
768
	if (read)
4518 jermar 769
		fibril_rwlock_read_lock(&file->node->contents_rwlock);
2689 jermar 770
	else
4518 jermar 771
		fibril_rwlock_write_lock(&file->node->contents_rwlock);
3653 jermar 772
 
773
	if (file->node->type == VFS_NODE_DIRECTORY) {
774
		/*
775
		 * Make sure that no one is modifying the namespace
776
		 * while we are in readdir().
777
		 */
778
		assert(read);
4518 jermar 779
		fibril_rwlock_read_lock(&namespace_rwlock);
3653 jermar 780
	}
3079 decky 781
 
2689 jermar 782
	int fs_phone = vfs_grab_phone(file->node->fs_handle);	
783
 
2707 jermar 784
	/* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
2689 jermar 785
	aid_t msg;
786
	ipc_call_t answer;
2709 jermar 787
	if (!read && file->append)
788
		file->pos = file->node->size;
4584 jermar 789
	msg = async_send_3(fs_phone, read ? VFS_OUT_READ : VFS_OUT_WRITE,
2689 jermar 790
	    file->node->dev_handle, file->node->index, file->pos, &answer);
791
 
792
	/*
793
	 * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
794
	 * destination FS server. The call will be routed as if sent by
795
	 * ourselves. Note that call arguments are immutable in this case so we
796
	 * don't have to bother.
797
	 */
798
	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
4518 jermar 799
 
2707 jermar 800
	/* Wait for reply from the FS server. */
2689 jermar 801
	ipcarg_t rc;
802
	async_wait_for(msg, &rc);
4463 decky 803
 
4551 jermar 804
	vfs_release_phone(fs_phone);
805
 
2689 jermar 806
	size_t bytes = IPC_GET_ARG1(answer);
3653 jermar 807
 
808
	if (file->node->type == VFS_NODE_DIRECTORY)
4518 jermar 809
		fibril_rwlock_read_unlock(&namespace_rwlock);
3079 decky 810
 
2707 jermar 811
	/* Unlock the VFS node. */
2689 jermar 812
	if (read)
4518 jermar 813
		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
2689 jermar 814
	else {
815
		/* Update the cached version of node's size. */
2710 jermar 816
		if (rc == EOK)
817
			file->node->size = IPC_GET_ARG2(answer); 
4518 jermar 818
		fibril_rwlock_write_unlock(&file->node->contents_rwlock);
2689 jermar 819
	}
3079 decky 820
 
2707 jermar 821
	/* Update the position pointer and unlock the open file. */
2710 jermar 822
	if (rc == EOK)
823
		file->pos += bytes;
4518 jermar 824
	fibril_mutex_unlock(&file->lock);
3079 decky 825
 
2689 jermar 826
	/*
827
	 * FS server's reply is the final result of the whole operation we
828
	 * return to the client.
829
	 */
830
	ipc_answer_1(rid, rc, bytes);
831
}
832
 
833
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
834
{
835
	vfs_rdwr(rid, request, true);
836
}
837
 
838
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
839
{
840
	vfs_rdwr(rid, request, false);
841
}
842
 
843
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
844
{
845
	int fd = (int) IPC_GET_ARG1(*request);
846
	off_t off = (off_t) IPC_GET_ARG2(*request);
847
	int whence = (int) IPC_GET_ARG3(*request);
848
 
849
 
2707 jermar 850
	/* Lookup the file structure corresponding to the file descriptor. */
2689 jermar 851
	vfs_file_t *file = vfs_file_get(fd);
852
	if (!file) {
853
		ipc_answer_0(rid, ENOENT);
854
		return;
855
	}
856
 
857
	off_t newpos;
4518 jermar 858
	fibril_mutex_lock(&file->lock);
2689 jermar 859
	if (whence == SEEK_SET) {
860
		file->pos = off;
4518 jermar 861
		fibril_mutex_unlock(&file->lock);
2689 jermar 862
		ipc_answer_1(rid, EOK, off);
863
		return;
864
	}
865
	if (whence == SEEK_CUR) {
866
		if (file->pos + off < file->pos) {
4518 jermar 867
			fibril_mutex_unlock(&file->lock);
2689 jermar 868
			ipc_answer_0(rid, EOVERFLOW);
869
			return;
870
		}
871
		file->pos += off;
872
		newpos = file->pos;
4518 jermar 873
		fibril_mutex_unlock(&file->lock);
2689 jermar 874
		ipc_answer_1(rid, EOK, newpos);
875
		return;
876
	}
877
	if (whence == SEEK_END) {
4518 jermar 878
		fibril_rwlock_read_lock(&file->node->contents_rwlock);
2689 jermar 879
		size_t size = file->node->size;
4518 jermar 880
		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
2689 jermar 881
		if (size + off < size) {
4518 jermar 882
			fibril_mutex_unlock(&file->lock);
2689 jermar 883
			ipc_answer_0(rid, EOVERFLOW);
884
			return;
885
		}
886
		newpos = size + off;
4518 jermar 887
		fibril_mutex_unlock(&file->lock);
2689 jermar 888
		ipc_answer_1(rid, EOK, newpos);
889
		return;
890
	}
4518 jermar 891
	fibril_mutex_unlock(&file->lock);
2689 jermar 892
	ipc_answer_0(rid, EINVAL);
893
}
894
 
2770 jermar 895
int
896
vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
897
    fs_index_t index, size_t size)
2748 jermar 898
{
899
	ipcarg_t rc;
900
	int fs_phone;
901
 
902
	fs_phone = vfs_grab_phone(fs_handle);
4584 jermar 903
	rc = async_req_3_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t)dev_handle,
2748 jermar 904
	    (ipcarg_t)index, (ipcarg_t)size);
905
	vfs_release_phone(fs_phone);
906
	return (int)rc;
907
}
908
 
2693 jermar 909
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
910
{
911
	int fd = IPC_GET_ARG1(*request);
912
	size_t size = IPC_GET_ARG2(*request);
2748 jermar 913
	int rc;
2693 jermar 914
 
915
	vfs_file_t *file = vfs_file_get(fd);
916
	if (!file) {
917
		ipc_answer_0(rid, ENOENT);
918
		return;
919
	}
4518 jermar 920
	fibril_mutex_lock(&file->lock);
2693 jermar 921
 
4518 jermar 922
	fibril_rwlock_write_lock(&file->node->contents_rwlock);
2748 jermar 923
	rc = vfs_truncate_internal(file->node->fs_handle,
924
	    file->node->dev_handle, file->node->index, size);
2693 jermar 925
	if (rc == EOK)
926
		file->node->size = size;
4518 jermar 927
	fibril_rwlock_write_unlock(&file->node->contents_rwlock);
2693 jermar 928
 
4518 jermar 929
	fibril_mutex_unlock(&file->lock);
2748 jermar 930
	ipc_answer_0(rid, (ipcarg_t)rc);
2693 jermar 931
}
932
 
4585 jermar 933
void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
934
{
935
	int fd = IPC_GET_ARG1(*request);
936
	size_t size = IPC_GET_ARG2(*request);
937
	ipcarg_t rc;
938
 
939
	vfs_file_t *file = vfs_file_get(fd);
940
	if (!file) {
941
		ipc_answer_0(rid, ENOENT);
942
		return;
943
	}
944
 
945
	ipc_callid_t callid;
946
	if (!ipc_data_read_receive(&callid, NULL)) {
947
		ipc_answer_0(callid, EINVAL);
948
		ipc_answer_0(rid, EINVAL);
949
		return;
950
	}
951
 
952
	fibril_mutex_lock(&file->lock);
953
 
954
	int fs_phone = vfs_grab_phone(file->node->fs_handle);
955
 
956
	aid_t msg;
957
	msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->dev_handle,
958
	    file->node->index, true, NULL);
959
	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
960
	async_wait_for(msg, &rc);
961
	vfs_release_phone(fs_phone);
962
 
963
	fibril_mutex_unlock(&file->lock);
964
	ipc_answer_0(rid, rc);
965
}
966
 
967
void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
968
{
4587 jermar 969
	size_t len;
970
	ipc_callid_t callid;
971
 
972
	if (!ipc_data_write_receive(&callid, &len)) {
973
		ipc_answer_0(callid, EINVAL);
974
		ipc_answer_0(rid, EINVAL);
975
		return;
976
	}
977
	char *path = malloc(len + 1);
978
	if (!path) {
979
		ipc_answer_0(callid, ENOMEM);
980
		ipc_answer_0(rid, ENOMEM);
981
		return;
982
	}
983
	int rc;
984
	if ((rc = ipc_data_write_finalize(callid, path, len))) {
985
		ipc_answer_0(rid, rc);
986
		free(path);
987
		return;
988
	}
989
	path[len] = '\0';
990
 
991
	if (!ipc_data_read_receive(&callid, NULL)) {
992
		free(path);
993
		ipc_answer_0(callid, EINVAL);
994
		ipc_answer_0(rid, EINVAL);
995
		return;
996
	}
997
 
998
	vfs_lookup_res_t lr;
999
	fibril_rwlock_read_lock(&namespace_rwlock);
1000
	rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
1001
	free(path);
1002
	if (rc != EOK) {
1003
		fibril_rwlock_read_unlock(&namespace_rwlock);
1004
		ipc_answer_0(callid, rc);
1005
		ipc_answer_0(rid, rc);
1006
		return;
1007
	}
1008
	vfs_node_t *node = vfs_node_get(&lr);
1009
	if (!node) {
1010
		fibril_rwlock_read_unlock(&namespace_rwlock);
1011
		ipc_answer_0(callid, ENOMEM);
1012
		ipc_answer_0(rid, ENOMEM);
1013
		return;
1014
	}
1015
 
1016
	fibril_rwlock_read_unlock(&namespace_rwlock);
1017
 
1018
	int fs_phone = vfs_grab_phone(node->fs_handle);
1019
	aid_t msg;
1020
	msg = async_send_3(fs_phone, VFS_OUT_STAT, node->dev_handle,
1021
	    node->index, false, NULL);
1022
	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
4593 decky 1023
 
1024
	ipcarg_t rv;
1025
	async_wait_for(msg, &rv);
4587 jermar 1026
	vfs_release_phone(fs_phone);
1027
 
4593 decky 1028
	ipc_answer_0(rid, rv);
4587 jermar 1029
 
1030
	vfs_node_put(node);
4585 jermar 1031
}
1032
 
2707 jermar 1033
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1034
{
1035
	int mode = IPC_GET_ARG1(*request);
2735 jermar 1036
 
2707 jermar 1037
	size_t len;
1038
	ipc_callid_t callid;
1039
 
1040
	if (!ipc_data_write_receive(&callid, &len)) {
1041
		ipc_answer_0(callid, EINVAL);
1042
		ipc_answer_0(rid, EINVAL);
1043
		return;
1044
	}
2752 jermar 1045
	char *path = malloc(len + 1);
2707 jermar 1046
	if (!path) {
1047
		ipc_answer_0(callid, ENOMEM);
1048
		ipc_answer_0(rid, ENOMEM);
1049
		return;
1050
	}
1051
	int rc;
1052
	if ((rc = ipc_data_write_finalize(callid, path, len))) {
1053
		ipc_answer_0(rid, rc);
1054
		free(path);
1055
		return;
1056
	}
2752 jermar 1057
	path[len] = '\0';
4684 svoboda 1058
 
1059
	/* Ignore mode for now. */
1060
	(void) mode;
2707 jermar 1061
 
4518 jermar 1062
	fibril_rwlock_write_lock(&namespace_rwlock);
2707 jermar 1063
	int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
2752 jermar 1064
	rc = vfs_lookup_internal(path, lflag, NULL, NULL);
4518 jermar 1065
	fibril_rwlock_write_unlock(&namespace_rwlock);
2707 jermar 1066
	free(path);
1067
	ipc_answer_0(rid, rc);
1068
}
1069
 
2735 jermar 1070
void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1071
{
1072
	int lflag = IPC_GET_ARG1(*request);
1073
 
1074
	size_t len;
1075
	ipc_callid_t callid;
1076
 
1077
	if (!ipc_data_write_receive(&callid, &len)) {
1078
		ipc_answer_0(callid, EINVAL);
1079
		ipc_answer_0(rid, EINVAL);
1080
		return;
1081
	}
2752 jermar 1082
	char *path = malloc(len + 1);
2735 jermar 1083
	if (!path) {
1084
		ipc_answer_0(callid, ENOMEM);
1085
		ipc_answer_0(rid, ENOMEM);
1086
		return;
1087
	}
1088
	int rc;
1089
	if ((rc = ipc_data_write_finalize(callid, path, len))) {
1090
		ipc_answer_0(rid, rc);
1091
		free(path);
1092
		return;
1093
	}
2752 jermar 1094
	path[len] = '\0';
2735 jermar 1095
 
4518 jermar 1096
	fibril_rwlock_write_lock(&namespace_rwlock);
2735 jermar 1097
	lflag &= L_DIRECTORY;	/* sanitize lflag */
1098
	vfs_lookup_res_t lr;
2763 jermar 1099
	rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
2735 jermar 1100
	free(path);
1101
	if (rc != EOK) {
4518 jermar 1102
		fibril_rwlock_write_unlock(&namespace_rwlock);
2735 jermar 1103
		ipc_answer_0(rid, rc);
1104
		return;
1105
	}
1106
 
1107
	/*
1108
	 * The name has already been unlinked by vfs_lookup_internal().
1109
	 * We have to get and put the VFS node to ensure that it is
4584 jermar 1110
	 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
2735 jermar 1111
	 */
1112
	vfs_node_t *node = vfs_node_get(&lr);
4555 jermar 1113
	fibril_mutex_lock(&nodes_mutex);
2735 jermar 1114
	node->lnkcnt--;
4555 jermar 1115
	fibril_mutex_unlock(&nodes_mutex);
4518 jermar 1116
	fibril_rwlock_write_unlock(&namespace_rwlock);
2735 jermar 1117
	vfs_node_put(node);
1118
	ipc_answer_0(rid, EOK);
1119
}
1120
 
2763 jermar 1121
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1122
{
4279 svoboda 1123
	size_t olen, nlen;
2763 jermar 1124
	ipc_callid_t callid;
1125
	int rc;
1126
 
1127
	/* Retrieve the old path. */
4279 svoboda 1128
	if (!ipc_data_write_receive(&callid, &olen)) {
2763 jermar 1129
		ipc_answer_0(callid, EINVAL);
1130
		ipc_answer_0(rid, EINVAL);
1131
		return;
1132
	}
4279 svoboda 1133
	char *old = malloc(olen + 1);
2763 jermar 1134
	if (!old) {
1135
		ipc_answer_0(callid, ENOMEM);
1136
		ipc_answer_0(rid, ENOMEM);
1137
		return;
1138
	}
4279 svoboda 1139
	if ((rc = ipc_data_write_finalize(callid, old, olen))) {
2763 jermar 1140
		ipc_answer_0(rid, rc);
1141
		free(old);
1142
		return;
1143
	}
4279 svoboda 1144
	old[olen] = '\0';
2763 jermar 1145
 
1146
	/* Retrieve the new path. */
4279 svoboda 1147
	if (!ipc_data_write_receive(&callid, &nlen)) {
2763 jermar 1148
		ipc_answer_0(callid, EINVAL);
1149
		ipc_answer_0(rid, EINVAL);
1150
		free(old);
1151
		return;
1152
	}
4279 svoboda 1153
	char *new = malloc(nlen + 1);
2763 jermar 1154
	if (!new) {
1155
		ipc_answer_0(callid, ENOMEM);
1156
		ipc_answer_0(rid, ENOMEM);
1157
		free(old);
1158
		return;
1159
	}
4279 svoboda 1160
	if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
2763 jermar 1161
		ipc_answer_0(rid, rc);
1162
		free(old);
1163
		free(new);
1164
		return;
1165
	}
4279 svoboda 1166
	new[nlen] = '\0';
2763 jermar 1167
 
4279 svoboda 1168
	char *oldc = canonify(old, &olen);
1169
	char *newc = canonify(new, &nlen);
2763 jermar 1170
	if (!oldc || !newc) {
1171
		ipc_answer_0(rid, EINVAL);
1172
		free(old);
1173
		free(new);
1174
		return;
1175
	}
4279 svoboda 1176
	oldc[olen] = '\0';
1177
	newc[nlen] = '\0';
4366 jermar 1178
	if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1179
	    ((newc[str_length(oldc)] == '/') ||
1180
	    (str_length(oldc) == 1) ||
1181
	    (str_length(oldc) == str_length(newc)))) {
1182
	    	/*
1183
		 * oldc is a prefix of newc and either
1184
		 * - newc continues with a / where oldc ends, or
1185
		 * - oldc was / itself, or
1186
		 * - oldc and newc are equal.
1187
		 */
2763 jermar 1188
		ipc_answer_0(rid, EINVAL);
1189
		free(old);
1190
		free(new);
1191
		return;
1192
	}
1193
 
1194
	vfs_lookup_res_t old_lr;
1195
	vfs_lookup_res_t new_lr;
1196
	vfs_lookup_res_t new_par_lr;
4518 jermar 1197
	fibril_rwlock_write_lock(&namespace_rwlock);
2763 jermar 1198
	/* Lookup the node belonging to the old file name. */
1199
	rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1200
	if (rc != EOK) {
4518 jermar 1201
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1202
		ipc_answer_0(rid, rc);
1203
		free(old);
1204
		free(new);
1205
		return;
1206
	}
1207
	vfs_node_t *old_node = vfs_node_get(&old_lr);
1208
	if (!old_node) {
4518 jermar 1209
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1210
		ipc_answer_0(rid, ENOMEM);
1211
		free(old);
1212
		free(new);
1213
		return;
1214
	}
4368 jermar 1215
	/* Determine the path to the parent of the node with the new name. */
1216
	char *parentc = str_dup(newc);
1217
	if (!parentc) {
4518 jermar 1218
		fibril_rwlock_write_unlock(&namespace_rwlock);
4368 jermar 1219
		ipc_answer_0(rid, rc);
1220
		free(old);
1221
		free(new);
1222
		return;
1223
	}
4427 jermar 1224
	char *lastsl = str_rchr(parentc + 1, '/');
4368 jermar 1225
	if (lastsl)
1226
		*lastsl = '\0';
1227
	else
1228
		parentc[1] = '\0';
2763 jermar 1229
	/* Lookup parent of the new file name. */
4368 jermar 1230
	rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1231
	free(parentc);	/* not needed anymore */
2763 jermar 1232
	if (rc != EOK) {
4518 jermar 1233
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1234
		ipc_answer_0(rid, rc);
1235
		free(old);
1236
		free(new);
1237
		return;
1238
	}
1239
	/* Check whether linking to the same file system instance. */
1240
	if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1241
	    (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
4518 jermar 1242
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1243
		ipc_answer_0(rid, EXDEV);	/* different file systems */
1244
		free(old);
1245
		free(new);
1246
		return;
1247
	}
1248
	/* Destroy the old link for the new name. */
1249
	vfs_node_t *new_node = NULL;
1250
	rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1251
	switch (rc) {
1252
	case ENOENT:
1253
		/* simply not in our way */
1254
		break;
1255
	case EOK:
1256
		new_node = vfs_node_get(&new_lr);
1257
		if (!new_node) {
4518 jermar 1258
			fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1259
			ipc_answer_0(rid, ENOMEM);
1260
			free(old);
1261
			free(new);
1262
			return;
1263
		}
4555 jermar 1264
		fibril_mutex_lock(&nodes_mutex);
2763 jermar 1265
		new_node->lnkcnt--;
4555 jermar 1266
		fibril_mutex_unlock(&nodes_mutex);
2763 jermar 1267
		break;
1268
	default:
4518 jermar 1269
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1270
		ipc_answer_0(rid, ENOTEMPTY);
1271
		free(old);
1272
		free(new);
1273
		return;
1274
	}
1275
	/* Create the new link for the new name. */
1276
	rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1277
	if (rc != EOK) {
4518 jermar 1278
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1279
		if (new_node)
1280
			vfs_node_put(new_node);
1281
		ipc_answer_0(rid, rc);
1282
		free(old);
1283
		free(new);
1284
		return;
1285
	}
4555 jermar 1286
	fibril_mutex_lock(&nodes_mutex);
2763 jermar 1287
	old_node->lnkcnt++;
4555 jermar 1288
	fibril_mutex_unlock(&nodes_mutex);
2763 jermar 1289
	/* Destroy the link for the old name. */
1290
	rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1291
	if (rc != EOK) {
4518 jermar 1292
		fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1293
		vfs_node_put(old_node);
1294
		if (new_node)
1295
			vfs_node_put(new_node);
1296
		ipc_answer_0(rid, rc);
1297
		free(old);
1298
		free(new);
1299
		return;
1300
	}
4555 jermar 1301
	fibril_mutex_lock(&nodes_mutex);
2763 jermar 1302
	old_node->lnkcnt--;
4555 jermar 1303
	fibril_mutex_unlock(&nodes_mutex);
4518 jermar 1304
	fibril_rwlock_write_unlock(&namespace_rwlock);
2763 jermar 1305
	vfs_node_put(old_node);
1306
	if (new_node)
1307
		vfs_node_put(new_node);
1308
	free(old);
1309
	free(new);
1310
	ipc_answer_0(rid, EOK);
1311
}
1312
 
2689 jermar 1313
/**
1314
 * @}
4463 decky 1315
 */