xref: /haiku/src/add-ons/kernel/file_systems/nfs4/kernel_interface.cpp (revision e5d65858f2361fe0552495b61620c84dcee6bc00)
1 /*
2  * Copyright 2012 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Paweł Dziepak, pdziepak@quarnos.org
7  */
8 
9 
10 #include <stdio.h>
11 
12 #include <AutoDeleter.h>
13 #include <fs_cache.h>
14 #include <fs_interface.h>
15 
16 #include "Connection.h"
17 #include "FileSystem.h"
18 #include "IdMap.h"
19 #include "Inode.h"
20 #include "NFS4Defs.h"
21 #include "RequestBuilder.h"
22 #include "ReplyInterpreter.h"
23 #include "RootInode.h"
24 #include "RPCCallbackServer.h"
25 #include "RPCServer.h"
26 #include "VnodeToInode.h"
27 #include "WorkQueue.h"
28 
29 #ifdef DEBUG
30 #define TRACE_NFS4
31 #endif
32 
33 #ifdef TRACE_NFS4
34 static mutex	gTraceLock	= MUTEX_INITIALIZER(NULL);
35 
36 #define TRACE(x...)	\
37 	{	\
38 		mutex_lock(&gTraceLock);	\
39 		dprintf("nfs4: %s(): ", __FUNCTION__);	\
40 		dprintf(x);	\
41 		dprintf("\n");	\
42 		mutex_unlock(&gTraceLock);	\
43 	}
44 #else
45 #define TRACE(x...)	(void)0
46 #endif
47 
48 extern fs_volume_ops gNFSv4VolumeOps;
49 extern fs_vnode_ops gNFSv4VnodeOps;
50 
51 
52 RPC::ServerManager* gRPCServerManager;
53 
54 
55 RPC::ProgramData*
56 CreateNFS4Server(RPC::Server* serv)
57 {
58 	return new NFS4Server(serv);
59 }
60 
61 
62 // Format: ip{4,6}_address:path options
63 // Available options:
64 //	hard		- retry requests until success
65 //	soft		- retry requests no more than retrans times (default)
66 //  timeo=X		- request time limit before next retransmission (default: 60s)
67 //	retrans=X	- retry requests X times (default: 5)
68 //	ac			- use metadata cache (default)
69 //	noac		- do not use metadata cache
70 //	xattr-emu	- emulate named attributes
71 //	noxattr-emu	- do not emulate named attributes (default)
72 //	port=X		- connect to port X (default: 2049)
73 //	proto=X		- user transport protocol X (default: tcp)
74 //	dirtime=X	- attempt revalidate directory cache not more often than each X
75 //				  seconds
76 static status_t
77 ParseArguments(const char* _args, AddressResolver** address, char** _path,
78 	MountConfiguration* conf)
79 {
80 	if (_args == NULL)
81 		return B_BAD_VALUE;
82 
83 	char* args = strdup(_args);
84 	if (args == NULL)
85 		return B_NO_MEMORY;
86 	MemoryDeleter argsDeleter(args);
87 
88 	char* options = strchr(args, ' ');
89 	if (options != NULL)
90 		*options++ = '\0';
91 
92 	char* path = strrchr(args, ':');
93 	if (path == NULL)
94 		return B_MISMATCHED_VALUES;
95 	*path++ = '\0';
96 
97 	*address = new AddressResolver(args);
98 	if (*address == NULL)
99 		return B_NO_MEMORY;
100 
101 	*_path = strdup(path);
102 	if (*_path == NULL) {
103 		delete *address;
104 		return B_NO_MEMORY;
105 	}
106 
107 	conf->fHard = false;
108 	conf->fRetryLimit = 5;
109 	conf->fRequestTimeout = sSecToBigTime(60);
110 	conf->fEmulateNamedAttrs = false;
111 	conf->fCacheMetadata = true;
112 	conf->fDirectoryCacheTime = sSecToBigTime(5);
113 
114 	char* optionsEnd = NULL;
115 	if (options != NULL)
116 		optionsEnd = strchr(options, ' ');
117 	while (options != NULL && *options != '\0') {
118 		if (optionsEnd != NULL)
119 			*optionsEnd++ = '\0';
120 
121 		if (strcmp(options, "hard") == 0)
122 			conf->fHard = true;
123 		else if (strncmp(options, "retrans=", 8) == 0) {
124 			options += strlen("retrans=");
125 			conf->fRetryLimit = atoi(options);
126 		} else if (strncmp(options, "timeo=", 6) == 0) {
127 			options += strlen("timeo=");
128 			conf->fRequestTimeout = atoi(options);
129 		} else if (strcmp(options, "noac") == 0)
130 			conf->fCacheMetadata = false;
131 		else if (strcmp(options, "xattr-emu") == 0)
132 			conf->fEmulateNamedAttrs = true;
133 		else if (strncmp(options, "port=", 5) == 0) {
134 			options += strlen("port=");
135 			(*address)->ForcePort(atoi(options));
136 		} else if (strncmp(options, "proto=", 6) == 0) {
137 			options += strlen("proto=");
138 			(*address)->ForceProtocol(options);
139 		} else if (strncmp(options, "dirtime=", 8) == 0) {
140 			options += strlen("dirtime=");
141 			conf->fDirectoryCacheTime = sSecToBigTime(atoi(options));
142 		}
143 
144 		options = optionsEnd;
145 		if (options != NULL)
146 			optionsEnd = strchr(options, ' ');
147 	}
148 
149 	return B_OK;
150 }
151 
152 
153 static status_t
154 nfs4_mount(fs_volume* volume, const char* device, uint32 flags,
155 			const char* args, ino_t* _rootVnodeID)
156 {
157 	TRACE("volume = %p, device = %s, flags = %" B_PRIu32 ", args = %s", volume,
158 		device, flags, args);
159 
160 	status_t result;
161 
162 	/* prepare idmapper server */
163 	MutexLocker locker(gIdMapperLock);
164 	gIdMapper = new(std::nothrow) IdMap;
165 	if (gIdMapper == NULL)
166 		return B_NO_MEMORY;
167 
168 	result = gIdMapper->InitStatus();
169 	if (result != B_OK) {
170 		delete gIdMapper;
171 		gIdMapper = NULL;
172 		return result;
173 	}
174 	locker.Unlock();
175 
176 	AddressResolver* resolver;
177 	MountConfiguration config;
178 	char* path;
179 	result = ParseArguments(args, &resolver, &path, &config);
180 	if (result != B_OK)
181 		return result;
182 	MemoryDeleter pathDeleter(path);
183 
184 	RPC::Server* server;
185 	result = gRPCServerManager->Acquire(&server, resolver, CreateNFS4Server);
186 	delete resolver;
187 	if (result != B_OK)
188 		return result;
189 
190 	FileSystem* fs;
191 	result = FileSystem::Mount(&fs, server, path, volume->id, config);
192 	if (result != B_OK) {
193 		gRPCServerManager->Release(server);
194 		return result;
195 	}
196 
197 	Inode* inode = fs->Root();
198 	if (inode == NULL) {
199 		delete fs;
200 		gRPCServerManager->Release(server);
201 
202 		return B_IO_ERROR;
203 	}
204 
205 	volume->private_volume = fs;
206 	volume->ops = &gNFSv4VolumeOps;
207 
208 	VnodeToInode* vti = new VnodeToInode(inode->ID(), fs);
209 	if (vti == NULL) {
210 		delete fs;
211 		gRPCServerManager->Release(server);
212 		return B_NO_MEMORY;
213 	}
214 
215 	vti->Replace(inode);
216 	result = publish_vnode(volume, inode->ID(), vti, &gNFSv4VnodeOps,
217 							inode->Type(), 0);
218 	if (result != B_OK)
219 		return result;
220 
221 	*_rootVnodeID = inode->ID();
222 
223 	TRACE("*_rootVnodeID = %" B_PRIi64, inode->ID());
224 
225 	return B_OK;
226 }
227 
228 
229 static status_t
230 nfs4_get_vnode(fs_volume* volume, ino_t id, fs_vnode* vnode, int* _type,
231 	uint32* _flags, bool reenter)
232 {
233 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
234 	TRACE("volume = %p, id = %" B_PRIi64, volume, id);
235 
236 	VnodeToInode* vnodeToInode = new VnodeToInode(id, fs);
237 	if (vnodeToInode == NULL)
238 		return B_NO_MEMORY;
239 
240 	Inode* inode;
241 	status_t result = fs->GetInode(id, &inode);
242 	if (result != B_OK) {
243 		delete vnodeToInode;
244 		return result;
245 	}
246 
247 	vnodeToInode->Replace(inode);
248 	vnode->ops = &gNFSv4VnodeOps;
249 	vnode->private_node = vnodeToInode;
250 
251 	*_type = inode->Type();
252 	*_flags = 0;
253 
254 	return B_OK;
255 }
256 
257 
258 static status_t
259 nfs4_unmount(fs_volume* volume)
260 {
261 	TRACE("volume = %p", volume);
262 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
263 	RPC::Server* server = fs->Server();
264 
265 	delete fs;
266 	gRPCServerManager->Release(server);
267 
268 	return B_OK;
269 }
270 
271 
272 static status_t
273 nfs4_read_fs_info(fs_volume* volume, struct fs_info* info)
274 {
275 	TRACE("volume = %p", volume);
276 
277 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
278 	RootInode* inode = reinterpret_cast<RootInode*>(fs->Root());
279 	return inode->ReadInfo(info);
280 }
281 
282 
283 static status_t
284 nfs4_lookup(fs_volume* volume, fs_vnode* dir, const char* name, ino_t* _id)
285 {
286 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(dir->private_node);
287 
288 	if (!strcmp(name, ".")) {
289 		*_id = vti->ID();
290 		void* ptr;
291 		return get_vnode(volume, *_id, &ptr);
292 	}
293 
294 	VnodeToInodeLocker locker(vti);
295 
296 	Inode* inode = vti->Get();
297 	if (inode == NULL)
298 		return B_ENTRY_NOT_FOUND;
299 
300 	TRACE("volume = %p, dir = %" B_PRIi64 ", name = %s", volume, vti->ID(),
301 		name);
302 
303 	status_t result = inode->LookUp(name, _id);
304 	if (result != B_OK)
305 		return result;
306 	locker.Unlock();
307 
308 	TRACE("*_id = %" B_PRIi64, *_id);
309 
310 	// If VTI holds an outdated Inode next operation performed on it will
311 	// return either ERR_STALE or ERR_FHEXPIRED. Both of these error codes
312 	// will cause FileInfo data to be updated (the former will also cause Inode
313 	// object to be recreated). We are taking an optimistic (an lazy) approach
314 	// here. The following code just ensures VTI won't be removed too soon.
315 	void* ptr;
316 	result = get_vnode(volume, *_id, &ptr);
317 	if (result == B_OK)
318 		unremove_vnode(volume, *_id);
319 
320 	return result;
321 }
322 
323 
324 static status_t
325 nfs4_put_vnode(fs_volume* volume, fs_vnode* vnode, bool reenter)
326 {
327 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
328 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
329 
330 	delete vti;
331 	return B_OK;
332 }
333 
334 
335 static status_t
336 nfs4_remove_vnode(fs_volume* volume, fs_vnode* vnode, bool reenter)
337 {
338 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
339 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
340 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
341 
342 	if (fs->Root() == vti->GetPointer())
343 		return B_OK;
344 
345 	ASSERT(vti->GetPointer() == NULL);
346 	delete vti;
347 
348 	return B_OK;
349 }
350 
351 
352 static status_t
353 nfs4_read_pages(fs_volume* _volume, fs_vnode* vnode, void* _cookie, off_t pos,
354 	const iovec* vecs, size_t count, size_t* _numBytes)
355 {
356 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
357 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, pos = %" B_PRIi64 \
358 		", count = %lu, numBytes = %lu", _volume, vti->ID(), _cookie, pos,
359 		count, *_numBytes);
360 
361 	VnodeToInodeLocker _(vti);
362 	Inode* inode = vti->Get();
363 	if (inode == NULL)
364 		return B_ENTRY_NOT_FOUND;
365 
366 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
367 
368 	status_t result;
369 	size_t totalRead = 0;
370 	bool eof = false;
371 	for (size_t i = 0; i < count && !eof; i++) {
372 		size_t bytesLeft = vecs[i].iov_len;
373 		char* buffer = reinterpret_cast<char*>(vecs[i].iov_base);
374 
375 		do {
376 			size_t bytesRead = bytesLeft;
377 			result = inode->ReadDirect(cookie, pos, buffer, &bytesRead, &eof);
378 			if (result != B_OK)
379 				return result;
380 
381 			totalRead += bytesRead;
382 			pos += bytesRead;
383 			buffer += bytesRead;
384 			bytesLeft -= bytesRead;
385 		} while (bytesLeft > 0 && !eof);
386 	}
387 
388 	*_numBytes = totalRead;
389 
390 	TRACE("*numBytes = %lu", totalRead);
391 
392 	return B_OK;
393 }
394 
395 
396 static status_t
397 nfs4_write_pages(fs_volume* _volume, fs_vnode* vnode, void* _cookie, off_t pos,
398 	const iovec* vecs, size_t count, size_t* _numBytes)
399 {
400 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
401 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, pos = %" B_PRIi64 \
402 		", count = %lu, numBytes = %lu", _volume, vti->ID(), _cookie, pos,
403 		count, *_numBytes);
404 
405 	VnodeToInodeLocker _(vti);
406 	Inode* inode = vti->Get();
407 	if (inode == NULL)
408 		return B_ENTRY_NOT_FOUND;
409 
410 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
411 
412 	status_t result;
413 	for (size_t i = 0; i < count; i++) {
414 		uint64 bytesLeft = vecs[i].iov_len;
415 		if (pos + bytesLeft > inode->MaxFileSize())
416 			bytesLeft = inode->MaxFileSize() - pos;
417 
418 		char* buffer = reinterpret_cast<char*>(vecs[i].iov_base);
419 
420 		do {
421 			size_t bytesWritten = bytesLeft;
422 
423 			result = inode->WriteDirect(cookie, pos, buffer, &bytesWritten);
424 			if (result != B_OK)
425 				return result;
426 
427 			bytesLeft -= bytesWritten;
428 			pos += bytesWritten;
429 			buffer += bytesWritten;
430 		} while (bytesLeft > 0);
431 	}
432 
433 	return B_OK;
434 }
435 
436 
437 static status_t
438 nfs4_io(fs_volume* volume, fs_vnode* vnode, void* cookie, io_request* request)
439 {
440 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
441 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume, vti->ID(),
442 		cookie);
443 
444 	VnodeToInodeLocker _(vti);
445 	Inode* inode = vti->Get();
446 	if (inode == NULL)
447 		return B_ENTRY_NOT_FOUND;
448 
449 	IORequestArgs* args = new(std::nothrow) IORequestArgs;
450 	if (args == NULL) {
451 		notify_io_request(request, B_NO_MEMORY);
452 		return B_NO_MEMORY;
453 	}
454 	args->fRequest = request;
455 	args->fInode = inode;
456 
457 	status_t result = gWorkQueue->EnqueueJob(IORequest, args);
458 	if (result != B_OK)
459 		notify_io_request(request, result);
460 
461 	return result;
462 }
463 
464 
465 static status_t
466 nfs4_get_file_map(fs_volume* volume, fs_vnode* vnode, off_t _offset,
467 	size_t size, struct file_io_vec* vecs, size_t* _count)
468 {
469 	return B_ERROR;
470 }
471 
472 
473 static status_t
474 nfs4_set_flags(fs_volume* volume, fs_vnode* vnode, void* _cookie, int flags)
475 {
476 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, flags = %d", volume,
477 		reinterpret_cast<VnodeToInode*>(vnode->private_node)->ID(), _cookie,
478 		flags);
479 
480 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
481 	cookie->fMode = (cookie->fMode & ~(O_APPEND | O_NONBLOCK)) | flags;
482 	return B_OK;
483 }
484 
485 
486 static status_t
487 nfs4_fsync(fs_volume* volume, fs_vnode* vnode)
488 {
489 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
490 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
491 
492 	VnodeToInodeLocker _(vti);
493 	Inode* inode = vti->Get();
494 	if (inode == NULL)
495 		return B_ENTRY_NOT_FOUND;
496 
497 	return inode->SyncAndCommit();
498 }
499 
500 
501 static status_t
502 nfs4_read_symlink(fs_volume* volume, fs_vnode* link, char* buffer,
503 	size_t* _bufferSize)
504 {
505 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(link->private_node);
506 	TRACE("volume = %p, link = %" B_PRIi64, volume, vti->ID());
507 
508 	VnodeToInodeLocker _(vti);
509 	Inode* inode = vti->Get();
510 	if (inode == NULL)
511 		return B_ENTRY_NOT_FOUND;
512 
513 	return inode->ReadLink(buffer, _bufferSize);
514 }
515 
516 
517 static status_t
518 nfs4_create_symlink(fs_volume* volume, fs_vnode* dir, const char* name,
519 	const char* path, int mode)
520 {
521 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(dir->private_node);
522 	TRACE("volume = %p, dir = %" B_PRIi64 ", name = %s, path = %s, mode = %d",
523 		volume, vti->ID(), name, path, mode);
524 
525 	VnodeToInodeLocker _(vti);
526 	Inode* inode = vti->Get();
527 	if (inode == NULL)
528 		return B_ENTRY_NOT_FOUND;
529 
530 	ino_t id;
531 	status_t result = inode->CreateLink(name, path, mode, &id);
532 	if (result != B_OK)
533 		return result;
534 
535 	result = get_vnode(volume, id, reinterpret_cast<void**>(&vti));
536 	if (result == B_OK) {
537 		unremove_vnode(volume, id);
538 		vti->Clear();
539 		put_vnode(volume, id);
540 	}
541 
542 	return B_OK;
543 }
544 
545 
546 static status_t
547 nfs4_link(fs_volume* volume, fs_vnode* dir, const char* name, fs_vnode* vnode)
548 {
549 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
550 	VnodeToInode* dirVti = reinterpret_cast<VnodeToInode*>(dir->private_node);
551 	TRACE("volume = %p, dir = %" B_PRIi64 ", name = %s, vnode = %" B_PRIi64,
552 		volume, dirVti->ID(), name, vti->ID());
553 
554 	VnodeToInodeLocker _dir(dirVti);
555 	Inode* dirInode = dirVti->Get();
556 	if (dirInode == NULL)
557 		return B_ENTRY_NOT_FOUND;
558 
559 
560 	VnodeToInodeLocker _(vti);
561 	Inode* inode = vti->Get();
562 	if (inode == NULL)
563 		return B_ENTRY_NOT_FOUND;
564 
565 	return inode->Link(dirInode, name);
566 }
567 
568 
569 static status_t
570 nfs4_unlink(fs_volume* volume, fs_vnode* dir, const char* name)
571 {
572 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(dir->private_node);
573 
574 	VnodeToInodeLocker locker(vti);
575 	Inode* inode = vti->Get();
576 	if (inode == NULL)
577 		return B_ENTRY_NOT_FOUND;
578 
579 	TRACE("volume = %p, dir = %" B_PRIi64 ", name = %s", volume, vti->ID(),
580 		name);
581 
582 	ino_t id;
583 	status_t result = inode->Remove(name, NF4REG, &id);
584 	if (result != B_OK)
585 		return result;
586 	locker.Unlock();
587 
588 	result = acquire_vnode(volume, id);
589 	if (result == B_OK) {
590 		result = get_vnode(volume, id, reinterpret_cast<void**>(&vti));
591 		ASSERT(result == B_OK);
592 
593 		if (vti->Unlink(inode->fInfo.fNames, name))
594 			remove_vnode(volume, id);
595 
596 		put_vnode(volume, id);
597 		put_vnode(volume, id);
598 	}
599 
600 	return B_OK;
601 }
602 
603 
604 static status_t
605 nfs4_rename(fs_volume* volume, fs_vnode* fromDir, const char* fromName,
606 	fs_vnode* toDir, const char* toName)
607 {
608 	VnodeToInode* fromVti
609 		= reinterpret_cast<VnodeToInode*>(fromDir->private_node);
610 	VnodeToInode* toVti = reinterpret_cast<VnodeToInode*>(toDir->private_node);
611 	TRACE("volume = %p, fromDir = %" B_PRIi64 ", toDir = %" B_PRIi64 ","	\
612 		" fromName = %s, toName = %s", volume, fromVti->ID(), toVti->ID(),	\
613 		fromName, toName);
614 
615 	VnodeToInodeLocker _from(fromVti);
616 	Inode* fromInode = fromVti->Get();
617 	if (fromInode == NULL)
618 		return B_ENTRY_NOT_FOUND;
619 
620 
621 	VnodeToInodeLocker _to(toVti);
622 	Inode* toInode = toVti->Get();
623 	if (toInode == NULL)
624 		return B_ENTRY_NOT_FOUND;
625 
626 	ino_t id;
627 	ino_t oldID;
628 	status_t result = Inode::Rename(fromInode, toInode, fromName, toName, false,
629 		&id, &oldID);
630 	if (result != B_OK)
631 		return result;
632 
633 	VnodeToInode* vti;
634 
635 	if (oldID != 0) {
636 		// we have overriden an inode
637 		result = acquire_vnode(volume, oldID);
638 		if (result == B_OK) {
639 			result = get_vnode(volume, oldID, reinterpret_cast<void**>(&vti));
640 			ASSERT(result == B_OK);
641 			if (vti->Unlink(toInode->fInfo.fNames, toName))
642 				remove_vnode(volume, oldID);
643 
644 			put_vnode(volume, oldID);
645 			put_vnode(volume, oldID);
646 		}
647 	}
648 
649 	result = get_vnode(volume, id, reinterpret_cast<void**>(&vti));
650 	if (result == B_OK) {
651 		Inode* child = vti->Get();
652 		if (child == NULL) {
653 			put_vnode(volume, id);
654 			return B_ENTRY_NOT_FOUND;
655 		}
656 
657 		unremove_vnode(volume, id);
658 		child->fInfo.fNames->RemoveName(fromInode->fInfo.fNames, fromName);
659 		child->fInfo.fNames->AddName(toInode->fInfo.fNames, toName);
660 		put_vnode(volume, id);
661 	}
662 
663 	return B_OK;
664 }
665 
666 
667 static status_t
668 nfs4_access(fs_volume* volume, fs_vnode* vnode, int mode)
669 {
670 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
671 	TRACE("volume = %p, vnode = %" B_PRIi64 ", mode = %d", volume, vti->ID(),
672 		mode);
673 
674 	VnodeToInodeLocker _(vti);
675 	Inode* inode = vti->Get();
676 	if (inode == NULL)
677 		return B_ENTRY_NOT_FOUND;
678 
679 	return inode->Access(mode);
680 }
681 
682 
683 static status_t
684 nfs4_read_stat(fs_volume* volume, fs_vnode* vnode, struct stat* stat)
685 {
686 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
687 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
688 
689 	VnodeToInodeLocker _(vti);
690 	Inode* inode = vti->Get();
691 	if (inode == NULL)
692 		return B_ENTRY_NOT_FOUND;
693 
694 	status_t result = inode->Stat(stat);
695 	if (inode->GetOpenState() != NULL)
696 		stat->st_size = inode->MaxFileSize();
697 	return result;
698 }
699 
700 
701 static status_t
702 nfs4_write_stat(fs_volume* volume, fs_vnode* vnode, const struct stat* stat,
703 	uint32 statMask)
704 {
705 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
706 	TRACE("volume = %p, vnode = %" B_PRIi64 ", statMask = %" B_PRIu32, volume,
707 		vti->ID(), statMask);
708 
709 	VnodeToInodeLocker _(vti);
710 	Inode* inode = vti->Get();
711 	if (inode == NULL)
712 		return B_ENTRY_NOT_FOUND;
713 
714 	return inode->WriteStat(stat, statMask);
715 }
716 
717 
718 static status_t
719 get_new_vnode(fs_volume* volume, ino_t id, VnodeToInode** _vti)
720 {
721 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
722 	Inode* inode;
723 	VnodeToInode* vti;
724 
725 	status_t result = acquire_vnode(volume, id);
726 	if (result == B_OK) {
727 		ASSERT(get_vnode(volume, id, reinterpret_cast<void**>(_vti)) == B_OK);
728 		unremove_vnode(volume, id);
729 
730 		// Release after acquire
731 		put_vnode(volume, id);
732 
733 		vti = *_vti;
734 
735 		if (vti->Get() == NULL) {
736 			result = fs->GetInode(id, &inode);
737 			if (result != B_OK) {
738 				put_vnode(volume, id);
739 				return result;
740 			}
741 
742 			vti->Replace(inode);
743 		}
744 		return B_OK;
745 	}
746 
747 	return get_vnode(volume, id, reinterpret_cast<void**>(_vti));
748 }
749 
750 
751 static status_t
752 nfs4_create(fs_volume* volume, fs_vnode* dir, const char* name, int openMode,
753 	int perms, void** _cookie, ino_t* _newVnodeID)
754 {
755 	OpenFileCookie* cookie = new OpenFileCookie;
756 	if (cookie == NULL)
757 		return B_NO_MEMORY;
758 	*_cookie = cookie;
759 
760 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(dir->private_node);
761 	TRACE("volume = %p, dir = %" B_PRIi64 ", name = %s, openMode = %d,"	\
762 		" perms = %d", volume, vti->ID(), name, openMode, perms);
763 
764 	VnodeToInodeLocker _(vti);
765 	Inode* inode = vti->Get();
766 	if (inode == NULL)
767 		return B_ENTRY_NOT_FOUND;
768 
769 	FileSystem* fs = reinterpret_cast<FileSystem*>(volume->private_volume);
770 	MutexLocker createLocker(fs->CreateFileLock());
771 
772 	OpenDelegationData data;
773 	status_t result = inode->Create(name, openMode, perms, cookie, &data,
774 		_newVnodeID);
775 	if (result != B_OK) {
776 		delete cookie;
777 		return result;
778 	}
779 
780 	result = get_new_vnode(volume, *_newVnodeID, &vti);
781 	if (result != B_OK) {
782 		delete cookie;
783 		return result;
784 	}
785 
786 	VnodeToInodeLocker _child(vti);
787 	Inode* child = vti->Get();
788 	if (child == NULL) {
789 		delete cookie;
790 		put_vnode(volume, *_newVnodeID);
791 		return B_ENTRY_NOT_FOUND;
792 	}
793 
794 	child->SetOpenState(cookie->fOpenState);
795 
796 	if (data.fType != OPEN_DELEGATE_NONE) {
797 		Delegation* delegation
798 			= new(std::nothrow) Delegation(data, child,
799 				cookie->fOpenState->fClientID);
800 		if (delegation != NULL) {
801 			delegation->fInfo = cookie->fOpenState->fInfo;
802 			delegation->fFileSystem = child->GetFileSystem();
803 			child->SetDelegation(delegation);
804 		}
805 	}
806 
807 	TRACE("*cookie = %p, *newVnodeID = %" B_PRIi64, *_cookie, *_newVnodeID);
808 	return result;
809 }
810 
811 
812 static status_t
813 nfs4_open(fs_volume* volume, fs_vnode* vnode, int openMode, void** _cookie)
814 {
815 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
816 	TRACE("volume = %p, vnode = %" B_PRIi64 ", openMode = %d", volume,
817 		vti->ID(), openMode);
818 
819 	VnodeToInodeLocker _(vti);
820 	Inode* inode = vti->Get();
821 	if (inode == NULL)
822 		return B_ENTRY_NOT_FOUND;
823 
824 	if (inode->Type() == S_IFDIR || inode->Type() == S_IFLNK) {
825 		*_cookie = NULL;
826 		return B_OK;
827 	}
828 
829 	OpenFileCookie* cookie = new OpenFileCookie;
830 	if (cookie == NULL)
831 		return B_NO_MEMORY;
832 	*_cookie = cookie;
833 
834 	status_t result = inode->Open(openMode, cookie);
835 	if (result != B_OK)
836 		delete cookie;
837 
838 	TRACE("*cookie = %p", *_cookie);
839 
840 	return result;
841 }
842 
843 
844 static status_t
845 nfs4_close(fs_volume* volume, fs_vnode* vnode, void* _cookie)
846 {
847 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
848 
849 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume, vti->ID(),
850 		_cookie);
851 
852 	VnodeToInodeLocker _(vti);
853 	Inode* inode = vti->Get();
854 	if (inode == NULL)
855 		return B_ENTRY_NOT_FOUND;
856 
857 
858 	if (inode->Type() == S_IFDIR || inode->Type() == S_IFLNK)
859 		return B_OK;
860 
861 	Cookie* cookie = reinterpret_cast<Cookie*>(_cookie);
862 	return cookie->CancelAll();
863 }
864 
865 
866 static status_t
867 nfs4_free_cookie(fs_volume* volume, fs_vnode* vnode, void* _cookie)
868 {
869 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
870 
871 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume, vti->ID(),
872 		_cookie);
873 
874 	VnodeToInodeLocker _(vti);
875 	Inode* inode = vti->Get();
876 	if (inode == NULL)
877 		return B_ENTRY_NOT_FOUND;
878 
879 	if (inode->Type() == S_IFDIR || inode->Type() == S_IFLNK)
880 		return B_OK;
881 
882 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
883 
884 	inode->Close(cookie);
885 	delete cookie;
886 
887 	return B_OK;
888 }
889 
890 
891 static status_t
892 nfs4_read(fs_volume* volume, fs_vnode* vnode, void* _cookie, off_t pos,
893 	void* buffer, size_t* length)
894 {
895 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
896 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, pos = %" B_PRIi64 \
897 		", length = %lu", volume, vti->ID(), _cookie, pos, *length);
898 
899 	VnodeToInodeLocker _(vti);
900 	Inode* inode = vti->Get();
901 	if (inode == NULL)
902 		return B_ENTRY_NOT_FOUND;
903 
904 	if (inode->Type() == S_IFDIR)
905 		return B_IS_A_DIRECTORY;
906 
907 	if (inode->Type() == S_IFLNK)
908 		return B_BAD_VALUE;
909 
910 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
911 
912 	return inode->Read(cookie, pos, buffer, length);;
913 }
914 
915 
916 static status_t
917 nfs4_write(fs_volume* volume, fs_vnode* vnode, void* _cookie, off_t pos,
918 	const void* _buffer, size_t* length)
919 {
920 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
921 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, pos = %" B_PRIi64 \
922 		", length = %lu", volume, vti->ID(), _cookie, pos, *length);
923 
924 	VnodeToInodeLocker _(vti);
925 	Inode* inode = vti->Get();
926 	if (inode == NULL)
927 		return B_ENTRY_NOT_FOUND;
928 
929 	if (inode->Type() == S_IFDIR)
930 		return B_IS_A_DIRECTORY;
931 
932 	if (inode->Type() == S_IFLNK)
933 		return B_BAD_VALUE;
934 
935 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
936 
937 	return inode->Write(cookie, pos, _buffer, length);
938 }
939 
940 
941 static status_t
942 nfs4_create_dir(fs_volume* volume, fs_vnode* parent, const char* name,
943 	int mode)
944 {
945 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(parent->private_node);
946 	TRACE("volume = %p, parent = %" B_PRIi64 ", mode = %d", volume, vti->ID(),
947 		mode);
948 
949 	VnodeToInodeLocker _(vti);
950 	Inode* inode = vti->Get();
951 	if (inode == NULL)
952 		return B_ENTRY_NOT_FOUND;
953 
954 	ino_t id;
955 	status_t result = inode->CreateDir(name, mode, &id);
956 	if (result != B_OK)
957 		return result;
958 
959 	result = get_vnode(volume, id, reinterpret_cast<void**>(&vti));
960 	if (result == B_OK) {
961 		unremove_vnode(volume, id);
962 		vti->Clear();
963 		put_vnode(volume, id);
964 	}
965 
966 	return B_OK;
967 }
968 
969 
970 static status_t
971 nfs4_remove_dir(fs_volume* volume, fs_vnode* parent, const char* name)
972 {
973 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(parent->private_node);
974 	TRACE("volume = %p, parent = %" B_PRIi64 ", name = %s", volume, vti->ID(),
975 		name);
976 
977 	VnodeToInodeLocker _(vti);
978 	Inode* inode = vti->Get();
979 	if (inode == NULL)
980 		return B_ENTRY_NOT_FOUND;
981 
982 	ino_t id;
983 	status_t result = inode->Remove(name, NF4DIR, &id);
984 	if (result != B_OK)
985 		return result;
986 
987 	result = acquire_vnode(volume, id);
988 	if (result == B_OK) {
989 		result = get_vnode(volume, id, reinterpret_cast<void**>(&vti));
990 		ASSERT(result == B_OK);
991 
992 		if (vti->Unlink(inode->fInfo.fNames, name))
993 			remove_vnode(volume, id);
994 
995 		put_vnode(volume, id);
996 		put_vnode(volume, id);
997 	}
998 
999 	return B_OK;
1000 }
1001 
1002 
1003 static status_t
1004 nfs4_open_dir(fs_volume* volume, fs_vnode* vnode, void** _cookie)
1005 {
1006 	OpenDirCookie* cookie = new(std::nothrow) OpenDirCookie;
1007 	if (cookie == NULL)
1008 		return B_NO_MEMORY;
1009 	*_cookie = cookie;
1010 
1011 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1012 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
1013 
1014 	VnodeToInodeLocker _(vti);
1015 	Inode* inode = vti->Get();
1016 	if (inode == NULL)
1017 		return B_ENTRY_NOT_FOUND;
1018 
1019 	status_t result = inode->OpenDir(cookie);
1020 	if (result != B_OK)
1021 		delete cookie;
1022 
1023 	TRACE("*cookie = %p", *_cookie);
1024 
1025 	return result;
1026 }
1027 
1028 
1029 static status_t
1030 nfs4_close_dir(fs_volume* volume, fs_vnode* vnode, void* _cookie)
1031 {
1032 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume,
1033 		reinterpret_cast<VnodeToInode*>(vnode->private_node)->ID(), _cookie);
1034 
1035 	Cookie* cookie = reinterpret_cast<Cookie*>(_cookie);
1036 	return cookie->CancelAll();
1037 }
1038 
1039 
1040 static status_t
1041 nfs4_free_dir_cookie(fs_volume* volume, fs_vnode* vnode, void* cookie)
1042 {
1043 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume,
1044 		reinterpret_cast<VnodeToInode*>(vnode->private_node)->ID(), cookie);
1045 
1046 	delete reinterpret_cast<OpenDirCookie*>(cookie);
1047 	return B_OK;
1048 }
1049 
1050 
1051 static status_t
1052 nfs4_read_dir(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1053 				struct dirent* buffer, size_t bufferSize, uint32* _num)
1054 {
1055 	OpenDirCookie* cookie = reinterpret_cast<OpenDirCookie*>(_cookie);
1056 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1057 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume, vti->ID(),
1058 		_cookie);
1059 
1060 	VnodeToInodeLocker _(vti);
1061 	Inode* inode = vti->Get();
1062 	if (inode == NULL)
1063 		return B_ENTRY_NOT_FOUND;
1064 
1065 	return inode->ReadDir(buffer, bufferSize, _num, cookie);
1066 }
1067 
1068 
1069 static status_t
1070 nfs4_rewind_dir(fs_volume* volume, fs_vnode* vnode, void* _cookie)
1071 {
1072 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p", volume,
1073 		reinterpret_cast<VnodeToInode*>(vnode->private_node)->ID(), _cookie);
1074 
1075 	OpenDirCookie* cookie = reinterpret_cast<OpenDirCookie*>(_cookie);
1076 	cookie->fSpecial = 0;
1077 	if (cookie->fSnapshot != NULL)
1078 		cookie->fSnapshot->ReleaseReference();
1079 	cookie->fSnapshot = NULL;
1080 	cookie->fCurrent = NULL;
1081 	cookie->fEOF = false;
1082 
1083 	return B_OK;
1084 }
1085 
1086 
1087 static status_t
1088 nfs4_open_attr_dir(fs_volume* volume, fs_vnode* vnode, void** _cookie)
1089 {
1090 	OpenDirCookie* cookie = new(std::nothrow) OpenDirCookie;
1091 	if (cookie == NULL)
1092 		return B_NO_MEMORY;
1093 	*_cookie = cookie;
1094 
1095 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1096 	TRACE("volume = %p, vnode = %" B_PRIi64, volume, vti->ID());
1097 
1098 	VnodeToInodeLocker _(vti);
1099 	Inode* inode = vti->Get();
1100 	if (inode == NULL)
1101 		return B_ENTRY_NOT_FOUND;
1102 
1103 	status_t result = inode->OpenAttrDir(cookie);
1104 	if (result != B_OK)
1105 		delete cookie;
1106 
1107 	return result;
1108 }
1109 
1110 
1111 static status_t
1112 nfs4_close_attr_dir(fs_volume* volume, fs_vnode* vnode, void* cookie)
1113 {
1114 	return nfs4_close_dir(volume, vnode, cookie);
1115 }
1116 
1117 
1118 static status_t
1119 nfs4_free_attr_dir_cookie(fs_volume* volume, fs_vnode* vnode, void* cookie)
1120 {
1121 	return nfs4_free_dir_cookie(volume, vnode, cookie);
1122 }
1123 
1124 
1125 static status_t
1126 nfs4_read_attr_dir(fs_volume* volume, fs_vnode* vnode, void* cookie,
1127 	struct dirent* buffer, size_t bufferSize, uint32* _num)
1128 {
1129 	return nfs4_read_dir(volume, vnode, cookie, buffer, bufferSize, _num);
1130 }
1131 
1132 
1133 static status_t
1134 nfs4_rewind_attr_dir(fs_volume* volume, fs_vnode* vnode, void* cookie)
1135 {
1136 	return nfs4_rewind_dir(volume, vnode, cookie);
1137 }
1138 
1139 
1140 static status_t
1141 nfs4_create_attr(fs_volume* volume, fs_vnode* vnode, const char* name,
1142 	uint32 type, int openMode, void** _cookie)
1143 {
1144 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1145 
1146 	VnodeToInodeLocker _(vti);
1147 	Inode* inode = vti->Get();
1148 	if (inode == NULL)
1149 		return B_ENTRY_NOT_FOUND;
1150 
1151 	OpenAttrCookie* cookie = new OpenAttrCookie;
1152 	if (cookie == NULL)
1153 		return B_NO_MEMORY;
1154 	*_cookie = cookie;
1155 
1156 	status_t result = inode->OpenAttr(name, openMode, cookie, true, type);
1157 	if (result != B_OK)
1158 		delete cookie;
1159 
1160 	return result;
1161 }
1162 
1163 
1164 static status_t
1165 nfs4_open_attr(fs_volume* volume, fs_vnode* vnode, const char* name,
1166 	int openMode, void** _cookie)
1167 {
1168 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1169 
1170 	VnodeToInodeLocker _(vti);
1171 	Inode* inode = vti->Get();
1172 	if (inode == NULL)
1173 		return B_ENTRY_NOT_FOUND;
1174 
1175 	OpenAttrCookie* cookie = new OpenAttrCookie;
1176 	if (cookie == NULL)
1177 		return B_NO_MEMORY;
1178 	*_cookie = cookie;
1179 
1180 	status_t result = inode->OpenAttr(name, openMode, cookie, false);
1181 	if (result != B_OK)
1182 		delete cookie;
1183 
1184 	return result;
1185 }
1186 
1187 
1188 static status_t
1189 nfs4_close_attr(fs_volume* volume, fs_vnode* vnode, void* _cookie)
1190 {
1191 	Cookie* cookie = reinterpret_cast<Cookie*>(_cookie);
1192 	return cookie->CancelAll();
1193 }
1194 
1195 
1196 static status_t
1197 nfs4_free_attr_cookie(fs_volume* volume, fs_vnode* vnode, void* _cookie)
1198 {
1199 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1200 
1201 	VnodeToInodeLocker _(vti);
1202 	Inode* inode = vti->Get();
1203 	if (inode == NULL)
1204 		return B_ENTRY_NOT_FOUND;
1205 
1206 	OpenAttrCookie* cookie = reinterpret_cast<OpenAttrCookie*>(_cookie);
1207 	inode->CloseAttr(cookie);
1208 	delete cookie;
1209 
1210 	return B_OK;
1211 }
1212 
1213 
1214 static status_t
1215 nfs4_read_attr(fs_volume* volume, fs_vnode* vnode, void* _cookie, off_t pos,
1216 	void* buffer, size_t* length)
1217 {
1218 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1219 	OpenAttrCookie* cookie = reinterpret_cast<OpenAttrCookie*>(_cookie);
1220 	bool eof;
1221 
1222 	VnodeToInodeLocker _(vti);
1223 	Inode* inode = vti->Get();
1224 	if (inode == NULL)
1225 		return B_ENTRY_NOT_FOUND;
1226 
1227 	return inode->ReadDirect(cookie, pos, buffer, length, &eof);
1228 }
1229 
1230 
1231 static status_t
1232 nfs4_write_attr(fs_volume* volume, fs_vnode* vnode, void* _cookie, off_t pos,
1233 	const void* buffer, size_t* length)
1234 {
1235 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1236 	OpenAttrCookie* cookie = reinterpret_cast<OpenAttrCookie*>(_cookie);
1237 
1238 	VnodeToInodeLocker _(vti);
1239 	Inode* inode = vti->Get();
1240 	if (inode == NULL)
1241 		return B_ENTRY_NOT_FOUND;
1242 
1243 	return inode->WriteDirect(cookie, pos, buffer, length);
1244 }
1245 
1246 
1247 static status_t
1248 nfs4_read_attr_stat(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1249 	struct stat* stat)
1250 {
1251 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1252 	OpenAttrCookie* cookie = reinterpret_cast<OpenAttrCookie*>(_cookie);
1253 
1254 	VnodeToInodeLocker _(vti);
1255 	Inode* inode = vti->Get();
1256 	if (inode == NULL)
1257 		return B_ENTRY_NOT_FOUND;
1258 
1259 	return inode->Stat(stat, cookie);
1260 }
1261 
1262 
1263 static status_t
1264 nfs4_write_attr_stat(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1265 	const struct stat* stat, int statMask)
1266 {
1267 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1268 	OpenAttrCookie* cookie = reinterpret_cast<OpenAttrCookie*>(_cookie);
1269 
1270 	VnodeToInodeLocker _(vti);
1271 	Inode* inode = vti->Get();
1272 	if (inode == NULL)
1273 		return B_ENTRY_NOT_FOUND;
1274 
1275 	return inode->WriteStat(stat, statMask, cookie);
1276 }
1277 
1278 
1279 static status_t
1280 nfs4_rename_attr(fs_volume* volume, fs_vnode* fromVnode, const char* fromName,
1281 	fs_vnode* toVnode, const char* toName)
1282 {
1283 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(toVnode->private_node);
1284 	VnodeToInodeLocker to(vti);
1285 	Inode* toInode = vti->Get();
1286 	if (toInode == NULL)
1287 		return B_ENTRY_NOT_FOUND;
1288 
1289 	vti = reinterpret_cast<VnodeToInode*>(fromVnode->private_node);
1290 	VnodeToInodeLocker from(vti);
1291 	Inode* fromInode = vti->Get();
1292 	if (fromInode == NULL)
1293 		return B_ENTRY_NOT_FOUND;
1294 
1295 	return Inode::Rename(fromInode, toInode, fromName, toName, true);
1296 }
1297 
1298 
1299 static status_t
1300 nfs4_remove_attr(fs_volume* volume, fs_vnode* vnode, const char* name)
1301 {
1302 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1303 
1304 	VnodeToInodeLocker _(vti);
1305 	Inode* inode = vti->Get();
1306 	if (inode == NULL)
1307 		return B_ENTRY_NOT_FOUND;
1308 
1309 	return inode->Remove(name, NF4NAMEDATTR, NULL);
1310 }
1311 
1312 
1313 static status_t
1314 nfs4_test_lock(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1315 	struct flock* lock)
1316 {
1317 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1318 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
1319 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, lock = %p", volume,
1320 		vti->ID(), _cookie, lock);
1321 
1322 	VnodeToInodeLocker _(vti);
1323 	Inode* inode = vti->Get();
1324 	if (inode == NULL)
1325 		return B_ENTRY_NOT_FOUND;
1326 
1327 	return inode->TestLock(cookie, lock);
1328 }
1329 
1330 
1331 static status_t
1332 nfs4_acquire_lock(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1333 			const struct flock* lock, bool wait)
1334 {
1335 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1336 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
1337 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, lock = %p", volume,
1338 		vti->ID(), _cookie, lock);
1339 
1340 
1341 	VnodeToInodeLocker _(vti);
1342 	Inode* inode = vti->Get();
1343 	if (inode == NULL)
1344 		return B_ENTRY_NOT_FOUND;
1345 
1346 	inode->RevalidateFileCache();
1347 	return inode->AcquireLock(cookie, lock, wait);
1348 }
1349 
1350 
1351 static status_t
1352 nfs4_release_lock(fs_volume* volume, fs_vnode* vnode, void* _cookie,
1353 			const struct flock* lock)
1354 {
1355 	VnodeToInode* vti = reinterpret_cast<VnodeToInode*>(vnode->private_node);
1356 	TRACE("volume = %p, vnode = %" B_PRIi64 ", cookie = %p, lock = %p", volume,
1357 		vti->ID(), _cookie, lock);
1358 
1359 	VnodeToInodeLocker _(vti);
1360 	Inode* inode = vti->Get();
1361 	if (inode == NULL)
1362 		return B_ENTRY_NOT_FOUND;
1363 
1364 	if (inode->Type() == S_IFDIR || inode->Type() == S_IFLNK)
1365 		return B_OK;
1366 
1367 	OpenFileCookie* cookie = reinterpret_cast<OpenFileCookie*>(_cookie);
1368 
1369 	if (lock != NULL)
1370 		return inode->ReleaseLock(cookie, lock);
1371 	else
1372 		return inode->ReleaseAllLocks(cookie);
1373 }
1374 
1375 
1376 status_t
1377 nfs4_init()
1378 {
1379 	gRPCServerManager = new(std::nothrow) RPC::ServerManager;
1380 	if (gRPCServerManager == NULL)
1381 		return B_NO_MEMORY;
1382 
1383 	mutex_init(&gIdMapperLock, "idmapper Init Lock");
1384 	gIdMapper = NULL;
1385 
1386 	gWorkQueue = new(std::nothrow) WorkQueue;
1387 	if (gWorkQueue == NULL || gWorkQueue->InitStatus() != B_OK) {
1388 		delete gWorkQueue;
1389 		mutex_destroy(&gIdMapperLock);
1390 		delete gRPCServerManager;
1391 		return B_NO_MEMORY;
1392 	}
1393 
1394 	return B_OK;
1395 }
1396 
1397 
1398 status_t
1399 nfs4_uninit()
1400 {
1401 	RPC::CallbackServer::ShutdownAll();
1402 
1403 	delete gIdMapper;
1404 	delete gWorkQueue;
1405 	delete gRPCServerManager;
1406 
1407 	mutex_destroy(&gIdMapperLock);
1408 
1409 	return B_OK;
1410 }
1411 
1412 
1413 static status_t
1414 nfs4_std_ops(int32 op, ...)
1415 {
1416 	switch (op) {
1417 		case B_MODULE_INIT:
1418 			return nfs4_init();
1419 		case B_MODULE_UNINIT:
1420 			return nfs4_uninit();
1421 		default:
1422 			return B_ERROR;
1423 	}
1424 }
1425 
1426 
1427 fs_volume_ops gNFSv4VolumeOps = {
1428 	nfs4_unmount,
1429 	nfs4_read_fs_info,
1430 	NULL,
1431 	NULL,
1432 	nfs4_get_vnode,
1433 };
1434 
1435 fs_vnode_ops gNFSv4VnodeOps = {
1436 	nfs4_lookup,
1437 	NULL,	// get_vnode_name()
1438 	nfs4_put_vnode,
1439 	nfs4_remove_vnode,
1440 
1441 	/* VM file access */
1442 	NULL,	// can_page()
1443 	nfs4_read_pages,
1444 	nfs4_write_pages,
1445 
1446 	nfs4_io,
1447 	NULL,	// cancel_io()
1448 
1449 	nfs4_get_file_map,
1450 
1451 	NULL,	// ioctl()
1452 	nfs4_set_flags,
1453 	NULL,	// fs_select()
1454 	NULL,	// fs_deselect()
1455 	nfs4_fsync,
1456 
1457 	nfs4_read_symlink,
1458 	nfs4_create_symlink,
1459 
1460 	nfs4_link,
1461 	nfs4_unlink,
1462 	nfs4_rename,
1463 
1464 	nfs4_access,
1465 	nfs4_read_stat,
1466 	nfs4_write_stat,
1467 	NULL,	// fs_preallocate()
1468 
1469 	/* file operations */
1470 	nfs4_create,
1471 	nfs4_open,
1472 	nfs4_close,
1473 	nfs4_free_cookie,
1474 	nfs4_read,
1475 	nfs4_write,
1476 
1477 	/* directory operations */
1478 	nfs4_create_dir,
1479 	nfs4_remove_dir,
1480 	nfs4_open_dir,
1481 	nfs4_close_dir,
1482 	nfs4_free_dir_cookie,
1483 	nfs4_read_dir,
1484 	nfs4_rewind_dir,
1485 
1486 	/* attribute directory operations */
1487 	nfs4_open_attr_dir,
1488 	nfs4_close_attr_dir,
1489 	nfs4_free_attr_dir_cookie,
1490 	nfs4_read_attr_dir,
1491 	nfs4_rewind_attr_dir,
1492 
1493 	/* attribute operations */
1494 	nfs4_create_attr,
1495 	nfs4_open_attr,
1496 	nfs4_close_attr,
1497 	nfs4_free_attr_cookie,
1498 	nfs4_read_attr,
1499 	nfs4_write_attr,
1500 
1501 	nfs4_read_attr_stat,
1502 	nfs4_write_attr_stat,
1503 	nfs4_rename_attr,
1504 	nfs4_remove_attr,
1505 
1506 	/* support for node and FS layers */
1507 	NULL,	// create_special_node
1508 	NULL,	// get_super_vnode
1509 
1510 	/* lock operations */
1511 	nfs4_test_lock,
1512 	nfs4_acquire_lock,
1513 	nfs4_release_lock,
1514 };
1515 
1516 static file_system_module_info sNFSv4ModuleInfo = {
1517 	{
1518 		"file_systems/nfs4" B_CURRENT_FS_API_VERSION,
1519 		0,
1520 		nfs4_std_ops,
1521 	},
1522 
1523 	"nfs4",								// short_name
1524 	"Network File System version 4",	// pretty_name
1525 
1526 	// DDM flags
1527 	0,
1528 
1529 	// scanning
1530 	NULL,	// identify_partition()
1531 	NULL,	// scan_partition()
1532 	NULL,	// free_identify_partition_cookie()
1533 	NULL,	// free_partition_content_cookie()
1534 
1535 	nfs4_mount,
1536 };
1537 
1538 module_info* modules[] = {
1539 	(module_info*)&sNFSv4ModuleInfo,
1540 	NULL,
1541 };
1542 
1543