Search in sources :

Example 1 with SETATTR3Response

use of org.apache.hadoop.nfs.nfs3.response.SETATTR3Response in project hadoop by apache.

the class RpcProgramNfs3 method setattr.

@VisibleForTesting
SETATTR3Response setattr(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    SETATTR3Response response = new SETATTR3Response(Nfs3Status.NFS3_OK);
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    SETATTR3Request request;
    try {
        request = SETATTR3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid SETATTR request");
        response.setStatus(Nfs3Status.NFS3ERR_INVAL);
        return response;
    }
    FileHandle handle = request.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS SETATTR fileId: " + handle.getFileId() + " client: " + remoteAddress);
    }
    if (request.getAttr().getUpdateFields().contains(SetAttrField.SIZE)) {
        LOG.error("Setting file size is not supported when setattr, fileId: " + handle.getFileId());
        response.setStatus(Nfs3Status.NFS3ERR_INVAL);
        return response;
    }
    String fileIdPath = Nfs3Utils.getFileIdPath(handle);
    Nfs3FileAttributes preOpAttr = null;
    try {
        preOpAttr = Nfs3Utils.getFileAttr(dfsClient, fileIdPath, iug);
        if (preOpAttr == null) {
            LOG.info("Can't get path for fileId: " + handle.getFileId());
            response.setStatus(Nfs3Status.NFS3ERR_STALE);
            return response;
        }
        WccAttr preOpWcc = Nfs3Utils.getWccAttr(preOpAttr);
        if (request.isCheck()) {
            if (!preOpAttr.getCtime().equals(request.getCtime())) {
                WccData wccData = new WccData(preOpWcc, preOpAttr);
                return new SETATTR3Response(Nfs3Status.NFS3ERR_NOT_SYNC, wccData);
            }
        }
        // check the write access privilege
        if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
            return new SETATTR3Response(Nfs3Status.NFS3ERR_ACCES, new WccData(preOpWcc, preOpAttr));
        }
        setattrInternal(dfsClient, fileIdPath, request.getAttr(), true);
        Nfs3FileAttributes postOpAttr = Nfs3Utils.getFileAttr(dfsClient, fileIdPath, iug);
        WccData wccData = new WccData(preOpWcc, postOpAttr);
        return new SETATTR3Response(Nfs3Status.NFS3_OK, wccData);
    } catch (IOException e) {
        LOG.warn("Exception ", e);
        WccData wccData = null;
        try {
            wccData = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(preOpAttr), dfsClient, fileIdPath, iug);
        } catch (IOException e1) {
            LOG.info("Can't get postOpAttr for fileIdPath: " + fileIdPath, e1);
        }
        int status = mapErrorStatus(e);
        return new SETATTR3Response(status, wccData);
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) WccAttr(org.apache.hadoop.nfs.nfs3.response.WccAttr) SETATTR3Response(org.apache.hadoop.nfs.nfs3.response.SETATTR3Response) SETATTR3Request(org.apache.hadoop.nfs.nfs3.request.SETATTR3Request) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with SETATTR3Response

use of org.apache.hadoop.nfs.nfs3.response.SETATTR3Response in project hadoop by apache.

the class TestRpcProgramNfs3 method testSetattr.

@Test(timeout = 60000)
public void testSetattr() throws Exception {
    HdfsFileStatus status = nn.getRpcServer().getFileInfo(testdir);
    long dirId = status.getFileId();
    XDR xdr_req = new XDR();
    FileHandle handle = new FileHandle(dirId);
    SetAttr3 symAttr = new SetAttr3(0, 1, 0, 0, null, null, EnumSet.of(SetAttrField.UID));
    SETATTR3Request req = new SETATTR3Request(handle, symAttr, false, null);
    req.serialize(xdr_req);
    // Attempt by an unprivileged user should fail.
    SETATTR3Response response1 = nfsd.setattr(xdr_req.asReadOnlyWrap(), securityHandlerUnpriviledged, new InetSocketAddress("localhost", 1234));
    assertEquals("Incorrect return code", Nfs3Status.NFS3ERR_ACCES, response1.getStatus());
    // Attempt by a priviledged user should pass.
    SETATTR3Response response2 = nfsd.setattr(xdr_req.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) SETATTR3Request(org.apache.hadoop.nfs.nfs3.request.SETATTR3Request) SETATTR3Response(org.apache.hadoop.nfs.nfs3.response.SETATTR3Response) Test(org.junit.Test)

Aggregations

FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)2 SETATTR3Request (org.apache.hadoop.nfs.nfs3.request.SETATTR3Request)2 SETATTR3Response (org.apache.hadoop.nfs.nfs3.response.SETATTR3Response)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 DFSClient (org.apache.hadoop.hdfs.DFSClient)1 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)1 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)1 SetAttr3 (org.apache.hadoop.nfs.nfs3.request.SetAttr3)1 WccAttr (org.apache.hadoop.nfs.nfs3.response.WccAttr)1 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)1 XDR (org.apache.hadoop.oncrpc.XDR)1 Test (org.junit.Test)1