Search in sources :

Example 11 with SetAttr3

use of org.apache.hadoop.nfs.nfs3.request.SetAttr3 in project hadoop by apache.

the class RpcProgramNfs3 method setattrInternal.

// Set attribute, don't support setting "size". For file/dir creation, mode is
// set during creation and setMode should be false here.
private void setattrInternal(DFSClient dfsClient, String fileIdPath, SetAttr3 newAttr, boolean setMode) throws IOException {
    EnumSet<SetAttrField> updateFields = newAttr.getUpdateFields();
    if (setMode && updateFields.contains(SetAttrField.MODE)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("set new mode: " + newAttr.getMode());
        }
        dfsClient.setPermission(fileIdPath, new FsPermission((short) (newAttr.getMode())));
    }
    if (updateFields.contains(SetAttrField.UID) || updateFields.contains(SetAttrField.GID)) {
        String uname = updateFields.contains(SetAttrField.UID) ? iug.getUserName(newAttr.getUid(), IdMappingConstant.UNKNOWN_USER) : null;
        String gname = updateFields.contains(SetAttrField.GID) ? iug.getGroupName(newAttr.getGid(), IdMappingConstant.UNKNOWN_GROUP) : null;
        dfsClient.setOwner(fileIdPath, uname, gname);
    }
    long atime = updateFields.contains(SetAttrField.ATIME) ? newAttr.getAtime().getMilliSeconds() : -1;
    long mtime = updateFields.contains(SetAttrField.MTIME) ? newAttr.getMtime().getMilliSeconds() : -1;
    if (atime != -1 || mtime != -1) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("set atime: " + +atime + " mtime: " + mtime);
        }
        dfsClient.setTimes(fileIdPath, mtime, atime);
    }
}
Also used : FsPermission(org.apache.hadoop.fs.permission.FsPermission) SetAttrField(org.apache.hadoop.nfs.nfs3.request.SetAttr3.SetAttrField)

Example 12 with SetAttr3

use of org.apache.hadoop.nfs.nfs3.request.SetAttr3 in project hadoop by apache.

the class RpcProgramNfs3 method mkdir.

@VisibleForTesting
MKDIR3Response mkdir(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    MKDIR3Response response = new MKDIR3Response(Nfs3Status.NFS3_OK);
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    MKDIR3Request request;
    try {
        request = MKDIR3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid MKDIR request");
        return new MKDIR3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle dirHandle = request.getHandle();
    String fileName = request.getName();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS MKDIR dirId: " + dirHandle.getFileId() + " filename: " + fileName + " client: " + remoteAddress);
    }
    if (request.getObjAttr().getUpdateFields().contains(SetAttrField.SIZE)) {
        LOG.error("Setting file size is not supported when mkdir: " + fileName + " in dirHandle" + dirHandle);
        return new MKDIR3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    String dirFileIdPath = Nfs3Utils.getFileIdPath(dirHandle);
    Nfs3FileAttributes preOpDirAttr = null;
    Nfs3FileAttributes postOpDirAttr = null;
    Nfs3FileAttributes postOpObjAttr = null;
    FileHandle objFileHandle = null;
    try {
        preOpDirAttr = Nfs3Utils.getFileAttr(dfsClient, dirFileIdPath, iug);
        if (preOpDirAttr == null) {
            LOG.info("Can't get path for dir fileId: " + dirHandle.getFileId());
            return new MKDIR3Response(Nfs3Status.NFS3ERR_STALE);
        }
        if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
            return new MKDIR3Response(Nfs3Status.NFS3ERR_ACCES, null, preOpDirAttr, new WccData(Nfs3Utils.getWccAttr(preOpDirAttr), preOpDirAttr));
        }
        final String fileIdPath = dirFileIdPath + "/" + fileName;
        SetAttr3 setAttr3 = request.getObjAttr();
        FsPermission permission = setAttr3.getUpdateFields().contains(SetAttrField.MODE) ? new FsPermission((short) setAttr3.getMode()) : FsPermission.getDefault().applyUMask(umask);
        if (!dfsClient.mkdirs(fileIdPath, permission, false)) {
            WccData dirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(preOpDirAttr), dfsClient, dirFileIdPath, iug);
            return new MKDIR3Response(Nfs3Status.NFS3ERR_IO, null, null, dirWcc);
        }
        // Set group if it's not specified in the request.
        if (!setAttr3.getUpdateFields().contains(SetAttrField.GID)) {
            setAttr3.getUpdateFields().add(SetAttrField.GID);
            setAttr3.setGid(securityHandler.getGid());
        }
        setattrInternal(dfsClient, fileIdPath, setAttr3, false);
        postOpObjAttr = Nfs3Utils.getFileAttr(dfsClient, fileIdPath, iug);
        objFileHandle = new FileHandle(postOpObjAttr.getFileId());
        WccData dirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(preOpDirAttr), dfsClient, dirFileIdPath, iug);
        return new MKDIR3Response(Nfs3Status.NFS3_OK, new FileHandle(postOpObjAttr.getFileId()), postOpObjAttr, dirWcc);
    } catch (IOException e) {
        LOG.warn("Exception ", e);
        // Try to return correct WccData
        if (postOpDirAttr == null) {
            try {
                postOpDirAttr = Nfs3Utils.getFileAttr(dfsClient, dirFileIdPath, iug);
            } catch (IOException e1) {
                LOG.info("Can't get postOpDirAttr for " + dirFileIdPath, e);
            }
        }
        WccData dirWcc = new WccData(Nfs3Utils.getWccAttr(preOpDirAttr), postOpDirAttr);
        int status = mapErrorStatus(e);
        return new MKDIR3Response(status, objFileHandle, postOpObjAttr, dirWcc);
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) SetAttr3(org.apache.hadoop.nfs.nfs3.request.SetAttr3) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) IOException(java.io.IOException) FsPermission(org.apache.hadoop.fs.permission.FsPermission) MKDIR3Response(org.apache.hadoop.nfs.nfs3.response.MKDIR3Response) MKDIR3Request(org.apache.hadoop.nfs.nfs3.request.MKDIR3Request) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with SetAttr3

use of org.apache.hadoop.nfs.nfs3.request.SetAttr3 in project hadoop by apache.

the class TestOutOfOrderWrite method create.

static XDR create() {
    XDR request = new XDR();
    RpcCall.getInstance(0x8000004c, Nfs3Constant.PROGRAM, Nfs3Constant.VERSION, Nfs3Constant.NFSPROC3.CREATE.getValue(), new CredentialsNone(), new VerifierNone()).write(request);
    SetAttr3 objAttr = new SetAttr3();
    CREATE3Request createReq = new CREATE3Request(new FileHandle("/"), "out-of-order-write" + System.currentTimeMillis(), 0, objAttr, 0);
    createReq.serialize(request);
    return request;
}
Also used : SetAttr3(org.apache.hadoop.nfs.nfs3.request.SetAttr3) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) CredentialsNone(org.apache.hadoop.oncrpc.security.CredentialsNone) CREATE3Request(org.apache.hadoop.nfs.nfs3.request.CREATE3Request)

Example 14 with SetAttr3

use of org.apache.hadoop.nfs.nfs3.request.SetAttr3 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 15 with SetAttr3

use of org.apache.hadoop.nfs.nfs3.request.SetAttr3 in project hadoop by apache.

the class TestWrites method testWriteStableHow.

@Test
public void testWriteStableHow() throws IOException, InterruptedException {
    NfsConfiguration config = new NfsConfiguration();
    DFSClient client = null;
    MiniDFSCluster cluster = null;
    RpcProgramNfs3 nfsd;
    SecurityHandler securityHandler = Mockito.mock(SecurityHandler.class);
    Mockito.when(securityHandler.getUser()).thenReturn(System.getProperty("user.name"));
    String currentUser = System.getProperty("user.name");
    config.set(DefaultImpersonationProvider.getTestProvider().getProxySuperuserGroupConfKey(currentUser), "*");
    config.set(DefaultImpersonationProvider.getTestProvider().getProxySuperuserIpConfKey(currentUser), "*");
    ProxyUsers.refreshSuperUserGroupsConfiguration(config);
    try {
        cluster = new MiniDFSCluster.Builder(config).numDataNodes(1).build();
        cluster.waitActive();
        client = new DFSClient(DFSUtilClient.getNNAddress(config), config);
        // Use emphral port in case tests are running in parallel
        config.setInt("nfs3.mountd.port", 0);
        config.setInt("nfs3.server.port", 0);
        // Start nfs
        Nfs3 nfs3 = new Nfs3(config);
        nfs3.startServiceInternal(false);
        nfsd = (RpcProgramNfs3) nfs3.getRpcProgram();
        HdfsFileStatus status = client.getFileInfo("/");
        FileHandle rootHandle = new FileHandle(status.getFileId());
        // Create file1
        CREATE3Request createReq = new CREATE3Request(rootHandle, "file1", Nfs3Constant.CREATE_UNCHECKED, new SetAttr3(), 0);
        XDR createXdr = new XDR();
        createReq.serialize(createXdr);
        CREATE3Response createRsp = nfsd.create(createXdr.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
        FileHandle handle = createRsp.getObjHandle();
        // Test DATA_SYNC
        byte[] buffer = new byte[10];
        for (int i = 0; i < 10; i++) {
            buffer[i] = (byte) i;
        }
        WRITE3Request writeReq = new WRITE3Request(handle, 0, 10, WriteStableHow.DATA_SYNC, ByteBuffer.wrap(buffer));
        XDR writeXdr = new XDR();
        writeReq.serialize(writeXdr);
        nfsd.write(writeXdr.asReadOnlyWrap(), null, 1, securityHandler, new InetSocketAddress("localhost", 1234));
        waitWrite(nfsd, handle, 60000);
        // Readback
        READ3Request readReq = new READ3Request(handle, 0, 10);
        XDR readXdr = new XDR();
        readReq.serialize(readXdr);
        READ3Response readRsp = nfsd.read(readXdr.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
        assertTrue(Arrays.equals(buffer, readRsp.getData().array()));
        // Test FILE_SYNC
        // Create file2
        CREATE3Request createReq2 = new CREATE3Request(rootHandle, "file2", Nfs3Constant.CREATE_UNCHECKED, new SetAttr3(), 0);
        XDR createXdr2 = new XDR();
        createReq2.serialize(createXdr2);
        CREATE3Response createRsp2 = nfsd.create(createXdr2.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
        FileHandle handle2 = createRsp2.getObjHandle();
        WRITE3Request writeReq2 = new WRITE3Request(handle2, 0, 10, WriteStableHow.FILE_SYNC, ByteBuffer.wrap(buffer));
        XDR writeXdr2 = new XDR();
        writeReq2.serialize(writeXdr2);
        nfsd.write(writeXdr2.asReadOnlyWrap(), null, 1, securityHandler, new InetSocketAddress("localhost", 1234));
        waitWrite(nfsd, handle2, 60000);
        // Readback
        READ3Request readReq2 = new READ3Request(handle2, 0, 10);
        XDR readXdr2 = new XDR();
        readReq2.serialize(readXdr2);
        READ3Response readRsp2 = nfsd.read(readXdr2.asReadOnlyWrap(), securityHandler, new InetSocketAddress("localhost", 1234));
        assertTrue(Arrays.equals(buffer, readRsp2.getData().array()));
        // FILE_SYNC should sync the file size
        status = client.getFileInfo("/file2");
        assertTrue(status.getLen() == 10);
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) SecurityHandler(org.apache.hadoop.oncrpc.security.SecurityHandler) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) InetSocketAddress(java.net.InetSocketAddress) XDR(org.apache.hadoop.oncrpc.XDR) WRITE3Request(org.apache.hadoop.nfs.nfs3.request.WRITE3Request) NfsConfiguration(org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration) SetAttr3(org.apache.hadoop.nfs.nfs3.request.SetAttr3) READ3Request(org.apache.hadoop.nfs.nfs3.request.READ3Request) CREATE3Response(org.apache.hadoop.nfs.nfs3.response.CREATE3Response) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) CREATE3Request(org.apache.hadoop.nfs.nfs3.request.CREATE3Request) READ3Response(org.apache.hadoop.nfs.nfs3.response.READ3Response) Test(org.junit.Test)

Aggregations

FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)16 SetAttr3 (org.apache.hadoop.nfs.nfs3.request.SetAttr3)11 XDR (org.apache.hadoop.oncrpc.XDR)9 InetSocketAddress (java.net.InetSocketAddress)8 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)8 Test (org.junit.Test)8 CREATE3Request (org.apache.hadoop.nfs.nfs3.request.CREATE3Request)6 DFSClient (org.apache.hadoop.hdfs.DFSClient)5 CREATE3Response (org.apache.hadoop.nfs.nfs3.response.CREATE3Response)5 IOException (java.io.IOException)3 FsPermission (org.apache.hadoop.fs.permission.FsPermission)3 MiniDFSCluster (org.apache.hadoop.hdfs.MiniDFSCluster)3 NfsConfiguration (org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration)3 READ3Request (org.apache.hadoop.nfs.nfs3.request.READ3Request)3 WRITE3Request (org.apache.hadoop.nfs.nfs3.request.WRITE3Request)3 READ3Response (org.apache.hadoop.nfs.nfs3.response.READ3Response)3 SecurityHandler (org.apache.hadoop.oncrpc.security.SecurityHandler)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)2 MKDIR3Request (org.apache.hadoop.nfs.nfs3.request.MKDIR3Request)2