os/kernelhwsrv/userlibandfileserver/fileserver/sfile/sf_plugin_man.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// f32\sfile\sf_plugin.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include "sf_std.h"
sl@0
    20
#include "sf_plugin_priv.h"
sl@0
    21
sl@0
    22
CFsObjectCon* FsPluginManager::iPluginFactories = NULL;
sl@0
    23
CFsObjectCon* FsPluginManager::iPluginConns     = NULL;
sl@0
    24
RFastLock FsPluginManager::iChainLock;
sl@0
    25
RPointerArray<CFsPlugin> FsPluginManager::iPluginChain;
sl@0
    26
CFsSyncMessageScheduler* FsPluginManager::iScheduler = NULL;
sl@0
    27
sl@0
    28
TBool IsPagableDrive(TInt aDrive)
sl@0
    29
	{
sl@0
    30
	if(LocalDrives::IsValidDriveMapping(aDrive))
sl@0
    31
		{
sl@0
    32
		TLocalDriveCapsBuf capsBuf;	
sl@0
    33
		if(!LocalDrives::IsProxyDrive(aDrive) && LocalDrives::GetLocalDrive(aDrive).Caps(capsBuf) && capsBuf().iDriveAtt & KDriveAttPageable)
sl@0
    34
			{
sl@0
    35
			return (TBool)ETrue;
sl@0
    36
			}
sl@0
    37
		}
sl@0
    38
	return (TBool) EFalse;
sl@0
    39
	}
sl@0
    40
sl@0
    41
/*
sl@0
    42
@param aNonPagableDriveMask - a drive mask of the drives a plugin wishes to mount on
sl@0
    43
@return A drive mask of the drives a plugin wishes to mount on not containing any pagable drives.
sl@0
    44
*/
sl@0
    45
TInt NonPagableDrivesMask(TInt aNonPagableDriveMask)
sl@0
    46
	{
sl@0
    47
	TInt driveMask = aNonPagableDriveMask;
sl@0
    48
	for(TInt i = 0; i < KMaxDrives; i++)
sl@0
    49
		{
sl@0
    50
		//If we're interested in this drive
sl@0
    51
		if((aNonPagableDriveMask & 1<<i) && IsPagableDrive(i)) 
sl@0
    52
			{
sl@0
    53
			driveMask ^= 1<<i; //remove this drive
sl@0
    54
			}
sl@0
    55
		}
sl@0
    56
	return driveMask;
sl@0
    57
	}
sl@0
    58
sl@0
    59
TInt FsPluginManager::UpdateMountedDrive(CFsPlugin* aPlugin, CFsPluginFactory* aFactory,TInt aDrive)
sl@0
    60
	{
sl@0
    61
	if(aDrive==KPluginAutoAttach) //KPluginAutoAttach
sl@0
    62
		{
sl@0
    63
		if(!(aFactory->SupportedDrives()&KPluginVersionTwo)) 
sl@0
    64
			{ //Version 1 Plugin - Mount on all (except Z and pagble drives) 
sl@0
    65
			TInt drivesAToY = (KPluginSupportAllDrives>>1);
sl@0
    66
			aPlugin->iMountedOn = NonPagableDrivesMask(drivesAToY);
sl@0
    67
			}
sl@0
    68
		else 
sl@0
    69
			{ //Version 2 Plugin - Mount on what the plugin supports (except pagable drives)
sl@0
    70
			aPlugin->iMountedOn = NonPagableDrivesMask((aFactory->SupportedDrives()&(~KPluginVersionTwo)));
sl@0
    71
			}
sl@0
    72
		
sl@0
    73
		if(!(aPlugin->iMountedOn &(~KPluginVersionTwo))) 
sl@0
    74
			{ //Are we mounted on anything?
sl@0
    75
			return KErrNotSupported;
sl@0
    76
			}
sl@0
    77
		return KErrNone;
sl@0
    78
		}
sl@0
    79
	
sl@0
    80
	// DriveZ special handling
sl@0
    81
	if(aDrive==KPluginMountDriveZ)
sl@0
    82
		aDrive = EDriveZ;
sl@0
    83
	
sl@0
    84
	//Mounting on a single drive
sl@0
    85
	if(!IsPagableDrive(aDrive))
sl@0
    86
		{
sl@0
    87
		aPlugin->iMountedOn |= 1<<aDrive;
sl@0
    88
		return KErrNone;
sl@0
    89
		}
sl@0
    90
sl@0
    91
	return KErrNotSupported;
sl@0
    92
	}
sl@0
    93
sl@0
    94
/**
sl@0
    95
FsPluginManager::Initialise
sl@0
    96
*/
sl@0
    97
void FsPluginManager::InitialiseL()
sl@0
    98
	{
sl@0
    99
	iPluginFactories = TheContainer->CreateL();
sl@0
   100
	iPluginConns = TheContainer->CreateL();
sl@0
   101
	iPluginChain.Reset();
sl@0
   102
	User::LeaveIfError(iChainLock.CreateLocal());
sl@0
   103
sl@0
   104
	// Create and install the synchronous message scheduler
sl@0
   105
	//  - Messages are dispatched here from plugin threads if they are
sl@0
   106
	//	  to be executed in the context of the main file server thread.
sl@0
   107
	iScheduler = CFsSyncMessageScheduler::NewL();
sl@0
   108
	iScheduler->RunL();
sl@0
   109
	}
sl@0
   110
sl@0
   111
/**
sl@0
   112
FsPluginManager::MountPlugin
sl@0
   113
*/
sl@0
   114
TInt FsPluginManager::MountPlugin(CFsPluginFactory& aPluginFactory, TInt aDrive, TInt aPos)
sl@0
   115
	{
sl@0
   116
	TInt uniquePosition = aPluginFactory.UniquePosition();
sl@0
   117
	CFsPlugin* pP = NULL;
sl@0
   118
	CFsPluginFactory* pF = &aPluginFactory;
sl@0
   119
	
sl@0
   120
	//Version1 plugins could either been mounted on 1 or all drives.
sl@0
   121
	//This isn't particularily desirable.
sl@0
   122
	// Thus -
sl@0
   123
	//For version2 plugins:
sl@0
   124
	//Check whether this plugin has already been mounted
sl@0
   125
	pP = FindByUniquePosition(uniquePosition);
sl@0
   126
	if(pP && (aPluginFactory.iSupportedDrives&KPluginVersionTwo))
sl@0
   127
		{
sl@0
   128
		//if this plugin has already been mounted then 
sl@0
   129
		//Instead of trying to mount and failing with KErrInUse,
sl@0
   130
		//lets update the iMountedOn instead.
sl@0
   131
		return UpdateMountedDrive(pP,pF,aDrive);
sl@0
   132
		}
sl@0
   133
	
sl@0
   134
	
sl@0
   135
	TRAPD(err, pP = aPluginFactory.NewPluginL());
sl@0
   136
	if(err != KErrNone)
sl@0
   137
		{
sl@0
   138
		if(pP)
sl@0
   139
			pP->Close();
sl@0
   140
		return err;
sl@0
   141
		}
sl@0
   142
sl@0
   143
	TFullName name = aPluginFactory.Name();
sl@0
   144
	pP->SetName(&name);
sl@0
   145
	pP->iUniquePos=aPluginFactory.UniquePosition();
sl@0
   146
	pP->SetDrive(aDrive);
sl@0
   147
	
sl@0
   148
	//Set which drive(s?) this plugin is mounted on.
sl@0
   149
	err = UpdateMountedDrive(pP,pF,aDrive);
sl@0
   150
	if(err != KErrNone)
sl@0
   151
		{
sl@0
   152
		pP->Close();
sl@0
   153
		return err;
sl@0
   154
		}
sl@0
   155
sl@0
   156
	LockChain();
sl@0
   157
	err = InsertInPluginStack(pP,aPos);
sl@0
   158
	UnlockChain();
sl@0
   159
	if(err != KErrNone)
sl@0
   160
		{
sl@0
   161
		pP->Close();
sl@0
   162
		return err;
sl@0
   163
		}
sl@0
   164
sl@0
   165
	err = InitPlugin(*pP);
sl@0
   166
	if(err != KErrNone)
sl@0
   167
		{
sl@0
   168
		return err;
sl@0
   169
		}
sl@0
   170
 	aPluginFactory.IncrementMounted();
sl@0
   171
sl@0
   172
	return KErrNone;
sl@0
   173
	}
sl@0
   174
sl@0
   175
/**
sl@0
   176
Dismounts a plugin
sl@0
   177
sl@0
   178
Must be called with the plugin chain locked.
sl@0
   179
*/
sl@0
   180
void FsPluginManager::DismountPlugin(CFsPluginFactory& aPluginFactory,TInt aPos)
sl@0
   181
	{
sl@0
   182
	CFsPlugin* plugin=iPluginChain[aPos];
sl@0
   183
sl@0
   184
	if (plugin != NULL)
sl@0
   185
		{
sl@0
   186
		// Check if there is any requests for this plugin
sl@0
   187
		// if so, deliver them to the next supporting plugin
sl@0
   188
		TransferRequests(plugin->iThreadP);
sl@0
   189
sl@0
   190
		plugin->iThreadP=NULL;
sl@0
   191
		
sl@0
   192
		//Remove the plugin from the chain
sl@0
   193
		iPluginChain.Remove(aPos);
sl@0
   194
		iPluginChain.Compress();
sl@0
   195
sl@0
   196
	    //Close the plugin (destructed when CPluginThread is destructed).
sl@0
   197
 		plugin->Close();
sl@0
   198
		plugin=NULL;
sl@0
   199
		}
sl@0
   200
	else
sl@0
   201
		{
sl@0
   202
		ASSERT(0);	// force panic in debug mode
sl@0
   203
		}
sl@0
   204
sl@0
   205
	aPluginFactory.DecrementMounted();
sl@0
   206
	}
sl@0
   207
sl@0
   208
/*
sl@0
   209
 * This will iterate through a plugins request queue and 
sl@0
   210
 * search for the first occurance it finds of a CancelPluginOp
sl@0
   211
 * request.
sl@0
   212
 */
sl@0
   213
void FsPluginManager::GetNextCancelPluginOpRequest(CPluginThread* aPluginThread, CFsRequest*& aCancelPluginRequest)
sl@0
   214
    {
sl@0
   215
    __THRD_PRINT(_L("FsPluginManager::GetNextCancelPluginOpRequest"));
sl@0
   216
    
sl@0
   217
    TDblQueIter<CFsRequest> iter(aPluginThread->iList);
sl@0
   218
    CFsRequest* request = NULL;
sl@0
   219
sl@0
   220
    while((request=iter++)!=NULL)
sl@0
   221
        {
sl@0
   222
        if(request->Operation()->iFunction == KCancelPlugin)
sl@0
   223
            {
sl@0
   224
            aCancelPluginRequest = request;
sl@0
   225
            break;
sl@0
   226
            }
sl@0
   227
        }
sl@0
   228
    }
sl@0
   229
 
sl@0
   230
sl@0
   231
sl@0
   232
sl@0
   233
sl@0
   234
/**
sl@0
   235
Transfer any outstanding requests to next/previous plugin depending on
sl@0
   236
if it is post filter or not
sl@0
   237
sl@0
   238
Must be called with the plugin chain locked.
sl@0
   239
Attains plugin-thread's listlock.
sl@0
   240
*/
sl@0
   241
void FsPluginManager::TransferRequests(CPluginThread* aPluginThread)
sl@0
   242
	{
sl@0
   243
	aPluginThread->iListLock.Wait();
sl@0
   244
	
sl@0
   245
	__THRD_PRINT(_L("FsPluginManager::TransferRequests - moving requests"));
sl@0
   246
	
sl@0
   247
	/*
sl@0
   248
	 * We are transferring requests up and down the chain
sl@0
   249
	 * because this plugin is being removed.
sl@0
   250
	 * 
sl@0
   251
	 * There is a potential problem when one of the outstanding requests
sl@0
   252
	 * is CancelPluginOp which is called when the Session is being closed.
sl@0
   253
	 * The CancelPluginOp will try to cancel all of the requests on a plugin's
sl@0
   254
	 * queue which is associated with that session.
sl@0
   255
	 * DismountPlugin(and thus TransferRequests) is trying to preserve the requests
sl@0
   256
	 * by passing them along to the Next/Previous plugins. 
sl@0
   257
	 * 
sl@0
   258
	 * If there is a cancel in the chain we must NOT pass any requests to Previous Plugins 
sl@0
   259
	 * as these have already had their chains emptied.
sl@0
   260
	 * We should also be wary of passing requests up the chain as they will simply be cancelled 
sl@0
   261
	 * somewhere closer to the drive [thread].
sl@0
   262
	 * 
sl@0
   263
	 * Therefore, we shall check whether there is a KCancelPlugin op in the 
sl@0
   264
	 * chain first. 
sl@0
   265
     * If there is a cancelPluginOp in the chain, we will cancel of the requests 
sl@0
   266
     * that are associated with that session.
sl@0
   267
     * 
sl@0
   268
     * After that is out of the way we preserve the remaining requests for different sessions by
sl@0
   269
     * passing them on.
sl@0
   270
	 */
sl@0
   271
	if(!aPluginThread->iList.IsEmpty())
sl@0
   272
	    {
sl@0
   273
	    CFsRequest* cancelRequest = NULL;
sl@0
   274
	    //For every CancelPluginOp 
sl@0
   275
	    while(FsPluginManager::GetNextCancelPluginOpRequest(aPluginThread, cancelRequest), cancelRequest!=NULL)
sl@0
   276
	        {
sl@0
   277
	        RDebug::Print(_L("Transferring Plugin Requests - CancelPluginOp"));
sl@0
   278
	        TDblQueIter<CFsRequest> iter(aPluginThread->iList);
sl@0
   279
	        CFsRequest* request = NULL;
sl@0
   280
	        //For every request
sl@0
   281
	        while((request=iter++)!=NULL)
sl@0
   282
	            {
sl@0
   283
	            if(request->Session() == cancelRequest->Session() && request != cancelRequest)
sl@0
   284
	                {
sl@0
   285
	                request->iLink.Deque();
sl@0
   286
	                request->Complete(KErrCancel);
sl@0
   287
	                }
sl@0
   288
	            }
sl@0
   289
	        cancelRequest->iLink.Deque();
sl@0
   290
	        cancelRequest->Complete(KErrNone);
sl@0
   291
	        cancelRequest = NULL;
sl@0
   292
	        }
sl@0
   293
sl@0
   294
	    /*
sl@0
   295
	     * Now that all requests that were to be cancelled have been cancelled,
sl@0
   296
	     * we can now go about moving the remaining ones on to , or back to, 
sl@0
   297
	     * the appropriate next or previous plugins.
sl@0
   298
	     * 
sl@0
   299
	     * ToDo: This next 'while' might be able to be replaced with a call to
sl@0
   300
	     * DispatchToPlugin/DispatchToDrive instead.
sl@0
   301
	     */
sl@0
   302
	    while(!aPluginThread->iList.IsEmpty())
sl@0
   303
	        {
sl@0
   304
	        CFsRequest* pR=aPluginThread->iList.First();
sl@0
   305
	        CFsMessageRequest& mR = *(CFsMessageRequest*) pR;
sl@0
   306
	        pR->iLink.Deque();
sl@0
   307
	        pR->iCurrentPlugin=NULL;
sl@0
   308
sl@0
   309
	        if(pR->IsPluginSpecific())
sl@0
   310
	            {
sl@0
   311
	            pR->Complete(KErrCancel);
sl@0
   312
	            continue;
sl@0
   313
	            }
sl@0
   314
sl@0
   315
	        if(pR->IsPostOperation())
sl@0
   316
	            {
sl@0
   317
	            //[set the plugin to] pass the request backwards in the chain
sl@0
   318
	            PrevPlugin(pR->iCurrentPlugin, &mR, EFalse);
sl@0
   319
	            }
sl@0
   320
	        else //IsPreOperations
sl@0
   321
	            {
sl@0
   322
	            //[set the plugin to] pass the request forwards in the chain
sl@0
   323
	            NextPlugin(pR->iCurrentPlugin, &mR, EFalse);
sl@0
   324
	            }
sl@0
   325
sl@0
   326
	        if(pR->iCurrentPlugin)
sl@0
   327
	            {
sl@0
   328
	            pR->iCurrentPlugin->iThreadP->DeliverBack(pR);
sl@0
   329
	            }
sl@0
   330
	        else
sl@0
   331
	            {
sl@0
   332
	            if(!pR->IsPostOperation() && (pR->DriveNumber()>=EDriveA && pR->DriveNumber()<=EDriveZ))
sl@0
   333
	                {
sl@0
   334
	                //Deliver to drive thread
sl@0
   335
	                CDriveThread* dT=NULL;
sl@0
   336
	                TInt r=FsThreadManager::GetDriveThread(pR->DriveNumber(),&dT);
sl@0
   337
	                __ASSERT_ALWAYS(r==KErrNone && dT,Fault(EFsDriveThreadError));
sl@0
   338
	                CRequestThread* pT = (CRequestThread*)dT;
sl@0
   339
	                pT->DeliverBack(pR);
sl@0
   340
	                }
sl@0
   341
	            else
sl@0
   342
	                {
sl@0
   343
	                pR->Complete(KErrCancel);
sl@0
   344
	                }
sl@0
   345
	            }
sl@0
   346
	        }
sl@0
   347
	    }
sl@0
   348
sl@0
   349
	aPluginThread->iExit = ETrue;
sl@0
   350
	aPluginThread->iListLock.Signal();
sl@0
   351
sl@0
   352
	__THRD_PRINT(_L("FsPluginManager::TransferRequests - all requests moved/cancelled"));
sl@0
   353
	}
sl@0
   354
sl@0
   355
/**
sl@0
   356
Install a plugin factory
sl@0
   357
*/
sl@0
   358
TInt FsPluginManager::InstallPluginFactory(CFsPluginFactory* aFactory,RLibrary aLib)
sl@0
   359
	{
sl@0
   360
	TInt r=aFactory->Install();
sl@0
   361
	__PRINT1TEMP(_L("InstallPluginFactory %S"),aFactory->Name());
sl@0
   362
	if (r==KErrNone)
sl@0
   363
		{TRAP(r,iPluginFactories->AddL(aFactory,ETrue))}
sl@0
   364
	if (r!=KErrNone)
sl@0
   365
		aFactory->Remove();
sl@0
   366
	if (r==KErrNone)
sl@0
   367
		aFactory->SetLibrary(aLib);
sl@0
   368
	else
sl@0
   369
		aFactory->Close();
sl@0
   370
	return(r);
sl@0
   371
	}
sl@0
   372
sl@0
   373
/**
sl@0
   374
Find a plugin factory by name
sl@0
   375
*/
sl@0
   376
CFsPluginFactory* FsPluginManager::GetPluginFactory(const TDesC& aName)
sl@0
   377
	{
sl@0
   378
sl@0
   379
	TInt h=0;
sl@0
   380
	TInt r=iPluginFactories->FindByName(h,aName);
sl@0
   381
	if (r!=KErrNone)
sl@0
   382
		return(NULL);
sl@0
   383
	return((CFsPluginFactory*)iPluginFactories->At(h));
sl@0
   384
	}
sl@0
   385
sl@0
   386
/**
sl@0
   387
Find the next plugin that supports the operation
sl@0
   388
sl@0
   389
@param aPlugin - On calling the function this may contain either NULL or the current plugin.
sl@0
   390
                 If it is called with NULL, then we start to look for plugins from the beginning of the chain.
sl@0
   391
                 If is is called with a plugin then we start to look after that plugin for the next one.
sl@0
   392
                 On return, aPlugin shall contain either a plugin or NULL.
sl@0
   393
                 
sl@0
   394
@param aLock - If this is set to ETRUE, then the function shall lock the plugin chain.
sl@0
   395
               If this is set to EFALSE, then the caller of the function MUST already hold the lock.
sl@0
   396
sl@0
   397
@param aCheckCurrentOperation - Optional, if false, will return the next plugin,
sl@0
   398
 								whether the plugin is currently registered
sl@0
   399
								for the current function or not. (so long as mounted on the current drive)
sl@0
   400
*/
sl@0
   401
TInt FsPluginManager::NextPlugin(CFsPlugin*& aPlugin, CFsMessageRequest* aMsgRequest,TBool aLock, TBool aCheckCurrentOperation)
sl@0
   402
	{
sl@0
   403
	if(aMsgRequest->DirectToDrive())
sl@0
   404
		{
sl@0
   405
		aPlugin = NULL;
sl@0
   406
		return KErrNotFound;
sl@0
   407
		}
sl@0
   408
sl@0
   409
	TInt start;
sl@0
   410
	TInt function = aMsgRequest->Operation()->Function();
sl@0
   411
	TInt drive = aMsgRequest->DriveNumber();
sl@0
   412
sl@0
   413
	if(aLock)
sl@0
   414
	    LockChain();
sl@0
   415
	
sl@0
   416
	//the plugin chain lock must be held by this point.
sl@0
   417
	TInt count = iPluginChain.Count();
sl@0
   418
sl@0
   419
	if(aPlugin == NULL)
sl@0
   420
		start=0;
sl@0
   421
	else
sl@0
   422
		start = iPluginChain.Find(aPlugin)+1;
sl@0
   423
sl@0
   424
	if(start!=KErrNotFound)
sl@0
   425
		{
sl@0
   426
		for(TInt i=start;i<count;i++)
sl@0
   427
			{
sl@0
   428
			if(!aCheckCurrentOperation || iPluginChain[i]->IsRegistered(function, CFsPlugin::EPreIntercept))
sl@0
   429
				{
sl@0
   430
				if((function == EFsDismountPlugin) || (iPluginChain[i]->IsMounted(drive))) 
sl@0
   431
					{
sl@0
   432
sl@0
   433
					aPlugin = iPluginChain[i];
sl@0
   434
					if(aLock)
sl@0
   435
					    UnlockChain();
sl@0
   436
					return KErrNone;
sl@0
   437
					}
sl@0
   438
				}
sl@0
   439
			}
sl@0
   440
		}
sl@0
   441
	aPlugin = NULL;
sl@0
   442
	if(aLock)
sl@0
   443
	    UnlockChain();
sl@0
   444
	return KErrNotFound;
sl@0
   445
	}
sl@0
   446
sl@0
   447
/**
sl@0
   448
Find the next plugin that supports the operation
sl@0
   449
sl@0
   450
@see FsPluginManager::NextPlugin
sl@0
   451
*/
sl@0
   452
TInt FsPluginManager::PrevPlugin(CFsPlugin*& aPlugin, CFsMessageRequest* aMsgRequest, TBool aLock)
sl@0
   453
	{
sl@0
   454
	if(aMsgRequest->DirectToDrive() && (aMsgRequest->CurrentOperationPtr() != NULL))
sl@0
   455
		{
sl@0
   456
		aPlugin = NULL;
sl@0
   457
		return KErrNotFound;
sl@0
   458
		}
sl@0
   459
sl@0
   460
	TInt start;
sl@0
   461
	TInt function = aMsgRequest->Operation()->Function();
sl@0
   462
	TInt drive = aMsgRequest->DriveNumber();
sl@0
   463
sl@0
   464
	if(aLock)
sl@0
   465
	    LockChain();
sl@0
   466
	
sl@0
   467
	//the plugin chain lock must be held by this point.
sl@0
   468
	TInt count= iPluginChain.Count();
sl@0
   469
sl@0
   470
	if(aPlugin == NULL)
sl@0
   471
		start = count-1;
sl@0
   472
	else
sl@0
   473
		start = iPluginChain.Find(aPlugin)-1;
sl@0
   474
sl@0
   475
	if(start!=KErrNotFound)
sl@0
   476
		{
sl@0
   477
		for(TInt i=start;i>=0;i--)
sl@0
   478
			{
sl@0
   479
			CFsPlugin* owner = aMsgRequest->iOwnerPlugin;
sl@0
   480
			if(owner == iPluginChain[i])
sl@0
   481
				break;
sl@0
   482
sl@0
   483
			if(iPluginChain[i]->IsRegistered(function, CFsPlugin::EPostIntercept))
sl@0
   484
				{
sl@0
   485
				if((function == EFsDismountPlugin) || (iPluginChain[i]->IsMounted(drive))) 
sl@0
   486
					{
sl@0
   487
sl@0
   488
					aPlugin = iPluginChain[i];
sl@0
   489
					if(aLock)
sl@0
   490
					    UnlockChain();
sl@0
   491
					return KErrNone;
sl@0
   492
					}
sl@0
   493
				}
sl@0
   494
			}
sl@0
   495
		}
sl@0
   496
	aPlugin = NULL;
sl@0
   497
	if(aLock)
sl@0
   498
	    UnlockChain();
sl@0
   499
	return KErrNotFound;
sl@0
   500
	}
sl@0
   501
/**
sl@0
   502
Inserts the plugin in the stack (chain)
sl@0
   503
sl@0
   504
if aPos absolute postion, it simply inserts it
sl@0
   505
if aPos unique position, it walks through chain and checks the unique positions
sl@0
   506
*/
sl@0
   507
TInt FsPluginManager::InsertInPluginStack(CFsPlugin*& aPlugin,TInt aPos)
sl@0
   508
	{
sl@0
   509
	TInt ret=KErrNone;
sl@0
   510
	TInt count= iPluginChain.Count();
sl@0
   511
	if(aPos == KPluginAutoLocate)
sl@0
   512
		{
sl@0
   513
		TInt uPos= aPlugin->iUniquePos;
sl@0
   514
		for(TInt i=0;i<count;i++)
sl@0
   515
			{
sl@0
   516
			if(uPos == iPluginChain[i]->iUniquePos)
sl@0
   517
				{
sl@0
   518
				return KErrInUse;
sl@0
   519
				}
sl@0
   520
sl@0
   521
			if(uPos < iPluginChain[i]->iUniquePos)
sl@0
   522
				{
sl@0
   523
				ret=iPluginChain.Insert(aPlugin,i);
sl@0
   524
				return ret;
sl@0
   525
				}
sl@0
   526
			}
sl@0
   527
		ret=iPluginChain.Append(aPlugin);
sl@0
   528
		}
sl@0
   529
	else
sl@0
   530
		{
sl@0
   531
		// Absolute position
sl@0
   532
sl@0
   533
		if(aPos > count)
sl@0
   534
			return(KErrNotFound);
sl@0
   535
		if(aPos == count)
sl@0
   536
			{
sl@0
   537
			ret=iPluginChain.Append(aPlugin);
sl@0
   538
			return ret;
sl@0
   539
			}
sl@0
   540
		ret=iPluginChain.Insert(aPlugin,aPos);
sl@0
   541
		}
sl@0
   542
	return ret;
sl@0
   543
	}
sl@0
   544
sl@0
   545
/**
sl@0
   546
Looks for a plugin in the chain
sl@0
   547
*/
sl@0
   548
TInt FsPluginManager::IsInChain(TInt aUPos,TInt aPos, TInt aDrive, CFsPluginFactory* aPluginFactory)
sl@0
   549
	{
sl@0
   550
	TInt count= iPluginChain.Count();
sl@0
   551
	
sl@0
   552
	if(aPos == KPluginAutoLocate)
sl@0
   553
		{
sl@0
   554
		for(TInt i=0;i<count;i++)
sl@0
   555
			{
sl@0
   556
			CFsPlugin* pP = iPluginChain[i];
sl@0
   557
			if(aPluginFactory->SupportedDrives()&KPluginVersionTwo) //Version2
sl@0
   558
				{
sl@0
   559
				//If KPluginAutoAttach, then we're dismounted from all drives.
sl@0
   560
				//If KPluginMountDriveZ, then check against 25 explicitly (cannot change aDrive:=Z as that==KPluginAutoAttach))
sl@0
   561
				//If any other drive mounted.
sl@0
   562
				if(aUPos == pP->iUniquePos && (aDrive==KPluginAutoAttach || (aDrive==KPluginMountDriveZ && pP->iMountedOn & (1<<EDriveZ)) || pP->iMountedOn & (1<<aDrive)))
sl@0
   563
					return i;
sl@0
   564
				}
sl@0
   565
			else //version1
sl@0
   566
				{
sl@0
   567
				if(aUPos == pP->iUniquePos && (aDrive==KPluginAutoAttach || pP->iMountedOn & (1<<aDrive)))
sl@0
   568
					return i;
sl@0
   569
				}
sl@0
   570
			}
sl@0
   571
		return KErrNotFound;
sl@0
   572
		}
sl@0
   573
sl@0
   574
	if(aPos+1>iPluginChain.Count())
sl@0
   575
		return(KErrNotFound);
sl@0
   576
sl@0
   577
	if(iPluginChain[aPos]->iUniquePos == aUPos && aDrive==iPluginChain[aPos]->Drive())
sl@0
   578
		return aPos;
sl@0
   579
	return KErrNotFound;
sl@0
   580
	}
sl@0
   581
sl@0
   582
/**
sl@0
   583
Finds a plugin by unique position
sl@0
   584
*/
sl@0
   585
CFsPlugin* FsPluginManager::FindByUniquePosition(TInt aUniquePosition)
sl@0
   586
	{
sl@0
   587
	LockChain();
sl@0
   588
	CFsPlugin* plugin = NULL;
sl@0
   589
	TInt count= iPluginChain.Count();
sl@0
   590
	for(TInt i=0;i<count;i++)
sl@0
   591
		{
sl@0
   592
		if(aUniquePosition == iPluginChain[i]->iUniquePos)
sl@0
   593
			{
sl@0
   594
			plugin = iPluginChain[i];
sl@0
   595
			break;
sl@0
   596
			}
sl@0
   597
		}
sl@0
   598
	UnlockChain();
sl@0
   599
	return plugin;
sl@0
   600
	}
sl@0
   601
sl@0
   602
/**
sl@0
   603
Create a connection to a plugin
sl@0
   604
*/
sl@0
   605
CFsPluginConn* FsPluginManager::CreatePluginConnL(TInt aUniquePosition, TUint aClientId)
sl@0
   606
	{
sl@0
   607
	CFsPlugin* pP = FindByUniquePosition(aUniquePosition);
sl@0
   608
	if(pP != NULL)
sl@0
   609
		{
sl@0
   610
		CFsPluginConn* pC = pP->NewPluginConnL();
sl@0
   611
		CleanupStack::PushL(pC);
sl@0
   612
		pC->iPluginP = pP;
sl@0
   613
		pC->iClientId = aClientId;
sl@0
   614
		iPluginConns->AddL(pC, ETrue);
sl@0
   615
		CleanupStack::Pop(pC);
sl@0
   616
		return pC;
sl@0
   617
		}
sl@0
   618
sl@0
   619
	User::Leave(KErrNotFound);
sl@0
   620
	return NULL;
sl@0
   621
	}
sl@0
   622
sl@0
   623
/**
sl@0
   624
Create a plugin thread
sl@0
   625
Should only by called from main file server thread with plugin thread unavailable
sl@0
   626
*/
sl@0
   627
TInt FsPluginManager::InitPlugin(CFsPlugin& aPlugin)
sl@0
   628
	{
sl@0
   629
	TInt err = KErrNone;
sl@0
   630
sl@0
   631
	if(!aPlugin.iThreadP)
sl@0
   632
		{
sl@0
   633
		TRAP(err,aPlugin.iThreadP=CPluginThread::NewL(aPlugin));
sl@0
   634
		if(err!=KErrNone)
sl@0
   635
			return err;
sl@0
   636
		}
sl@0
   637
sl@0
   638
	aPlugin.iThreadId = aPlugin.iThreadP->StartL();
sl@0
   639
	return err;
sl@0
   640
	}
sl@0
   641
/**
sl@0
   642
Cancels plugin requests
sl@0
   643
*/
sl@0
   644
void FsPluginManager::CancelPlugin(CFsPlugin* aPlugin,CSessionFs* aSession)
sl@0
   645
	{
sl@0
   646
	aPlugin->iThreadP->CompleteSessionRequests(aSession,KErrCancel);
sl@0
   647
	}
sl@0
   648
sl@0
   649
/**
sl@0
   650
Gets number of plugins in the plugin stack
sl@0
   651
*/
sl@0
   652
TInt FsPluginManager::ChainCount()
sl@0
   653
	{
sl@0
   654
	return(iPluginChain.Count());
sl@0
   655
	}
sl@0
   656
sl@0
   657
/**
sl@0
   658
 * Returns a CFsPlugin* from aPos in the plugin chain.
sl@0
   659
 * 
sl@0
   660
 * To be called whilst already holding the iChainLock.
sl@0
   661
 * 
sl@0
   662
 * @returns (via parameter) CFsPlugin*& aPlugin
sl@0
   663
*/
sl@0
   664
TInt FsPluginManager::Plugin(CFsPlugin*& aPlugin, TInt aPos)
sl@0
   665
	{
sl@0
   666
	if(aPos >= iPluginChain.Count() || aPos < 0)
sl@0
   667
	    return KErrNotFound;
sl@0
   668
	
sl@0
   669
	aPlugin = iPluginChain[aPos];
sl@0
   670
	return KErrNone;
sl@0
   671
	}
sl@0
   672
sl@0
   673
/**
sl@0
   674
Locks the chain
sl@0
   675
*/
sl@0
   676
void FsPluginManager::LockChain()
sl@0
   677
	{
sl@0
   678
	iChainLock.Wait();
sl@0
   679
	}
sl@0
   680
sl@0
   681
/**
sl@0
   682
Unlocks the chain
sl@0
   683
*/
sl@0
   684
void FsPluginManager::UnlockChain()
sl@0
   685
	{
sl@0
   686
	iChainLock.Signal();
sl@0
   687
	}
sl@0
   688
sl@0
   689
/**
sl@0
   690
Gets plugin conn from handle
sl@0
   691
*/
sl@0
   692
CFsPluginConn* FsPluginManager::GetPluginConnFromHandle(CSessionFs* aSession, TInt aHandle)
sl@0
   693
	{
sl@0
   694
	return((CFsPluginConn*)(SessionObjectFromHandle(aHandle,iPluginConns->UniqueID(),aSession)));
sl@0
   695
	}
sl@0
   696
sl@0
   697
/**
sl@0
   698
Checks if the current thread is plugin conn's thread
sl@0
   699
*/
sl@0
   700
TBool FsPluginManager::IsPluginConnThread(TThreadId tid, CFsPlugin* aPlugin)
sl@0
   701
	{
sl@0
   702
	iPluginConns->Lock();
sl@0
   703
	TInt count = iPluginConns->Count();
sl@0
   704
	while(count--)
sl@0
   705
		{
sl@0
   706
		CFsPluginConn* conn = (CFsPluginConn*)(*iPluginConns)[count];
sl@0
   707
		if(conn->Plugin() == aPlugin)
sl@0
   708
			{
sl@0
   709
			if(conn->ClientId() == tid)
sl@0
   710
				{
sl@0
   711
				iPluginConns->Unlock();
sl@0
   712
				return ETrue;
sl@0
   713
				}
sl@0
   714
			}
sl@0
   715
		}
sl@0
   716
	iPluginConns->Unlock();
sl@0
   717
	return EFalse;
sl@0
   718
	}
sl@0
   719
sl@0
   720
/**
sl@0
   721
Dispatch a synchronous message
sl@0
   722
*/
sl@0
   723
void FsPluginManager::DispatchSync(CFsRequest* aRequest)
sl@0
   724
	{
sl@0
   725
	__THRD_PRINT(_L("FsPluginManager::DispatchSync"));
sl@0
   726
	if(!FsThreadManager::IsMainThread() && iScheduler)
sl@0
   727
		{
sl@0
   728
		iScheduler->Dispatch(aRequest);
sl@0
   729
		}
sl@0
   730
	else
sl@0
   731
		{
sl@0
   732
		aRequest->Process();
sl@0
   733
		}
sl@0
   734
	}
sl@0
   735
sl@0
   736
void FsPluginManager::CompleteSessionRequests(CSessionFs* aSession, TInt aValue, CFsInternalRequest* aRequest)
sl@0
   737
/**
sl@0
   738
 * Complete outstanding requests for the specified session
sl@0
   739
 */
sl@0
   740
	{
sl@0
   741
	__PRINT2(_L("FsPluginManager::CompleteSessionRequests(%08x, %d)"), aSession, aValue);
sl@0
   742
sl@0
   743
	// Iterate through all plugins, cancelling outstanding session requests
sl@0
   744
	aRequest->Set(CancelPluginOp, aSession);
sl@0
   745
sl@0
   746
	FsPluginManager::LockChain();
sl@0
   747
	TInt count = FsPluginManager::ChainCount();
sl@0
   748
	TInt i;
sl@0
   749
	for(i=0; i<count; i++)
sl@0
   750
	    {
sl@0
   751
	    CFsPlugin* plugin = NULL;
sl@0
   752
	    User::LeaveIfError(FsPluginManager::Plugin(plugin, i));
sl@0
   753
	    __ASSERT_DEBUG(plugin, User::Leave(KErrNotFound));
sl@0
   754
	    aRequest->iCurrentPlugin = plugin;
sl@0
   755
	    aRequest->Status() = KRequestPending;
sl@0
   756
	    aRequest->Dispatch();
sl@0
   757
	    //Cancel is delivered to the front of the request queue
sl@0
   758
	    //so hopefully this wont take too long.
sl@0
   759
	    FsPluginManager::UnlockChain();
sl@0
   760
	    User::WaitForRequest(aRequest->Status());
sl@0
   761
	    FsPluginManager::LockChain();
sl@0
   762
	    __ASSERT_ALWAYS(aRequest->Status().Int()==KErrNone||aRequest->Status().Int()==KErrCancel,Fault(ESessionDisconnectThread2));
sl@0
   763
	    count = FsPluginManager::ChainCount();
sl@0
   764
	    }
sl@0
   765
	FsPluginManager::UnlockChain();
sl@0
   766
	
sl@0
   767
//	RDebug::Print(_L("FsPluginManager::CompleteSessionRequests - CSRs"));
sl@0
   768
	iScheduler->CompleteSessionRequests(aSession, aValue);
sl@0
   769
	}
sl@0
   770
sl@0
   771