Esempio n. 1
0
        public virtual void TestGetAccessRightsForUserGroup()
        {
            Nfs3FileAttributes attr = Org.Mockito.Mockito.Mock <Nfs3FileAttributes>();

            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(3);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(448);
            // 700
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsreg.ToValue());
            NUnit.Framework.Assert.AreEqual("No access should be allowed as UID does not match attribute over mode 700"
                                            , 0, Nfs3Utils.GetAccessRightsForUserGroup(3, 3, null, attr));
            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(3);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(56);
            // 070
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsreg.ToValue());
            NUnit.Framework.Assert.AreEqual("No access should be allowed as GID does not match attribute over mode 070"
                                            , 0, Nfs3Utils.GetAccessRightsForUserGroup(2, 4, null, attr));
            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(3);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(7);
            // 007
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsreg.ToValue());
            NUnit.Framework.Assert.AreEqual("Access should be allowed as mode is 007 and UID/GID do not match"
                                            , 61, Nfs3Utils.GetAccessRightsForUserGroup(1, 4, new int[] { 5, 6 }, attr));
            /* RWX */
            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(10);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(288);
            // 440
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsreg.ToValue());
            NUnit.Framework.Assert.AreEqual("Access should be allowed as mode is 440 and Aux GID does match"
                                            , 1, Nfs3Utils.GetAccessRightsForUserGroup(3, 4, new int[] { 5, 16, 10 }, attr));
            /* R */
            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(10);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(448);
            // 700
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsdir.ToValue());
            NUnit.Framework.Assert.AreEqual("Access should be allowed for dir as mode is 700 and UID does match"
                                            , 31, Nfs3Utils.GetAccessRightsForUserGroup(2, 4, new int[] { 5, 16, 10 }, attr)
                                            );
            /* Lookup */
            NUnit.Framework.Assert.AreEqual("No access should be allowed for dir as mode is 700 even though GID does match"
                                            , 0, Nfs3Utils.GetAccessRightsForUserGroup(3, 10, new int[] { 5, 16, 4 }, attr));
            NUnit.Framework.Assert.AreEqual("No access should be allowed for dir as mode is 700 even though AuxGID does match"
                                            , 0, Nfs3Utils.GetAccessRightsForUserGroup(3, 20, new int[] { 5, 10 }, attr));
            Org.Mockito.Mockito.When(attr.GetUid()).ThenReturn(2);
            Org.Mockito.Mockito.When(attr.GetGid()).ThenReturn(10);
            Org.Mockito.Mockito.When(attr.GetMode()).ThenReturn(457);
            // 711
            Org.Mockito.Mockito.When(attr.GetType()).ThenReturn(NfsFileType.Nfsdir.ToValue());
            NUnit.Framework.Assert.AreEqual("Access should be allowed for dir as mode is 711 and GID matches"
                                            , 2, Nfs3Utils.GetAccessRightsForUserGroup(3, 10, new int[] { 5, 16, 11 }, attr)
                                            );
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"/>
        internal virtual Nfs3FileAttributes GetFileAttr(DFSClient client, FileHandle dirHandle
                                                        , string fileName)
        {
            string             fileIdPath = Nfs3Utils.GetFileIdPath(dirHandle) + "/" + fileName;
            Nfs3FileAttributes attr       = Nfs3Utils.GetFileAttr(client, fileIdPath, iug);

            if ((attr != null) && (attr.GetType() == NfsFileType.Nfsreg.ToValue()))
            {
                OpenFileCtx openFileCtx = fileContextCache.Get(new FileHandle(attr.GetFileId()));
                if (openFileCtx != null)
                {
                    attr.SetSize(openFileCtx.GetNextOffset());
                    attr.SetUsed(openFileCtx.GetNextOffset());
                }
            }
            return(attr);
        }
Esempio n. 3
0
        /// <summary>If the file is in cache, update the size based on the cached data size</summary>
        /// <exception cref="System.IO.IOException"/>
        internal virtual Nfs3FileAttributes GetFileAttr(DFSClient client, FileHandle fileHandle
                                                        , IdMappingServiceProvider iug)
        {
            string             fileIdPath = Nfs3Utils.GetFileIdPath(fileHandle);
            Nfs3FileAttributes attr       = Nfs3Utils.GetFileAttr(client, fileIdPath, iug);

            if (attr != null)
            {
                OpenFileCtx openFileCtx = fileContextCache.Get(fileHandle);
                if (openFileCtx != null)
                {
                    attr.SetSize(openFileCtx.GetNextOffset());
                    attr.SetUsed(openFileCtx.GetNextOffset());
                }
            }
            return(attr);
        }
Esempio n. 4
0
        internal virtual void HandleCommit(DFSClient dfsClient, FileHandle fileHandle, long
                                           commitOffset, Org.Jboss.Netty.Channel.Channel channel, int xid, Nfs3FileAttributes
                                           preOpAttr)
        {
            long        startTime = Runtime.NanoTime();
            int         status;
            OpenFileCtx openFileCtx = fileContextCache.Get(fileHandle);

            if (openFileCtx == null)
            {
                Log.Info("No opened stream for fileId: " + fileHandle.GetFileId() + " commitOffset="
                         + commitOffset + ". Return success in this case.");
                status = Nfs3Status.Nfs3Ok;
            }
            else
            {
                OpenFileCtx.COMMIT_STATUS ret = openFileCtx.CheckCommit(dfsClient, commitOffset,
                                                                        channel, xid, preOpAttr, false);
                switch (ret)
                {
                case OpenFileCtx.COMMIT_STATUS.CommitFinished:
                case OpenFileCtx.COMMIT_STATUS.CommitInactiveCtx:
                {
                    status = Nfs3Status.Nfs3Ok;
                    break;
                }

                case OpenFileCtx.COMMIT_STATUS.CommitInactiveWithPendingWrite:
                case OpenFileCtx.COMMIT_STATUS.CommitError:
                {
                    status = Nfs3Status.Nfs3errIo;
                    break;
                }

                case OpenFileCtx.COMMIT_STATUS.CommitWait:
                {
                    // Do nothing. Commit is async now.
                    return;
                }

                case OpenFileCtx.COMMIT_STATUS.CommitSpecialWait:
                {
                    status = Nfs3Status.Nfs3errJukebox;
                    break;
                }

                case OpenFileCtx.COMMIT_STATUS.CommitSpecialSuccess:
                {
                    status = Nfs3Status.Nfs3Ok;
                    break;
                }

                default:
                {
                    Log.Error("Should not get commit return code: " + ret.ToString());
                    throw new RuntimeException("Should not get commit return code: " + ret.ToString()
                                               );
                }
                }
            }
            // Send out the response
            Nfs3FileAttributes postOpAttr = null;

            try
            {
                postOpAttr = GetFileAttr(dfsClient, new FileHandle(preOpAttr.GetFileId()), iug);
            }
            catch (IOException e1)
            {
                Log.Info("Can't get postOpAttr for fileId: " + preOpAttr.GetFileId(), e1);
            }
            WccData         fileWcc  = new WccData(Nfs3Utils.GetWccAttr(preOpAttr), postOpAttr);
            COMMIT3Response response = new COMMIT3Response(status, fileWcc, Nfs3Constant.WriteCommitVerf
                                                           );

            RpcProgramNfs3.metrics.AddCommit(Nfs3Utils.GetElapsedTime(startTime));
            Nfs3Utils.WriteChannelCommit(channel, response.Serialize(new XDR(), xid, new VerifierNone
                                                                         ()), xid);
        }
Esempio n. 5
0
        /// <exception cref="System.IO.IOException"/>
        internal virtual void HandleWrite(DFSClient dfsClient, WRITE3Request request, Org.Jboss.Netty.Channel.Channel
                                          channel, int xid, Nfs3FileAttributes preOpAttr)
        {
            int count = request.GetCount();

            byte[] data = ((byte[])request.GetData().Array());
            if (data.Length < count)
            {
                WRITE3Response response = new WRITE3Response(Nfs3Status.Nfs3errInval);
                Nfs3Utils.WriteChannel(channel, response.Serialize(new XDR(), xid, new VerifierNone
                                                                       ()), xid);
                return;
            }
            FileHandle handle = request.GetHandle();

            if (Log.IsDebugEnabled())
            {
                Log.Debug("handleWrite " + request);
            }
            // Check if there is a stream to write
            FileHandle  fileHandle  = request.GetHandle();
            OpenFileCtx openFileCtx = fileContextCache.Get(fileHandle);

            if (openFileCtx == null)
            {
                Log.Info("No opened stream for fileId: " + fileHandle.GetFileId());
                string fileIdPath               = Nfs3Utils.GetFileIdPath(fileHandle.GetFileId());
                HdfsDataOutputStream fos        = null;
                Nfs3FileAttributes   latestAttr = null;
                try
                {
                    int bufferSize = config.GetInt(CommonConfigurationKeysPublic.IoFileBufferSizeKey,
                                                   CommonConfigurationKeysPublic.IoFileBufferSizeDefault);
                    fos = dfsClient.Append(fileIdPath, bufferSize, EnumSet.Of(CreateFlag.Append), null
                                           , null);
                    latestAttr = Nfs3Utils.GetFileAttr(dfsClient, fileIdPath, iug);
                }
                catch (RemoteException e)
                {
                    IOException io = e.UnwrapRemoteException();
                    if (io is AlreadyBeingCreatedException)
                    {
                        Log.Warn("Can't append file: " + fileIdPath + ". Possibly the file is being closed. Drop the request: "
                                 + request + ", wait for the client to retry...");
                        return;
                    }
                    throw;
                }
                catch (IOException e)
                {
                    Log.Error("Can't append to file: " + fileIdPath, e);
                    if (fos != null)
                    {
                        fos.Close();
                    }
                    WccData        fileWcc  = new WccData(Nfs3Utils.GetWccAttr(preOpAttr), preOpAttr);
                    WRITE3Response response = new WRITE3Response(Nfs3Status.Nfs3errIo, fileWcc, count
                                                                 , request.GetStableHow(), Nfs3Constant.WriteCommitVerf);
                    Nfs3Utils.WriteChannel(channel, response.Serialize(new XDR(), xid, new VerifierNone
                                                                           ()), xid);
                    return;
                }
                // Add open stream
                string writeDumpDir = config.Get(NfsConfigKeys.DfsNfsFileDumpDirKey, NfsConfigKeys
                                                 .DfsNfsFileDumpDirDefault);
                openFileCtx = new OpenFileCtx(fos, latestAttr, writeDumpDir + "/" + fileHandle.GetFileId
                                                  (), dfsClient, iug, aixCompatMode, config);
                if (!AddOpenFileStream(fileHandle, openFileCtx))
                {
                    Log.Info("Can't add new stream. Close it. Tell client to retry.");
                    try
                    {
                        fos.Close();
                    }
                    catch (IOException e)
                    {
                        Log.Error("Can't close stream for fileId: " + handle.GetFileId(), e);
                    }
                    // Notify client to retry
                    WccData        fileWcc  = new WccData(latestAttr.GetWccAttr(), latestAttr);
                    WRITE3Response response = new WRITE3Response(Nfs3Status.Nfs3errJukebox, fileWcc,
                                                                 0, request.GetStableHow(), Nfs3Constant.WriteCommitVerf);
                    Nfs3Utils.WriteChannel(channel, response.Serialize(new XDR(), xid, new VerifierNone
                                                                           ()), xid);
                    return;
                }
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("Opened stream for appending file: " + fileHandle.GetFileId());
                }
            }
            // Add write into the async job queue
            openFileCtx.ReceivedNewWrite(dfsClient, request, channel, xid, asyncDataService,
                                         iug);
            return;
        }