1// ClientConnection.cpp
2
3#include "ClientConnection.h"
4
5#include <new>
6#include <typeinfo>
7
8#include <dirent.h>
9#include <fcntl.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <utime.h>
14
15#include <Entry.h>
16#include <fs_query.h>
17#include <GraphicsDefs.h>
18#include <NodeMonitor.h>
19#include <Path.h>
20#include <Rect.h>
21#include <Mime.h>
22
23#include <fsproto.h>
24
25#include "AutoDeleter.h"
26#include "AutoLocker.h"
27#include "Compatibility.h"
28#include "Connection.h"
29#include "Debug.h"
30#include "Directory.h"
31#include "Entry.h"
32#include "FDManager.h"
33#include "HashMap.h"
34#include "NodeHandle.h"
35#include "NodeHandleMap.h"
36#include "NodeMonitoringEvent.h"
37#include "Path.h"
38#include "RequestBufferReplacer.h"
39#include "RequestChannel.h"
40#include "RequestConnection.h"
41#include "RequestDumper.h"
42#include "RequestFlattener.h"
43#include "Requests.h"
44#include "SecurityContext.h"
45#include "ServerNodeID.h"
46#include "UserSecurityContext.h"
47#include "Utils.h"
48#include "Volume.h"
49#include "VolumeManager.h"
50
51static const int32 kMaxSaneReadLinkSize		= 10240;	// 10 KB
52static const int32 kMaxReadBufferSize		= 10240;	// 10 KB
53static const int32 kMaxReadDirBufferSize	= 10240;
54
55// Locking:
56//
57// fLock: Guards fReferenceCount and fClosed.
58// fSecurityContextLock: Guards fSecurityContext.
59// fVolumes: Guards the map itself.
60
61
62// #pragma mark -
63// #pragma mark ----- ClientConnection -----
64
65// ConnectionReference
66class ClientConnection::ConnectionReference {
67public:
68	ConnectionReference(ClientConnection* connection)
69		: fConnection(connection)
70	{
71		if (!fConnection || !fConnection->GetReference())
72			fConnection = NULL;
73	}
74
75	~ConnectionReference()
76	{
77		if (fConnection)
78			fConnection->PutReference();
79	}
80
81	bool IsValid() const
82	{
83		return fConnection;
84	}
85
86private:
87	ClientConnection*	fConnection;
88};
89
90// VolumeMap
91struct ClientConnection::VolumeMap
92	: public SynchronizedHashMap<HashKey32<int32>, ClientVolume*> {
93};
94
95// ClientVolumePutter
96class ClientConnection::ClientVolumePutter {
97public:
98	ClientVolumePutter(ClientConnection* connection, ClientVolume* volume)
99		: fConnection(connection),
100		  fVolume(volume)
101	{
102	}
103
104	~ClientVolumePutter()
105	{
106		if (fConnection && fVolume)
107			fConnection->_PutVolume(fVolume);
108	}
109
110	void Detach()
111	{
112		fConnection = NULL;
113		fVolume = NULL;
114	}
115
116private:
117	ClientConnection*	fConnection;
118	ClientVolume*		fVolume;
119};
120
121// VolumeNodeMonitoringEvent
122struct ClientConnection::VolumeNodeMonitoringEvent {
123	VolumeNodeMonitoringEvent(int32 volumeID, NodeMonitoringEvent* event)
124		: volumeID(volumeID),
125		  event(event)
126	{
127		if (event)
128			event->AddReference();
129	}
130
131	~VolumeNodeMonitoringEvent()
132	{
133		if (event)
134			event->RemoveReference();
135	}
136
137	int32					volumeID;
138	NodeMonitoringEvent*	event;
139};
140
141// NodeMonitoringEventQueue
142struct ClientConnection::NodeMonitoringEventQueue
143	: BlockingQueue<NodeMonitoringRequest> {
144	NodeMonitoringEventQueue()
145		: BlockingQueue<NodeMonitoringRequest>("client NM requests")
146	{
147	}
148};
149
150// QueryHandleUnlocker
151struct ClientConnection::QueryHandleUnlocker {
152	QueryHandleUnlocker(ClientConnection* connection, NodeHandle* nodeHandle)
153		: fConnection(connection),
154		  fHandle(nodeHandle)
155	{
156	}
157
158	~QueryHandleUnlocker()
159	{
160		if (fConnection && fHandle) {
161			fConnection->_UnlockQueryHandle(fHandle);
162			fConnection = NULL;
163			fHandle = NULL;
164		}
165	}
166
167private:
168	ClientConnection*	fConnection;
169	NodeHandle*			fHandle;
170};
171
172// ClientVolumeFilter
173struct ClientConnection::ClientVolumeFilter {
174	virtual ~ClientVolumeFilter() {}
175
176	virtual bool FilterVolume(ClientConnection* connection,
177		ClientVolume* volume) = 0;
178};
179
180// HasQueryPermissionClientVolumeFilter
181struct ClientConnection::HasQueryPermissionClientVolumeFilter
182	: ClientConnection::ClientVolumeFilter {
183	virtual bool FilterVolume(ClientConnection* connection,
184		ClientVolume* volume)
185	{
186		return volume->GetSharePermissions().ImpliesQuerySharePermission();
187	}
188};
189
190
191// #pragma mark -
192
193// constructor
194ClientConnection::ClientConnection(Connection* connection,
195	SecurityContext* securityContext, User* user,
196	ClientConnectionListener* listener)
197	: RequestHandler(),
198	  ClientVolume::NodeMonitoringProcessor(),
199	  fConnection(NULL),
200	  fSecurityContext(securityContext),
201	  fSecurityContextLock("security context lock"),
202	  fUser(user),
203	  fVolumes(NULL),
204	  fQueryHandles(NULL),
205	  fListener(listener),
206	  fNodeMonitoringEvents(NULL),
207	  fNodeMonitoringProcessor(-1),
208	  fLock("client connection locker"),
209	  fReferenceCount(0),
210	  fInitialized(0),
211	  fClosed(false),
212	  fError(false),
213	  fInverseClientEndianess(false)
214{
215	fConnection = new(nothrow) RequestConnection(connection, this);
216	if (!fConnection)
217		delete connection;
218}
219
220// destructor
221ClientConnection::~ClientConnection()
222{
223	_Close();
224	delete fConnection;
225
226	// delete all volumes
227	for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();)
228		delete it.Next().value;
229	delete fVolumes;
230
231	delete fQueryHandles;
232	delete fNodeMonitoringEvents;
233}
234
235// Init
236status_t
237ClientConnection::Init()
238{
239	// create a client volume map
240	fVolumes = new(nothrow) VolumeMap;
241	if (!fVolumes)
242		return B_NO_MEMORY;
243	status_t error = fVolumes->InitCheck();
244	if (error != B_OK)
245		return error;
246
247	// create the query handle map
248	fQueryHandles = new(nothrow) NodeHandleMap("query handles");
249	if (!fQueryHandles)
250		return B_NO_MEMORY;
251	error = fQueryHandles->Init();
252	if (error != B_OK)
253		return error;
254
255	// create the node monitoring event queue
256	fNodeMonitoringEvents = new(nothrow) NodeMonitoringEventQueue;
257	if (!fNodeMonitoringEvents)
258		return B_NO_MEMORY;
259	error = fNodeMonitoringEvents->InitCheck();
260	if (error != B_OK)
261		return error;
262
263	// initialize the connection
264	error = fConnection->Init();
265	if (error != B_OK)
266		return error;
267
268	// start the node monitoring processor
269	fNodeMonitoringProcessor = spawn_thread(_NodeMonitoringProcessorEntry,
270		"client connection NM processor", B_NORMAL_PRIORITY, this);
271	if (fNodeMonitoringProcessor < 0) {
272		_Close();
273		return fNodeMonitoringProcessor;
274	}
275	resume_thread(fNodeMonitoringProcessor);
276	return B_OK;
277}
278
279// Close
280/*!
281	Called by the NetFSServer. Not for internal use. Waits for the connection
282	to be closed (at least for the node monitoring thread to be gone).
283*/
284void
285ClientConnection::Close()
286{
287	{
288		ConnectionReference connectionReference(this);
289		if (connectionReference.IsValid())
290			_MarkClosed(false);
291		fListener = NULL;
292	}
293
294	// Wait at least for the node monitoring processor; this is not perfect,
295	// but not too bad either.
296	if (fNodeMonitoringProcessor >= 0
297		&& find_thread(NULL) != fNodeMonitoringProcessor) {
298		int32 result;
299		wait_for_thread(fNodeMonitoringProcessor, &result);
300	}
301}
302
303// GetReference
304bool
305ClientConnection::GetReference()
306{
307	AutoLocker<Locker> _(fLock);
308	if (fClosed || !atomic_or(&fInitialized, 0))
309		return false;
310	fReferenceCount++;
311	return true;
312}
313
314// PutReference
315void
316ClientConnection::PutReference()
317{
318	bool close = false;
319	{
320		AutoLocker<Locker> _(fLock);
321		--fReferenceCount;
322		if (fClosed)
323			close = (fReferenceCount == 0);
324	}
325	if (close)
326		_Close();
327}
328
329// UserRemoved
330void
331ClientConnection::UserRemoved(User* user)
332{
333	// get all volumes
334	ClientVolume** volumes = NULL;
335	int32 volumeCount = 0;
336	AutoLocker<VolumeMap> volumesLocker(fVolumes);
337	volumes = new(nothrow) ClientVolume*[fVolumes->Size()];
338	if (!volumes) {
339		ERROR(("ClientConnection::UserRemoved(): ERROR: Failed to "
340			"allocate memory for volume array.\n"));
341		volumesLocker.Unlock();
342		_UnmountAllVolumes();
343		return;
344	}
345	for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();) {
346		if (ClientVolume* volume = _GetVolume(it.Next().value->GetID()))
347			volumes[volumeCount++] = volume;
348	}
349	volumesLocker.Unlock();
350
351	// unmount the concerned volumes
352	for (int32 i = 0; i < volumeCount; i++) {
353		ClientVolume* volume = volumes[i];
354
355		fSecurityContextLock.Lock();
356		bool unmount = (volume->GetSecurityContext()->GetUser() == user);
357		fSecurityContextLock.Unlock();
358
359		if (unmount)
360			_UnmountVolume(volume);
361	}
362
363	// put the volumes
364	for (int32 i = 0; i < volumeCount; i++)
365		_PutVolume(volumes[i]);
366	delete[] volumes;
367}
368
369// ShareRemoved
370void
371ClientConnection::ShareRemoved(Share* share)
372{
373	// get all volumes
374	ClientVolume** volumes = NULL;
375	int32 volumeCount = 0;
376	AutoLocker<VolumeMap> volumesLocker(fVolumes);
377	volumes = new(nothrow) ClientVolume*[fVolumes->Size()];
378	if (!volumes) {
379		ERROR(("ClientConnection::ShareRemoved(): ERROR: Failed to "
380			"allocate memory for volume array.\n"));
381		volumesLocker.Unlock();
382		_UnmountAllVolumes();
383		return;
384	}
385	for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();) {
386		if (ClientVolume* volume = _GetVolume(it.Next().value->GetID()))
387			volumes[volumeCount++] = volume;
388	}
389	volumesLocker.Unlock();
390
391	// unmount the concerned volumes
392	for (int32 i = 0; i < volumeCount; i++) {
393		ClientVolume* volume = volumes[i];
394
395		fSecurityContextLock.Lock();
396		bool unmount = (volume->GetShare() == share);
397		fSecurityContextLock.Unlock();
398
399		if (unmount)
400			_UnmountVolume(volume);
401	}
402
403	// put the volumes
404	for (int32 i = 0; i < volumeCount; i++)
405		_PutVolume(volumes[i]);
406	delete[] volumes;
407}
408
409// UserPermissionsChanged
410void
411ClientConnection::UserPermissionsChanged(Share* share, User* user,
412	Permissions permissions)
413{
414	bool unmountAll = (!permissions.ImpliesMountSharePermission());
415
416	// get all volumes
417	ClientVolume** volumes = NULL;
418	int32 volumeCount = 0;
419	AutoLocker<VolumeMap> volumesLocker(fVolumes);
420	volumes = new(nothrow) ClientVolume*[fVolumes->Size()];
421	if (!volumes) {
422		ERROR(("ClientConnection::ShareRemoved(): ERROR: Failed to "
423			"allocate memory for volume array.\n"));
424		volumesLocker.Unlock();
425		_UnmountAllVolumes();
426		return;
427	}
428	for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();) {
429		if (ClientVolume* volume = _GetVolume(it.Next().value->GetID()))
430			volumes[volumeCount++] = volume;
431	}
432	volumesLocker.Unlock();
433
434	// update the concerned volumes
435	for (int32 i = 0; i < volumeCount; i++) {
436		ClientVolume* volume = volumes[i];
437
438		fSecurityContextLock.Lock();
439		bool concerned = (volume->GetShare() == share
440			&& volume->GetSecurityContext()->GetUser() == user);
441		fSecurityContextLock.Unlock();
442
443		if (concerned) {
444			// create a new user security context for the volume
445			status_t error = B_OK;
446
447			if (unmountAll) {
448				_UnmountVolume(volume);
449			} else {
450				// create a new user security context
451				AutoLocker<Locker> securityContextLocker(fSecurityContextLock);
452				UserSecurityContext* userSecurityContext
453					= new(nothrow) UserSecurityContext;
454
455				// init it
456				if (userSecurityContext) {
457					error = fSecurityContext->GetUserSecurityContext(user,
458						userSecurityContext);
459				} else
460					error = B_NO_MEMORY;
461				if (error != B_OK) {
462					delete userSecurityContext;
463					securityContextLocker.Unlock();
464					_UnmountVolume(volume);
465					continue;
466				}
467
468				// set the volume's new user security context
469				securityContextLocker.Unlock();
470				volume->SetSecurityContext(userSecurityContext);
471			}
472		}
473	}
474
475	// put the volumes
476	for (int32 i = 0; i < volumeCount; i++)
477		_PutVolume(volumes[i]);
478	delete[] volumes;
479}
480
481
482// #pragma mark -
483
484// VisitConnectionBrokenRequest
485status_t
486ClientConnection::VisitConnectionBrokenRequest(ConnectionBrokenRequest* request)
487{
488	ConnectionReference connectionReference(this);
489	if (!connectionReference.IsValid())
490		return B_OK;
491
492	_MarkClosed(true);
493	return B_OK;
494}
495
496// VisitInitConnectionRequest
497status_t
498ClientConnection::VisitInitConnectionRequest(InitConnectionRequest* request)
499{
500	bool alreadyInitialized = atomic_or(&fInitialized, ~0);
501
502	ConnectionReference connectionReference(this);
503	if (!connectionReference.IsValid())
504		return B_OK;
505
506	if (!alreadyInitialized)
507		fInverseClientEndianess = (request->bigEndian != B_HOST_IS_BENDIAN);
508
509	// prepare the reply
510	InitConnectionReply reply;
511
512	// send the reply
513	reply.error = (alreadyInitialized ? B_BAD_VALUE : B_OK);
514	status_t error = GetChannel()->SendRequest(&reply);
515
516	// on error just close
517	if (error != B_OK)
518		_MarkClosed(true);
519	return B_OK;
520}
521
522// VisitMountRequest
523status_t
524ClientConnection::VisitMountRequest(MountRequest* request)
525{
526	ConnectionReference connectionReference(this);
527	if (!connectionReference.IsValid())
528		return B_OK;
529
530	status_t result = B_OK;
531	const char* shareName = request->share.GetString();
532	if (!shareName)
533		SET_ERROR(result, B_BAD_DATA);
534
535	// create a volume
536	ClientVolume* volume = NULL;
537	if (result == B_OK)
538		result = _CreateVolume(&volume);
539	ClientVolumePutter volumePutter(this, volume);
540
541	// if we haven't been supplied with a user yet, use the info from the
542	// mount request for authentication
543	VolumeManagerLocker managerLocker;
544	AutoLocker<Locker> securityContextLocker(fSecurityContextLock);
545	const char* userName = request->user.GetString();
546	User* user = fUser;
547	Reference<User> userReference(user);
548	bool noPermission = false;
549	if (result == B_OK && !user) {
550		if (userName) {
551			SET_ERROR(result, fSecurityContext->AuthenticateUser(userName,
552				request->password.GetString(), &user));
553			if (result == B_OK)
554				userReference.SetTo(user, true);
555		} else
556			result = B_PERMISSION_DENIED;
557		if (result == B_PERMISSION_DENIED)
558			noPermission = true;
559	}
560
561	// create a user security context
562	UserSecurityContext* securityContext = NULL;
563	if (result == B_OK) {
564		securityContext = new(nothrow) UserSecurityContext;
565		if (securityContext) {
566			SET_ERROR(result, fSecurityContext->GetUserSecurityContext(user,
567				securityContext));
568		} else
569			SET_ERROR(result, B_NO_MEMORY);
570	}
571	ObjectDeleter<UserSecurityContext> securityContextDeleter(securityContext);
572
573	// get the share
574	Share* share = NULL;
575	Permissions sharePermissions;
576	node_ref mountPoint;
577	if (result == B_OK) {
578		AutoLocker<SecurityContext> _(fSecurityContext);
579		share = fSecurityContext->FindShare(shareName);
580		if (share) {
581			mountPoint = share->GetNodeRef();
582			sharePermissions = securityContext->GetNodePermissions(
583				mountPoint);
584			if (!sharePermissions.ImpliesMountSharePermission()) {
585				SET_ERROR(result, B_PERMISSION_DENIED);
586				noPermission = true;
587			}
588		} else
589			SET_ERROR(result, B_ENTRY_NOT_FOUND);
590	}
591	Reference<Share> shareReference(share, true);
592
593	// mount the volume
594	MountReply reply;
595	if (result == B_OK) {
596		SET_ERROR(result, volume->Mount(securityContext, share));
597		securityContextDeleter.Detach();
598	}
599	if (result == B_OK) {
600		_GetNodeInfo(volume->GetRootDirectory(), &reply.nodeInfo);
601		reply.sharePermissions = sharePermissions.GetPermissions();
602		reply.volumeID = volume->GetID();
603	}
604
605	// make sure, the volume is removed on error
606	if (result != B_OK && volume) {
607		AutoLocker<VolumeMap> volumeMapLocker(fVolumes);
608		volume->MarkRemoved();
609	}
610
611	securityContextLocker.Unlock();
612	managerLocker.Unlock();
613
614	// send the reply
615	reply.error = result;
616	reply.noPermission = noPermission;
617	return GetChannel()->SendRequest(&reply);
618}
619
620// VisitUnmountRequest
621status_t
622ClientConnection::VisitUnmountRequest(UnmountRequest* request)
623{
624	ConnectionReference connectionReference(this);
625	if (!connectionReference.IsValid())
626		return B_OK;
627
628	if (ClientVolume* volume = _GetVolume(request->volumeID)) {
629		_UnmountVolume(volume);
630		_PutVolume(volume);
631	}
632
633	return B_OK;
634}
635
636// VisitReadVNodeRequest
637status_t
638ClientConnection::VisitReadVNodeRequest(ReadVNodeRequest* request)
639{
640	ConnectionReference connectionReference(this);
641	if (!connectionReference.IsValid())
642		return B_OK;
643
644	// get the volume
645	status_t result = B_OK;
646	ClientVolume* volume = _GetVolume(request->volumeID);
647	if (!volume)
648		result = B_BAD_VALUE;
649	ClientVolumePutter volumePutter(this, volume);
650
651	VolumeManagerLocker managerLocker;
652
653	// get the node
654	Node* node = NULL;
655	if (result == B_OK) {
656		node = volume->GetNode(request->nodeID);
657		if (!node)
658			result = B_ENTRY_NOT_FOUND;
659	}
660
661	// prepare the reply
662	ReadVNodeReply reply;
663	if (result == B_OK)
664		_GetNodeInfo(node, &reply.nodeInfo);
665
666	managerLocker.Unlock();
667
668	// send the reply
669	reply.error = result;
670	return GetChannel()->SendRequest(&reply);
671}
672
673// VisitWriteStatRequest
674status_t
675ClientConnection::VisitWriteStatRequest(WriteStatRequest* request)
676{
677	ConnectionReference connectionReference(this);
678	if (!connectionReference.IsValid())
679		return B_OK;
680
681	// get the volume
682	status_t result = B_OK;
683	ClientVolume* volume = _GetVolume(request->volumeID);
684	if (!volume)
685		result = B_BAD_VALUE;
686	ClientVolumePutter volumePutter(this, volume);
687
688	VolumeManagerLocker managerLocker;
689
690	// get the node
691	Node* node = NULL;
692	if (result == B_OK) {
693		node = volume->GetNode(request->nodeID);
694		if (!node)
695			result = B_ENTRY_NOT_FOUND;
696	}
697
698	// check permissions
699	if (result == B_OK) {
700		if (!volume->GetNodePermissions(node).ImpliesWritePermission())
701			result = B_PERMISSION_DENIED;
702	}
703
704	// get the path
705	Path path;
706	if (result == B_OK)
707		result = node->GetPath(&path);
708
709	// write the stat
710	uint32 mask = request->mask;
711	// size
712	if (result == B_OK && (mask & WSTAT_SIZE)) {
713		if (truncate(path.GetPath(), request->nodeInfo.st.st_size) < 0)
714			result = errno;
715	}
716	// mode
717	if (result == B_OK && (mask & WSTAT_MODE)) {
718		if (chmod(path.GetPath(), request->nodeInfo.st.st_mode) < 0)
719			result = errno;
720	}
721	// mtime
722	if (result == B_OK && (mask & (WSTAT_ATIME | WSTAT_MTIME))) {
723		utimbuf buffer;
724		buffer.actime = (mask & WSTAT_ATIME)
725			? request->nodeInfo.st.st_atime
726			: node->GetStat().st_atime;
727		buffer.modtime = (mask & WSTAT_MTIME)
728			? request->nodeInfo.st.st_mtime
729			: node->GetStat().st_mtime;
730		if (utime(path.GetPath(), &buffer) < 0)
731			result = errno;
732	}
733	// ignore WSTAT_CRTIME, WSTAT_UID, WSTAT_GID for the time being
734
735	// prepare the reply
736	WriteStatReply reply;
737	// update the node stat
738	reply.nodeInfoValid = false;
739	if (node) {
740		if (node->UpdateStat() == B_OK) {
741			_GetNodeInfo(node, &reply.nodeInfo);
742			reply.nodeInfoValid = true;
743		}
744	}
745
746	managerLocker.Unlock();
747
748	// send the reply
749	reply.error = result;
750	return GetChannel()->SendRequest(&reply);
751}
752
753// VisitCreateFileRequest
754status_t
755ClientConnection::VisitCreateFileRequest(CreateFileRequest* request)
756{
757	ConnectionReference connectionReference(this);
758	if (!connectionReference.IsValid())
759		return B_OK;
760
761	// get the volume
762	status_t result = B_OK;
763	ClientVolume* volume = _GetVolume(request->volumeID);
764	if (!volume)
765		result = B_BAD_VALUE;
766	ClientVolumePutter volumePutter(this, volume);
767
768	VolumeManagerLocker managerLocker;
769
770	// get the directory
771	Directory* directory = NULL;
772	if (result == B_OK) {
773		Node* node = volume->GetNode(request->directoryID);
774		if (node) {
775			directory = dynamic_cast<Directory*>(node);
776			if (!directory)
777				result = B_NOT_A_DIRECTORY;
778		} else
779			result = B_ENTRY_NOT_FOUND;
780	}
781
782	// check permissions
783	int openMode = request->openMode;
784	if (result == B_OK) {
785		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
786			result = B_PERMISSION_DENIED;
787	}
788
789	// get the path
790	Path path;
791	if (result == B_OK) {
792		result = directory->GetPath(&path);
793		if (result == B_OK)
794			result = path.Append(request->name.GetString());
795	}
796
797	// create the file
798	if (result == B_OK) {
799		int fd = -1;
800		result = FDManager::Open(path.GetPath(),
801			openMode | O_CREAT | O_NOTRAVERSE, request->mode, fd);
802		if (result == B_OK)
803			close(fd);
804	}
805
806	// load the new entry
807	Entry* entry = NULL;
808	if (result == B_OK) {
809		VolumeManager* volumeManager = VolumeManager::GetDefault();
810
811		// if there existed an entry before, we need to delete it, to avoid that
812		// we open the wrong node
813		entry = volumeManager->GetEntry(directory->GetVolumeID(),
814			directory->GetID(), request->name.GetString());
815		if (entry)
816			volumeManager->DeleteEntry(entry, false);
817
818		// load the new entry
819		entry = NULL;
820		result = volume->LoadEntry(directory, request->name.GetString(),
821			&entry);
822	}
823
824	// open the node
825	FileHandle* handle = NULL;
826	if (result == B_OK) {
827		openMode &= ~(O_CREAT | O_EXCL | O_TRUNC);
828		result = volume->Open(entry->GetNode(), openMode, &handle);
829	}
830	NodeHandleUnlocker handleUnlocker(volume, handle);
831
832	// prepare the reply
833	CreateFileReply reply;
834	if (result == B_OK) {
835		_GetEntryInfo(entry, &reply.entryInfo);
836		reply.cookie = handle->GetCookie();
837	}
838
839	managerLocker.Unlock();
840
841	// send the reply
842	reply.error = result;
843	status_t error = GetChannel()->SendRequest(&reply);
844
845	// close the handle, if a send error occurred
846	if (error != B_OK && result == B_OK)
847		volume->Close(handle);
848
849	return error;
850}
851
852// VisitOpenRequest
853status_t
854ClientConnection::VisitOpenRequest(OpenRequest* request)
855{
856	ConnectionReference connectionReference(this);
857	if (!connectionReference.IsValid())
858		return B_OK;
859
860	// get the volume
861	status_t result = B_OK;
862	ClientVolume* volume = _GetVolume(request->volumeID);
863	if (!volume)
864		result = B_BAD_VALUE;
865	ClientVolumePutter volumePutter(this, volume);
866
867	VolumeManagerLocker managerLocker;
868
869	// get the node
870	Node* node = NULL;
871	if (result == B_OK) {
872		node = volume->GetNode(request->nodeID);
873		if (!node)
874			result = B_ENTRY_NOT_FOUND;
875	}
876
877	// check permissions
878	int openMode = request->openMode;
879	if (result == B_OK) {
880		Permissions permissions = volume->GetNodePermissions(node);
881		if ((openMode & O_RWMASK) == O_RDWR) {
882			// read+write: fall back to read/write only, if the other permission
883			// is missing
884			if (!permissions.ImpliesReadPermission())
885				openMode = openMode & ~O_RWMASK | O_WRONLY;
886			else if (!permissions.ImpliesWritePermission())
887				openMode = openMode & ~O_RWMASK | O_RDONLY;
888		}
889		if ((openMode & O_RWMASK) == O_RDONLY) {
890			if (!permissions.ImpliesReadPermission())
891				result = B_PERMISSION_DENIED;
892		} else if ((openMode & O_RWMASK) == O_WRONLY) {
893			if (!permissions.ImpliesWritePermission())
894				result = B_PERMISSION_DENIED;
895		}
896	}
897
898	// open the node
899	FileHandle* handle = NULL;
900	if (result == B_OK)
901		result = volume->Open(node, openMode, &handle);
902	NodeHandleUnlocker handleUnlocker(volume, handle);
903
904	// prepare the reply
905	OpenReply reply;
906	if (result == B_OK) {
907		_GetNodeInfo(node, &reply.nodeInfo);
908		reply.cookie = handle->GetCookie();
909	}
910
911	managerLocker.Unlock();
912
913	// send the reply
914	reply.error = result;
915	status_t error = GetChannel()->SendRequest(&reply);
916
917	// close the handle, if a send error occurred
918	if (error != B_OK && result == B_OK)
919		volume->Close(handle);
920
921	return error;
922}
923
924// VisitCloseRequest
925status_t
926ClientConnection::VisitCloseRequest(CloseRequest* request)
927{
928	ConnectionReference connectionReference(this);
929	if (!connectionReference.IsValid())
930		return B_OK;
931
932	status_t result = B_OK;
933
934	if (request->volumeID >= 0) {
935		// get the volume
936		ClientVolume* volume = _GetVolume(request->volumeID);
937		if (!volume)
938			SET_ERROR(result, B_BAD_VALUE);
939		ClientVolumePutter volumePutter(this, volume);
940
941		// get the node handle
942		NodeHandle* handle = NULL;
943		if (result == B_OK)
944			SET_ERROR(result, volume->LockNodeHandle(request->cookie, &handle));
945		NodeHandleUnlocker handleUnlocker(volume, handle);
946
947		VolumeManagerLocker managerLocker;
948
949		// close it
950		if (result == B_OK)
951			SET_ERROR(result, volume->Close(handle));
952
953		managerLocker.Unlock();
954	} else {
955		// no volume ID given, so this is a query handle
956		// lock the handle
957		QueryHandle* handle = NULL;
958		if (result == B_OK)
959			SET_ERROR(result, _LockQueryHandle(request->cookie, &handle));
960		QueryHandleUnlocker handleUnlocker(this, handle);
961
962		// close it
963		if (result == B_OK)
964			SET_ERROR(result, _CloseQuery(handle));
965	}
966
967	// send the reply
968	CloseReply reply;
969	reply.error = result;
970	return GetChannel()->SendRequest(&reply);
971}
972
973// VisitReadRequest
974status_t
975ClientConnection::VisitReadRequest(ReadRequest* request)
976{
977	ConnectionReference connectionReference(this);
978	if (!connectionReference.IsValid())
979		return B_OK;
980
981	// get the volume
982	status_t result = B_OK;
983	ClientVolume* volume = _GetVolume(request->volumeID);
984	if (!volume)
985		result = B_BAD_VALUE;
986	ClientVolumePutter volumePutter(this, volume);
987
988	// get the node handle
989	NodeHandle* handle = NULL;
990	if (result == B_OK)
991		result = volume->LockNodeHandle(request->cookie, &handle);
992	NodeHandleUnlocker handleUnlocker(volume, handle);
993
994	// check if it is a file handle
995	FileHandle* fileHandle = NULL;
996	if (result == B_OK) {
997		fileHandle = dynamic_cast<FileHandle*>(handle);
998		if (!fileHandle)
999			result = B_BAD_VALUE;
1000	}
1001
1002	VolumeManagerLocker managerLocker;
1003
1004	// check read permission
1005	if (result == B_OK) {
1006		Node* node = volume->GetNode(fileHandle->GetNodeRef());
1007		if (!node || !volume->GetNodePermissions(node).ImpliesReadPermission())
1008			result = B_PERMISSION_DENIED;
1009	}
1010
1011	managerLocker.Unlock();
1012
1013	off_t pos = request->pos;
1014	int32 size = request->size;
1015	int32 bufferSize = min(size, kMaxReadBufferSize);
1016	// allocate a buffer
1017	uint8* buffer = NULL;
1018	if (result == B_OK) {
1019		buffer = (uint8*)malloc(bufferSize);
1020		if (!buffer)
1021			result = B_NO_MEMORY;
1022	}
1023	MemoryDeleter bufferDeleter(buffer);
1024
1025	// read as long as there are bytes left to read or an error occurs
1026	bool moreToRead = true;
1027	do {
1028		int32 bytesToRead = min(size, bufferSize);
1029		size_t bytesRead = 0;
1030		if (result == B_OK)
1031			result = fileHandle->Read(pos, buffer, bytesToRead, &bytesRead);
1032		moreToRead = (result == B_OK && bytesRead > 0
1033			&& (int32)bytesRead < size);
1034
1035		// prepare the reply
1036		ReadReply reply;
1037		if (result == B_OK) {
1038			reply.pos = pos;
1039			reply.data.SetTo(buffer, bytesRead);
1040			reply.moreToCome = moreToRead;
1041			pos += bytesRead;
1042			size -= bytesRead;
1043		}
1044
1045		// send the reply
1046		reply.error = result;
1047		status_t error = GetChannel()->SendRequest(&reply);
1048		if (error != B_OK)
1049			return error;
1050	} while (moreToRead);
1051
1052	return B_OK;
1053}
1054
1055// VisitWriteRequest
1056status_t
1057ClientConnection::VisitWriteRequest(WriteRequest* request)
1058{
1059	ConnectionReference connectionReference(this);
1060	if (!connectionReference.IsValid())
1061		return B_OK;
1062
1063	// get the volume
1064	status_t result = B_OK;
1065	ClientVolume* volume = _GetVolume(request->volumeID);
1066	if (!volume)
1067		result = B_BAD_VALUE;
1068	ClientVolumePutter volumePutter(this, volume);
1069
1070	// get the node handle
1071	NodeHandle* handle = NULL;
1072	if (result == B_OK)
1073		result = volume->LockNodeHandle(request->cookie, &handle);
1074	NodeHandleUnlocker handleUnlocker(volume, handle);
1075
1076	// check if it is a file handle
1077	FileHandle* fileHandle = NULL;
1078	if (result == B_OK) {
1079		fileHandle = dynamic_cast<FileHandle*>(handle);
1080		if (!fileHandle)
1081			result = B_BAD_VALUE;
1082	}
1083
1084	VolumeManagerLocker managerLocker;
1085
1086	// check read permission
1087	if (result == B_OK) {
1088		Node* node = volume->GetNode(fileHandle->GetNodeRef());
1089		if (!node || !volume->GetNodePermissions(node).ImpliesWritePermission())
1090			result = B_PERMISSION_DENIED;
1091	}
1092
1093	managerLocker.Unlock();
1094
1095	// write until all has been written or an error occurs
1096	off_t pos = request->pos;
1097	int32 size = request->data.GetSize();
1098	const char* buffer = (const char*)request->data.GetData();
1099	while (result == B_OK && size > 0) {
1100		size_t bytesWritten;
1101		result = fileHandle->Write(pos, buffer, size, &bytesWritten);
1102		if (result == B_OK) {
1103			pos += bytesWritten;
1104			buffer += bytesWritten;
1105			size -= bytesWritten;
1106		}
1107	}
1108
1109	// prepare the reply
1110	WriteReply reply;
1111	// send the reply
1112	reply.error = result;
1113	return GetChannel()->SendRequest(&reply);
1114}
1115
1116// VisitCreateLinkRequest
1117status_t
1118ClientConnection::VisitCreateLinkRequest(CreateLinkRequest* request)
1119{
1120	ConnectionReference connectionReference(this);
1121	if (!connectionReference.IsValid())
1122		return B_OK;
1123
1124	// get the volume
1125	status_t result = B_OK;
1126	ClientVolume* volume = _GetVolume(request->volumeID);
1127	if (!volume)
1128		result = B_BAD_VALUE;
1129	ClientVolumePutter volumePutter(this, volume);
1130
1131	VolumeManagerLocker managerLocker;
1132
1133	// get the target node
1134	Node* node = NULL;
1135	if (result == B_OK) {
1136		node = volume->GetNode(request->nodeID);
1137		if (!node)
1138			result = B_ENTRY_NOT_FOUND;
1139	}
1140
1141	// get the target node path
1142	Path targetPath;
1143	if (result == B_OK)
1144		result = node->GetPath(&targetPath);
1145
1146	// get the directory
1147	Directory* directory = NULL;
1148	if (result == B_OK) {
1149		Node* node = volume->GetNode(request->directoryID);
1150		if (node) {
1151			directory = dynamic_cast<Directory*>(node);
1152			if (!directory)
1153				result = B_NOT_A_DIRECTORY;
1154		} else
1155			result = B_ENTRY_NOT_FOUND;
1156	}
1157
1158	// check permissions
1159	if (result == B_OK) {
1160		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
1161			result = B_PERMISSION_DENIED;
1162	}
1163
1164	// get the new entry's path
1165	Path path;
1166	if (result == B_OK) {
1167		result = directory->GetPath(&path);
1168		if (result == B_OK)
1169			result = path.Append(request->name.GetString());
1170	}
1171
1172	managerLocker.Unlock();
1173
1174	// create the link
1175	if (result == B_OK) {
1176		if (link(targetPath.GetPath(), path.GetPath()) < 0)
1177			result = errno;
1178	}
1179
1180	// prepare the reply
1181	CreateSymlinkReply reply;
1182	// send the reply
1183	reply.error = result;
1184	return GetChannel()->SendRequest(&reply);
1185}
1186
1187// VisitUnlinkRequest
1188status_t
1189ClientConnection::VisitUnlinkRequest(UnlinkRequest* request)
1190{
1191	ConnectionReference connectionReference(this);
1192	if (!connectionReference.IsValid())
1193		return B_OK;
1194
1195	// get the volume
1196	status_t result = B_OK;
1197	ClientVolume* volume = _GetVolume(request->volumeID);
1198	if (!volume)
1199		result = B_BAD_VALUE;
1200	ClientVolumePutter volumePutter(this, volume);
1201
1202	VolumeManagerLocker managerLocker;
1203
1204	// get the directory
1205	Directory* directory = NULL;
1206	if (result == B_OK) {
1207		Node* node = volume->GetNode(request->directoryID);
1208		if (node) {
1209			directory = dynamic_cast<Directory*>(node);
1210			if (!directory)
1211				result = B_NOT_A_DIRECTORY;
1212		} else
1213			result = B_ENTRY_NOT_FOUND;
1214	}
1215
1216	// check permissions
1217	if (result == B_OK) {
1218		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
1219			result = B_PERMISSION_DENIED;
1220	}
1221
1222	// get the entry's path
1223	Path path;
1224	if (result == B_OK) {
1225		result = directory->GetPath(&path);
1226		if (result == B_OK)
1227			result = path.Append(request->name.GetString());
1228	}
1229
1230	managerLocker.Unlock();
1231
1232	// remove the entry
1233	if (result == B_OK) {
1234		if (unlink(path.GetPath()) < 0)
1235			result = errno;
1236	}
1237
1238	// prepare the reply
1239	UnlinkReply reply;
1240	// send the reply
1241	reply.error = result;
1242	return GetChannel()->SendRequest(&reply);
1243}
1244
1245// VisitCreateSymlinkRequest
1246status_t
1247ClientConnection::VisitCreateSymlinkRequest(CreateSymlinkRequest* request)
1248{
1249	ConnectionReference connectionReference(this);
1250	if (!connectionReference.IsValid())
1251		return B_OK;
1252
1253	// get the volume
1254	status_t result = B_OK;
1255	ClientVolume* volume = _GetVolume(request->volumeID);
1256	if (!volume)
1257		result = B_BAD_VALUE;
1258	ClientVolumePutter volumePutter(this, volume);
1259
1260	VolumeManagerLocker managerLocker;
1261
1262	// get the directory
1263	Directory* directory = NULL;
1264	if (result == B_OK) {
1265		Node* node = volume->GetNode(request->directoryID);
1266		if (node) {
1267			directory = dynamic_cast<Directory*>(node);
1268			if (!directory)
1269				result = B_NOT_A_DIRECTORY;
1270		} else
1271			result = B_ENTRY_NOT_FOUND;
1272	}
1273
1274	// check permissions
1275	if (result == B_OK) {
1276		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
1277			result = B_PERMISSION_DENIED;
1278	}
1279
1280	// get the new entry's path
1281	Path path;
1282	if (result == B_OK) {
1283		result = directory->GetPath(&path);
1284		if (result == B_OK)
1285			result = path.Append(request->name.GetString());
1286	}
1287
1288	managerLocker.Unlock();
1289
1290	// create the symlink
1291	if (result == B_OK) {
1292		if (symlink(request->target.GetString(), path.GetPath()) < 0)
1293			result = errno;
1294	}
1295
1296	// prepare the reply
1297	CreateSymlinkReply reply;
1298	// send the reply
1299	reply.error = result;
1300	return GetChannel()->SendRequest(&reply);
1301}
1302
1303// VisitReadLinkRequest
1304status_t
1305ClientConnection::VisitReadLinkRequest(ReadLinkRequest* request)
1306{
1307	ConnectionReference connectionReference(this);
1308	if (!connectionReference.IsValid())
1309		return B_OK;
1310
1311	// get the volume
1312	status_t result = B_OK;
1313	ClientVolume* volume = _GetVolume(request->volumeID);
1314	if (!volume)
1315		result = B_BAD_VALUE;
1316	ClientVolumePutter volumePutter(this, volume);
1317
1318	VolumeManagerLocker managerLocker;
1319
1320	// get the node
1321	Node* node = NULL;
1322	if (result == B_OK) {
1323		node = volume->GetNode(request->nodeID);
1324		if (!node)
1325			result = B_ENTRY_NOT_FOUND;
1326	}
1327
1328	int32 bufferSize = request->maxSize;
1329
1330	// check read permission
1331	if (result == B_OK) {
1332		if (!volume->GetNodePermissions(node).ImpliesReadPermission())
1333			result = B_PERMISSION_DENIED;
1334	}
1335
1336	// allocate a buffer
1337	void* buffer = NULL;
1338	if (result == B_OK) {
1339		if (bufferSize < 0 || bufferSize > kMaxSaneReadLinkSize)
1340			result = B_BAD_DATA;
1341	}
1342	if (result == B_OK) {
1343		buffer = malloc(bufferSize);
1344		if (!buffer)
1345			result = B_NO_MEMORY;
1346	}
1347	MemoryDeleter bufferDeleter(buffer);
1348
1349	// read the link and prepare the reply
1350	ReadLinkReply reply;
1351	int32 bytesRead = 0;
1352	if (result == B_OK)
1353		result = node->ReadSymlink((char*)buffer, bufferSize, &bytesRead);
1354	if (result == B_OK) {
1355		reply.data.SetTo(buffer, bytesRead);
1356		_GetNodeInfo(node, &reply.nodeInfo);
1357	}
1358
1359	managerLocker.Unlock();
1360
1361	// send the reply
1362	reply.error = result;
1363	return GetChannel()->SendRequest(&reply);
1364}
1365
1366// VisitRenameRequest
1367status_t
1368ClientConnection::VisitRenameRequest(RenameRequest* request)
1369{
1370	ConnectionReference connectionReference(this);
1371	if (!connectionReference.IsValid())
1372		return B_OK;
1373
1374	// get the volume
1375	status_t result = B_OK;
1376	ClientVolume* volume = _GetVolume(request->volumeID);
1377	if (!volume)
1378		result = B_BAD_VALUE;
1379	ClientVolumePutter volumePutter(this, volume);
1380
1381	VolumeManagerLocker managerLocker;
1382
1383	// get the new directory
1384	Directory* newDirectory = NULL;
1385	if (result == B_OK) {
1386		Node* node = volume->GetNode(request->newDirectoryID);
1387		if (node) {
1388			newDirectory = dynamic_cast<Directory*>(node);
1389			if (!newDirectory)
1390				result = B_NOT_A_DIRECTORY;
1391		} else
1392			result = B_ENTRY_NOT_FOUND;
1393	}
1394
1395	// check permissions
1396	if (result == B_OK) {
1397		if (!volume->GetNodePermissions(newDirectory).ImpliesWritePermission())
1398			result = B_PERMISSION_DENIED;
1399	}
1400
1401	// get the new path
1402	Path newPath;
1403	if (result == B_OK) {
1404		result = newDirectory->GetPath(&newPath);
1405		if (result == B_OK)
1406			result = newPath.Append(request->newName.GetString());
1407	}
1408
1409	// get the old directory
1410	Directory* oldDirectory = NULL;
1411	if (result == B_OK) {
1412		Node* node = volume->GetNode(request->oldDirectoryID);
1413		if (node) {
1414			oldDirectory = dynamic_cast<Directory*>(node);
1415			if (!oldDirectory)
1416				result = B_NOT_A_DIRECTORY;
1417		} else
1418			result = B_ENTRY_NOT_FOUND;
1419	}
1420
1421	// check permissions
1422	if (result == B_OK) {
1423		if (!volume->GetNodePermissions(oldDirectory).ImpliesWritePermission())
1424			result = B_PERMISSION_DENIED;
1425	}
1426
1427	// get the new path
1428	Path oldPath;
1429	if (result == B_OK) {
1430		result = oldDirectory->GetPath(&oldPath);
1431		if (result == B_OK)
1432			result = oldPath.Append(request->oldName.GetString());
1433	}
1434
1435	managerLocker.Unlock();
1436
1437	// rename the entry
1438	if (result == B_OK) {
1439		if (rename(oldPath.GetPath(), newPath.GetPath()) < 0)
1440			result = errno;
1441	}
1442
1443	// prepare the reply
1444	RenameReply reply;
1445	// send the reply
1446	reply.error = result;
1447	return GetChannel()->SendRequest(&reply);
1448}
1449
1450// VisitMakeDirRequest
1451status_t
1452ClientConnection::VisitMakeDirRequest(MakeDirRequest* request)
1453{
1454	ConnectionReference connectionReference(this);
1455	if (!connectionReference.IsValid())
1456		return B_OK;
1457
1458	// get the volume
1459	status_t result = B_OK;
1460	ClientVolume* volume = _GetVolume(request->volumeID);
1461	if (!volume)
1462		result = B_BAD_VALUE;
1463	ClientVolumePutter volumePutter(this, volume);
1464
1465	VolumeManagerLocker managerLocker;
1466
1467	// get the directory
1468	Directory* directory = NULL;
1469	if (result == B_OK) {
1470		Node* node = volume->GetNode(request->directoryID);
1471		if (node) {
1472			directory = dynamic_cast<Directory*>(node);
1473			if (!directory)
1474				result = B_NOT_A_DIRECTORY;
1475		} else
1476			result = B_ENTRY_NOT_FOUND;
1477	}
1478
1479	// check permissions
1480	if (result == B_OK) {
1481		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
1482			result = B_PERMISSION_DENIED;
1483	}
1484
1485	// get the path
1486	Path path;
1487	if (result == B_OK) {
1488		result = directory->GetPath(&path);
1489		if (result == B_OK)
1490			result = path.Append(request->name.GetString());
1491	}
1492
1493	managerLocker.Unlock();
1494
1495	// create the directory
1496	if (result == B_OK) {
1497		if (mkdir(path.GetPath(), request->mode) < 0)
1498			result = errno;
1499	}
1500
1501	// prepare the reply
1502	MakeDirReply reply;
1503	// send the reply
1504	reply.error = result;
1505	return GetChannel()->SendRequest(&reply);
1506}
1507
1508// VisitRemoveDirRequest
1509status_t
1510ClientConnection::VisitRemoveDirRequest(RemoveDirRequest* request)
1511{
1512	ConnectionReference connectionReference(this);
1513	if (!connectionReference.IsValid())
1514		return B_OK;
1515
1516	// get the volume
1517	status_t result = B_OK;
1518	ClientVolume* volume = _GetVolume(request->volumeID);
1519	if (!volume)
1520		result = B_BAD_VALUE;
1521	ClientVolumePutter volumePutter(this, volume);
1522
1523	VolumeManagerLocker managerLocker;
1524
1525	// get the directory
1526	Directory* directory = NULL;
1527	if (result == B_OK) {
1528		Node* node = volume->GetNode(request->directoryID);
1529		if (node) {
1530			directory = dynamic_cast<Directory*>(node);
1531			if (!directory)
1532				result = B_NOT_A_DIRECTORY;
1533		} else
1534			result = B_ENTRY_NOT_FOUND;
1535	}
1536
1537	// check permissions
1538	if (result == B_OK) {
1539		if (!volume->GetNodePermissions(directory).ImpliesWritePermission())
1540			result = B_PERMISSION_DENIED;
1541	}
1542
1543	// get the path
1544	Path path;
1545	if (result == B_OK) {
1546		result = directory->GetPath(&path);
1547		if (result == B_OK)
1548			result = path.Append(request->name.GetString());
1549	}
1550
1551	managerLocker.Unlock();
1552
1553	// remove the directory
1554	if (result == B_OK) {
1555		if (rmdir(path.GetPath()) < 0)
1556			result = errno;
1557	}
1558
1559	// prepare the reply
1560	RemoveDirReply reply;
1561	// send the reply
1562	reply.error = result;
1563	return GetChannel()->SendRequest(&reply);
1564}
1565
1566// VisitOpenDirRequest
1567status_t
1568ClientConnection::VisitOpenDirRequest(OpenDirRequest* request)
1569{
1570	ConnectionReference connectionReference(this);
1571	if (!connectionReference.IsValid())
1572		return B_OK;
1573
1574	// get the volume
1575	status_t result = B_OK;
1576	ClientVolume* volume = _GetVolume(request->volumeID);
1577	if (!volume)
1578		result = B_BAD_VALUE;
1579	ClientVolumePutter volumePutter(this, volume);
1580
1581	VolumeManagerLocker managerLocker;
1582
1583	// get the directory
1584	Directory* directory = NULL;
1585	if (result == B_OK) {
1586		Node* node = volume->GetNode(request->nodeID);
1587		if (node) {
1588			directory = dynamic_cast<Directory*>(node);
1589			if (!directory)
1590				result = B_NOT_A_DIRECTORY;
1591		} else
1592			result = B_ENTRY_NOT_FOUND;
1593	}
1594
1595	// check permission
1596	if (result == B_OK) {
1597		if (!volume->GetNodePermissions(directory).ImpliesReadDirPermission())
1598			result = B_PERMISSION_DENIED;
1599	}
1600
1601	// open the directory
1602	DirIterator* handle = NULL;
1603	if (result == B_OK)
1604		result = volume->OpenDir(directory, &handle);
1605	NodeHandleUnlocker handleUnlocker(volume, handle);
1606
1607	// prepare the reply
1608	OpenDirReply reply;
1609	if (result == B_OK) {
1610		_GetNodeInfo(directory, &reply.nodeInfo);
1611		reply.cookie = handle->GetCookie();
1612	}
1613else {
1614if (directory)
1615PRINT(("OpenDir() failed: client volume: %ld, node: (%ld, %lld)\n",
1616volume->GetID(), directory->GetVolumeID(), directory->GetID()));
1617}
1618
1619	managerLocker.Unlock();
1620
1621	// send the reply
1622	reply.error = result;
1623	status_t error = GetChannel()->SendRequest(&reply);
1624
1625	// close the handle, if a send error occurred
1626	if (error != B_OK && result == B_OK)
1627		volume->Close(handle);
1628
1629	return error;
1630}
1631
1632// VisitReadDirRequest
1633status_t
1634ClientConnection::VisitReadDirRequest(ReadDirRequest* request)
1635{
1636	ConnectionReference connectionReference(this);
1637	if (!connectionReference.IsValid())
1638		return B_OK;
1639
1640	// get the volume
1641	status_t result = B_OK;
1642	ClientVolume* volume = _GetVolume(request->volumeID);
1643	if (!volume)
1644		result = B_BAD_VALUE;
1645	ClientVolumePutter volumePutter(this, volume);
1646
1647	// get the node handle
1648	NodeHandle* handle = NULL;
1649	if (result == B_OK)
1650		result = volume->LockNodeHandle(request->cookie, &handle);
1651	NodeHandleUnlocker handleUnlocker(volume, handle);
1652
1653	// check if it is a directory iterator
1654	DirIterator* iterator = NULL;
1655	if (result == B_OK) {
1656		iterator = dynamic_cast<DirIterator*>(handle);
1657		if (!iterator)
1658			result = B_BAD_VALUE;
1659	}
1660
1661	VolumeManagerLocker managerLocker;
1662
1663	// get the directory
1664	Directory* directory = NULL;
1665	if (result == B_OK) {
1666		Node* node = volume->GetNode(iterator->GetNodeRef());
1667		if (node) {
1668			directory = dynamic_cast<Directory*>(node);
1669			if (!directory)
1670				result = B_NOT_A_DIRECTORY;
1671		} else
1672			result = B_ENTRY_NOT_FOUND;
1673	}
1674
1675	// check read permission
1676	if (result == B_OK) {
1677		if (!volume->GetNodePermissions(directory).ImpliesReadDirPermission())
1678			result = B_PERMISSION_DENIED;
1679	}
1680
1681if (result == B_OK) {
1682PRINT(("ReadDir: (%ld, %lld)\n", request->volumeID, directory->GetID()));
1683}
1684
1685	// rewind, if requested
1686	if (result == B_OK && request->rewind)
1687		iterator->Rewind();
1688
1689	// read the directory
1690	bool done = false;
1691	ReadDirReply reply;
1692	int32 toRead = request->count;
1693	while (result == B_OK && toRead > 0) {
1694		// get the next entry
1695		Entry* entry = iterator->NextEntry();
1696		if (!entry) {
1697			done = true;
1698			break;
1699		}
1700
1701		// get and add an entry info
1702		EntryInfo entryInfo;
1703		_GetEntryInfo(entry, &entryInfo);
1704		result = reply.entryInfos.Append(entryInfo);
1705
1706		toRead--;
1707	}
1708
1709	reply.revision = VolumeManager::GetDefault()->GetRevision();
1710
1711//PRINT(("ReadDir: (%lld) -> (%lx, %ld, dir: %lld, node: %lld, `%s')\n",
1712//directoryID, reply.error, reply.entryInfos.CountElements(),
1713//reply.entryInfo.directoryID,
1714//reply.entryInfo.nodeID, reply.entryInfo.name.GetString()));
1715if (directory) {
1716PRINT(("ReadDir done: volume: %ld, (%ld, %lld) -> (%lx, %ld)\n",
1717volume->GetID(), directory->GetVolumeID(), directory->GetID(), result,
1718reply.entryInfos.CountElements()));
1719}
1720
1721	managerLocker.Unlock();
1722
1723	// send the reply
1724	reply.error = result;
1725	reply.done = (result != B_OK || done);
1726	return GetChannel()->SendRequest(&reply);
1727}
1728
1729// VisitWalkRequest
1730status_t
1731ClientConnection::VisitWalkRequest(WalkRequest* request)
1732{
1733	ConnectionReference connectionReference(this);
1734	if (!connectionReference.IsValid())
1735		return B_OK;
1736
1737	// get the volume
1738	status_t result = B_OK;
1739	ClientVolume* volume = _GetVolume(request->volumeID);
1740	if (!volume)
1741		result = B_BAD_VALUE;
1742	ClientVolumePutter volumePutter(this, volume);
1743
1744	VolumeManagerLocker managerLocker;
1745
1746	// get the directory
1747	Directory* directory = NULL;
1748	if (result == B_OK) {
1749		Node* node = volume->GetNode(request->nodeID);
1750		if (node) {
1751			directory = dynamic_cast<Directory*>(node);
1752			if (!directory)
1753				result = B_NOT_A_DIRECTORY;
1754		} else
1755			result = B_ENTRY_NOT_FOUND;
1756	}
1757
1758	// check permission
1759	if (result == B_OK) {
1760		if (!volume->GetNodePermissions(directory)
1761				.ImpliesResolveDirEntryPermission()) {
1762			result = B_PERMISSION_DENIED;
1763		}
1764	}
1765
1766	WalkReply reply;
1767	char linkPath[B_PATH_NAME_LENGTH];
1768	if (result == B_OK) {
1769		// load the entry
1770		Entry* entry;
1771		result = volume->LoadEntry(directory, request->name.GetString(),
1772			&entry);
1773
1774		// fill in the reply
1775		if (result == B_OK) {
1776			_GetEntryInfo(entry, &reply.entryInfo);
1777
1778			// resolve a symlink, if desired
1779			Node* node = entry->GetNode();
1780			if (request->resolveLink && node->IsSymlink()) {
1781				result = node->ReadSymlink(linkPath, B_PATH_NAME_LENGTH);
1782				if (result == B_OK)
1783					reply.linkPath.SetTo(linkPath);
1784			}
1785		}
1786	}
1787
1788	managerLocker.Unlock();
1789
1790	// send the reply
1791	reply.error = result;
1792PRINT(("Walk: (%ld, %lld, `%s') -> (%lx, (%ld, %lld), `%s')\n",
1793request->nodeID.volumeID, request->nodeID.nodeID, request->name.GetString(),
1794result, reply.entryInfo.nodeInfo.st.st_dev, reply.entryInfo.nodeInfo.st.st_ino,
1795reply.linkPath.GetString()));
1796	return GetChannel()->SendRequest(&reply);
1797}
1798
1799// VisitMultiWalkRequest
1800status_t
1801ClientConnection::VisitMultiWalkRequest(MultiWalkRequest* request)
1802{
1803	ConnectionReference connectionReference(this);
1804	if (!connectionReference.IsValid())
1805		return B_OK;
1806
1807	// get the volume
1808	status_t result = B_OK;
1809	ClientVolume* volume = _GetVolume(request->volumeID);
1810	if (!volume)
1811		result = B_BAD_VALUE;
1812	ClientVolumePutter volumePutter(this, volume);
1813
1814	VolumeManagerLocker managerLocker;
1815
1816	// get the directory
1817	Directory* directory = NULL;
1818	if (result == B_OK) {
1819		Node* node = volume->GetNode(request->nodeID);
1820		if (node) {
1821			directory = dynamic_cast<Directory*>(node);
1822			if (!directory)
1823				result = B_NOT_A_DIRECTORY;
1824		} else
1825			result = B_ENTRY_NOT_FOUND;
1826	}
1827
1828	// check permission
1829	if (result == B_OK) {
1830		if (!volume->GetNodePermissions(directory)
1831				.ImpliesResolveDirEntryPermission()) {
1832			result = B_PERMISSION_DENIED;
1833		}
1834	}
1835
1836	MultiWalkReply reply;
1837	StringData* names = request->names.GetElements();
1838	int32 count = request->names.CountElements();
1839	for (int32 i = 0; result == B_OK && i < count; i++) {
1840		// load the entry
1841		Entry* entry;
1842		if (volume->LoadEntry(directory, names[i].GetString(), &entry)
1843				== B_OK) {
1844			// add an entry info
1845			EntryInfo entryInfo;
1846			_GetEntryInfo(entry, &entryInfo);
1847
1848			// append the info
1849			result = reply.entryInfos.Append(entryInfo);
1850		}
1851	}
1852
1853	managerLocker.Unlock();
1854
1855	// send the reply
1856	reply.error = result;
1857PRINT(("MultiWalk: (%ld, %lld, %ld) -> (%lx, %ld)\n",
1858request->nodeID.volumeID, request->nodeID.nodeID, count,
1859result, reply.entryInfos.CountElements()));
1860	return GetChannel()->SendRequest(&reply);
1861}
1862
1863// VisitOpenAttrDirRequest
1864status_t
1865ClientConnection::VisitOpenAttrDirRequest(OpenAttrDirRequest* request)
1866{
1867	ConnectionReference connectionReference(this);
1868	if (!connectionReference.IsValid())
1869		return B_OK;
1870
1871	// get the volume
1872	status_t result = B_OK;
1873	ClientVolume* volume = _GetVolume(request->volumeID);
1874	if (!volume)
1875		result = B_BAD_VALUE;
1876	ClientVolumePutter volumePutter(this, volume);
1877
1878	VolumeManagerLocker managerLocker;
1879
1880	// get the node
1881	Node* node = NULL;
1882	if (result == B_OK) {
1883		node = volume->GetNode(request->nodeID);
1884		if (!node)
1885			result = B_ENTRY_NOT_FOUND;
1886	}
1887
1888	// check permission
1889	if (result == B_OK) {
1890		if (!volume->GetNodePermissions(node).ImpliesReadPermission())
1891			result = B_PERMISSION_DENIED;
1892	}
1893
1894	// load/cache the attribute directory
1895	bool attrDirCached = (node->LoadAttrDir() == B_OK);
1896
1897	// open the attribute directory, if caching it failed
1898	AttrDirIterator* handle = NULL;
1899	if (result == B_OK && !attrDirCached)
1900		result = volume->OpenAttrDir(node, &handle);
1901	NodeHandleUnlocker handleUnlocker(volume, handle);
1902
1903	// prepare the reply
1904	OpenAttrDirReply reply;
1905	if (result == B_OK) {
1906		if (handle) {
1907			reply.cookie = handle->GetCookie();
1908		} else {
1909			// the attribute directory is cached
1910			reply.cookie = -1;
1911			result = _GetAttrDirInfo(request, node, &reply.attrDirInfo);
1912		}
1913	}
1914
1915	managerLocker.Unlock();
1916
1917	// send the reply
1918	reply.error = result;
1919	status_t error = GetChannel()->SendRequest(&reply);
1920
1921	// close the handle, if a send error occurred
1922	if (error != B_OK && result == B_OK && handle)
1923		volume->Close(handle);
1924
1925	return error;
1926}
1927
1928// VisitReadAttrDirRequest
1929status_t
1930ClientConnection::VisitReadAttrDirRequest(ReadAttrDirRequest* request)
1931{
1932	ConnectionReference connectionReference(this);
1933	if (!connectionReference.IsValid())
1934		return B_OK;
1935
1936	// get the volume
1937	status_t result = B_OK;
1938	ClientVolume* volume = _GetVolume(request->volumeID);
1939	if (!volume)
1940		result = B_BAD_VALUE;
1941	ClientVolumePutter volumePutter(this, volume);
1942
1943	// get the node handle
1944	NodeHandle* handle = NULL;
1945	if (result == B_OK)
1946		result = volume->LockNodeHandle(request->cookie, &handle);
1947	NodeHandleUnlocker handleUnlocker(volume, handle);
1948
1949	// check if it is a attribute directory iterator
1950	AttrDirIterator* iterator = NULL;
1951	if (result == B_OK) {
1952		iterator = dynamic_cast<AttrDirIterator*>(handle);
1953		if (!iterator)
1954			result = B_BAD_VALUE;
1955	}
1956
1957	VolumeManagerLocker managerLocker;
1958
1959	// get the node
1960	Node* node = NULL;
1961	if (result == B_OK) {
1962		node = volume->GetNode(iterator->GetNodeRef());
1963		if (!node)
1964			result = B_ENTRY_NOT_FOUND;
1965	}
1966
1967	// check read permission (we already checked when opening, but anyway...)
1968	if (result == B_OK) {
1969		if (!volume->GetNodePermissions(node).ImpliesReadPermission())
1970			result = B_PERMISSION_DENIED;
1971	}
1972
1973	managerLocker.Unlock();
1974
1975	// read the attribute directory
1976	uint8 buffer[sizeof(struct dirent) + B_FILE_NAME_LENGTH];
1977	struct dirent* dirEntry = (struct dirent*)buffer;
1978	int32 countRead = 0;
1979	bool done = true;
1980	if (result == B_OK) {
1981		if (request->rewind)
1982			result = iterator->RewindDir();
1983		if (result == B_OK) {
1984			result = iterator->ReadDir(dirEntry, sizeof(buffer), 1,
1985				&countRead, &done);
1986		}
1987	}
1988
1989	// prepare the reply
1990	ReadAttrDirReply reply;
1991	reply.name.SetTo(dirEntry->d_name);
1992
1993	// send the reply
1994	reply.error = result;
1995	reply.count = countRead;
1996	return GetChannel()->SendRequest(&reply);
1997}
1998
1999// VisitReadAttrRequest
2000status_t
2001ClientConnection::VisitReadAttrRequest(ReadAttrRequest* request)
2002{
2003	ConnectionReference connectionReference(this);
2004	if (!connectionReference.IsValid())
2005		return B_OK;
2006
2007	// get the volume
2008	status_t result = B_OK;
2009	ClientVolume* volume = _GetVolume(request->volumeID);
2010	if (!volume)
2011		result = B_BAD_VALUE;
2012	ClientVolumePutter volumePutter(this, volume);
2013
2014	VolumeManagerLocker managerLocker;
2015
2016	// get the node
2017	Node* node = NULL;
2018	if (result == B_OK) {
2019		node = volume->GetNode(request->nodeID);
2020		if (!node)
2021			result = B_ENTRY_NOT_FOUND;
2022	}
2023
2024	// check read permission
2025	if (result == B_OK) {
2026		if (!volume->GetNodePermissions(node).ImpliesReadPermission())
2027			result = B_PERMISSION_DENIED;
2028	}
2029
2030	// open the node
2031	FileHandle* handle = NULL;
2032	if (result == B_OK)
2033		result = volume->Open(node, O_RDONLY, &handle);
2034	NodeHandleUnlocker handleUnlocker(volume, handle);
2035
2036	managerLocker.Unlock();
2037
2038	// read the attribute
2039	if (result == B_OK) {
2040		// Due to a bug in BFS the `pos' is ignored. This means that the loop
2041		// below won't work if the attribute is non-empty and the buffer is
2042		// larger than the attribute, because the we would again and again
2043		// read the beginning of the attribute until the buffer is full.
2044		// Hence we first get an attr_info and don't try to read more than
2045		// the size of the attribute.
2046		attr_info info;
2047		result = handle->StatAttr(request->name.GetString(), &info);
2048		off_t originalPos = max(request->pos, 0LL);
2049		int32 originalSize = max(request->size, 0L);
2050		off_t pos = originalPos;
2051		int32 size = originalSize;
2052		type_code type = B_SWAP_INT32(request->type);
2053		bool convert = false;
2054
2055		if (result == B_OK) {
2056			originalSize = min((off_t)originalSize, max(0LL, info.size - pos));
2057			size = originalSize;
2058
2059			// deal with inverse endianess clients
2060			if (fInverseClientEndianess) {
2061				convert = _KnownAttributeType(info.type);
2062				if (convert) {
2063					// read the whole attribute
2064					pos = 0;
2065					size = info.size;
2066				} else
2067					type = B_SWAP_INT32(request->type);
2068			}
2069		}
2070		int32 bufferSize = min(size, kMaxReadBufferSize);
2071
2072		// allocate a buffer
2073		uint8* buffer = NULL;
2074		if (result == B_OK) {
2075			buffer = (uint8*)malloc(bufferSize);
2076			if (!buffer)
2077				result = B_NO_MEMORY;
2078		}
2079		MemoryDeleter bufferDeleter(buffer);
2080
2081		if (convert) {
2082			// read the whole attribute and convert it
2083			if (result == B_OK) {
2084				// read
2085				size_t bytesRead = 0;
2086				result = handle->ReadAttr(request->name.GetString(),
2087					type, 0, buffer, size, &bytesRead);
2088				if (result == B_OK && (int32)bytesRead != size)
2089					result = B_ERROR;
2090
2091				// convert
2092				if (result == B_OK)
2093					_ConvertAttribute(info, buffer);
2094			}
2095
2096			// prepare the reply
2097			ReadAttrReply reply;
2098			if (result == B_OK) {
2099				reply.pos = originalPos;
2100				reply.data.SetTo(buffer + originalPos, originalSize);
2101				reply.moreToCome = false;
2102			}
2103
2104			// send the reply
2105			reply.error = result;
2106			status_t error = GetChannel()->SendRequest(&reply);
2107			if (error != B_OK)
2108				return error;
2109		} else {
2110			// read as long as there are bytes left to read or an error occurs
2111			bool moreToRead = true;
2112			do {
2113				int32 bytesToRead = min(size, bufferSize);
2114				size_t bytesRead = 0;
2115				if (result == B_OK) {
2116					result = handle->ReadAttr(request->name.GetString(),
2117						request->type, pos, buffer, bytesToRead, &bytesRead);
2118				}
2119				moreToRead = (result == B_OK && bytesRead > 0
2120					&& (int32)bytesRead < size);
2121
2122				// prepare the reply
2123				ReadAttrReply reply;
2124				if (result == B_OK) {
2125					reply.pos = pos;
2126					reply.data.SetTo(buffer, bytesRead);
2127					reply.moreToCome = moreToRead;
2128					pos += bytesRead;
2129					size -= bytesRead;
2130				}
2131
2132				// send the reply
2133				reply.error = result;
2134				status_t error = GetChannel()->SendRequest(&reply);
2135				if (error != B_OK)
2136					return error;
2137			} while (moreToRead);
2138		}
2139
2140		// close the handle
2141		volume->Close(handle);
2142	} else {
2143		// opening the node failed (or something even earlier): send an error
2144		// reply
2145		ReadAttrReply reply;
2146		reply.error = result;
2147		status_t error = GetChannel()->SendRequest(&reply);
2148		if (error != B_OK)
2149			return error;
2150	}
2151
2152	return B_OK;
2153}
2154
2155// VisitWriteAttrRequest
2156status_t
2157ClientConnection::VisitWriteAttrRequest(WriteAttrRequest* request)
2158{
2159	ConnectionReference connectionReference(this);
2160	if (!connectionReference.IsValid())
2161		return B_OK;
2162
2163	// get the volume
2164	status_t result = B_OK;
2165	ClientVolume* volume = _GetVolume(request->volumeID);
2166	if (!volume)
2167		result = B_BAD_VALUE;
2168	ClientVolumePutter volumePutter(this, volume);
2169
2170	VolumeManagerLocker managerLocker;
2171
2172	// get the node
2173	Node* node = NULL;
2174	if (result == B_OK) {
2175		node = volume->GetNode(request->nodeID);
2176		if (!node)
2177			result = B_ENTRY_NOT_FOUND;
2178	}
2179
2180	// check read permission
2181	if (result == B_OK) {
2182		if (!volume->GetNodePermissions(node).ImpliesWritePermission())
2183			result = B_PERMISSION_DENIED;
2184	}
2185
2186	// open the node
2187	FileHandle* handle = NULL;
2188	if (result == B_OK)
2189		result = volume->Open(node, O_RDWR, &handle);
2190	NodeHandleUnlocker handleUnlocker(volume, handle);
2191
2192	managerLocker.Unlock();
2193
2194	if (result == B_OK) {
2195		off_t pos = max(request->pos, 0LL);
2196		int32 size = request->data.GetSize();
2197		type_code type = request->type;
2198		char* buffer = (char*)request->data.GetData();
2199
2200		// convert the data, if necessary
2201		if (fInverseClientEndianess) {
2202			if (_KnownAttributeType(type)) {
2203				if (pos != 0) {
2204					WARN(("WriteAttr(): WARNING: Need to convert attribute "
2205						"endianess, but position is not 0: attribute: %s, "
2206						"pos: %lld, size: %ld\n", request->name.GetString(),
2207						pos, size));
2208				}
2209				swap_data(type, buffer, size, B_SWAP_ALWAYS);
2210			} else
2211				type = B_SWAP_INT32(type);
2212		}
2213
2214		// write the data
2215		while (result == B_OK && size > 0) {
2216			size_t bytesWritten;
2217			result = handle->WriteAttr(request->name.GetString(),
2218				type, pos, buffer, size, &bytesWritten);
2219			if (result == B_OK) {
2220				pos += bytesWritten;
2221				buffer += bytesWritten;
2222				size -= bytesWritten;
2223			}
2224		}
2225
2226		// close the handle
2227		volume->Close(handle);
2228	}
2229
2230	// prepare the reply
2231	WriteAttrReply reply;
2232	// send the reply
2233	reply.error = result;
2234	return GetChannel()->SendRequest(&reply);
2235}
2236
2237// VisitRemoveAttrRequest
2238status_t
2239ClientConnection::VisitRemoveAttrRequest(RemoveAttrRequest* request)
2240{
2241	ConnectionReference connectionReference(this);
2242	if (!connectionReference.IsValid())
2243		return B_OK;
2244
2245	// get the volume
2246	status_t result = B_OK;
2247	ClientVolume* volume = _GetVolume(request->volumeID);
2248	if (!volume)
2249		result = B_BAD_VALUE;
2250	ClientVolumePutter volumePutter(this, volume);
2251
2252	VolumeManagerLocker managerLocker;
2253
2254	// get the node
2255	Node* node = NULL;
2256	if (result == B_OK) {
2257		node = volume->GetNode(request->nodeID);
2258		if (!node)
2259			result = B_ENTRY_NOT_FOUND;
2260	}
2261
2262	// check read permission
2263	if (result == B_OK) {
2264		if (!volume->GetNodePermissions(node).ImpliesWritePermission())
2265			result = B_PERMISSION_DENIED;
2266	}
2267
2268	// open the node
2269	FileHandle* handle = NULL;
2270	if (result == B_OK)
2271		result = volume->Open(node, O_RDWR, &handle);
2272	NodeHandleUnlocker handleUnlocker(volume, handle);
2273
2274	managerLocker.Unlock();
2275
2276	// remove the attribute and close the node
2277	if (result == B_OK) {
2278		result = handle->RemoveAttr(request->name.GetString());
2279		volume->Close(handle);
2280	}
2281
2282	// send the reply
2283	RemoveAttrReply reply;
2284	reply.error = result;
2285	return GetChannel()->SendRequest(&reply);
2286}
2287
2288// VisitRenameAttrRequest
2289status_t
2290ClientConnection::VisitRenameAttrRequest(RenameAttrRequest* request)
2291{
2292	// Not supported, since there's no API function to rename an attribute.
2293	// send the reply
2294	RemoveAttrReply reply;
2295	reply.error = B_UNSUPPORTED;
2296	return GetChannel()->SendRequest(&reply);
2297}
2298
2299// VisitStatAttrRequest
2300status_t
2301ClientConnection::VisitStatAttrRequest(StatAttrRequest* request)
2302{
2303	ConnectionReference connectionReference(this);
2304	if (!connectionReference.IsValid())
2305		return B_OK;
2306
2307	// get the volume
2308	status_t result = B_OK;
2309	ClientVolume* volume = _GetVolume(request->volumeID);
2310	if (!volume)
2311		result = B_BAD_VALUE;
2312	ClientVolumePutter volumePutter(this, volume);
2313
2314	VolumeManagerLocker managerLocker;
2315
2316	// get the node
2317	Node* node = NULL;
2318	if (result == B_OK) {
2319		node = volume->GetNode(request->nodeID);
2320		if (!node)
2321			result = B_ENTRY_NOT_FOUND;
2322	}
2323
2324	// check read permission
2325	if (result == B_OK) {
2326		if (!volume->GetNodePermissions(node).ImpliesReadPermission())
2327			result = B_PERMISSION_DENIED;
2328	}
2329
2330	// open the node
2331	FileHandle* handle = NULL;
2332	if (result == B_OK)
2333		result = volume->Open(node, O_RDONLY, &handle);
2334	NodeHandleUnlocker handleUnlocker(volume, handle);
2335
2336	managerLocker.Unlock();
2337
2338	// stat the attribute and close the node
2339	attr_info attrInfo;
2340	StatAttrReply reply;
2341	if (result == B_OK) {
2342		result = handle->StatAttr(request->name.GetString(), &attrInfo);
2343		volume->Close(handle);
2344	}
2345
2346	// set the attribute info
2347	if (result == B_OK) {
2348		result = _GetAttrInfo(request, request->name.GetString(), attrInfo,
2349			NULL, &reply.attrInfo);
2350	}
2351
2352	// send the reply
2353	reply.error = result;
2354	return GetChannel()->SendRequest(&reply);
2355}
2356
2357// VisitOpenQueryRequest
2358status_t
2359ClientConnection::VisitOpenQueryRequest(OpenQueryRequest* request)
2360{
2361	ConnectionReference connectionReference(this);
2362	if (!connectionReference.IsValid())
2363		return B_OK;
2364
2365	VolumeManagerLocker managerLocker;
2366
2367	// open the query
2368	status_t result = B_OK;
2369	QueryHandle* handle = NULL;
2370	if (result == B_OK) {
2371		result = _OpenQuery(request->queryString.GetString(),
2372			request->flags, request->port, request->token, &handle);
2373	}
2374	QueryHandleUnlocker handleUnlocker(this, handle);
2375
2376	// prepare the reply
2377	OpenQueryReply reply;
2378	if (result == B_OK)
2379		reply.cookie = handle->GetCookie();
2380
2381	managerLocker.Unlock();
2382
2383	// send the reply
2384	reply.error = result;
2385	status_t error = GetChannel()->SendRequest(&reply);
2386
2387	// close the handle, if a send error occurred
2388	if (error != B_OK && result == B_OK)
2389		_CloseQuery(handle);
2390
2391	return error;
2392}
2393
2394// VisitReadQueryRequest
2395status_t
2396ClientConnection::VisitReadQueryRequest(ReadQueryRequest* request)
2397{
2398	ConnectionReference connectionReference(this);
2399	if (!connectionReference.IsValid())
2400		return B_OK;
2401
2402	// create an array for the IDs of the client volumes a found entry may
2403	// reside on
2404	status_t result = B_OK;
2405	int32 volumeCount = fVolumes->Size();
2406	int32* volumeIDs = new(nothrow) int32[volumeCount];
2407	if (!volumeIDs)
2408		result = B_NO_MEMORY;
2409	ArrayDeleter<int32> volumeIDsDeleter(volumeIDs);
2410
2411	// get the query handle
2412	QueryHandle* handle = NULL;
2413	if (result == B_OK)
2414		result = _LockQueryHandle(request->cookie, &handle);
2415	QueryHandleUnlocker handleUnlocker(this, handle);
2416
2417	// check if it is a query handle
2418	QueryHandle* queryHandle = NULL;
2419	if (result == B_OK) {
2420		queryHandle = dynamic_cast<QueryHandle*>(handle);
2421		if (!queryHandle)
2422			result = B_BAD_VALUE;
2423	}
2424
2425	// read the query
2426	ReadQueryReply reply;
2427	int32 countRead = 0;
2428	while (result == B_OK) {
2429		uint8 buffer[sizeof(struct dirent) + B_FILE_NAME_LENGTH];
2430		struct dirent* dirEntry = (struct dirent*)buffer;
2431
2432		result = queryHandle->ReadDir(dirEntry, 1, &countRead);
2433		if (result != B_OK)
2434			break;
2435		if (countRead == 0)
2436			break;
2437PRINT(("  query entry: %ld, %lld, \"%s\"\n", dirEntry->d_pdev, dirEntry->d_pino, dirEntry->d_name));
2438
2439		VolumeManagerLocker managerLocker;
2440		VolumeManager* volumeManager = VolumeManager::GetDefault();
2441
2442		// load the entry
2443		Entry* entry = NULL;
2444		result = volumeManager->LoadEntry(dirEntry->d_pdev,
2445			dirEntry->d_pino, dirEntry->d_name, true, &entry);
2446
2447		// if at least one client volume contains the entry, get an entry info
2448		if (result == B_OK) {
2449			HasQueryPermissionClientVolumeFilter filter;
2450			int32 entryVolumeCount = _GetContainingClientVolumes(
2451				entry->GetDirectory(), volumeIDs, volumeCount, &filter);
2452			if (entryVolumeCount > 0) {
2453				// store all the client volume IDs in the reply
2454				for (int32 i = 0; i < entryVolumeCount; i++) {
2455					result = reply.clientVolumeIDs.Append(volumeIDs[i]);
2456					if (result != B_OK)
2457						break;
2458				}
2459
2460				// get an entry info
2461				_GetNodeInfo(entry->GetDirectory(), &reply.dirInfo);
2462				_GetEntryInfo(entry, &reply.entryInfo);
2463				break;
2464			}
2465else
2466PRINT(("  -> no client volumes\n"));
2467		}
2468
2469		// entry is not in the volume: next round...
2470		result = B_OK;
2471	}
2472
2473	// send the reply
2474	reply.error = result;
2475	reply.count = countRead;
2476PRINT(("ReadQuery: (%lx, %ld, dir: (%ld, %lld), node: (%ld, %lld, `%s')\n",
2477reply.error, reply.count, reply.entryInfo.directoryID.volumeID,
2478reply.entryInfo.directoryID.nodeID, reply.entryInfo.nodeInfo.st.st_dev,
2479reply.entryInfo.nodeInfo.st.st_ino, reply.entryInfo.name.GetString()));
2480	return GetChannel()->SendRequest(&reply);
2481}
2482
2483
2484// #pragma mark -
2485
2486// ProcessNodeMonitoringEvent
2487void
2488ClientConnection::ProcessNodeMonitoringEvent(int32 volumeID,
2489	NodeMonitoringEvent* event)
2490{
2491	// get a connection reference
2492	ConnectionReference connectionReference(this);
2493	if (!connectionReference.IsValid())
2494		return;
2495
2496	_PushNodeMonitoringEvent(volumeID, event);
2497}
2498
2499// CloseNodeMonitoringEventQueue
2500void
2501ClientConnection::CloseNodeMonitoringEventQueue()
2502{
2503	typedef Vector<NodeMonitoringRequest*> RequestVector;
2504	const RequestVector* requests;
2505	if (fNodeMonitoringEvents->Close(false, &requests) == B_OK) {
2506		for (RequestVector::ConstIterator it = requests->Begin();
2507			 it != requests->End();
2508			 it++) {
2509			delete *it;
2510		}
2511	}
2512}
2513
2514
2515// #pragma mark -
2516
2517// QueryDomainIntersectsWith
2518bool
2519ClientConnection::QueryDomainIntersectsWith(Volume* volume)
2520{
2521	// Iterate through the the client volumes and check whether any one contains
2522	// the supplied volume or its root dir is on the volume. We don't check
2523	// directory inclusion for the latter, since we don't need to query the
2524	// volume, if the client volume is located on a volume mounted somewhere
2525	// under the supplied volume (e.g. the root FS contains everything, but does
2526	// seldomly need to be queried).
2527	VolumeManager* volumeManager = VolumeManager::GetDefault();
2528	AutoLocker<VolumeMap> volumesLocker(fVolumes);
2529	for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();) {
2530		ClientVolume* clientVolume = it.Next().value;
2531		Directory* volumeRoot = volume->GetRootDirectory();
2532		Directory* clientVolumeRoot = clientVolume->GetRootDirectory();
2533		if (volumeManager->DirectoryContains(clientVolumeRoot, volumeRoot, true)
2534			|| volumeRoot->GetVolumeID() == clientVolumeRoot->GetVolumeID()) {
2535			return true;
2536		}
2537	}
2538
2539	return false;
2540}
2541
2542// ProcessQueryEvent
2543void
2544ClientConnection::ProcessQueryEvent(NodeMonitoringEvent* event)
2545{
2546	dev_t volumeID;
2547	ino_t directoryID;
2548	if (event->opcode == B_ENTRY_CREATED) {
2549		// "entry created" event
2550		EntryCreatedEvent* createdEvent
2551			= dynamic_cast<EntryCreatedEvent*>(event);
2552		if (!createdEvent)
2553			return;
2554		volumeID = createdEvent->volumeID;
2555		directoryID = createdEvent->directoryID;
2556
2557	} else if (event->opcode == B_ENTRY_REMOVED) {
2558		// "entry removed" event
2559		EntryRemovedEvent* removedEvent
2560			= dynamic_cast<EntryRemovedEvent*>(event);
2561		if (!removedEvent)
2562			return;
2563		volumeID = removedEvent->volumeID;
2564		directoryID = removedEvent->directoryID;
2565
2566	} else {
2567		// We only support "entry created" and "entry removed" query events.
2568		// "entry moved" is split by the volume manager into those.
2569		ERROR(("Ignoring unexpected query event: opcode: 0x%lx\n",
2570			event->opcode));
2571		return;
2572	}
2573PRINT(("ClientConnection::ProcessQueryEvent(): event: %p, type: %s: directory: (%ld, %lld)\n", event, typeid(event).name(), volumeID, directoryID));
2574
2575	// create an array for the IDs of the client volumes a found entry may
2576	// reside on
2577	status_t result = B_OK;
2578	int32 volumeCount = fVolumes->Size();
2579	int32* volumeIDs = new(nothrow) int32[volumeCount];
2580	if (!volumeIDs)
2581		result = B_NO_MEMORY;
2582	ArrayDeleter<int32> volumeIDsDeleter(volumeIDs);
2583
2584	HasQueryPermissionClientVolumeFilter filter;
2585
2586	// load the directory the concerned entry belongs/belonged to
2587	Directory* directory;
2588	int32 concernedVolumes = 0;
2589	if (VolumeManager::GetDefault()->LoadDirectory(volumeID, directoryID,
2590			&directory) == B_OK) {
2591		// find out, which client volumes the directory is located in
2592		concernedVolumes = _GetContainingClientVolumes(directory, volumeIDs,
2593			volumeCount, &filter);
2594	} else {
2595		// Failed to load the directory, so maybe it has already been
2596		// deleted. For "entry removed" events, we consider all client
2597		// volumes to be notified -- those that don't know the entry will
2598		// ignore the event.
2599		if (event->opcode == B_ENTRY_REMOVED) {
2600			concernedVolumes = _GetAllClientVolumeIDs(volumeIDs, volumeCount,
2601				&filter);
2602		}
2603	}
2604
2605	// now push the event for each concerned client volume
2606	for (int32 i = 0; i < concernedVolumes; i++)
2607		_PushNodeMonitoringEvent(volumeIDs[i], event);
2608	// TODO: More than one volume will usually only be concerned in case of
2609	// nested client volumes. We could optimize the case by having an array of
2610	// volume IDs in the respective requests sent over the net (just as in the
2611	// ReadQueryReply).
2612}
2613
2614
2615// #pragma mark -
2616
2617// _Close
2618void
2619ClientConnection::_Close()
2620{
2621	// terminate node monitoring processor
2622	CloseNodeMonitoringEventQueue();
2623	if (fNodeMonitoringProcessor >= 0
2624		&& find_thread(NULL) != fNodeMonitoringProcessor) {
2625		int32 result;
2626		wait_for_thread(fNodeMonitoringProcessor, &result);
2627		// The variable is not unset, when this is the node monitoring
2628		// processor thread -- which is good, since the destructor will
2629		// wait for the thread in this case.
2630		fNodeMonitoringProcessor = -1;
2631	}
2632	if (fConnection)
2633		fConnection->Close();
2634	// notify the listener
2635	ClientConnectionListener* listener = fListener;
2636	fListener = NULL;
2637	if (listener)
2638		listener->ClientConnectionClosed(this, fError);
2639}
2640
2641// _MarkClosed
2642void
2643ClientConnection::_MarkClosed(bool error)
2644{
2645	AutoLocker<Locker> _(fLock);
2646	if (!fClosed) {
2647		fClosed = true;
2648		fError = error;
2649	}
2650}
2651
2652// _GetNodeInfo
2653void
2654ClientConnection::_GetNodeInfo(Node* node, NodeInfo* info)
2655{
2656	if (node && info) {
2657		info->st = node->GetStat();
2658		info->revision = VolumeManager::GetDefault()->GetRevision();
2659	}
2660}
2661
2662// _GetEntryInfo
2663void
2664ClientConnection::_GetEntryInfo(Entry* entry, EntryInfo* info)
2665{
2666	if (entry && info) {
2667		info->directoryID.volumeID = entry->GetVolumeID();
2668		info->directoryID.nodeID = entry->GetDirectoryID();
2669		info->name.SetTo(entry->GetName());
2670		_GetNodeInfo(entry->GetNode(), &info->nodeInfo);
2671	}
2672}
2673
2674// _GetAttrInfo
2675status_t
2676ClientConnection::_GetAttrInfo(Request* request, const char* name,
2677	const attr_info& attrInfo, const void* data, AttributeInfo* info)
2678{
2679	if (!request || !name || !info)
2680		return B_BAD_VALUE;
2681
2682	info->name.SetTo(name);
2683	info->info = attrInfo;
2684	data = (attrInfo.size > 0 ? data : NULL);
2685	int32 dataSize = (data ? attrInfo.size : 0);
2686	info->data.SetTo(data, dataSize);
2687
2688	// if the client has inverse endianess, swap the type, if we don't know it
2689	if (fInverseClientEndianess) {
2690		if (_KnownAttributeType(info->info.type)) {
2691			// we need to convert the data, if supplied
2692			if (data) {
2693				// allocate a buffer
2694				RequestBuffer* requestBuffer = RequestBuffer::Create(dataSize);
2695				if (!requestBuffer)
2696					return B_NO_MEMORY;
2697
2698				// convert the data
2699				memcpy(requestBuffer->GetData(), data, dataSize);
2700				_ConvertAttribute(info->info, requestBuffer->GetData());
2701			}
2702		} else
2703			info->info.type = B_SWAP_INT32(info->info.type);
2704	}
2705
2706	return B_OK;
2707}
2708
2709// _GetAttrDirInfo
2710status_t
2711ClientConnection::_GetAttrDirInfo(Request* request, AttributeDirectory* attrDir,
2712	AttrDirInfo* info)
2713{
2714	if (!request || !attrDir || !info || !attrDir->IsAttrDirValid())
2715		return B_BAD_VALUE;
2716
2717	// add the attribute infos
2718	for (Attribute* attribute = attrDir->GetFirstAttribute();
2719		 attribute;
2720		 attribute = attrDir->GetNextAttribute(attribute)) {
2721		// get the attribute info
2722		AttributeInfo attrInfo;
2723		attr_info bAttrInfo;
2724		attribute->GetInfo(&bAttrInfo);
2725		status_t error = _GetAttrInfo(request, attribute->GetName(), bAttrInfo,
2726			attribute->GetData(), &attrInfo);
2727
2728		// append it
2729		if (error == B_OK)
2730			error = info->attributeInfos.Append(attrInfo);
2731		if (error != B_OK)
2732			return error;
2733	}
2734
2735	info->revision = VolumeManager::GetDefault()->GetRevision();
2736	info->isValid = true;
2737
2738	return B_OK;
2739}
2740
2741// _CreateVolume
2742status_t
2743ClientConnection::_CreateVolume(ClientVolume** _volume)
2744{
2745	// create and init the volume
2746	ClientVolume* volume = new(nothrow) ClientVolume(fSecurityContextLock,
2747		this);
2748	if (!volume)
2749		return B_NO_MEMORY;
2750	status_t error = volume->Init();
2751	if (error != B_OK)
2752		return error;
2753
2754	// add it to the volume map
2755	AutoLocker<VolumeMap> locker(fVolumes);
2756	error = fVolumes->Put(volume->GetID(), volume);
2757	locker.Unlock();
2758
2759	if (error == B_OK)
2760		*_volume = volume;
2761	else
2762		delete volume;
2763
2764	return error;
2765}
2766
2767// _GetVolume
2768ClientVolume*
2769ClientConnection::_GetVolume(int32 id)
2770{
2771	AutoLocker<VolumeMap> _(fVolumes);
2772	ClientVolume* volume = fVolumes->Get(id);
2773	if (!volume || volume->IsRemoved())
2774		return NULL;
2775	volume->AddReference();
2776	return volume;
2777}
2778
2779// _PutVolume
2780//
2781// The VolumeManager may be locked, but no other lock must be held.
2782void
2783ClientConnection::_PutVolume(ClientVolume* volume)
2784{
2785	if (!volume)
2786		return;
2787
2788	// decrement reference counter and remove the volume, if 0
2789	AutoLocker<VolumeMap> locker(fVolumes);
2790	bool removed = (volume->RemoveReference() && volume->IsRemoved());
2791	if (removed)
2792		fVolumes->Remove(volume->GetID());
2793	locker.Unlock();
2794
2795	if (removed) {
2796		VolumeManagerLocker managerLocker;
2797		delete volume;
2798	}
2799}
2800
2801// _UnmountVolume
2802//
2803// The caller must have a reference to the volume.
2804void
2805ClientConnection::_UnmountVolume(ClientVolume* volume)
2806{
2807	if (!volume)
2808		return;
2809	AutoLocker<VolumeMap> locker(fVolumes);
2810	volume->MarkRemoved();
2811	locker.Unlock();
2812
2813	// push a notification event
2814	if (VolumeUnmountedEvent* event = new(nothrow) VolumeUnmountedEvent) {
2815		VolumeManagerLocker managerLocker;
2816
2817		event->opcode = B_DEVICE_UNMOUNTED;
2818		_PushNodeMonitoringEvent(volume->GetID(), event);
2819		event->RemoveReference();
2820	}
2821}
2822
2823// _UnmountAllVolumes
2824void
2825ClientConnection::_UnmountAllVolumes()
2826{
2827	while (true) {
2828		// To avoid heap allocation (which can fail) we unmount the volumes
2829		// chunkwise.
2830		// get the volumes
2831		const int32 volumeChunkSize = 32;
2832		ClientVolume* volumes[volumeChunkSize];
2833		int32 volumeCount = 0;
2834		AutoLocker<VolumeMap> volumesLocker(fVolumes);
2835		for (VolumeMap::Iterator it = fVolumes->GetIterator(); it.HasNext();) {
2836			if (ClientVolume* volume = _GetVolume(it.Next().value->GetID())) {
2837				volumes[volumeCount++] = volume;
2838			}
2839			if (volumeCount == volumeChunkSize)
2840				break;
2841		}
2842		volumesLocker.Unlock();
2843
2844		// unmount and put the volumes
2845		for (int32 i = 0; i < volumeCount; i++) {
2846			ClientVolume* volume = volumes[i];
2847			_UnmountVolume(volume);
2848			_PutVolume(volume);
2849		}
2850
2851		if (volumeCount < volumeChunkSize)
2852			break;
2853	}
2854}
2855
2856// _NodeMonitoringProcessorEntry
2857int32
2858ClientConnection::_NodeMonitoringProcessorEntry(void* data)
2859{
2860	return ((ClientConnection*)data)->_NodeMonitoringProcessor();
2861}
2862
2863// _NodeMonitoringProcessor
2864int32
2865ClientConnection::_NodeMonitoringProcessor()
2866{
2867	while (!fClosed) {
2868		// get the next request
2869		NodeMonitoringRequest* request;
2870		status_t error = fNodeMonitoringEvents->Pop(&request);
2871
2872		// get a client connection reference
2873		ConnectionReference connectionReference(this);
2874		if (!connectionReference.IsValid())
2875			return B_OK;
2876
2877		// No request? Next round...
2878		if (error != B_OK)
2879			continue;
2880		ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
2881
2882		// send the request
2883		if (error == B_OK) {
2884			error = fConnection->SendRequest(request);
2885			if (error != B_OK) {
2886				ERROR(("ClientConnection::_NodeMonitoringProcessor(): "
2887					"Failed to send request.\n"));
2888			}
2889		}
2890	}
2891	return 0;
2892}
2893
2894// _PushNodeMonitoringEvent
2895//
2896// The caller must have a connection reference. Moreover the VolumeManager
2897// must be locked.
2898status_t
2899ClientConnection::_PushNodeMonitoringEvent(int32 volumeID,
2900	NodeMonitoringEvent* event)
2901{
2902	if (!event)
2903		return B_BAD_VALUE;
2904
2905	// get the volume
2906	ClientVolume* volume = _GetVolume(volumeID);
2907	if (!volume && event->opcode != B_DEVICE_UNMOUNTED)
2908		return B_BAD_VALUE;
2909	ClientVolumePutter volumePutter(this, volume);
2910
2911	// create a node monitoring request
2912	NodeMonitoringRequest* request = NULL;
2913	status_t error = B_ERROR;
2914	switch (event->opcode) {
2915		case B_ENTRY_CREATED:
2916			error = _EntryCreated(volume,
2917				dynamic_cast<EntryCreatedEvent*>(event), request);
2918			break;
2919		case B_ENTRY_REMOVED:
2920			error = _EntryRemoved(volume,
2921				dynamic_cast<EntryRemovedEvent*>(event), request);
2922			break;
2923		case B_ENTRY_MOVED:
2924			error = _EntryMoved(volume,
2925				dynamic_cast<EntryMovedEvent*>(event), request);
2926			break;
2927		case B_STAT_CHANGED:
2928			error = _NodeStatChanged(volume,
2929				dynamic_cast<StatChangedEvent*>(event), request);
2930			break;
2931		case B_ATTR_CHANGED:
2932			error = _NodeAttributeChanged(volume,
2933				dynamic_cast<AttributeChangedEvent*>(event), request);
2934			break;
2935		case B_DEVICE_UNMOUNTED:
2936			error = B_OK;
2937			break;
2938	}
2939
2940	// replace all data buffers -- when the request is actually sent, they
2941	// might no longer exist
2942	if (error == B_OK)
2943		error = RequestBufferReplacer().ReplaceBuffer(request);
2944
2945	if (error == B_OK) {
2946		// common initialization
2947		request->volumeID = volumeID;
2948		request->opcode = event->opcode;
2949		request->revision = VolumeManager::GetDefault()->GetRevision();
2950
2951		// push the request
2952		error = fNodeMonitoringEvents->Push(request);
2953		if (error != B_OK)
2954			delete request;
2955	}
2956
2957	return error;
2958}
2959
2960// _EntryCreated
2961status_t
2962ClientConnection::_EntryCreated(ClientVolume* volume, EntryCreatedEvent* event,
2963	NodeMonitoringRequest*& _request)
2964{
2965	// allocate the request
2966	EntryCreatedRequest* request = new(nothrow) EntryCreatedRequest;
2967	if (!request)
2968		return B_NO_MEMORY;
2969	ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
2970
2971	// get the name
2972	const char* name = event->name.GetString();
2973
2974	// set the request fields
2975	request->directoryID = NodeID(event->volumeID, event->directoryID);
2976	request->nodeID = NodeID(event->volumeID, event->nodeID);
2977	request->name.SetTo(name);
2978	if (event->queryHandler) {
2979		request->port = event->remotePort;
2980		request->token = event->remoteToken;
2981		request->queryUpdate = true;
2982	} else
2983		request->queryUpdate = false;
2984
2985	// try to get an entry info
2986	Entry* entry;
2987	if (VolumeManager::GetDefault()->LoadEntry(event->volumeID,
2988			event->directoryID, name, true, &entry) == B_OK
2989		&& entry->GetNode()->GetVolumeID() == event->volumeID
2990		&& entry->GetNode()->GetID() == event->nodeID) {
2991		_GetEntryInfo(entry, &request->entryInfo);
2992		request->entryInfoValid = true;
2993	} else
2994		request->entryInfoValid = false;
2995
2996	requestDeleter.Detach();
2997	_request = request;
2998	return B_OK;
2999}
3000
3001// _EntryRemoved
3002status_t
3003ClientConnection::_EntryRemoved(ClientVolume* volume, EntryRemovedEvent* event,
3004	NodeMonitoringRequest*& _request)
3005{
3006	// special handling, if it is the root node of the client volume that has
3007	// been removed
3008	if (!event->queryHandler
3009		&& NodeRef(event->nodeVolumeID, event->nodeID)
3010			== volume->GetRootNodeRef()) {
3011		NoAllocEntryRef ref(event->nodeVolumeID, event->nodeID, ".");
3012		BEntry entry;
3013		if (FDManager::SetEntry(&entry, &ref) != B_OK || !entry.Exists())
3014			_UnmountVolume(volume);
3015
3016		// don't send the "entry removed" event
3017		return B_ERROR;
3018	}
3019
3020	// allocate the request
3021	EntryRemovedRequest* request = new(nothrow) EntryRemovedRequest;
3022	if (!request)
3023		return B_NO_MEMORY;
3024	ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
3025
3026	// get the name
3027	const char* name = event->name.GetString();
3028
3029	// set the request fields
3030	request->directoryID = NodeID(event->volumeID, event->directoryID);
3031	request->nodeID = NodeID(event->nodeVolumeID, event->nodeID);
3032	request->name.SetTo(name);
3033	if (event->queryHandler) {
3034		request->port = event->remotePort;
3035		request->token = event->remoteToken;
3036		request->queryUpdate = true;
3037	} else
3038		request->queryUpdate = false;
3039
3040	requestDeleter.Detach();
3041	_request = request;
3042	return B_OK;
3043}
3044
3045// _EntryMoved
3046status_t
3047ClientConnection::_EntryMoved(ClientVolume* volume, EntryMovedEvent* event,
3048	NodeMonitoringRequest*& _request)
3049{
3050	// allocate the request
3051	EntryMovedRequest* request = new(nothrow) EntryMovedRequest;
3052	if (!request)
3053		return B_NO_MEMORY;
3054	ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
3055
3056	// allocate memory for the names
3057	int32 fromNameLen = event->fromName.GetLength();
3058	const char* fromName
3059		= (fromNameLen > 0 ? event->fromName.GetString() : NULL);
3060	const char* toName = event->toName.GetString();
3061
3062	// set the request fields
3063	request->fromDirectoryID = NodeID(event->volumeID, event->fromDirectoryID);
3064	request->toDirectoryID = NodeID(event->volumeID, event->toDirectoryID);
3065	request->nodeID = NodeID(event->nodeVolumeID, event->nodeID);
3066	request->fromName.SetTo(fromName);
3067	request->toName.SetTo(toName);
3068	request->queryUpdate = false;
3069
3070	// try to get an entry info
3071	Entry* entry;
3072	if (VolumeManager::GetDefault()->LoadEntry(event->volumeID,
3073			event->toDirectoryID, toName, true, &entry) == B_OK
3074		&& entry->GetNode()->GetVolumeID() == event->nodeVolumeID
3075		&& entry->GetNode()->GetID() == event->nodeID) {
3076		_GetEntryInfo(entry, &request->entryInfo);
3077		request->entryInfoValid = true;
3078	} else
3079		request->entryInfoValid = false;
3080
3081	requestDeleter.Detach();
3082	_request = request;
3083	return B_OK;
3084}
3085
3086// _NodeStatChanged
3087status_t
3088ClientConnection::_NodeStatChanged(ClientVolume* volume,
3089	StatChangedEvent* event, NodeMonitoringRequest*& _request)
3090{
3091	// get the node
3092	Node* node = volume->GetNode(event->volumeID, event->nodeID);
3093	if (!node)
3094		return B_ENTRY_NOT_FOUND;
3095
3096	// allocate the request
3097	StatChangedRequest* request = new(nothrow) StatChangedRequest;
3098	if (!request)
3099		return B_NO_MEMORY;
3100	ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
3101
3102	// set the request fields
3103	request->nodeID = NodeID(event->volumeID, event->nodeID);
3104	_GetNodeInfo(node, &request->nodeInfo);
3105	request->queryUpdate = false;
3106
3107	requestDeleter.Detach();
3108	_request = request;
3109	return B_OK;
3110}
3111
3112// _NodeAttributeChanged
3113status_t
3114ClientConnection::_NodeAttributeChanged(ClientVolume* volume,
3115	AttributeChangedEvent* event, NodeMonitoringRequest*& _request)
3116{
3117	// get the node
3118	Node* node = volume->GetNode(event->volumeID, event->nodeID);
3119	if (!node)
3120		return B_ENTRY_NOT_FOUND;
3121
3122	// update the attribute directory
3123	bool removed = false;
3124	bool valid = false;
3125	attr_info info;
3126	const void* data = NULL;
3127	status_t error = node->UpdateAttribute(event->attribute.GetString(),
3128		&removed, &info, &data);
3129	valid = (error == B_OK);
3130
3131	// allocate the request
3132	AttributeChangedRequest* request = new(nothrow) AttributeChangedRequest;
3133	if (!request)
3134		return B_NO_MEMORY;
3135	ObjectDeleter<NodeMonitoringRequest> requestDeleter(request);
3136
3137	// get an attr dir info, if the directory is valid
3138	if (node->IsAttrDirValid()) {
3139		status_t error = _GetAttrDirInfo(request, node, &request->attrDirInfo);
3140		if (error != B_OK)
3141			return error;
3142	}
3143
3144	// get name and the data size
3145	int32 dataSize = (data ? info.size : 0);
3146	const char* name = event->attribute.GetString();
3147
3148	// set the request fields
3149	request->nodeID = NodeID(event->volumeID, event->nodeID);
3150	request->attrInfo.name.SetTo(name);
3151	request->valid = valid;
3152	request->removed = removed;
3153	if (!removed && valid) {
3154		request->attrInfo.info = info;
3155		request->attrInfo.data.SetTo(data, dataSize);
3156	}
3157	request->queryUpdate = false;
3158
3159	requestDeleter.Detach();
3160	_request = request;
3161	return B_OK;
3162}
3163
3164// _KnownAttributeType
3165bool
3166ClientConnection::_KnownAttributeType(type_code type)
3167{
3168	if (!fInverseClientEndianess)
3169		return false;
3170
3171	switch (type) {
3172		case B_BOOL_TYPE:
3173		case B_CHAR_TYPE:
3174		case B_COLOR_8_BIT_TYPE:
3175		case B_DOUBLE_TYPE:
3176		case B_FLOAT_TYPE:
3177		case B_GRAYSCALE_8_BIT_TYPE:
3178		case B_INT64_TYPE:
3179		case B_INT32_TYPE:
3180		case B_INT16_TYPE:
3181		case B_INT8_TYPE:
3182		case B_MESSAGE_TYPE:
3183		case B_MESSENGER_TYPE:
3184		case B_MIME_TYPE:
3185		case B_MONOCHROME_1_BIT_TYPE:
3186		case B_OFF_T_TYPE:
3187		case B_POINTER_TYPE:
3188		case B_POINT_TYPE:
3189		case B_RECT_TYPE:
3190		case B_REF_TYPE:
3191		case B_RGB_COLOR_TYPE:
3192		case B_SIZE_T_TYPE:
3193		case B_SSIZE_T_TYPE:
3194		case B_STRING_TYPE:
3195		case B_TIME_TYPE:
3196		case B_UINT64_TYPE:
3197		case B_UINT32_TYPE:
3198		case B_UINT16_TYPE:
3199		case B_UINT8_TYPE:
3200		case B_ASCII_TYPE:
3201		case B_MIME_STRING_TYPE:
3202			return true;
3203
3204		//B_RGB_32_BIT_TYPE: We could translate it, but it's heavy...
3205	}
3206
3207	return false;
3208}
3209
3210// _ConvertAttribute
3211void
3212ClientConnection::_ConvertAttribute(const attr_info& info, void* buffer)
3213{
3214	swap_data(info.type, buffer, info.size, B_SWAP_ALWAYS);
3215}
3216
3217
3218// #pragma mark -
3219
3220// _OpenQuery
3221status_t
3222ClientConnection::_OpenQuery(const char* queryString, uint32 flags,
3223	port_id remotePort, int32 remoteToken, QueryHandle** _handle)
3224{
3225	if (!queryString || !_handle)
3226		return B_BAD_VALUE;
3227
3228	// open query
3229	QueryHandle* queryHandle;
3230	status_t error = VolumeManager::GetDefault()->OpenQuery(this, queryString,
3231		flags, remotePort, remoteToken, &queryHandle);
3232	if (error != B_OK)
3233		return error;
3234	Reference<QueryHandle> handleReference(queryHandle, true);
3235
3236	// lock the handle
3237	queryHandle->Lock();
3238
3239	// add the handle
3240	error = fQueryHandles->AddNodeHandle(queryHandle);
3241	if (error != B_OK)
3242		return error;
3243
3244	handleReference.Detach();
3245	*_handle = queryHandle;
3246	return B_OK;
3247}
3248
3249// _CloseQuery
3250status_t
3251ClientConnection::_CloseQuery(QueryHandle* handle)
3252{
3253	if (!handle || !fQueryHandles->RemoveNodeHandle(handle))
3254		return B_BAD_VALUE;
3255
3256	return B_OK;
3257}
3258
3259// _LockQueryHandle
3260//
3261// VolumeManager must NOT be locked.
3262status_t
3263ClientConnection::_LockQueryHandle(int32 cookie, QueryHandle** _handle)
3264{
3265	NodeHandle* handle;
3266	status_t error = fQueryHandles->LockNodeHandle(cookie, &handle);
3267	if (error == B_OK)
3268		*_handle = static_cast<QueryHandle*>(handle);
3269	return error;
3270}
3271
3272// _UnlockQueryHandle
3273//
3274// VolumeManager may or may not be locked.
3275void
3276ClientConnection::_UnlockQueryHandle(NodeHandle* nodeHandle)
3277{
3278	fQueryHandles->UnlockNodeHandle(nodeHandle);
3279}
3280
3281
3282// #pragma mark -
3283
3284// _GetAllClientVolumeIDs
3285int32
3286ClientConnection::_GetAllClientVolumeIDs(int32* volumeIDs, int32 arraySize,
3287	ClientVolumeFilter* filter)
3288{
3289	int32 count = 0;
3290	AutoLocker<VolumeMap> volumesLocker(fVolumes);
3291	for (VolumeMap::Iterator it = fVolumes->GetIterator();
3292		 it.HasNext() && arraySize > count;) {
3293		ClientVolume* clientVolume = it.Next().value;
3294		if (!filter || filter->FilterVolume(this, clientVolume))
3295			volumeIDs[count++] = clientVolume->GetID();
3296	}
3297
3298	return count;
3299}
3300
3301// _GetContainingClientVolumes
3302int32
3303ClientConnection::_GetContainingClientVolumes(Directory* directory,
3304	int32* volumeIDs, int32 arraySize, ClientVolumeFilter* filter)
3305{
3306	int32 count = 0;
3307	VolumeManager* volumeManager = VolumeManager::GetDefault();
3308	AutoLocker<VolumeMap> volumesLocker(fVolumes);
3309	for (VolumeMap::Iterator it = fVolumes->GetIterator();
3310		 it.HasNext() && arraySize > count;) {
3311		ClientVolume* clientVolume = it.Next().value;
3312		Directory* clientVolumeRoot = clientVolume->GetRootDirectory();
3313		if (volumeManager->DirectoryContains(clientVolumeRoot, directory, true)
3314			&& (!filter || filter->FilterVolume(this, clientVolume))) {
3315			volumeIDs[count++] = clientVolume->GetID();
3316		}
3317	}
3318
3319	return count;
3320}
3321
3322
3323// #pragma mark -
3324// #pragma mark ----- ClientConnectionListener -----
3325
3326// constructor
3327ClientConnectionListener::ClientConnectionListener()
3328{
3329}
3330
3331// destructor
3332ClientConnectionListener::~ClientConnectionListener()
3333{
3334}
3335
3336