xref: /haiku/src/system/boot/loader/vfs.cpp (revision 16c83730262f1e4f0fc69d80744bb36dcfbbe3af)
1 /*
2  * Copyright 2003-2013, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <boot/vfs.h>
8 
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <sys/uio.h>
13 #include <unistd.h>
14 
15 #include <StorageDefs.h>
16 
17 #include <AutoDeleter.h>
18 
19 #include <boot/platform.h>
20 #include <boot/partitions.h>
21 #include <boot/stdio.h>
22 #include <boot/stage2.h>
23 #include <syscall_utils.h>
24 
25 #include "RootFileSystem.h"
26 #include "file_systems/packagefs/packagefs.h"
27 
28 
29 using namespace boot;
30 
31 //#define TRACE_VFS
32 #ifdef TRACE_VFS
33 #	define TRACE(x) dprintf x
34 #else
35 #	define TRACE(x) ;
36 #endif
37 
38 
39 class Descriptor {
40 	public:
41 		Descriptor(Node *node, void *cookie);
42 		~Descriptor();
43 
44 		ssize_t ReadAt(off_t pos, void *buffer, size_t bufferSize);
45 		ssize_t Read(void *buffer, size_t bufferSize);
46 		ssize_t WriteAt(off_t pos, const void *buffer, size_t bufferSize);
47 		ssize_t Write(const void *buffer, size_t bufferSize);
48 
49 		status_t Stat(struct stat &stat);
50 
51 		off_t Offset() const { return fOffset; }
52 		int32 RefCount() const { return fRefCount; }
53 
54 		status_t Acquire();
55 		status_t Release();
56 
57 		Node *GetNode() const { return fNode; }
58 
59 	private:
60 		Node	*fNode;
61 		void	*fCookie;
62 		off_t	fOffset;
63 		int32	fRefCount;
64 };
65 
66 #define MAX_VFS_DESCRIPTORS 64
67 
68 NodeList gBootDevices;
69 NodeList gPartitions;
70 RootFileSystem *gRoot;
71 static Descriptor *sDescriptors[MAX_VFS_DESCRIPTORS];
72 static Node *sBootDevice;
73 
74 
75 Node::Node()
76 	:
77 	fRefCount(1)
78 {
79 }
80 
81 
82 Node::~Node()
83 {
84 }
85 
86 
87 status_t
88 Node::Open(void **_cookie, int mode)
89 {
90 	TRACE(("%p::Open()\n", this));
91 	return Acquire();
92 }
93 
94 
95 status_t
96 Node::Close(void *cookie)
97 {
98 	TRACE(("%p::Close()\n", this));
99 	return Release();
100 }
101 
102 
103 status_t
104 Node::ReadLink(char* buffer, size_t bufferSize)
105 {
106 	return B_BAD_VALUE;
107 }
108 
109 
110 status_t
111 Node::GetName(char *nameBuffer, size_t bufferSize) const
112 {
113 	return B_ERROR;
114 }
115 
116 
117 status_t
118 Node::GetFileMap(struct file_map_run *runs, int32 *count)
119 {
120 	return B_ERROR;
121 }
122 
123 
124 int32
125 Node::Type() const
126 {
127 	return 0;
128 }
129 
130 
131 off_t
132 Node::Size() const
133 {
134 	return 0LL;
135 }
136 
137 
138 ino_t
139 Node::Inode() const
140 {
141 	return 0;
142 }
143 
144 
145 status_t
146 Node::Acquire()
147 {
148 	fRefCount++;
149 	TRACE(("%p::Acquire(), fRefCount = %ld\n", this, fRefCount));
150 	return B_OK;
151 }
152 
153 
154 status_t
155 Node::Release()
156 {
157 	TRACE(("%p::Release(), fRefCount = %ld\n", this, fRefCount));
158 	if (--fRefCount == 0) {
159 		TRACE(("delete node: %p\n", this));
160 		delete this;
161 		return 1;
162 	}
163 
164 	return B_OK;
165 }
166 
167 
168 //	#pragma mark -
169 
170 
171 ConsoleNode::ConsoleNode()
172 	: Node()
173 {
174 }
175 
176 
177 ssize_t
178 ConsoleNode::Read(void *buffer, size_t bufferSize)
179 {
180 	return ReadAt(NULL, -1, buffer, bufferSize);
181 }
182 
183 
184 ssize_t
185 ConsoleNode::Write(const void *buffer, size_t bufferSize)
186 {
187 	return WriteAt(NULL, -1, buffer, bufferSize);
188 }
189 
190 
191 //	#pragma mark -
192 
193 
194 Directory::Directory()
195 	: Node()
196 {
197 }
198 
199 
200 ssize_t
201 Directory::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
202 {
203 	return B_ERROR;
204 }
205 
206 
207 ssize_t
208 Directory::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize)
209 {
210 	return B_ERROR;
211 }
212 
213 
214 int32
215 Directory::Type() const
216 {
217 	return S_IFDIR;
218 }
219 
220 
221 Node*
222 Directory::Lookup(const char* name, bool traverseLinks)
223 {
224 	Node* node = LookupDontTraverse(name);
225 	if (node == NULL)
226 		return NULL;
227 
228 	if (!traverseLinks || !S_ISLNK(node->Type()))
229 		return node;
230 
231 	// the node is a symbolic link, so we have to resolve the path
232 	char linkPath[B_PATH_NAME_LENGTH];
233 	status_t error = node->ReadLink(linkPath, sizeof(linkPath));
234 
235 	node->Release();
236 		// we don't need this one anymore
237 
238 	if (error != B_OK)
239 		return NULL;
240 
241 	// let open_from() do the real work
242 	int fd = open_from(this, linkPath, O_RDONLY);
243 	if (fd < 0)
244 		return NULL;
245 
246 	node = get_node_from(fd);
247 	if (node != NULL)
248 		node->Acquire();
249 
250 	close(fd);
251 	return node;
252 }
253 
254 
255 status_t
256 Directory::CreateFile(const char *name, mode_t permissions, Node **_node)
257 {
258 	return EROFS;
259 }
260 
261 
262 //	#pragma mark -
263 
264 
265 MemoryDisk::MemoryDisk(const uint8* data, size_t size, const char* name)
266 	: Node(),
267 	  fData(data),
268 	  fSize(size)
269 {
270 	strlcpy(fName, name, sizeof(fName));
271 }
272 
273 
274 ssize_t
275 MemoryDisk::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize)
276 {
277 	if (pos >= fSize)
278 		return 0;
279 
280 	if (pos + bufferSize > fSize)
281 		bufferSize = fSize - pos;
282 
283 	memcpy(buffer, fData + pos, bufferSize);
284 	return bufferSize;
285 }
286 
287 
288 ssize_t
289 MemoryDisk::WriteAt(void* cookie, off_t pos, const void* buffer,
290 	size_t bufferSize)
291 {
292 	return B_NOT_ALLOWED;
293 }
294 
295 
296 off_t
297 MemoryDisk::Size() const
298 {
299 	return fSize;
300 }
301 
302 
303 status_t
304 MemoryDisk::GetName(char *nameBuffer, size_t bufferSize) const
305 {
306 	if (!nameBuffer)
307 		return B_BAD_VALUE;
308 
309 	strlcpy(nameBuffer, fName, bufferSize);
310 	return B_OK;
311 }
312 
313 
314 //	#pragma mark -
315 
316 
317 Descriptor::Descriptor(Node *node, void *cookie)
318 	:
319 	fNode(node),
320 	fCookie(cookie),
321 	fOffset(0),
322 	fRefCount(1)
323 {
324 }
325 
326 
327 Descriptor::~Descriptor()
328 {
329 }
330 
331 
332 ssize_t
333 Descriptor::Read(void *buffer, size_t bufferSize)
334 {
335 	ssize_t bytesRead = fNode->ReadAt(fCookie, fOffset, buffer, bufferSize);
336 	if (bytesRead > B_OK)
337 		fOffset += bytesRead;
338 
339 	return bytesRead;
340 }
341 
342 
343 ssize_t
344 Descriptor::ReadAt(off_t pos, void *buffer, size_t bufferSize)
345 {
346 	return fNode->ReadAt(fCookie, pos, buffer, bufferSize);
347 }
348 
349 
350 ssize_t
351 Descriptor::Write(const void *buffer, size_t bufferSize)
352 {
353 	ssize_t bytesWritten = fNode->WriteAt(fCookie, fOffset, buffer, bufferSize);
354 	if (bytesWritten > B_OK)
355 		fOffset += bytesWritten;
356 
357 	return bytesWritten;
358 }
359 
360 
361 ssize_t
362 Descriptor::WriteAt(off_t pos, const void *buffer, size_t bufferSize)
363 {
364 	return fNode->WriteAt(fCookie, pos, buffer, bufferSize);
365 }
366 
367 
368 status_t
369 Descriptor::Stat(struct stat &stat)
370 {
371 	stat.st_mode = fNode->Type();
372 	stat.st_size = fNode->Size();
373 	stat.st_ino = fNode->Inode();
374 
375 	return B_OK;
376 }
377 
378 
379 status_t
380 Descriptor::Acquire()
381 {
382 	fRefCount++;
383 	return B_OK;
384 }
385 
386 
387 status_t
388 Descriptor::Release()
389 {
390 	if (--fRefCount == 0) {
391 		status_t status = fNode->Close(fCookie);
392 		if (status != B_OK)
393 			return status;
394 	}
395 
396 	return B_OK;
397 }
398 
399 
400 //	#pragma mark -
401 
402 
403 BootVolume::BootVolume()
404 	:
405 	fRootDirectory(NULL),
406 	fSystemDirectory(NULL),
407 	fPackaged(false)
408 {
409 }
410 
411 
412 BootVolume::~BootVolume()
413 {
414 	Unset();
415 }
416 
417 
418 status_t
419 BootVolume::SetTo(Directory* rootDirectory)
420 {
421 	Unset();
422 
423 	if (rootDirectory == NULL)
424 		return B_BAD_VALUE;
425 
426 	fRootDirectory = rootDirectory;
427 
428 	// find the system directory
429 	Node* systemNode = fRootDirectory->Lookup("system", true);
430 	if (systemNode == NULL || !S_ISDIR(systemNode->Type())) {
431 		if (systemNode != NULL)
432 			systemNode->Release();
433 		Unset();
434 		return B_ENTRY_NOT_FOUND;
435 	}
436 
437 	fSystemDirectory = static_cast<Directory*>(systemNode);
438 
439 	// try opening the system package
440 	int packageFD = _OpenSystemPackage();
441 	fPackaged = packageFD >= 0;
442 	if (!fPackaged)
443 		return B_OK;
444 
445 	// the system is packaged -- mount the packagefs
446 	Directory* packageRootDirectory;
447 	status_t error = packagefs_mount_file(packageFD, packageRootDirectory);
448 	close(packageFD);
449 	if (error != B_OK) {
450 		Unset();
451 		return error;
452 	}
453 
454 	fSystemDirectory->Release();
455 	fSystemDirectory = packageRootDirectory;
456 
457 	return B_OK;
458 }
459 
460 
461 void
462 BootVolume::Unset()
463 {
464 	if (fRootDirectory != NULL) {
465 		fRootDirectory->Release();
466 		fRootDirectory = NULL;
467 	}
468 
469 	if (fSystemDirectory != NULL) {
470 		fSystemDirectory->Release();
471 		fSystemDirectory = NULL;
472 	}
473 
474 	fPackaged = false;
475 }
476 
477 
478 int
479 BootVolume::_OpenSystemPackage()
480 {
481 	// open the packages directory
482 	Node* packagesNode = fSystemDirectory->Lookup("packages", false);
483 	if (packagesNode == NULL)
484 		return -1;
485 	MethodDeleter<Node, status_t> packagesNodeReleaser(packagesNode,
486 		&Node::Release);
487 
488 	if (!S_ISDIR(packagesNode->Type()))
489 		return -1;
490 	Directory* packageDirectory = (Directory*)packagesNode;
491 
492 	// search for the system package
493 	int fd = -1;
494 	void* cookie;
495 	if (packageDirectory->Open(&cookie, O_RDONLY) == B_OK) {
496 		char name[B_FILE_NAME_LENGTH];
497 		while (packageDirectory->GetNextEntry(cookie, name, sizeof(name))
498 				== B_OK) {
499 			// The name must end with ".hpkg".
500 			size_t nameLength = strlen(name);
501 			if (nameLength < 6 || strcmp(name + nameLength - 5, ".hpkg") != 0)
502 				continue;
503 
504 			// The name must either be "haiku.hpkg" or start with "haiku-".
505 			if (strcmp(name, "haiku.hpkg") == 0
506 				|| strncmp(name, "haiku-", 6) == 0) {
507 				fd = open_from(packageDirectory, name, O_RDONLY);
508 				break;
509 			}
510 		}
511 		packageDirectory->Close(cookie);
512 	}
513 
514 	return fd;
515 }
516 
517 
518 //	#pragma mark -
519 
520 
521 status_t
522 vfs_init(stage2_args *args)
523 {
524 	gRoot = new(nothrow) RootFileSystem();
525 	if (gRoot == NULL)
526 		return B_NO_MEMORY;
527 
528 	return B_OK;
529 }
530 
531 
532 status_t
533 register_boot_file_system(BootVolume& bootVolume)
534 {
535 	Directory* rootDirectory = bootVolume.RootDirectory();
536 	gRoot->AddLink("boot", rootDirectory);
537 
538 	Partition *partition;
539 	status_t status = gRoot->GetPartitionFor(rootDirectory, &partition);
540 	if (status != B_OK) {
541 		dprintf("register_boot_file_system(): could not locate boot volume in "
542 			"root!\n");
543 		return status;
544 	}
545 
546 	gBootVolume.SetInt64(BOOT_VOLUME_PARTITION_OFFSET,
547 		partition->offset);
548 	if (bootVolume.IsPackaged())
549 		gBootVolume.SetBool(BOOT_VOLUME_PACKAGED, true);
550 
551 	Node *device = get_node_from(partition->FD());
552 	if (device == NULL) {
553 		dprintf("register_boot_file_system(): could not get boot device!\n");
554 		return B_ERROR;
555 	}
556 
557 	return platform_register_boot_device(device);
558 }
559 
560 
561 /*! Gets the boot device, scans all of its partitions, gets the
562 	boot partition, and mounts its file system.
563 
564 	\param args The stage 2 arguments.
565 	\param _bootVolume On success set to the boot volume.
566 	\return \c B_OK on success, another error code otherwise.
567 */
568 status_t
569 get_boot_file_system(stage2_args* args, BootVolume& _bootVolume)
570 {
571 	Node *device;
572 	status_t error = platform_add_boot_device(args, &gBootDevices);
573 	if (error != B_OK)
574 		return error;
575 
576 	// the boot device must be the first device in the list
577 	device = gBootDevices.First();
578 
579 	error = add_partitions_for(device, false, true);
580 	if (error != B_OK)
581 		return error;
582 
583 	Partition *partition;
584 	error = platform_get_boot_partition(args, device, &gPartitions, &partition);
585 	if (error != B_OK)
586 		return error;
587 
588 	Directory *fileSystem;
589 	error = partition->Mount(&fileSystem, true);
590 	if (error != B_OK) {
591 		// this partition doesn't contain any known file system; we
592 		// don't need it anymore
593 		gPartitions.Remove(partition);
594 		delete partition;
595 		return error;
596 	}
597 
598 	// init the BootVolume
599 	error = _bootVolume.SetTo(fileSystem);
600 	if (error != B_OK)
601 		return error;
602 
603 	sBootDevice = device;
604 	return B_OK;
605 }
606 
607 
608 /** Mounts all file systems recognized on the given device by
609  *	calling the add_partitions_for() function on them.
610  */
611 
612 status_t
613 mount_file_systems(stage2_args *args)
614 {
615 	// mount other partitions on boot device (if any)
616 	NodeIterator iterator = gPartitions.GetIterator();
617 
618 	Partition *partition = NULL;
619 	while ((partition = (Partition *)iterator.Next()) != NULL) {
620 		// don't scan known partitions again
621 		if (partition->IsFileSystem())
622 			continue;
623 
624 		// remove the partition if it doesn't contain a (known) file system
625 		if (partition->Scan(true) != B_OK && !partition->IsFileSystem()) {
626 			gPartitions.Remove(partition);
627 			delete partition;
628 		}
629 	}
630 
631 	// add all block devices the platform has for us
632 
633 	status_t status = platform_add_block_devices(args, &gBootDevices);
634 	if (status < B_OK)
635 		return status;
636 
637 	iterator = gBootDevices.GetIterator();
638 	Node *device = NULL, *last = NULL;
639 	while ((device = iterator.Next()) != NULL) {
640 		// don't scan former boot device again
641 		if (device == sBootDevice)
642 			continue;
643 
644 		if (add_partitions_for(device, true) == B_OK) {
645 			// ToDo: we can't delete the object here, because it must
646 			//	be removed from the list before we know that it was
647 			//	deleted.
648 
649 /*			// if the Release() deletes the object, we need to skip it
650 			if (device->Release() > 0) {
651 				list_remove_item(&gBootDevices, device);
652 				device = last;
653 			}
654 */
655 (void)last;
656 		}
657 		last = device;
658 	}
659 
660 	if (gPartitions.IsEmpty())
661 		return B_ENTRY_NOT_FOUND;
662 
663 #if 0
664 	void *cookie;
665 	if (gRoot->Open(&cookie, O_RDONLY) == B_OK) {
666 		Directory *directory;
667 		while (gRoot->GetNextNode(cookie, (Node **)&directory) == B_OK) {
668 			char name[256];
669 			if (directory->GetName(name, sizeof(name)) == B_OK)
670 				printf(":: %s (%p)\n", name, directory);
671 
672 			void *subCookie;
673 			if (directory->Open(&subCookie, O_RDONLY) == B_OK) {
674 				while (directory->GetNextEntry(subCookie, name, sizeof(name)) == B_OK) {
675 					printf("\t%s\n", name);
676 				}
677 				directory->Close(subCookie);
678 			}
679 		}
680 		gRoot->Close(cookie);
681 	}
682 #endif
683 
684 	return B_OK;
685 }
686 
687 
688 /*!	Resolves \a directory + \a path to a node.
689 	Note that \a path will be modified by the function.
690 */
691 static status_t
692 get_node_for_path(Directory *directory, char *path, Node **_node)
693 {
694 	directory->Acquire();
695 		// balance Acquire()/Release() calls
696 
697 	while (true) {
698 		Node *nextNode;
699 		char *nextPath;
700 
701 		// walk to find the next path component ("path" will point to a single
702 		// path component), and filter out multiple slashes
703 		for (nextPath = path + 1; nextPath[0] != '\0' && nextPath[0] != '/'; nextPath++);
704 
705 		if (*nextPath == '/') {
706 			*nextPath = '\0';
707 			do
708 				nextPath++;
709 			while (*nextPath == '/');
710 		}
711 
712 		nextNode = directory->Lookup(path, true);
713 		directory->Release();
714 
715 		if (nextNode == NULL)
716 			return B_ENTRY_NOT_FOUND;
717 
718 		path = nextPath;
719 		if (S_ISDIR(nextNode->Type()))
720 			directory = (Directory *)nextNode;
721 		else if (path[0])
722 			return B_NOT_ALLOWED;
723 
724 		// are we done?
725 		if (path[0] == '\0') {
726 			*_node = nextNode;
727 			return B_OK;
728 		}
729 	}
730 
731 	return B_ENTRY_NOT_FOUND;
732 }
733 
734 
735 //	#pragma mark -
736 
737 
738 static Descriptor *
739 get_descriptor(int fd)
740 {
741 	if (fd < 0 || fd >= MAX_VFS_DESCRIPTORS)
742 		return NULL;
743 
744 	return sDescriptors[fd];
745 }
746 
747 
748 static void
749 free_descriptor(int fd)
750 {
751 	if (fd >= MAX_VFS_DESCRIPTORS)
752 		return;
753 
754 	delete sDescriptors[fd];
755 	sDescriptors[fd] = NULL;
756 }
757 
758 
759 /**	Reserves an entry of the descriptor table and
760  *	assigns the given node to it.
761  */
762 
763 int
764 open_node(Node *node, int mode)
765 {
766 	if (node == NULL)
767 		return B_ERROR;
768 
769 	// get free descriptor
770 
771 	int fd = 0;
772 	for (; fd < MAX_VFS_DESCRIPTORS; fd++) {
773 		if (sDescriptors[fd] == NULL)
774 			break;
775 	}
776 	if (fd == MAX_VFS_DESCRIPTORS)
777 		return B_ERROR;
778 
779 	TRACE(("got descriptor %d for node %p\n", fd, node));
780 
781 	// we got a free descriptor entry, now try to open the node
782 
783 	void *cookie;
784 	status_t status = node->Open(&cookie, mode);
785 	if (status < B_OK)
786 		return status;
787 
788 	TRACE(("could open node at %p\n", node));
789 
790 	Descriptor *descriptor = new(nothrow) Descriptor(node, cookie);
791 	if (descriptor == NULL)
792 		return B_NO_MEMORY;
793 
794 	sDescriptors[fd] = descriptor;
795 
796 	return fd;
797 }
798 
799 
800 int
801 dup(int fd)
802 {
803 	Descriptor *descriptor = get_descriptor(fd);
804 	if (descriptor == NULL)
805 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
806 
807 	descriptor->Acquire();
808 	RETURN_AND_SET_ERRNO(fd);
809 }
810 
811 
812 ssize_t
813 read_pos(int fd, off_t offset, void *buffer, size_t bufferSize)
814 {
815 	Descriptor *descriptor = get_descriptor(fd);
816 	if (descriptor == NULL)
817 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
818 
819 	RETURN_AND_SET_ERRNO(descriptor->ReadAt(offset, buffer, bufferSize));
820 }
821 
822 
823 ssize_t
824 pread(int fd, void* buffer, size_t bufferSize, off_t offset)
825 {
826 	return read_pos(fd, offset, buffer, bufferSize);
827 }
828 
829 
830 ssize_t
831 read(int fd, void *buffer, size_t bufferSize)
832 {
833 	Descriptor *descriptor = get_descriptor(fd);
834 	if (descriptor == NULL)
835 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
836 
837 	RETURN_AND_SET_ERRNO(descriptor->Read(buffer, bufferSize));
838 }
839 
840 
841 ssize_t
842 write_pos(int fd, off_t offset, const void *buffer, size_t bufferSize)
843 {
844 	Descriptor *descriptor = get_descriptor(fd);
845 	if (descriptor == NULL)
846 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
847 
848 	RETURN_AND_SET_ERRNO(descriptor->WriteAt(offset, buffer, bufferSize));
849 }
850 
851 
852 ssize_t
853 write(int fd, const void *buffer, size_t bufferSize)
854 {
855 	Descriptor *descriptor = get_descriptor(fd);
856 	if (descriptor == NULL)
857 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
858 
859 	RETURN_AND_SET_ERRNO(descriptor->Write(buffer, bufferSize));
860 }
861 
862 
863 ssize_t
864 writev(int fd, const struct iovec* vecs, size_t count)
865 {
866 	size_t totalWritten = 0;
867 
868 	for (size_t i = 0; i < count; i++) {
869 		ssize_t written = write(fd, vecs[i].iov_base, vecs[i].iov_len);
870 		if (written < 0)
871 			return totalWritten == 0 ? written : totalWritten;
872 
873 		totalWritten += written;
874 
875 		if ((size_t)written != vecs[i].iov_len)
876 			break;
877 	}
878 
879 	return totalWritten;
880 }
881 
882 
883 int
884 open(const char *name, int mode, ...)
885 {
886 	mode_t permissions = 0;
887 	if ((mode & O_CREAT) != 0) {
888         va_list args;
889         va_start(args, mode);
890         permissions = va_arg(args, int) /*& ~__gUmask*/;
891             // adapt the permissions as required by POSIX
892         va_end(args);
893 	}
894 
895 	// we always start at the top (there is no notion of a current directory (yet?))
896 	RETURN_AND_SET_ERRNO(open_from(gRoot, name, mode, permissions));
897 }
898 
899 
900 int
901 open_from(Directory *directory, const char *name, int mode, mode_t permissions)
902 {
903 	if (name[0] == '/') {
904 		// ignore the directory and start from root if we are asked to do that
905 		directory = gRoot;
906 		name++;
907 	}
908 
909 	char path[B_PATH_NAME_LENGTH];
910 	if (strlcpy(path, name, sizeof(path)) >= sizeof(path))
911 		return B_NAME_TOO_LONG;
912 
913 	Node *node;
914 	status_t error = get_node_for_path(directory, path, &node);
915 	if (error != B_OK) {
916 		if (error != B_ENTRY_NOT_FOUND)
917 			return error;
918 
919 		if ((mode & O_CREAT) == 0)
920 			return B_ENTRY_NOT_FOUND;
921 
922 		// try to resolve the parent directory
923 		strlcpy(path, name, sizeof(path));
924 		if (char* lastSlash = strrchr(path, '/')) {
925 			if (lastSlash[1] == '\0')
926 				return B_ENTRY_NOT_FOUND;
927 
928 			lastSlash = '\0';
929 			name = lastSlash + 1;
930 
931 			// resolve the directory
932 			if (get_node_for_path(directory, path, &node) != B_OK)
933 				return B_ENTRY_NOT_FOUND;
934 
935 			if (node->Type() != S_IFDIR) {
936 				node->Release();
937 				return B_NOT_A_DIRECTORY;
938 			}
939 
940 			directory = static_cast<Directory*>(node);
941 		} else
942 			directory->Acquire();
943 
944 		// create the file
945 		error = directory->CreateFile(name, permissions, &node);
946 		directory->Release();
947 
948 		if (error != B_OK)
949 			return error;
950 	} else if ((mode & O_EXCL) != 0) {
951 		node->Release();
952 		return B_FILE_EXISTS;
953 	}
954 
955 	int fd = open_node(node, mode);
956 
957 	node->Release();
958 	return fd;
959 }
960 
961 
962 /** Since we don't have directory functions yet, this
963  *	function is needed to get the contents of a directory.
964  *	It should be removed once readdir() & co. are in place.
965  */
966 
967 Node *
968 get_node_from(int fd)
969 {
970 	Descriptor *descriptor = get_descriptor(fd);
971 	if (descriptor == NULL)
972 		return NULL;
973 
974 	return descriptor->GetNode();
975 }
976 
977 
978 int
979 close(int fd)
980 {
981 	Descriptor *descriptor = get_descriptor(fd);
982 	if (descriptor == NULL)
983 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
984 
985 	status_t status = descriptor->Release();
986 	if (!descriptor->RefCount())
987 		free_descriptor(fd);
988 
989 	RETURN_AND_SET_ERRNO(status);
990 }
991 
992 
993 // ToDo: remove this kludge when possible
994 int
995 #if defined(fstat) && !defined(main)
996 _fstat(int fd, struct stat *stat, size_t /*statSize*/)
997 #else
998 fstat(int fd, struct stat *stat)
999 #endif
1000 {
1001 	if (stat == NULL)
1002 		RETURN_AND_SET_ERRNO(B_BAD_VALUE);
1003 
1004 	Descriptor *descriptor = get_descriptor(fd);
1005 	if (descriptor == NULL)
1006 		RETURN_AND_SET_ERRNO(B_FILE_ERROR);
1007 
1008 	RETURN_AND_SET_ERRNO(descriptor->Stat(*stat));
1009 }
1010