private boolean isDescendant(final INodesInPath ancestorDirIIP) { int ancestorDirINodesLength = ancestorDirIIP.length(); int myParentINodesLength = length() - 1; if (myParentINodesLength < ancestorDirINodesLength) { return false; } int index = 0; while (index < ancestorDirINodesLength) { if (inodes[index] != ancestorDirIIP.getINode(index)) { return false; } index++; } return true; }
private static boolean deleteAllowed(final INodesInPath iip) { if (iip.length() < 1 || iip.getLastINode() == null) { if (NameNode.stateChangeLog.isDebugEnabled()) { NameNode.stateChangeLog.debug( "DIR* FSDirectory.unprotectedDelete: failed to remove " + iip.getPath() + " because it does not exist"); } return false; } else if (iip.length() == 1) { // src is the root NameNode.stateChangeLog.warn( "DIR* FSDirectory.unprotectedDelete: failed to remove " + iip.getPath() + " because the root is not allowed to be deleted"); return false; } return true; }
/** * Replace an inode of the given INodesInPath in the given position. We do a * deep copy of the INode array. * @param pos the position of the replacement * @param inode the new inode * @return a new INodesInPath instance */ public static INodesInPath replace(INodesInPath iip, int pos, INode inode) { Preconditions.checkArgument(iip.length() > 0 && pos > 0 // no for root && pos < iip.length()); if (iip.getINode(pos) == null) { Preconditions.checkState(iip.getINode(pos - 1) != null); } INode[] inodes = new INode[iip.inodes.length]; System.arraycopy(iip.inodes, 0, inodes, 0, inodes.length); inodes[pos] = inode; return new INodesInPath(inodes, iip.path, iip.isRaw, iip.isSnapshot, iip.snapshotId); }
/** * Extend a given INodesInPath with a child INode. The child INode will be * appended to the end of the new INodesInPath. */ public static INodesInPath append(INodesInPath iip, INode child, byte[] childName) { Preconditions.checkArgument(iip.length() > 0); Preconditions.checkArgument(iip.getLastINode() != null && iip .getLastINode().isDirectory()); INode[] inodes = new INode[iip.length() + 1]; System.arraycopy(iip.inodes, 0, inodes, 0, inodes.length - 1); inodes[inodes.length - 1] = child; byte[][] path = new byte[iip.path.length + 1][]; System.arraycopy(iip.path, 0, path, 0, path.length - 1); path[path.length - 1] = childName; return new INodesInPath(inodes, path, iip.isRaw, iip.isSnapshot, iip.snapshotId); }
private static void validateRenameSource(FSDirectory fsd, INodesInPath srcIIP) throws IOException { String error; final INode srcInode = srcIIP.getLastINode(); // validate source if (srcInode == null) { error = "rename source " + srcIIP.getPath() + " is not found."; NameNode.stateChangeLog.warn("DIR* FSDirectory.unprotectedRenameTo: " + error); throw new FileNotFoundException(error); } if (srcIIP.length() == 1) { error = "rename source cannot be the root"; NameNode.stateChangeLog.warn("DIR* FSDirectory.unprotectedRenameTo: " + error); throw new IOException(error); } // srcInode and its subtree cannot contain snapshottable directories with // snapshots FSDirSnapshotOp.checkSnapshot(fsd, srcIIP, null); }
private static void checkSimpleTraverse(INodesInPath iip) throws UnresolvedPathException, ParentNotDirectoryException { byte[][] components = iip.getPathComponents(); for (int i=0; i < iip.length() - 1; i++) { INode inode = iip.getINode(i); if (inode == null) { break; } checkIsDirectory(inode, components, i); } }
/** update count of each inode with quota * * @param iip inodes in a path * @param numOfINodes the number of inodes to update starting from index 0 * @param counts the count of space/namespace/type usage to be update * @param checkQuota if true then check if quota is exceeded * @throws QuotaExceededException if the new count violates any quota limit */ void updateCount(INodesInPath iip, int numOfINodes, QuotaCounts counts, boolean checkQuota) throws QuotaExceededException { assert hasWriteLock(); if (!namesystem.isImageLoaded()) { //still initializing. do not check or update quotas. return; } if (numOfINodes > iip.length()) { numOfINodes = iip.length(); } if (checkQuota && !skipQuotaCheck) { verifyQuota(iip, numOfINodes, counts, null); } unprotectedUpdateCount(iip, numOfINodes, counts); }
public void updateCount(INodesInPath iip, INode.QuotaDelta quotaDelta, boolean check) throws QuotaExceededException { QuotaCounts counts = quotaDelta.getCountsCopy(); updateCount(iip, iip.length() - 1, counts.negation(), check); Map<INode, QuotaCounts> deltaInOtherPaths = quotaDelta.getUpdateMap(); for (Map.Entry<INode, QuotaCounts> entry : deltaInOtherPaths.entrySet()) { INodesInPath path = INodesInPath.fromINode(entry.getKey()); updateCount(path, path.length() - 1, entry.getValue().negation(), check); } for (Map.Entry<INodeDirectory, QuotaCounts> entry : quotaDelta.getQuotaDirMap().entrySet()) { INodeDirectory quotaDir = entry.getKey(); quotaDir.getDirectoryWithQuotaFeature().addSpaceConsumed2Cache( entry.getValue().negation()); } }
long removeSrc() throws IOException { long removedNum = fsd.removeLastINode(srcIIP); if (removedNum == -1) { String error = "Failed to rename " + srcIIP.getPath() + " to " + dstIIP.getPath() + " because the source can not be removed"; NameNode.stateChangeLog.warn("DIR* FSDirRenameOp.unprotectedRenameTo:" + error); throw new IOException(error); } else { // update the quota count if necessary fsd.updateCountForDelete(srcChild, srcIIP); srcIIP = INodesInPath.replace(srcIIP, srcIIP.length() - 1, null); return removedNum; } }
private static void verifyQuota(FSDirectory fsd, INodesInPath targetIIP, QuotaCounts deltas) throws QuotaExceededException { if (!fsd.getFSNamesystem().isImageLoaded() || fsd.shouldSkipQuotaChecks()) { // Do not check quota if editlog is still being processed return; } FSDirectory.verifyQuota(targetIIP, targetIIP.length() - 1, deltas, null); }
long removeDst() { long removedNum = fsd.removeLastINode(dstIIP); if (removedNum != -1) { oldDstChild = dstIIP.getLastINode(); // update the quota count if necessary fsd.updateCountForDelete(oldDstChild, dstIIP); dstIIP = INodesInPath.replace(dstIIP, dstIIP.length() - 1, null); } return removedNum; }
boolean removeSrc4OldRename() { final long removedSrc = fsd.removeLastINode(srcIIP); if (removedSrc == -1) { NameNode.stateChangeLog.warn("DIR* FSDirRenameOp.unprotectedRenameTo: " + "failed to rename " + srcIIP.getPath() + " to " + dstIIP.getPath() + " because the source can not be removed"); return false; } else { // update the quota count if necessary fsd.updateCountForDelete(srcChild, srcIIP); srcIIP = INodesInPath.replace(srcIIP, srcIIP.length() - 1, null); return true; } }
/** * Verify that parent directory of src exists. */ void verifyParentDir(INodesInPath iip) throws FileNotFoundException, ParentNotDirectoryException { if (iip.length() > 2) { final INode parentNode = iip.getINode(-2); if (parentNode == null) { throw new FileNotFoundException("Parent directory doesn't exist: " + iip.getParentPath()); } else if (!parentNode.isDirectory()) { throw new ParentNotDirectoryException("Parent path is not a directory: " + iip.getParentPath()); } } }
/** * Update the quota usage after deletion. The quota update is only necessary * when image/edits have been loaded and the file/dir to be deleted is not * contained in snapshots. */ void updateCountForDelete(final INode inode, final INodesInPath iip) { if (getFSNamesystem().isImageLoaded() && !inode.isInLatestSnapshot(iip.getLatestSnapshotId())) { QuotaCounts counts = inode.computeQuotaUsage(getBlockStoragePolicySuite()); unprotectedUpdateCount(iip, iip.length() - 1, counts.negation()); } }
/** * Update usage count with replication factor change due to setReplication */ void updateCount(INodesInPath iip, long nsDelta, long ssDelta, short oldRep, short newRep, boolean checkQuota) throws QuotaExceededException { final INodeFile fileINode = iip.getLastINode().asFile(); EnumCounters<StorageType> typeSpaceDeltas = getStorageTypeDeltas(fileINode.getStoragePolicyID(), ssDelta, oldRep, newRep); updateCount(iip, iip.length() - 1, new QuotaCounts.Builder().nameSpace(nsDelta). storageSpace(ssDelta * (newRep - oldRep)). typeSpaces(typeSpaceDeltas).build(), checkQuota); }
/** * Update usage count without replication factor change */ void updateCount(INodesInPath iip, long nsDelta, long ssDelta, short replication, boolean checkQuota) throws QuotaExceededException { final INodeFile fileINode = iip.getLastINode().asFile(); EnumCounters<StorageType> typeSpaceDeltas = getStorageTypeDeltas(fileINode.getStoragePolicyID(), ssDelta, replication, replication); updateCount(iip, iip.length() - 1, new QuotaCounts.Builder().nameSpace(nsDelta).storageSpace(ssDelta * replication). typeSpaces(typeSpaceDeltas).build(), checkQuota); }
int last = iip.length() - 1; checkNotSymlink(iip.getINode(last), iip.getPathComponents(), last);
/** * Verify quota when using the preferred block size for UC block. This is * usually used by append and truncate. * * @throws QuotaExceededException when violating the storage quota * @return expected quota usage update. null means no change or no need to * update quota usage later */ private static QuotaCounts verifyQuotaForUCBlock(FSNamesystem fsn, INodeFile file, INodesInPath iip) throws QuotaExceededException { FSDirectory fsd = fsn.getFSDirectory(); if (!fsn.isImageLoaded() || fsd.shouldSkipQuotaChecks()) { // Do not check quota if editlog is still being processed return null; } if (file.getLastBlock() != null) { final QuotaCounts delta = computeQuotaDeltaForUCBlock(fsn, file); fsd.readLock(); try { FSDirectory.verifyQuota(iip, iip.length() - 1, delta, null); return delta; } finally { fsd.readUnlock(); } } return null; }
private static void verifyQuotaForTruncate(FSNamesystem fsn, INodesInPath iip, INodeFile file, long newLength, QuotaCounts delta) throws QuotaExceededException { FSDirectory fsd = fsn.getFSDirectory(); if (!fsn.isImageLoaded() || fsd.shouldSkipQuotaChecks()) { // Do not check quota if edit log is still being processed return; } final BlockStoragePolicy policy = fsd.getBlockStoragePolicySuite() .getPolicy(file.getStoragePolicyID()); file.computeQuotaDeltaForTruncate(newLength, policy, delta); fsd.readLock(); try { FSDirectory.verifyQuota(iip, iip.length() - 1, delta, null); } finally { fsd.readUnlock(); } }
/** * Verify quota for rename operation where srcInodes[srcInodes.length-1] moves * dstInodes[dstInodes.length-1] */ private static void verifyQuotaForRename(FSDirectory fsd, INodesInPath src, INodesInPath dst) throws QuotaExceededException { if (!fsd.getFSNamesystem().isImageLoaded() || fsd.shouldSkipQuotaChecks()) { // Do not check quota if edits log is still being processed return; } int i = 0; while(src.getINode(i) == dst.getINode(i)) { i++; } // src[i - 1] is the last common ancestor. BlockStoragePolicySuite bsps = fsd.getBlockStoragePolicySuite(); final QuotaCounts delta = src.getLastINode().computeQuotaUsage(bsps); // Reduce the required quota by dst that is being removed final INode dstINode = dst.getLastINode(); if (dstINode != null) { delta.subtract(dstINode.computeQuotaUsage(bsps)); } FSDirectory.verifyQuota(dst, dst.length() - 1, delta, src.getINode(i - 1)); }