Search in sources :

Example 61 with FileHandle

use of org.apache.hadoop.nfs.nfs3.FileHandle in project hadoop by apache.

the class TestClientAccessPrivilege method testClientAccessPrivilegeForRemove.

@Test(timeout = 60000)
public void testClientAccessPrivilegeForRemove() throws Exception {
    // Configure ro access for nfs1 service
    config.set("dfs.nfs.exports.allowed.hosts", "* ro");
    // Start nfs
    Nfs3 nfs = new Nfs3(config);
    nfs.startServiceInternal(false);
    RpcProgramNfs3 nfsd = (RpcProgramNfs3) nfs.getRpcProgram();
    // Create a remove request
    HdfsFileStatus status = nn.getRpcServer().getFileInfo(testdir);
    long dirId = status.getFileId();
    XDR xdr_req = new XDR();
    FileHandle handle = new FileHandle(dirId);
    handle.serialize(xdr_req);
    xdr_req.writeString("f1");
    // Remove operation
    REMOVE3Response response = nfsd.remove(xdr_req.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
    // Assert on return code
    assertEquals("Incorrect return code", Nfs3Status.NFS3ERR_ACCES, response.getStatus());
}
Also used : FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) InetSocketAddress(java.net.InetSocketAddress) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) XDR(org.apache.hadoop.oncrpc.XDR) REMOVE3Response(org.apache.hadoop.nfs.nfs3.response.REMOVE3Response) Test(org.junit.Test)

Example 62 with FileHandle

use of org.apache.hadoop.nfs.nfs3.FileHandle in project hadoop by apache.

the class RpcProgramMountd method mnt.

@Override
public XDR mnt(XDR xdr, XDR out, int xid, InetAddress client) {
    if (hostsMatcher == null) {
        return MountResponse.writeMNTResponse(Nfs3Status.NFS3ERR_ACCES, out, xid, null);
    }
    AccessPrivilege accessPrivilege = hostsMatcher.getAccessPrivilege(client);
    if (accessPrivilege == AccessPrivilege.NONE) {
        return MountResponse.writeMNTResponse(Nfs3Status.NFS3ERR_ACCES, out, xid, null);
    }
    String path = xdr.readString();
    if (LOG.isDebugEnabled()) {
        LOG.debug("MOUNT MNT path: " + path + " client: " + client);
    }
    String host = client.getHostName();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Got host: " + host + " path: " + path);
    }
    if (!exports.contains(path)) {
        LOG.info("Path " + path + " is not shared.");
        MountResponse.writeMNTResponse(Nfs3Status.NFS3ERR_NOENT, out, xid, null);
        return out;
    }
    FileHandle handle = null;
    try {
        HdfsFileStatus exFileStatus = dfsClient.getFileInfo(path);
        handle = new FileHandle(exFileStatus.getFileId());
    } catch (IOException e) {
        LOG.error("Can't get handle for export:" + path, e);
        MountResponse.writeMNTResponse(Nfs3Status.NFS3ERR_NOENT, out, xid, null);
        return out;
    }
    assert (handle != null);
    LOG.info("Giving handle (fileId:" + handle.getFileId() + ") to client for export " + path);
    mounts.add(new MountEntry(host, path));
    MountResponse.writeMNTResponse(Nfs3Status.NFS3_OK, out, xid, handle.getContent());
    return out;
}
Also used : MountEntry(org.apache.hadoop.mount.MountEntry) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) IOException(java.io.IOException) AccessPrivilege(org.apache.hadoop.nfs.AccessPrivilege)

Example 63 with FileHandle

use of org.apache.hadoop.nfs.nfs3.FileHandle in project hadoop by apache.

the class TestRpcProgramNfs3 method testMkdir.

@Test(timeout = 60000)
public void testMkdir() throws Exception {
    //FixME
    HdfsFileStatus status = nn.getRpcServer().getFileInfo(testdir);
    long dirId = status.getFileId();
    XDR xdr_req = new XDR();
    FileHandle handle = new FileHandle(dirId);
    MKDIR3Request req = new MKDIR3Request(handle, "fubar1", new SetAttr3());
    req.serialize(xdr_req);
    // Attempt to mkdir by an unprivileged user should fail.
    MKDIR3Response response1 = nfsd.mkdir(xdr_req.asReadOnlyWrap(), securityHandlerUnpriviledged, new InetSocketAddress("localhost", 1234));
    assertEquals("Incorrect return code:", Nfs3Status.NFS3ERR_ACCES, response1.getStatus());
    XDR xdr_req2 = new XDR();
    MKDIR3Request req2 = new MKDIR3Request(handle, "fubar2", new SetAttr3());
    req2.serialize(xdr_req2);
    // Attempt to mkdir by a privileged user should pass.
    MKDIR3Response response2 = nfsd.mkdir(xdr_req2.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
    assertEquals("Incorrect return code:", Nfs3Status.NFS3_OK, response2.getStatus());
}
Also used : SetAttr3(org.apache.hadoop.nfs.nfs3.request.SetAttr3) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) InetSocketAddress(java.net.InetSocketAddress) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) XDR(org.apache.hadoop.oncrpc.XDR) MKDIR3Response(org.apache.hadoop.nfs.nfs3.response.MKDIR3Response) MKDIR3Request(org.apache.hadoop.nfs.nfs3.request.MKDIR3Request) Test(org.junit.Test)

Example 64 with FileHandle

use of org.apache.hadoop.nfs.nfs3.FileHandle in project hadoop by apache.

the class TestWrites method testAlterWriteRequest.

@Test
public void testAlterWriteRequest() throws IOException {
    int len = 20;
    byte[] data = new byte[len];
    ByteBuffer buffer = ByteBuffer.wrap(data);
    for (int i = 0; i < len; i++) {
        buffer.put((byte) i);
    }
    buffer.flip();
    int originalCount = buffer.array().length;
    WRITE3Request request = new WRITE3Request(new FileHandle(), 0, data.length, WriteStableHow.UNSTABLE, buffer);
    WriteCtx writeCtx1 = new WriteCtx(request.getHandle(), request.getOffset(), request.getCount(), WriteCtx.INVALID_ORIGINAL_COUNT, request.getStableHow(), request.getData(), null, 1, false, WriteCtx.DataState.NO_DUMP);
    Assert.assertTrue(writeCtx1.getData().array().length == originalCount);
    // Now change the write request
    OpenFileCtx.alterWriteRequest(request, 12);
    WriteCtx writeCtx2 = new WriteCtx(request.getHandle(), request.getOffset(), request.getCount(), originalCount, request.getStableHow(), request.getData(), null, 2, false, WriteCtx.DataState.NO_DUMP);
    ByteBuffer appendedData = writeCtx2.getData();
    int position = appendedData.position();
    int limit = appendedData.limit();
    Assert.assertTrue(position == 12);
    Assert.assertTrue(limit - position == 8);
    Assert.assertTrue(appendedData.get(position) == (byte) 12);
    Assert.assertTrue(appendedData.get(position + 1) == (byte) 13);
    Assert.assertTrue(appendedData.get(position + 2) == (byte) 14);
    Assert.assertTrue(appendedData.get(position + 7) == (byte) 19);
    // Test current file write offset is at boundaries
    buffer.position(0);
    request = new WRITE3Request(new FileHandle(), 0, data.length, WriteStableHow.UNSTABLE, buffer);
    OpenFileCtx.alterWriteRequest(request, 1);
    WriteCtx writeCtx3 = new WriteCtx(request.getHandle(), request.getOffset(), request.getCount(), originalCount, request.getStableHow(), request.getData(), null, 2, false, WriteCtx.DataState.NO_DUMP);
    appendedData = writeCtx3.getData();
    position = appendedData.position();
    limit = appendedData.limit();
    Assert.assertTrue(position == 1);
    Assert.assertTrue(limit - position == 19);
    Assert.assertTrue(appendedData.get(position) == (byte) 1);
    Assert.assertTrue(appendedData.get(position + 18) == (byte) 19);
    // Reset buffer position before test another boundary
    buffer.position(0);
    request = new WRITE3Request(new FileHandle(), 0, data.length, WriteStableHow.UNSTABLE, buffer);
    OpenFileCtx.alterWriteRequest(request, 19);
    WriteCtx writeCtx4 = new WriteCtx(request.getHandle(), request.getOffset(), request.getCount(), originalCount, request.getStableHow(), request.getData(), null, 2, false, WriteCtx.DataState.NO_DUMP);
    appendedData = writeCtx4.getData();
    position = appendedData.position();
    limit = appendedData.limit();
    Assert.assertTrue(position == 19);
    Assert.assertTrue(limit - position == 1);
    Assert.assertTrue(appendedData.get(position) == (byte) 19);
}
Also used : FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) WRITE3Request(org.apache.hadoop.nfs.nfs3.request.WRITE3Request) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 65 with FileHandle

use of org.apache.hadoop.nfs.nfs3.FileHandle in project hadoop by apache.

the class TestWrites method testCheckCommitFromRead.

@Test
public // COMMIT_INACTIVE_WITH_PENDING_WRITE, COMMIT_ERROR, and COMMIT_DO_SYNC.
void testCheckCommitFromRead() throws IOException {
    DFSClient dfsClient = Mockito.mock(DFSClient.class);
    Nfs3FileAttributes attr = new Nfs3FileAttributes();
    HdfsDataOutputStream fos = Mockito.mock(HdfsDataOutputStream.class);
    Mockito.when(fos.getPos()).thenReturn((long) 0);
    NfsConfiguration config = new NfsConfiguration();
    config.setBoolean(NfsConfigKeys.LARGE_FILE_UPLOAD, false);
    OpenFileCtx ctx = new OpenFileCtx(fos, attr, "/dumpFilePath", dfsClient, new ShellBasedIdMapping(config), false, config);
    // fake handle for "/dumpFilePath"
    FileHandle h = new FileHandle(1);
    COMMIT_STATUS ret;
    WriteManager wm = new WriteManager(new ShellBasedIdMapping(config), config, false);
    assertTrue(wm.addOpenFileStream(h, ctx));
    // Test inactive open file context
    ctx.setActiveStatusForTest(false);
    Channel ch = Mockito.mock(Channel.class);
    ret = ctx.checkCommit(dfsClient, 0, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_INACTIVE_CTX, ret);
    assertEquals(Nfs3Status.NFS3_OK, wm.commitBeforeRead(dfsClient, h, 0));
    ctx.getPendingWritesForTest().put(new OffsetRange(10, 15), new WriteCtx(null, 0, 0, 0, null, null, null, 0, false, null));
    ret = ctx.checkCommit(dfsClient, 0, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_INACTIVE_WITH_PENDING_WRITE, ret);
    assertEquals(Nfs3Status.NFS3ERR_IO, wm.commitBeforeRead(dfsClient, h, 0));
    // Test request with non zero commit offset
    ctx.setActiveStatusForTest(true);
    Mockito.when(fos.getPos()).thenReturn((long) 10);
    ctx.setNextOffsetForTest((long) 10);
    COMMIT_STATUS status = ctx.checkCommitInternal(5, ch, 1, attr, false);
    assertEquals(COMMIT_STATUS.COMMIT_DO_SYNC, status);
    // Do_SYNC state will be updated to FINISHED after data sync
    ret = ctx.checkCommit(dfsClient, 5, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_FINISHED, ret);
    assertEquals(Nfs3Status.NFS3_OK, wm.commitBeforeRead(dfsClient, h, 5));
    status = ctx.checkCommitInternal(10, ch, 1, attr, true);
    assertTrue(status == COMMIT_STATUS.COMMIT_DO_SYNC);
    ret = ctx.checkCommit(dfsClient, 10, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_FINISHED, ret);
    assertEquals(Nfs3Status.NFS3_OK, wm.commitBeforeRead(dfsClient, h, 10));
    ConcurrentNavigableMap<Long, CommitCtx> commits = ctx.getPendingCommitsForTest();
    assertTrue(commits.size() == 0);
    ret = ctx.checkCommit(dfsClient, 11, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_WAIT, ret);
    // commit triggered by read doesn't wait
    assertEquals(0, commits.size());
    assertEquals(Nfs3Status.NFS3ERR_JUKEBOX, wm.commitBeforeRead(dfsClient, h, 11));
    // Test request with zero commit offset
    // There is one pending write [5,10]
    ret = ctx.checkCommit(dfsClient, 0, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_WAIT, ret);
    assertEquals(0, commits.size());
    assertEquals(Nfs3Status.NFS3ERR_JUKEBOX, wm.commitBeforeRead(dfsClient, h, 0));
    // Empty pending writes
    ctx.getPendingWritesForTest().remove(new OffsetRange(10, 15));
    ret = ctx.checkCommit(dfsClient, 0, ch, 1, attr, true);
    assertEquals(COMMIT_STATUS.COMMIT_FINISHED, ret);
    assertEquals(Nfs3Status.NFS3_OK, wm.commitBeforeRead(dfsClient, h, 0));
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) CommitCtx(org.apache.hadoop.hdfs.nfs.nfs3.OpenFileCtx.CommitCtx) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) Channel(org.jboss.netty.channel.Channel) NfsConfiguration(org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration) COMMIT_STATUS(org.apache.hadoop.hdfs.nfs.nfs3.OpenFileCtx.COMMIT_STATUS) ShellBasedIdMapping(org.apache.hadoop.security.ShellBasedIdMapping) HdfsDataOutputStream(org.apache.hadoop.hdfs.client.HdfsDataOutputStream) Test(org.junit.Test)

Aggregations

FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)79 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)34 XDR (org.apache.hadoop.oncrpc.XDR)34 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)31 Test (org.junit.Test)31 InetSocketAddress (java.net.InetSocketAddress)28 DFSClient (org.apache.hadoop.hdfs.DFSClient)26 IOException (java.io.IOException)24 VisibleForTesting (com.google.common.annotations.VisibleForTesting)19 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)12 SetAttr3 (org.apache.hadoop.nfs.nfs3.request.SetAttr3)11 WRITE3Request (org.apache.hadoop.nfs.nfs3.request.WRITE3Request)8 NfsConfiguration (org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration)7 HdfsDataOutputStream (org.apache.hadoop.hdfs.client.HdfsDataOutputStream)6 CREATE3Request (org.apache.hadoop.nfs.nfs3.request.CREATE3Request)6 READ3Request (org.apache.hadoop.nfs.nfs3.request.READ3Request)5 CREATE3Response (org.apache.hadoop.nfs.nfs3.response.CREATE3Response)5 READ3Response (org.apache.hadoop.nfs.nfs3.response.READ3Response)5 WRITE3Response (org.apache.hadoop.nfs.nfs3.response.WRITE3Response)5 VerifierNone (org.apache.hadoop.oncrpc.security.VerifierNone)5