Ejemplo n.º 1
0
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        private static bool UnprotectedSetTimes(FSDirectory fsd, INode inode, long mtime,
                                                long atime, bool force, int latest)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            bool status = false;

            if (mtime != -1)
            {
                inode  = inode.SetModificationTime(mtime, latest);
                status = true;
            }
            if (atime != -1)
            {
                long inodeTime = inode.GetAccessTime();
                // if the last access time update was within the last precision interval, then
                // no need to store access time
                if (atime <= inodeTime + fsd.GetFSNamesystem().GetAccessTimePrecision() && !force)
                {
                    status = false;
                }
                else
                {
                    inode.SetAccessTime(atime, latest);
                    status = true;
                }
            }
            return(status);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Delete a path from the name space
        /// Update the count at each ancestor directory with quota
        /// <br />
        /// Note: This is to be used by
        /// <see cref="FSEditLog"/>
        /// only.
        /// <br />
        /// </summary>
        /// <param name="src">a string representation of a path to an inode</param>
        /// <param name="mtime">the time the inode is removed</param>
        /// <exception cref="System.IO.IOException"/>
        internal static void DeleteForEditLog(FSDirectory fsd, string src, long mtime)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            FSNamesystem fsn = fsd.GetFSNamesystem();

            INode.BlocksMapUpdateInfo collectedBlocks = new INode.BlocksMapUpdateInfo();
            IList <INode>             removedINodes   = new ChunkedArrayList <INode>();
            INodesInPath iip = fsd.GetINodesInPath4Write(FSDirectory.NormalizePath(src), false
                                                         );

            if (!DeleteAllowed(iip, src))
            {
                return;
            }
            IList <INodeDirectory> snapshottableDirs = new AList <INodeDirectory>();

            FSDirSnapshotOp.CheckSnapshot(iip.GetLastINode(), snapshottableDirs);
            long filesRemoved = UnprotectedDelete(fsd, iip, collectedBlocks, removedINodes, mtime
                                                  );

            fsn.RemoveSnapshottableDirs(snapshottableDirs);
            if (filesRemoved >= 0)
            {
                fsn.RemoveLeasesAndINodes(src, removedINodes, false);
                fsn.RemoveBlocksAndUpdateSafemodeTotal(collectedBlocks);
            }
        }
Ejemplo n.º 3
0
        /// <exception cref="System.IO.IOException"/>
        internal static IList <AclEntry> UnprotectedSetAcl(FSDirectory fsd, string src, IList
                                                           <AclEntry> aclSpec, bool fromEdits)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath iip = fsd.GetINodesInPath4Write(FSDirectory.NormalizePath(src), true
                                                         );

            // ACL removal is logged to edits as OP_SET_ACL with an empty list.
            if (aclSpec.IsEmpty())
            {
                UnprotectedRemoveAcl(fsd, iip);
                return(AclFeature.EmptyEntryList);
            }
            INode            inode      = FSDirectory.ResolveLastINode(iip);
            int              snapshotId = iip.GetLatestSnapshotId();
            IList <AclEntry> newAcl     = aclSpec;

            if (!fromEdits)
            {
                IList <AclEntry> existingAcl = AclStorage.ReadINodeLogicalAcl(inode);
                newAcl = AclTransformation.ReplaceAclEntries(existingAcl, aclSpec);
            }
            AclStorage.UpdateINodeAcl(inode, newAcl, snapshotId);
            return(newAcl);
        }
Ejemplo n.º 4
0
        /// <exception cref="System.IO.IOException"/>
        private static void UnprotectedRemoveAcl(FSDirectory fsd, INodesInPath iip)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INode      inode      = FSDirectory.ResolveLastINode(iip);
            int        snapshotId = iip.GetLatestSnapshotId();
            AclFeature f          = inode.GetAclFeature();

            if (f == null)
            {
                return;
            }
            FsPermission     perm           = inode.GetFsPermission();
            IList <AclEntry> featureEntries = AclStorage.GetEntriesFromAclFeature(f);

            if (featureEntries[0].GetScope() == AclEntryScope.Access)
            {
                // Restore group permissions from the feature's entry to permission
                // bits, overwriting the mask, which is not part of a minimal ACL.
                AclEntry groupEntryKey = new AclEntry.Builder().SetScope(AclEntryScope.Access).SetType
                                             (AclEntryType.Group).Build();
                int groupEntryIndex = Sharpen.Collections.BinarySearch(featureEntries, groupEntryKey
                                                                       , AclTransformation.AclEntryComparator);
                System.Diagnostics.Debug.Assert(groupEntryIndex >= 0);
                FsAction     groupPerm = featureEntries[groupEntryIndex].GetPermission();
                FsPermission newPerm   = new FsPermission(perm.GetUserAction(), groupPerm, perm.GetOtherAction
                                                              (), perm.GetStickyBit());
                inode.SetPermission(newPerm, snapshotId);
            }
            inode.RemoveAclFeature(snapshotId);
        }
Ejemplo n.º 5
0
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        internal static bool UnprotectedSetTimes(FSDirectory fsd, string src, long mtime,
                                                 long atime, bool force)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath i = fsd.GetINodesInPath(src, true);

            return(UnprotectedSetTimes(fsd, i.GetLastINode(), mtime, atime, force, i.GetLatestSnapshotId
                                           ()));
        }
Ejemplo n.º 6
0
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        internal static INodeSymlink UnprotectedAddSymlink(FSDirectory fsd, INodesInPath
                                                           iip, byte[] localName, long id, string target, long mtime, long atime, PermissionStatus
                                                           perm)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodeSymlink symlink = new INodeSymlink(id, null, perm, mtime, atime, target);

            symlink.SetLocalName(localName);
            return(fsd.AddINode(iip, symlink) != null ? symlink : null);
        }
Ejemplo n.º 7
0
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.FileAlreadyExistsException"/>
        internal static void MkdirForEditLog(FSDirectory fsd, long inodeId, string src, PermissionStatus
                                             permissions, IList <AclEntry> aclEntries, long timestamp)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath iip = fsd.GetINodesInPath(src, false);

            byte[]       localName = iip.GetLastLocalName();
            INodesInPath existing  = iip.GetParentINodesInPath();

            Preconditions.CheckState(existing.GetLastINode() != null);
            UnprotectedMkdir(fsd, inodeId, existing, localName, permissions, aclEntries, timestamp
                             );
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Delete a path from the name space
        /// Update the count at each ancestor directory with quota
        /// </summary>
        /// <param name="iip">the inodes resolved from the path</param>
        /// <param name="collectedBlocks">blocks collected from the deleted path</param>
        /// <param name="removedINodes">inodes that should be removed from inodeMap</param>
        /// <param name="mtime">the time the inode is removed</param>
        /// <returns>the number of inodes deleted; 0 if no inodes are deleted.</returns>
        private static long UnprotectedDelete(FSDirectory fsd, INodesInPath iip, INode.BlocksMapUpdateInfo
                                              collectedBlocks, IList <INode> removedINodes, long mtime)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            // check if target node exists
            INode targetNode = iip.GetLastINode();

            if (targetNode == null)
            {
                return(-1);
            }
            // record modification
            int latestSnapshot = iip.GetLatestSnapshotId();

            targetNode.RecordModification(latestSnapshot);
            // Remove the node from the namespace
            long removed = fsd.RemoveLastINode(iip);

            if (removed == -1)
            {
                return(-1);
            }
            // set the parent's modification time
            INodeDirectory parent = targetNode.GetParent();

            parent.UpdateModificationTime(mtime, latestSnapshot);
            fsd.UpdateCountForDelete(targetNode, iip);
            if (removed == 0)
            {
                return(0);
            }
            // collect block and update quota
            if (!targetNode.IsInLatestSnapshot(latestSnapshot))
            {
                targetNode.DestroyAndCollectBlocks(fsd.GetBlockStoragePolicySuite(), collectedBlocks
                                                   , removedINodes);
            }
            else
            {
                QuotaCounts counts = targetNode.CleanSubtree(fsd.GetBlockStoragePolicySuite(), Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot
                                                             .CurrentStateId, latestSnapshot, collectedBlocks, removedINodes);
                removed = counts.GetNameSpace();
                fsd.UpdateCountNoQuotaCheck(iip, iip.Length() - 1, counts.Negation());
            }
            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* FSDirectory.unprotectedDelete: " + iip.GetPath
                                                  () + " is removed");
            }
            return(removed);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Create the directory
 /// <c>parent</c>
 /// /
 /// <paramref name="children"/>
 /// and all ancestors
 /// along the path.
 /// </summary>
 /// <param name="fsd">FSDirectory</param>
 /// <param name="existing">
 /// The INodesInPath instance containing all the existing
 /// ancestral INodes
 /// </param>
 /// <param name="children">
 /// The relative path from the parent towards children,
 /// starting with "/"
 /// </param>
 /// <param name="perm">
 /// the permission of the directory. Note that all ancestors
 /// created along the path has implicit
 /// <c>u+wx</c>
 /// permissions.
 /// </param>
 /// <returns>
 ///
 /// <see cref="INodesInPath"/>
 /// which contains all inodes to the
 /// target directory, After the execution parentPath points to the path of
 /// the returned INodesInPath. The function return null if the operation has
 /// failed.
 /// </returns>
 /// <exception cref="System.IO.IOException"/>
 private static INodesInPath CreateChildrenDirectories(FSDirectory fsd, INodesInPath
                                                       existing, IList <string> children, PermissionStatus perm)
 {
     System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
     foreach (string component in children)
     {
         existing = CreateSingleDirectory(fsd, existing, component, perm);
         if (existing == null)
         {
             return(null);
         }
     }
     return(existing);
 }
Ejemplo n.º 10
0
        /// <exception cref="System.IO.FileNotFoundException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.SnapshotAccessControlException"/
        ///     >
        internal static void UnprotectedSetPermission(FSDirectory fsd, string src, FsPermission
                                                      permissions)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath inodesInPath = fsd.GetINodesInPath4Write(src, true);
            INode        inode        = inodesInPath.GetLastINode();

            if (inode == null)
            {
                throw new FileNotFoundException("File does not exist: " + src);
            }
            int snapshotId = inodesInPath.GetLatestSnapshotId();

            inode.SetPermission(permissions, snapshotId);
        }
        /// <summary>
        /// Relinquish locks held during computation for a short while
        /// and reacquire them.
        /// </summary>
        /// <remarks>
        /// Relinquish locks held during computation for a short while
        /// and reacquire them. This will give other threads a chance
        /// to acquire the contended locks and run.
        /// </remarks>
        /// <returns>true if locks were released and reacquired.</returns>
        public virtual bool Yield()
        {
            // Are we set up to do this?
            if (limitPerRun <= 0 || dir == null || fsn == null)
            {
                return(false);
            }
            // Have we reached the limit?
            long currentCount = counts.GetFileCount() + counts.GetSymlinkCount() + counts.GetDirectoryCount
                                    () + counts.GetSnapshotableDirectoryCount();

            if (currentCount <= nextCountLimit)
            {
                return(false);
            }
            // Update the next limit
            nextCountLimit = currentCount + limitPerRun;
            bool hadDirReadLock  = dir.HasReadLock();
            bool hadDirWriteLock = dir.HasWriteLock();
            bool hadFsnReadLock  = fsn.HasReadLock();
            bool hadFsnWriteLock = fsn.HasWriteLock();

            // sanity check.
            if (!hadDirReadLock || !hadFsnReadLock || hadDirWriteLock || hadFsnWriteLock || dir
                .GetReadHoldCount() != 1 || fsn.GetReadHoldCount() != 1)
            {
                // cannot relinquish
                return(false);
            }
            // unlock
            dir.ReadUnlock();
            fsn.ReadUnlock();
            try
            {
                Sharpen.Thread.Sleep(sleepMilliSec, sleepNanoSec);
            }
            catch (Exception)
            {
            }
            finally
            {
                // reacquire
                fsn.ReadLock();
                dir.ReadLock();
            }
            yieldCount++;
            return(true);
        }
Ejemplo n.º 12
0
        /// <exception cref="System.IO.IOException"/>
        internal static void UnprotectedSetStoragePolicy(FSDirectory fsd, BlockManager bm
                                                         , INodesInPath iip, byte policyId)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INode inode = iip.GetLastINode();

            if (inode == null)
            {
                throw new FileNotFoundException("File/Directory does not exist: " + iip.GetPath()
                                                );
            }
            int snapshotId = iip.GetLatestSnapshotId();

            if (inode.IsFile())
            {
                BlockStoragePolicy newPolicy = bm.GetStoragePolicy(policyId);
                if (newPolicy.IsCopyOnCreateFile())
                {
                    throw new HadoopIllegalArgumentException("Policy " + newPolicy + " cannot be set after file creation."
                                                             );
                }
                BlockStoragePolicy currentPolicy = bm.GetStoragePolicy(inode.GetLocalStoragePolicyID
                                                                           ());
                if (currentPolicy != null && currentPolicy.IsCopyOnCreateFile())
                {
                    throw new HadoopIllegalArgumentException("Existing policy " + currentPolicy.GetName
                                                                 () + " cannot be changed after file creation.");
                }
                inode.AsFile().SetStoragePolicyID(policyId, snapshotId);
            }
            else
            {
                if (inode.IsDirectory())
                {
                    SetDirStoragePolicy(fsd, inode.AsDirectory(), policyId, snapshotId);
                }
                else
                {
                    throw new FileNotFoundException(iip.GetPath() + " is not a file or directory");
                }
            }
        }
Ejemplo n.º 13
0
        /// <exception cref="System.IO.FileNotFoundException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.SnapshotAccessControlException"/
        ///     >
        internal static void UnprotectedSetOwner(FSDirectory fsd, string src, string username
                                                 , string groupname)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath inodesInPath = fsd.GetINodesInPath4Write(src, true);
            INode        inode        = inodesInPath.GetLastINode();

            if (inode == null)
            {
                throw new FileNotFoundException("File does not exist: " + src);
            }
            if (username != null)
            {
                inode = inode.SetUser(username, inodesInPath.GetLatestSnapshotId());
            }
            if (groupname != null)
            {
                inode.SetGroup(groupname, inodesInPath.GetLatestSnapshotId());
            }
        }
Ejemplo n.º 14
0
        /// <exception cref="System.IO.IOException"/>
        internal static IList <XAttr> UnprotectedRemoveXAttrs(FSDirectory fsd, string src,
                                                              IList <XAttr> toRemove)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath iip = fsd.GetINodesInPath4Write(FSDirectory.NormalizePath(src), true
                                                         );
            INode         inode          = FSDirectory.ResolveLastINode(iip);
            int           snapshotId     = iip.GetLatestSnapshotId();
            IList <XAttr> existingXAttrs = XAttrStorage.ReadINodeXAttrs(inode);
            IList <XAttr> removedXAttrs  = Lists.NewArrayListWithCapacity(toRemove.Count);
            IList <XAttr> newXAttrs      = FilterINodeXAttrs(existingXAttrs, toRemove, removedXAttrs
                                                             );

            if (existingXAttrs.Count != newXAttrs.Count)
            {
                XAttrStorage.UpdateINodeXAttrs(inode, newXAttrs, snapshotId);
                return(removedXAttrs);
            }
            return(null);
        }
Ejemplo n.º 15
0
        /// <summary>create a directory at path specified by parent</summary>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.AclException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.FileAlreadyExistsException"/>
        private static INodesInPath UnprotectedMkdir(FSDirectory fsd, long inodeId, INodesInPath
                                                     parent, byte[] name, PermissionStatus permission, IList <AclEntry> aclEntries, long
                                                     timestamp)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            System.Diagnostics.Debug.Assert(parent.GetLastINode() != null);
            if (!parent.GetLastINode().IsDirectory())
            {
                throw new FileAlreadyExistsException("Parent path is not a directory: " + parent.
                                                     GetPath() + " " + DFSUtil.Bytes2String(name));
            }
            INodeDirectory dir = new INodeDirectory(inodeId, name, permission, timestamp);
            INodesInPath   iip = fsd.AddLastINode(parent, dir, true);

            if (iip != null && aclEntries != null)
            {
                AclStorage.UpdateINodeAcl(dir, aclEntries, Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot
                                          .CurrentStateId);
            }
            return(iip);
        }
Ejemplo n.º 16
0
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException"/>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.SnapshotAccessControlException"/
        ///     >
        internal static Block[] UnprotectedSetReplication(FSDirectory fsd, string src, short
                                                          replication, short[] blockRepls)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath iip   = fsd.GetINodesInPath4Write(src, true);
            INode        inode = iip.GetLastINode();

            if (inode == null || !inode.IsFile())
            {
                return(null);
            }
            INodeFile file  = inode.AsFile();
            short     oldBR = file.GetBlockReplication();

            // before setFileReplication, check for increasing block replication.
            // if replication > oldBR, then newBR == replication.
            // if replication < oldBR, we don't know newBR yet.
            if (replication > oldBR)
            {
                long dsDelta = file.StoragespaceConsumed() / oldBR;
                fsd.UpdateCount(iip, 0L, dsDelta, oldBR, replication, true);
            }
            file.SetFileReplication(replication, iip.GetLatestSnapshotId());
            short newBR = file.GetBlockReplication();

            // check newBR < oldBR case.
            if (newBR < oldBR)
            {
                long dsDelta = file.StoragespaceConsumed() / newBR;
                fsd.UpdateCount(iip, 0L, dsDelta, oldBR, newBR, true);
            }
            if (blockRepls != null)
            {
                blockRepls[0] = oldBR;
                blockRepls[1] = newBR;
            }
            return(file.GetBlocks());
        }
Ejemplo n.º 17
0
        /// <exception cref="System.IO.IOException"/>
        internal static INode UnprotectedSetXAttrs(FSDirectory fsd, string src, IList <XAttr
                                                                                       > xAttrs, EnumSet <XAttrSetFlag> flag)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INodesInPath iip = fsd.GetINodesInPath4Write(FSDirectory.NormalizePath(src), true
                                                         );
            INode         inode          = FSDirectory.ResolveLastINode(iip);
            int           snapshotId     = iip.GetLatestSnapshotId();
            IList <XAttr> existingXAttrs = XAttrStorage.ReadINodeXAttrs(inode);
            IList <XAttr> newXAttrs      = SetINodeXAttrs(fsd, existingXAttrs, xAttrs, flag);
            bool          isFile         = inode.IsFile();

            foreach (XAttr xattr in newXAttrs)
            {
                string xaName = XAttrHelper.GetPrefixName(xattr);

                /*
                 * If we're adding the encryption zone xattr, then add src to the list
                 * of encryption zones.
                 */
                if (HdfsServerConstants.CryptoXattrEncryptionZone.Equals(xaName))
                {
                    HdfsProtos.ZoneEncryptionInfoProto ezProto = HdfsProtos.ZoneEncryptionInfoProto.ParseFrom
                                                                     (xattr.GetValue());
                    fsd.ezManager.AddEncryptionZone(inode.GetId(), PBHelper.Convert(ezProto.GetSuite(
                                                                                        )), PBHelper.Convert(ezProto.GetCryptoProtocolVersion()), ezProto.GetKeyName());
                }
                if (!isFile && HdfsServerConstants.SecurityXattrUnreadableBySuperuser.Equals(xaName
                                                                                             ))
                {
                    throw new IOException("Can only set '" + HdfsServerConstants.SecurityXattrUnreadableBySuperuser
                                          + "' on a file.");
                }
            }
            XAttrStorage.UpdateINodeXAttrs(inode, newXAttrs, snapshotId);
            return(inode);
        }
Ejemplo n.º 18
0
        /// <summary>Concat all the blocks from srcs to trg and delete the srcs files</summary>
        /// <param name="fsd">FSDirectory</param>
        /// <exception cref="System.IO.IOException"/>
        internal static void UnprotectedConcat(FSDirectory fsd, INodesInPath targetIIP, INodeFile
                                               [] srcList, long timestamp)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* FSNamesystem.concat to " + targetIIP.GetPath(
                                                  ));
            }
            INodeFile   trgInode = targetIIP.GetLastINode().AsFile();
            QuotaCounts deltas   = ComputeQuotaDeltas(fsd, trgInode, srcList);

            VerifyQuota(fsd, targetIIP, deltas);
            // the target file can be included in a snapshot
            trgInode.RecordModification(targetIIP.GetLatestSnapshotId());
            INodeDirectory trgParent = targetIIP.GetINode(-2).AsDirectory();

            trgInode.ConcatBlocks(srcList);
            // since we are in the same dir - we can use same parent to remove files
            int count = 0;

            foreach (INodeFile nodeToRemove in srcList)
            {
                if (nodeToRemove != null)
                {
                    nodeToRemove.SetBlocks(null);
                    nodeToRemove.GetParent().RemoveChild(nodeToRemove);
                    fsd.GetINodeMap().Remove(nodeToRemove);
                    count++;
                }
            }
            trgInode.SetModificationTime(timestamp, targetIIP.GetLatestSnapshotId());
            trgParent.UpdateModificationTime(timestamp, targetIIP.GetLatestSnapshotId());
            // update quota on the parent directory with deltas
            FSDirectory.UnprotectedUpdateCount(targetIIP, targetIIP.Length() - 1, deltas);
        }
Ejemplo n.º 19
0
        /// <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);
        }
Ejemplo n.º 20
0
        internal static bool UnprotectedRenameTo(FSDirectory fsd, string src, string dst,
                                                 INodesInPath srcIIP, INodesInPath dstIIP, long timestamp)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            INode srcInode = srcIIP.GetLastINode();

            try
            {
                ValidateRenameSource(srcIIP);
            }
            catch (SnapshotException e)
            {
                throw;
            }
            catch (IOException)
            {
                return(false);
            }
            // validate the destination
            if (dst.Equals(src))
            {
                return(true);
            }
            try
            {
                ValidateDestination(src, dst, srcInode);
            }
            catch (IOException)
            {
                return(false);
            }
            if (dstIIP.GetLastINode() != null)
            {
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                             + src + " to " + dst + " because destination " + "exists");
                return(false);
            }
            INode dstParent = dstIIP.GetINode(-2);

            if (dstParent == null)
            {
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                             + src + " to " + dst + " because destination's " + "parent does not exist");
                return(false);
            }
            fsd.ezManager.CheckMoveValidity(srcIIP, dstIIP, src);
            // Ensure dst has quota to accommodate rename
            VerifyFsLimitsForRename(fsd, srcIIP, dstIIP);
            VerifyQuotaForRename(fsd, srcIIP, dstIIP);
            FSDirRenameOp.RenameOperation tx = new FSDirRenameOp.RenameOperation(fsd, src, dst
                                                                                 , srcIIP, dstIIP);
            bool added = false;

            try
            {
                // remove src
                if (!tx.RemoveSrc4OldRename())
                {
                    return(false);
                }
                added = tx.AddSourceToDestination();
                if (added)
                {
                    if (NameNode.stateChangeLog.IsDebugEnabled())
                    {
                        NameNode.stateChangeLog.Debug("DIR* FSDirectory" + ".unprotectedRenameTo: " + src
                                                      + " is renamed to " + dst);
                    }
                    tx.UpdateMtimeAndLease(timestamp);
                    tx.UpdateQuotasInSourceTree(fsd.GetBlockStoragePolicySuite());
                    return(true);
                }
            }
            finally
            {
                if (!added)
                {
                    tx.RestoreSource();
                }
            }
            NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                         + src + " to " + dst);
            return(false);
        }
Ejemplo n.º 21
0
        /// <summary>Rename src to dst.</summary>
        /// <remarks>
        /// Rename src to dst.
        /// See
        /// <see cref="Org.Apache.Hadoop.Hdfs.DistributedFileSystem.Rename(Org.Apache.Hadoop.FS.Path, Org.Apache.Hadoop.FS.Path, Org.Apache.Hadoop.FS.Options.Rename[])
        ///     "/>
        /// for details related to rename semantics and exceptions.
        /// </remarks>
        /// <param name="fsd">FSDirectory</param>
        /// <param name="src">source path</param>
        /// <param name="dst">destination path</param>
        /// <param name="timestamp">modification time</param>
        /// <param name="collectedBlocks">blocks to be removed</param>
        /// <param name="options">Rename options</param>
        /// <returns>whether a file/directory gets overwritten in the dst path</returns>
        /// <exception cref="System.IO.IOException"/>
        internal static bool UnprotectedRenameTo(FSDirectory fsd, string src, string dst,
                                                 INodesInPath srcIIP, INodesInPath dstIIP, long timestamp, INode.BlocksMapUpdateInfo
                                                 collectedBlocks, params Options.Rename[] options)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            bool overwrite = options != null && Arrays.AsList(options).Contains(Options.Rename
                                                                                .Overwrite);
            string error;
            INode  srcInode = srcIIP.GetLastINode();

            ValidateRenameSource(srcIIP);
            // validate the destination
            if (dst.Equals(src))
            {
                throw new FileAlreadyExistsException("The source " + src + " and destination " +
                                                     dst + " are the same");
            }
            ValidateDestination(src, dst, srcInode);
            if (dstIIP.Length() == 1)
            {
                error = "rename destination cannot be the root";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new IOException(error);
            }
            BlockStoragePolicySuite bsps = fsd.GetBlockStoragePolicySuite();

            fsd.ezManager.CheckMoveValidity(srcIIP, dstIIP, src);
            INode dstInode = dstIIP.GetLastINode();
            IList <INodeDirectory> snapshottableDirs = new AList <INodeDirectory>();

            if (dstInode != null)
            {
                // Destination exists
                ValidateOverwrite(src, dst, overwrite, srcInode, dstInode);
                FSDirSnapshotOp.CheckSnapshot(dstInode, snapshottableDirs);
            }
            INode dstParent = dstIIP.GetINode(-2);

            if (dstParent == null)
            {
                error = "rename destination parent " + dst + " not found.";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new FileNotFoundException(error);
            }
            if (!dstParent.IsDirectory())
            {
                error = "rename destination parent " + dst + " is a file.";
                NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + error);
                throw new ParentNotDirectoryException(error);
            }
            // Ensure dst has quota to accommodate rename
            VerifyFsLimitsForRename(fsd, srcIIP, dstIIP);
            VerifyQuotaForRename(fsd, srcIIP, dstIIP);
            FSDirRenameOp.RenameOperation tx = new FSDirRenameOp.RenameOperation(fsd, src, dst
                                                                                 , srcIIP, dstIIP);
            bool undoRemoveSrc = true;

            tx.RemoveSrc();
            bool undoRemoveDst = false;
            long removedNum    = 0;

            try
            {
                if (dstInode != null)
                {
                    // dst exists, remove it
                    removedNum = tx.RemoveDst();
                    if (removedNum != -1)
                    {
                        undoRemoveDst = true;
                    }
                }
                // add src as dst to complete rename
                if (tx.AddSourceToDestination())
                {
                    undoRemoveSrc = false;
                    if (NameNode.stateChangeLog.IsDebugEnabled())
                    {
                        NameNode.stateChangeLog.Debug("DIR* FSDirectory.unprotectedRenameTo: " + src + " is renamed to "
                                                      + dst);
                    }
                    tx.UpdateMtimeAndLease(timestamp);
                    // Collect the blocks and remove the lease for previous dst
                    bool filesDeleted = false;
                    if (undoRemoveDst)
                    {
                        undoRemoveDst = false;
                        if (removedNum > 0)
                        {
                            filesDeleted = tx.CleanDst(bsps, collectedBlocks);
                        }
                    }
                    if (snapshottableDirs.Count > 0)
                    {
                        // There are snapshottable directories (without snapshots) to be
                        // deleted. Need to update the SnapshotManager.
                        fsd.GetFSNamesystem().RemoveSnapshottableDirs(snapshottableDirs);
                    }
                    tx.UpdateQuotasInSourceTree(bsps);
                    return(filesDeleted);
                }
            }
            finally
            {
                if (undoRemoveSrc)
                {
                    tx.RestoreSource();
                }
                if (undoRemoveDst)
                {
                    // Rename failed - restore dst
                    tx.RestoreDst(bsps);
                }
            }
            NameNode.stateChangeLog.Warn("DIR* FSDirectory.unprotectedRenameTo: " + "failed to rename "
                                         + src + " to " + dst);
            throw new IOException("rename from " + src + " to " + dst + " failed.");
        }
Ejemplo n.º 22
0
        /// <summary>
        /// See
        /// <see cref="Org.Apache.Hadoop.Hdfs.Protocol.ClientProtocol.SetQuota(string, long, long, Org.Apache.Hadoop.FS.StorageType)
        ///     "/>
        /// for the contract.
        /// Sets quota for for a directory.
        /// </summary>
        /// <returns>INodeDirectory if any of the quotas have changed. null otherwise.</returns>
        /// <exception cref="System.IO.FileNotFoundException">if the path does not exist.</exception>
        /// <exception cref="Org.Apache.Hadoop.FS.PathIsNotDirectoryException">if the path is not a directory.
        ///     </exception>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.QuotaExceededException">
        /// if the directory tree size is
        /// greater than the given quota
        /// </exception>
        /// <exception cref="Org.Apache.Hadoop.FS.UnresolvedLinkException">if a symlink is encountered in src.
        ///     </exception>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Protocol.SnapshotAccessControlException">if path is in RO snapshot
        ///     </exception>
        /// <exception cref="Org.Apache.Hadoop.Hdfs.Server.Namenode.UnsupportedActionException
        ///     "/>
        internal static INodeDirectory UnprotectedSetQuota(FSDirectory fsd, string src, long
                                                           nsQuota, long ssQuota, StorageType type)
        {
            System.Diagnostics.Debug.Assert(fsd.HasWriteLock());
            // sanity check
            if ((nsQuota < 0 && nsQuota != HdfsConstants.QuotaDontSet && nsQuota != HdfsConstants
                 .QuotaReset) || (ssQuota < 0 && ssQuota != HdfsConstants.QuotaDontSet && ssQuota
                                  != HdfsConstants.QuotaReset))
            {
                throw new ArgumentException("Illegal value for nsQuota or " + "ssQuota : " + nsQuota
                                            + " and " + ssQuota);
            }
            // sanity check for quota by storage type
            if ((type != null) && (!fsd.IsQuotaByStorageTypeEnabled() || nsQuota != HdfsConstants
                                   .QuotaDontSet))
            {
                throw new UnsupportedActionException("Failed to set quota by storage type because either"
                                                     + DFSConfigKeys.DfsQuotaByStoragetypeEnabledKey + " is set to " + fsd.IsQuotaByStorageTypeEnabled
                                                         () + " or nsQuota value is illegal " + nsQuota);
            }
            string         srcs    = FSDirectory.NormalizePath(src);
            INodesInPath   iip     = fsd.GetINodesInPath4Write(srcs, true);
            INodeDirectory dirNode = INodeDirectory.ValueOf(iip.GetLastINode(), srcs);

            if (dirNode.IsRoot() && nsQuota == HdfsConstants.QuotaReset)
            {
                throw new ArgumentException("Cannot clear namespace quota on root.");
            }
            else
            {
                // a directory inode
                QuotaCounts oldQuota   = dirNode.GetQuotaCounts();
                long        oldNsQuota = oldQuota.GetNameSpace();
                long        oldSsQuota = oldQuota.GetStorageSpace();
                if (nsQuota == HdfsConstants.QuotaDontSet)
                {
                    nsQuota = oldNsQuota;
                }
                if (ssQuota == HdfsConstants.QuotaDontSet)
                {
                    ssQuota = oldSsQuota;
                }
                // unchanged space/namespace quota
                if (type == null && oldNsQuota == nsQuota && oldSsQuota == ssQuota)
                {
                    return(null);
                }
                // unchanged type quota
                if (type != null)
                {
                    EnumCounters <StorageType> oldTypeQuotas = oldQuota.GetTypeSpaces();
                    if (oldTypeQuotas != null && oldTypeQuotas.Get(type) == ssQuota)
                    {
                        return(null);
                    }
                }
                int latest = iip.GetLatestSnapshotId();
                dirNode.RecordModification(latest);
                dirNode.SetQuota(fsd.GetBlockStoragePolicySuite(), nsQuota, ssQuota, type);
                return(dirNode);
            }
        }
Ejemplo n.º 23
0
 /// <summary>Add a new encryption zone.</summary>
 /// <remarks>
 /// Add a new encryption zone.
 /// <p/>
 /// Called while holding the FSDirectory lock.
 /// </remarks>
 /// <param name="inodeId">of the encryption zone</param>
 /// <param name="keyName">encryption zone key name</param>
 internal virtual void AddEncryptionZone(long inodeId, CipherSuite suite, CryptoProtocolVersion
                                         version, string keyName)
 {
     System.Diagnostics.Debug.Assert(dir.HasWriteLock());
     UnprotectedAddEncryptionZone(inodeId, suite, version, keyName);
 }