Search in sources :

Example 36 with Nfs3FileAttributes

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

the class RpcProgramNfs3 method create.

@VisibleForTesting
CREATE3Response create(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    CREATE3Response response = new CREATE3Response(Nfs3Status.NFS3_OK);
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    CREATE3Request request;
    try {
        request = CREATE3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid CREATE request");
        return new CREATE3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle dirHandle = request.getHandle();
    String fileName = request.getName();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS CREATE dir fileId: " + dirHandle.getFileId() + " filename: " + fileName + " client: " + remoteAddress);
    }
    int createMode = request.getMode();
    if ((createMode != Nfs3Constant.CREATE_EXCLUSIVE) && request.getObjAttr().getUpdateFields().contains(SetAttrField.SIZE) && request.getObjAttr().getSize() != 0) {
        LOG.error("Setting file size is not supported when creating file: " + fileName + " dir fileId: " + dirHandle.getFileId());
        return new CREATE3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    HdfsDataOutputStream fos = null;
    String dirFileIdPath = Nfs3Utils.getFileIdPath(dirHandle);
    Nfs3FileAttributes preOpDirAttr = null;
    Nfs3FileAttributes postOpObjAttr = null;
    FileHandle fileHandle = null;
    WccData dirWcc = null;
    try {
        preOpDirAttr = Nfs3Utils.getFileAttr(dfsClient, dirFileIdPath, iug);
        if (preOpDirAttr == null) {
            LOG.error("Can't get path for dirHandle: " + dirHandle);
            return new CREATE3Response(Nfs3Status.NFS3ERR_STALE);
        }
        if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
            return new CREATE3Response(Nfs3Status.NFS3ERR_ACCES, null, preOpDirAttr, new WccData(Nfs3Utils.getWccAttr(preOpDirAttr), preOpDirAttr));
        }
        String fileIdPath = Nfs3Utils.getFileIdPath(dirHandle) + "/" + fileName;
        SetAttr3 setAttr3 = request.getObjAttr();
        assert (setAttr3 != null);
        FsPermission permission = setAttr3.getUpdateFields().contains(SetAttrField.MODE) ? new FsPermission((short) setAttr3.getMode()) : FsPermission.getDefault().applyUMask(umask);
        EnumSet<CreateFlag> flag = (createMode != Nfs3Constant.CREATE_EXCLUSIVE) ? EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE) : EnumSet.of(CreateFlag.CREATE);
        fos = dfsClient.createWrappedOutputStream(dfsClient.create(fileIdPath, permission, flag, false, replication, blockSize, null, bufferSize, null), null);
        if ((createMode == Nfs3Constant.CREATE_UNCHECKED) || (createMode == Nfs3Constant.CREATE_GUARDED)) {
            // 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);
        dirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(preOpDirAttr), dfsClient, dirFileIdPath, iug);
        // Add open stream
        OpenFileCtx openFileCtx = new OpenFileCtx(fos, postOpObjAttr, writeDumpDir + "/" + postOpObjAttr.getFileId(), dfsClient, iug, aixCompatMode, config);
        fileHandle = new FileHandle(postOpObjAttr.getFileId());
        if (!writeManager.addOpenFileStream(fileHandle, openFileCtx)) {
            LOG.warn("Can't add more stream, close it." + " Future write will become append");
            fos.close();
            fos = null;
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Opened stream for file: " + fileName + ", fileId: " + fileHandle.getFileId());
            }
        }
    } catch (IOException e) {
        LOG.error("Exception", e);
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                LOG.error("Can't close stream for dirFileId: " + dirHandle.getFileId() + " filename: " + fileName, e1);
            }
        }
        if (dirWcc == null) {
            try {
                dirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(preOpDirAttr), dfsClient, dirFileIdPath, iug);
            } catch (IOException e1) {
                LOG.error("Can't get postOpDirAttr for dirFileId: " + dirHandle.getFileId(), e1);
            }
        }
        int status = mapErrorStatus(e);
        return new CREATE3Response(status, fileHandle, postOpObjAttr, dirWcc);
    }
    return new CREATE3Response(Nfs3Status.NFS3_OK, fileHandle, postOpObjAttr, dirWcc);
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) CreateFlag(org.apache.hadoop.fs.CreateFlag) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) IOException(java.io.IOException) SetAttr3(org.apache.hadoop.nfs.nfs3.request.SetAttr3) CREATE3Response(org.apache.hadoop.nfs.nfs3.response.CREATE3Response) HdfsDataOutputStream(org.apache.hadoop.hdfs.client.HdfsDataOutputStream) FsPermission(org.apache.hadoop.fs.permission.FsPermission) CREATE3Request(org.apache.hadoop.nfs.nfs3.request.CREATE3Request) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 37 with Nfs3FileAttributes

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

the class RpcProgramNfs3 method fsinfo.

@VisibleForTesting
FSINFO3Response fsinfo(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    FSINFO3Response response = new FSINFO3Response(Nfs3Status.NFS3_OK);
    if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_ONLY)) {
        response.setStatus(Nfs3Status.NFS3ERR_ACCES);
        return response;
    }
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    FSINFO3Request request;
    try {
        request = FSINFO3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid FSINFO request");
        return new FSINFO3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle handle = request.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS FSINFO fileId: " + handle.getFileId() + " client: " + remoteAddress);
    }
    try {
        int rtmax = config.getInt(NfsConfigKeys.DFS_NFS_MAX_READ_TRANSFER_SIZE_KEY, NfsConfigKeys.DFS_NFS_MAX_READ_TRANSFER_SIZE_DEFAULT);
        int wtmax = config.getInt(NfsConfigKeys.DFS_NFS_MAX_WRITE_TRANSFER_SIZE_KEY, NfsConfigKeys.DFS_NFS_MAX_WRITE_TRANSFER_SIZE_DEFAULT);
        int dtperf = config.getInt(NfsConfigKeys.DFS_NFS_MAX_READDIR_TRANSFER_SIZE_KEY, NfsConfigKeys.DFS_NFS_MAX_READDIR_TRANSFER_SIZE_DEFAULT);
        Nfs3FileAttributes attrs = Nfs3Utils.getFileAttr(dfsClient, Nfs3Utils.getFileIdPath(handle), iug);
        if (attrs == null) {
            LOG.info("Can't get path for fileId: " + handle.getFileId());
            return new FSINFO3Response(Nfs3Status.NFS3ERR_STALE);
        }
        int fsProperty = Nfs3Constant.FSF3_CANSETTIME | Nfs3Constant.FSF3_HOMOGENEOUS;
        return new FSINFO3Response(Nfs3Status.NFS3_OK, attrs, rtmax, rtmax, 1, wtmax, wtmax, 1, dtperf, Long.MAX_VALUE, new NfsTime(1), fsProperty);
    } catch (IOException e) {
        LOG.warn("Exception ", e);
        int status = mapErrorStatus(e);
        return new FSINFO3Response(status);
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) FSINFO3Response(org.apache.hadoop.nfs.nfs3.response.FSINFO3Response) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) NfsTime(org.apache.hadoop.nfs.NfsTime) FSINFO3Request(org.apache.hadoop.nfs.nfs3.request.FSINFO3Request) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 38 with Nfs3FileAttributes

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

the class RpcProgramNfs3 method rename.

@VisibleForTesting
RENAME3Response rename(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    RENAME3Response response = new RENAME3Response(Nfs3Status.NFS3_OK);
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    RENAME3Request request = null;
    try {
        request = RENAME3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid RENAME request");
        return new RENAME3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle fromHandle = request.getFromDirHandle();
    String fromName = request.getFromName();
    FileHandle toHandle = request.getToDirHandle();
    String toName = request.getToName();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS RENAME from: " + fromHandle.getFileId() + "/" + fromName + " to: " + toHandle.getFileId() + "/" + toName + " client: " + remoteAddress);
    }
    String fromDirFileIdPath = Nfs3Utils.getFileIdPath(fromHandle);
    String toDirFileIdPath = Nfs3Utils.getFileIdPath(toHandle);
    Nfs3FileAttributes fromPreOpAttr = null;
    Nfs3FileAttributes toPreOpAttr = null;
    WccData fromDirWcc = null;
    WccData toDirWcc = null;
    try {
        fromPreOpAttr = Nfs3Utils.getFileAttr(dfsClient, fromDirFileIdPath, iug);
        if (fromPreOpAttr == null) {
            LOG.info("Can't get path for fromHandle fileId: " + fromHandle.getFileId());
            return new RENAME3Response(Nfs3Status.NFS3ERR_STALE);
        }
        toPreOpAttr = Nfs3Utils.getFileAttr(dfsClient, toDirFileIdPath, iug);
        if (toPreOpAttr == null) {
            LOG.info("Can't get path for toHandle fileId: " + toHandle.getFileId());
            return new RENAME3Response(Nfs3Status.NFS3ERR_STALE);
        }
        if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_WRITE)) {
            WccData fromWcc = new WccData(Nfs3Utils.getWccAttr(fromPreOpAttr), fromPreOpAttr);
            WccData toWcc = new WccData(Nfs3Utils.getWccAttr(toPreOpAttr), toPreOpAttr);
            return new RENAME3Response(Nfs3Status.NFS3ERR_ACCES, fromWcc, toWcc);
        }
        String src = fromDirFileIdPath + "/" + fromName;
        String dst = toDirFileIdPath + "/" + toName;
        dfsClient.rename(src, dst, Options.Rename.NONE);
        // Assemble the reply
        fromDirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(fromPreOpAttr), dfsClient, fromDirFileIdPath, iug);
        toDirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(toPreOpAttr), dfsClient, toDirFileIdPath, iug);
        return new RENAME3Response(Nfs3Status.NFS3_OK, fromDirWcc, toDirWcc);
    } catch (IOException e) {
        LOG.warn("Exception ", e);
        // Try to return correct WccData
        try {
            fromDirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(fromPreOpAttr), dfsClient, fromDirFileIdPath, iug);
            toDirWcc = Nfs3Utils.createWccData(Nfs3Utils.getWccAttr(toPreOpAttr), dfsClient, toDirFileIdPath, iug);
        } catch (IOException e1) {
            LOG.info("Can't get postOpDirAttr for " + fromDirFileIdPath + " or" + toDirFileIdPath, e1);
        }
        int status = mapErrorStatus(e);
        return new RENAME3Response(status, fromDirWcc, toDirWcc);
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) WccData(org.apache.hadoop.nfs.nfs3.response.WccData) RENAME3Request(org.apache.hadoop.nfs.nfs3.request.RENAME3Request) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) RENAME3Response(org.apache.hadoop.nfs.nfs3.response.RENAME3Response) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 39 with Nfs3FileAttributes

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

the class RpcProgramNfs3 method readlink.

@VisibleForTesting
READLINK3Response readlink(XDR xdr, SecurityHandler securityHandler, SocketAddress remoteAddress) {
    READLINK3Response response = new READLINK3Response(Nfs3Status.NFS3_OK);
    if (!checkAccessPrivilege(remoteAddress, AccessPrivilege.READ_ONLY)) {
        response.setStatus(Nfs3Status.NFS3ERR_ACCES);
        return response;
    }
    DFSClient dfsClient = clientCache.getDfsClient(securityHandler.getUser());
    if (dfsClient == null) {
        response.setStatus(Nfs3Status.NFS3ERR_SERVERFAULT);
        return response;
    }
    READLINK3Request request;
    try {
        request = READLINK3Request.deserialize(xdr);
    } catch (IOException e) {
        LOG.error("Invalid READLINK request");
        return new READLINK3Response(Nfs3Status.NFS3ERR_INVAL);
    }
    FileHandle handle = request.getHandle();
    if (LOG.isDebugEnabled()) {
        LOG.debug("NFS READLINK fileId: " + handle.getFileId() + " client: " + remoteAddress);
    }
    String fileIdPath = Nfs3Utils.getFileIdPath(handle);
    try {
        String target = dfsClient.getLinkTarget(fileIdPath);
        Nfs3FileAttributes postOpAttr = Nfs3Utils.getFileAttr(dfsClient, fileIdPath, iug);
        if (postOpAttr == null) {
            LOG.info("Can't get path for fileId: " + handle.getFileId());
            return new READLINK3Response(Nfs3Status.NFS3ERR_STALE);
        }
        if (postOpAttr.getType() != NfsFileType.NFSLNK.toValue()) {
            LOG.error("Not a symlink, fileId: " + handle.getFileId());
            return new READLINK3Response(Nfs3Status.NFS3ERR_INVAL);
        }
        if (target == null) {
            LOG.error("Symlink target should not be null, fileId: " + handle.getFileId());
            return new READLINK3Response(Nfs3Status.NFS3ERR_SERVERFAULT);
        }
        int rtmax = config.getInt(NfsConfigKeys.DFS_NFS_MAX_READ_TRANSFER_SIZE_KEY, NfsConfigKeys.DFS_NFS_MAX_READ_TRANSFER_SIZE_DEFAULT);
        if (rtmax < target.getBytes(Charset.forName("UTF-8")).length) {
            LOG.error("Link size: " + target.getBytes(Charset.forName("UTF-8")).length + " is larger than max transfer size: " + rtmax);
            return new READLINK3Response(Nfs3Status.NFS3ERR_IO, postOpAttr, new byte[0]);
        }
        return new READLINK3Response(Nfs3Status.NFS3_OK, postOpAttr, target.getBytes(Charset.forName("UTF-8")));
    } catch (IOException e) {
        LOG.warn("Readlink error: " + e.getClass(), e);
        int status = mapErrorStatus(e);
        return new READLINK3Response(status);
    }
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) READLINK3Request(org.apache.hadoop.nfs.nfs3.request.READLINK3Request) FileHandle(org.apache.hadoop.nfs.nfs3.FileHandle) Nfs3FileAttributes(org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes) READLINK3Response(org.apache.hadoop.nfs.nfs3.response.READLINK3Response) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 40 with Nfs3FileAttributes

use of org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes 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)

Aggregations

Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)50 FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)30 DFSClient (org.apache.hadoop.hdfs.DFSClient)27 IOException (java.io.IOException)22 VisibleForTesting (com.google.common.annotations.VisibleForTesting)18 WccData (org.apache.hadoop.nfs.nfs3.response.WccData)11 HdfsDataOutputStream (org.apache.hadoop.hdfs.client.HdfsDataOutputStream)10 Test (org.junit.Test)9 NfsConfiguration (org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration)8 ShellBasedIdMapping (org.apache.hadoop.security.ShellBasedIdMapping)8 COMMIT_STATUS (org.apache.hadoop.hdfs.nfs.nfs3.OpenFileCtx.COMMIT_STATUS)6 CommitCtx (org.apache.hadoop.hdfs.nfs.nfs3.OpenFileCtx.CommitCtx)5 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)5 RemoteException (org.apache.hadoop.ipc.RemoteException)4 Channel (org.jboss.netty.channel.Channel)4 ArrayList (java.util.ArrayList)2 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)2 FsPermission (org.apache.hadoop.fs.permission.FsPermission)2 DirectoryListing (org.apache.hadoop.hdfs.protocol.DirectoryListing)2 NfsTime (org.apache.hadoop.nfs.NfsTime)2