/// <summary>Set the namespace, storagespace and typespace quota for a directory.</summary> /// <remarks> /// Set the namespace, storagespace and typespace quota for a directory. /// Note: This does not support ".inodes" relative path. /// </remarks> /// <exception cref="System.IO.IOException"/> internal static void SetQuota(FSDirectory fsd, string src, long nsQuota, long ssQuota , StorageType type) { if (fsd.IsPermissionEnabled()) { FSPermissionChecker pc = fsd.GetPermissionChecker(); pc.CheckSuperuserPrivilege(); } fsd.WriteLock(); try { INodeDirectory changed = UnprotectedSetQuota(fsd, src, nsQuota, ssQuota, type); if (changed != null) { QuotaCounts q = changed.GetQuotaCounts(); if (type == null) { fsd.GetEditLog().LogSetQuota(src, q.GetNameSpace(), q.GetStorageSpace()); } else { fsd.GetEditLog().LogSetQuotaByStorageType(src, q.GetTypeSpaces().Get(type), type); } } } finally { fsd.WriteUnlock(); } }
/// <exception cref="System.IO.IOException"/> internal static bool SetReplication(FSDirectory fsd, BlockManager bm, string src, short replication) { bm.VerifyReplication(src, replication, null); bool isFile; FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); fsd.WriteLock(); try { src = fsd.ResolvePath(pc, src, pathComponents); INodesInPath iip = fsd.GetINodesInPath4Write(src); if (fsd.IsPermissionEnabled()) { fsd.CheckPathAccess(pc, iip, FsAction.Write); } short[] blockRepls = new short[2]; // 0: old, 1: new Block[] blocks = UnprotectedSetReplication(fsd, src, replication, blockRepls); isFile = blocks != null; if (isFile) { fsd.GetEditLog().LogSetReplication(src, replication); bm.SetReplication(blockRepls[0], blockRepls[1], src, blocks); } } finally { fsd.WriteUnlock(); } return(isFile); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetOwner(FSDirectory fsd, string src, string username , string group) { FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); INodesInPath iip; fsd.WriteLock(); try { src = fsd.ResolvePath(pc, src, pathComponents); iip = fsd.GetINodesInPath4Write(src); fsd.CheckOwner(pc, iip); if (!pc.IsSuperUser()) { if (username != null && !pc.GetUser().Equals(username)) { throw new AccessControlException("Non-super user cannot change owner"); } if (group != null && !pc.ContainsGroup(group)) { throw new AccessControlException("User does not belong to " + group); } } UnprotectedSetOwner(fsd, src, username, group); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogSetOwner(src, username, group); return(fsd.GetAuditFileInfo(iip)); }
/// <summary>Delete a snapshot of a snapshottable directory</summary> /// <param name="snapshotRoot">The snapshottable directory</param> /// <param name="snapshotName">The name of the to-be-deleted snapshot</param> /// <exception cref="System.IO.IOException"/> internal static INode.BlocksMapUpdateInfo DeleteSnapshot(FSDirectory fsd, SnapshotManager snapshotManager, string snapshotRoot, string snapshotName, bool logRetryCache) { INodesInPath iip = fsd.GetINodesInPath4Write(snapshotRoot); if (fsd.IsPermissionEnabled()) { FSPermissionChecker pc = fsd.GetPermissionChecker(); fsd.CheckOwner(pc, iip); } INode.BlocksMapUpdateInfo collectedBlocks = new INode.BlocksMapUpdateInfo(); ChunkedArrayList <INode> removedINodes = new ChunkedArrayList <INode>(); fsd.WriteLock(); try { snapshotManager.DeleteSnapshot(iip, snapshotName, collectedBlocks, removedINodes); fsd.RemoveFromInodeMap(removedINodes); } finally { fsd.WriteUnlock(); } removedINodes.Clear(); fsd.GetEditLog().LogDeleteSnapshot(snapshotRoot, snapshotName, logRetryCache); return(collectedBlocks); }
/// <summary>Remove a file/directory from the namespace.</summary> /// <remarks> /// Remove a file/directory from the namespace. /// <p> /// For large directories, deletion is incremental. The blocks under /// the directory are collected and deleted a small number at a time holding /// the /// <see cref="FSNamesystem"/> /// lock. /// <p> /// For small directory or file the deletion is done in one shot. /// </remarks> /// <exception cref="System.IO.IOException"/> internal static INode.BlocksMapUpdateInfo DeleteInternal(FSNamesystem fsn, string src, INodesInPath iip, bool logRetryCache) { System.Diagnostics.Debug.Assert(fsn.HasWriteLock()); if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("DIR* NameSystem.delete: " + src); } FSDirectory fsd = fsn.GetFSDirectory(); INode.BlocksMapUpdateInfo collectedBlocks = new INode.BlocksMapUpdateInfo(); IList <INode> removedINodes = new ChunkedArrayList <INode>(); long mtime = Time.Now(); // Unlink the target directory from directory tree long filesRemoved = Delete(fsd, iip, collectedBlocks, removedINodes, mtime); if (filesRemoved < 0) { return(null); } fsd.GetEditLog().LogDelete(src, mtime, logRetryCache); IncrDeletedFileCount(filesRemoved); fsn.RemoveLeasesAndINodes(src, removedINodes, true); if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("DIR* Namesystem.delete: " + src + " is removed"); } return(collectedBlocks); }
/// <summary>Set xattr for a file or directory.</summary> /// <param name="src">- path on which it sets the xattr</param> /// <param name="xAttr">- xAttr details to set</param> /// <param name="flag">- xAttrs flags</param> /// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetXAttr(FSDirectory fsd, string src, XAttr xAttr, EnumSet <XAttrSetFlag> flag, bool logRetryCache) { CheckXAttrsConfigFlag(fsd); CheckXAttrSize(fsd, xAttr); FSPermissionChecker pc = fsd.GetPermissionChecker(); XAttrPermissionFilter.CheckPermissionForApi(pc, xAttr, FSDirectory.IsReservedRawName (src)); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); src = fsd.ResolvePath(pc, src, pathComponents); IList <XAttr> xAttrs = Lists.NewArrayListWithCapacity(1); xAttrs.AddItem(xAttr); INodesInPath iip; fsd.WriteLock(); try { iip = fsd.GetINodesInPath4Write(src); CheckXAttrChangeAccess(fsd, iip, xAttr, pc); UnprotectedSetXAttrs(fsd, src, xAttrs, flag); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogSetXAttrs(src, xAttrs, logRetryCache); return(fsd.GetAuditFileInfo(iip)); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus RemoveAcl(FSDirectory fsd, string srcArg) { string src = srcArg; CheckAclsConfigFlag(fsd); FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); src = fsd.ResolvePath(pc, src, pathComponents); INodesInPath iip; fsd.WriteLock(); try { iip = fsd.GetINodesInPath4Write(src); fsd.CheckOwner(pc, iip); UnprotectedRemoveAcl(fsd, iip); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogSetAcl(src, AclFeature.EmptyEntryList); return(fsd.GetAuditFileInfo(iip)); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetAcl(FSDirectory fsd, string srcArg, IList <AclEntry > aclSpec) { string src = srcArg; CheckAclsConfigFlag(fsd); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); FSPermissionChecker pc = fsd.GetPermissionChecker(); src = fsd.ResolvePath(pc, src, pathComponents); INodesInPath iip; fsd.WriteLock(); try { iip = fsd.GetINodesInPath4Write(src); fsd.CheckOwner(pc, iip); IList <AclEntry> newAcl = UnprotectedSetAcl(fsd, src, aclSpec, false); fsd.GetEditLog().LogSetAcl(src, newAcl); } finally { fsd.WriteUnlock(); } return(fsd.GetAuditFileInfo(iip)); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus RemoveDefaultAcl(FSDirectory fsd, string srcArg) { string src = srcArg; CheckAclsConfigFlag(fsd); FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); src = fsd.ResolvePath(pc, src, pathComponents); INodesInPath iip; fsd.WriteLock(); try { iip = fsd.GetINodesInPath4Write(FSDirectory.NormalizePath(src), true); fsd.CheckOwner(pc, iip); INode inode = FSDirectory.ResolveLastINode(iip); int snapshotId = iip.GetLatestSnapshotId(); IList <AclEntry> existingAcl = AclStorage.ReadINodeLogicalAcl(inode); IList <AclEntry> newAcl = AclTransformation.FilterDefaultAclEntries(existingAcl); AclStorage.UpdateINodeAcl(inode, newAcl, snapshotId); fsd.GetEditLog().LogSetAcl(src, newAcl); } finally { fsd.WriteUnlock(); } return(fsd.GetAuditFileInfo(iip)); }
/// <exception cref="System.IO.IOException"/> internal static void DisallowSnapshot(FSDirectory fsd, SnapshotManager snapshotManager , string path) { fsd.WriteLock(); try { snapshotManager.ResetSnapshottable(path); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogDisallowSnapshot(path); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus Concat(FSDirectory fsd, string target, string[] srcs , bool logRetryCache) { Preconditions.CheckArgument(!target.IsEmpty(), "Target file name is empty"); Preconditions.CheckArgument(srcs != null && srcs.Length > 0, "No sources given"); System.Diagnostics.Debug.Assert(srcs != null); if (FSDirectory.Log.IsDebugEnabled()) { FSDirectory.Log.Debug("concat {} to {}", Arrays.ToString(srcs), target); } INodesInPath targetIIP = fsd.GetINodesInPath4Write(target); // write permission for the target FSPermissionChecker pc = null; if (fsd.IsPermissionEnabled()) { pc = fsd.GetPermissionChecker(); fsd.CheckPathAccess(pc, targetIIP, FsAction.Write); } // check the target VerifyTargetFile(fsd, target, targetIIP); // check the srcs INodeFile[] srcFiles = VerifySrcFiles(fsd, srcs, targetIIP, pc); if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("DIR* NameSystem.concat: " + Arrays.ToString(srcs) + " to " + target); } long timestamp = Time.Now(); fsd.WriteLock(); try { UnprotectedConcat(fsd, targetIIP, srcFiles, timestamp); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogConcat(target, srcs, timestamp, logRetryCache); return(fsd.GetAuditFileInfo(targetIIP)); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetTimes(FSDirectory fsd, string src, long mtime, long atime) { if (!fsd.IsAccessTimeSupported() && atime != -1) { throw new IOException("Access time for hdfs is not configured. " + " Please set " + DFSConfigKeys.DfsNamenodeAccesstimePrecisionKey + " configuration parameter." ); } FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); INodesInPath iip; fsd.WriteLock(); try { src = fsd.ResolvePath(pc, src, pathComponents); iip = fsd.GetINodesInPath4Write(src); // Write access is required to set access and modification times if (fsd.IsPermissionEnabled()) { fsd.CheckPathAccess(pc, iip, FsAction.Write); } INode inode = iip.GetLastINode(); if (inode == null) { throw new FileNotFoundException("File/Directory " + src + " does not exist."); } bool changed = UnprotectedSetTimes(fsd, inode, mtime, atime, true, iip.GetLatestSnapshotId ()); if (changed) { fsd.GetEditLog().LogTimes(src, mtime, atime); } } finally { fsd.WriteUnlock(); } return(fsd.GetAuditFileInfo(iip)); }
private static bool RenameTo(FSDirectory fsd, FSPermissionChecker pc, string src, string dst, bool logRetryCache) { // Rename does not operate on link targets // Do not resolveLink when checking permissions of src and dst // Check write access to parent of src INodesInPath srcIIP = fsd.GetINodesInPath4Write(src, false); // Note: We should not be doing this. This is move() not renameTo(). string actualDst = fsd.IsDir(dst) ? dst + Path.Separator + new Path(src).GetName( ) : dst; INodesInPath dstIIP = fsd.GetINodesInPath4Write(actualDst, false); if (fsd.IsPermissionEnabled()) { fsd.CheckPermission(pc, srcIIP, false, null, FsAction.Write, null, null, false); // Check write access to ancestor of dst fsd.CheckPermission(pc, dstIIP, false, FsAction.Write, null, null, null, false); } if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("DIR* FSDirectory.renameTo: " + src + " to " + dst); } long mtime = Time.Now(); bool stat = false; fsd.WriteLock(); try { stat = UnprotectedRenameTo(fsd, src, actualDst, srcIIP, dstIIP, mtime); } finally { fsd.WriteUnlock(); } if (stat) { fsd.GetEditLog().LogRename(src, actualDst, mtime, logRetryCache); return(true); } return(false); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetStoragePolicy(FSDirectory fsd, BlockManager bm, string src, string policyName) { if (!fsd.IsStoragePolicyEnabled()) { throw new IOException("Failed to set storage policy since " + DFSConfigKeys.DfsStoragePolicyEnabledKey + " is set to false."); } FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); INodesInPath iip; fsd.WriteLock(); try { src = FSDirectory.ResolvePath(src, pathComponents, fsd); iip = fsd.GetINodesInPath4Write(src); if (fsd.IsPermissionEnabled()) { fsd.CheckPathAccess(pc, iip, FsAction.Write); } // get the corresponding policy and make sure the policy name is valid BlockStoragePolicy policy = bm.GetStoragePolicy(policyName); if (policy == null) { throw new HadoopIllegalArgumentException("Cannot find a block policy with the name " + policyName); } UnprotectedSetStoragePolicy(fsd, bm, iip, policy.GetId()); fsd.GetEditLog().LogSetStoragePolicy(src, policy.GetId()); } finally { fsd.WriteUnlock(); } return(fsd.GetAuditFileInfo(iip)); }
/// <summary>Create a snapshot</summary> /// <param name="snapshotRoot">The directory path where the snapshot is taken</param> /// <param name="snapshotName">The name of the snapshot</param> /// <exception cref="System.IO.IOException"/> internal static string CreateSnapshot(FSDirectory fsd, SnapshotManager snapshotManager , string snapshotRoot, string snapshotName, bool logRetryCache) { INodesInPath iip = fsd.GetINodesInPath4Write(snapshotRoot); if (fsd.IsPermissionEnabled()) { FSPermissionChecker pc = fsd.GetPermissionChecker(); fsd.CheckOwner(pc, iip); } if (snapshotName == null || snapshotName.IsEmpty()) { snapshotName = Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot.GenerateDefaultSnapshotName (); } else { if (!DFSUtil.IsValidNameForComponent(snapshotName)) { throw new InvalidPathException("Invalid snapshot name: " + snapshotName); } } string snapshotPath = null; VerifySnapshotName(fsd, snapshotName, snapshotRoot); fsd.WriteLock(); try { snapshotPath = snapshotManager.CreateSnapshot(iip, snapshotRoot, snapshotName); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogCreateSnapshot(snapshotRoot, snapshotName, logRetryCache); return(snapshotPath); }
/// <seealso> /// /// <see cref="UnprotectedRenameTo(FSDirectory, string, string, INodesInPath, INodesInPath, long, BlocksMapUpdateInfo, Org.Apache.Hadoop.FS.Options.Rename[]) /// "/> /// </seealso> /// <exception cref="System.IO.IOException"/> internal static void RenameTo(FSDirectory fsd, FSPermissionChecker pc, string src , string dst, INode.BlocksMapUpdateInfo collectedBlocks, bool logRetryCache, params Options.Rename[] options) { INodesInPath srcIIP = fsd.GetINodesInPath4Write(src, false); INodesInPath dstIIP = fsd.GetINodesInPath4Write(dst, false); if (fsd.IsPermissionEnabled()) { // Rename does not operate on link targets // Do not resolveLink when checking permissions of src and dst // Check write access to parent of src fsd.CheckPermission(pc, srcIIP, false, null, FsAction.Write, null, null, false); // Check write access to ancestor of dst fsd.CheckPermission(pc, dstIIP, false, FsAction.Write, null, null, null, false); } if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("DIR* FSDirectory.renameTo: " + src + " to " + dst); } long mtime = Time.Now(); fsd.WriteLock(); try { if (UnprotectedRenameTo(fsd, src, dst, srcIIP, dstIIP, mtime, collectedBlocks, options )) { FSDirDeleteOp.IncrDeletedFileCount(1); } } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogRename(src, dst, mtime, logRetryCache, options); }
/// <summary>Remove an xattr for a file or directory.</summary> /// <param name="src">- path to remove the xattr from</param> /// <param name="xAttr">- xAttr to remove</param> /// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus RemoveXAttr(FSDirectory fsd, string src, XAttr xAttr , bool logRetryCache) { FSDirXAttrOp.CheckXAttrsConfigFlag(fsd); FSPermissionChecker pc = fsd.GetPermissionChecker(); XAttrPermissionFilter.CheckPermissionForApi(pc, xAttr, FSDirectory.IsReservedRawName (src)); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); IList <XAttr> xAttrs = Lists.NewArrayListWithCapacity(1); xAttrs.AddItem(xAttr); INodesInPath iip; fsd.WriteLock(); try { src = fsd.ResolvePath(pc, src, pathComponents); iip = fsd.GetINodesInPath4Write(src); CheckXAttrChangeAccess(fsd, iip, xAttr, pc); IList <XAttr> removedXAttrs = UnprotectedRemoveXAttrs(fsd, src, xAttrs); if (removedXAttrs != null && !removedXAttrs.IsEmpty()) { fsd.GetEditLog().LogRemoveXAttrs(src, removedXAttrs, logRetryCache); } else { throw new IOException("No matching attributes found for remove operation"); } } finally { fsd.WriteUnlock(); } return(fsd.GetAuditFileInfo(iip)); }
/// <exception cref="System.IO.IOException"/> internal static void RenameSnapshot(FSDirectory fsd, SnapshotManager snapshotManager , string path, string snapshotOldName, string snapshotNewName, bool logRetryCache ) { INodesInPath iip = fsd.GetINodesInPath4Write(path); if (fsd.IsPermissionEnabled()) { FSPermissionChecker pc = fsd.GetPermissionChecker(); fsd.CheckOwner(pc, iip); } VerifySnapshotName(fsd, snapshotNewName, path); fsd.WriteLock(); try { snapshotManager.RenameSnapshot(iip, path, snapshotOldName, snapshotNewName); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogRenameSnapshot(path, snapshotOldName, snapshotNewName, logRetryCache ); }
/// <summary>Add the given symbolic link to the fs.</summary> /// <remarks>Add the given symbolic link to the fs. Record it in the edits log.</remarks> /// <exception cref="System.IO.IOException"/> private static INodeSymlink AddSymlink(FSDirectory fsd, string path, INodesInPath iip, string target, PermissionStatus dirPerms, bool createParent, bool logRetryCache ) { long mtime = Time.Now(); byte[] localName = iip.GetLastLocalName(); if (createParent) { KeyValuePair <INodesInPath, string> e = FSDirMkdirOp.CreateAncestorDirectories(fsd , iip, dirPerms); if (e == null) { return(null); } iip = INodesInPath.Append(e.Key, null, localName); } string userName = dirPerms.GetUserName(); long id = fsd.AllocateNewInodeId(); PermissionStatus perm = new PermissionStatus(userName, null, FsPermission.GetDefault ()); INodeSymlink newNode = UnprotectedAddSymlink(fsd, iip.GetExistingINodes(), localName , id, target, mtime, mtime, perm); if (newNode == null) { NameNode.stateChangeLog.Info("addSymlink: failed to add " + path); return(null); } fsd.GetEditLog().LogSymlink(path, target, mtime, mtime, newNode, logRetryCache); if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("addSymlink: " + path + " is added"); } return(newNode); }
/// <exception cref="System.IO.IOException"/> private static INodesInPath CreateSingleDirectory(FSDirectory fsd, INodesInPath existing , string localName, PermissionStatus perm) { System.Diagnostics.Debug.Assert(fsd.HasWriteLock()); existing = UnprotectedMkdir(fsd, fsd.AllocateNewInodeId(), existing, Sharpen.Runtime.GetBytesForString (localName, Charsets.Utf8), perm, null, Time.Now()); if (existing == null) { return(null); } INode newNode = existing.GetLastINode(); // Directory creation also count towards FilesCreated // to match count of FilesDeleted metric. NameNode.GetNameNodeMetrics().IncrFilesCreated(); string cur = existing.GetPath(); fsd.GetEditLog().LogMkDir(cur, newNode); if (NameNode.stateChangeLog.IsDebugEnabled()) { NameNode.stateChangeLog.Debug("mkdirs: created directory " + cur); } return(existing); }
/// <exception cref="System.IO.IOException"/> internal static HdfsFileStatus SetPermission(FSDirectory fsd, string srcArg, FsPermission permission) { string src = srcArg; FSPermissionChecker pc = fsd.GetPermissionChecker(); byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src); INodesInPath iip; fsd.WriteLock(); try { src = fsd.ResolvePath(pc, src, pathComponents); iip = fsd.GetINodesInPath4Write(src); fsd.CheckOwner(pc, iip); UnprotectedSetPermission(fsd, src, permission); } finally { fsd.WriteUnlock(); } fsd.GetEditLog().LogSetPermissions(src, permission); return(fsd.GetAuditFileInfo(iip)); }