Ejemplo n.º 1
0
        internal static bool RenameForEditLog(FSDirectory fsd, string src, string dst, long
                                              timestamp)
        {
            if (fsd.IsDir(dst))
            {
                dst += Path.Separator + new Path(src).GetName();
            }
            INodesInPath srcIIP = fsd.GetINodesInPath4Write(src, false);
            INodesInPath dstIIP = fsd.GetINodesInPath4Write(dst, false);

            return(UnprotectedRenameTo(fsd, src, dst, srcIIP, dstIIP, timestamp));
        }
Ejemplo n.º 2
0
        /// <summary>Rename src to dst.</summary>
        /// <remarks>
        /// Rename src to dst.
        /// <br />
        /// Note: This is to be used by
        /// <see cref="FSEditLogLoader"/>
        /// only.
        /// <br />
        /// </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="options">Rename options</param>
        /// <exception cref="System.IO.IOException"/>
        internal static bool RenameForEditLog(FSDirectory fsd, string src, string dst, long
                                              timestamp, params Options.Rename[] options)
        {
            INode.BlocksMapUpdateInfo collectedBlocks = new INode.BlocksMapUpdateInfo();
            INodesInPath srcIIP = fsd.GetINodesInPath4Write(src, false);
            INodesInPath dstIIP = fsd.GetINodesInPath4Write(dst, false);
            bool         ret    = UnprotectedRenameTo(fsd, src, dst, srcIIP, dstIIP, timestamp, collectedBlocks
                                                      , options);

            if (!collectedBlocks.GetToDeleteList().IsEmpty())
            {
                fsd.GetFSNamesystem().RemoveBlocksAndUpdateSafemodeTotal(collectedBlocks);
            }
            return(ret);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <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));
        }
Ejemplo n.º 5
0
        /// <summary>Create a new encryption zone.</summary>
        /// <remarks>
        /// Create a new encryption zone.
        /// <p/>
        /// Called while holding the FSDirectory lock.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        internal virtual XAttr CreateEncryptionZone(string src, CipherSuite suite, CryptoProtocolVersion
                                                    version, string keyName)
        {
            System.Diagnostics.Debug.Assert(dir.HasWriteLock());
            INodesInPath srcIIP = dir.GetINodesInPath4Write(src, false);

            if (dir.IsNonEmptyDirectory(srcIIP))
            {
                throw new IOException("Attempt to create an encryption zone for a non-empty directory."
                                      );
            }
            if (srcIIP != null && srcIIP.GetLastINode() != null && !srcIIP.GetLastINode().IsDirectory
                    ())
            {
                throw new IOException("Attempt to create an encryption zone for a file.");
            }
            EncryptionZoneManager.EncryptionZoneInt ezi = GetEncryptionZoneForPath(srcIIP);
            if (ezi != null)
            {
                throw new IOException("Directory " + src + " is already in an " + "encryption zone. ("
                                      + GetFullPathName(ezi) + ")");
            }
            HdfsProtos.ZoneEncryptionInfoProto proto = PBHelper.Convert(suite, version, keyName
                                                                        );
            XAttr ezXAttr = XAttrHelper.BuildXAttr(HdfsServerConstants.CryptoXattrEncryptionZone
                                                   , proto.ToByteArray());
            IList <XAttr> xattrs = Lists.NewArrayListWithCapacity(1);

            xattrs.AddItem(ezXAttr);
            // updating the xattr will call addEncryptionZone,
            // done this way to handle edit log loading
            FSDirXAttrOp.UnprotectedSetXAttrs(dir, src, xattrs, EnumSet.Of(XAttrSetFlag.Create
                                                                           ));
            return(ezXAttr);
        }
Ejemplo n.º 6
0
        /// <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));
        }
Ejemplo n.º 7
0
        /// <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));
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
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.º 10
0
        /// <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));
        }
Ejemplo n.º 11
0
        /// <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));
        }
Ejemplo n.º 12
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.º 13
0
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus CreateSymlinkInt(FSNamesystem fsn, string target,
                                                        string linkArg, PermissionStatus dirPerms, bool createParent, bool logRetryCache
                                                        )
        {
            FSDirectory fsd  = fsn.GetFSDirectory();
            string      link = linkArg;

            if (!DFSUtil.IsValidName(link))
            {
                throw new InvalidPathException("Invalid link name: " + link);
            }
            if (FSDirectory.IsReservedName(target) || target.IsEmpty())
            {
                throw new InvalidPathException("Invalid target name: " + target);
            }
            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* NameSystem.createSymlink: target=" + target +
                                              " link=" + link);
            }
            FSPermissionChecker pc = fsn.GetPermissionChecker();

            byte[][]     pathComponents = FSDirectory.GetPathComponentsForReservedPath(link);
            INodesInPath iip;

            fsd.WriteLock();
            try
            {
                link = fsd.ResolvePath(pc, link, pathComponents);
                iip  = fsd.GetINodesInPath4Write(link, false);
                if (!createParent)
                {
                    fsd.VerifyParentDir(iip, link);
                }
                if (!fsd.IsValidToCreate(link, iip))
                {
                    throw new IOException("failed to create link " + link + " either because the filename is invalid or the file exists"
                                          );
                }
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckAncestorAccess(pc, iip, FsAction.Write);
                }
                // validate that we have enough inodes.
                fsn.CheckFsObjectLimit();
                // add symbolic link to namespace
                AddSymlink(fsd, link, iip, target, dirPerms, createParent, logRetryCache);
            }
            finally
            {
                fsd.WriteUnlock();
            }
            NameNode.GetNameNodeMetrics().IncrCreateSymlinkOps();
            return(fsd.GetAuditFileInfo(iip));
        }
Ejemplo n.º 14
0
        /// <summary>Set the given directory as a snapshottable directory.</summary>
        /// <remarks>
        /// Set the given directory as a snapshottable directory.
        /// If the path is already a snapshottable directory, update the quota.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        public virtual void SetSnapshottable(string path, bool checkNestedSnapshottable)
        {
            INodesInPath   iip = fsdir.GetINodesInPath4Write(path);
            INodeDirectory d   = INodeDirectory.ValueOf(iip.GetLastINode(), path);

            if (checkNestedSnapshottable)
            {
                CheckNestedSnapshottable(d, path);
            }
            if (d.IsSnapshottable())
            {
                //The directory is already a snapshottable directory.
                d.SetSnapshotQuota(DirectorySnapshottableFeature.SnapshotLimit);
            }
            else
            {
                d.AddSnapshottableFeature();
            }
            AddSnapshottable(d);
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        /// <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);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        /// <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));
        }
Ejemplo n.º 19
0
        /// <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));
        }
Ejemplo n.º 20
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.º 21
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.º 22
0
        /// <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 Delete(FSNamesystem fsn, string src, bool
                                                         recursive, bool logRetryCache)
        {
            FSDirectory         fsd = fsn.GetFSDirectory();
            FSPermissionChecker pc  = fsd.GetPermissionChecker();

            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            src = fsd.ResolvePath(pc, src, pathComponents);
            INodesInPath iip = fsd.GetINodesInPath4Write(src, false);

            if (!recursive && fsd.IsNonEmptyDirectory(iip))
            {
                throw new PathIsNotEmptyDirectoryException(src + " is non empty");
            }
            if (fsd.IsPermissionEnabled())
            {
                fsd.CheckPermission(pc, iip, false, null, FsAction.Write, null, FsAction.All, true
                                    );
            }
            return(DeleteInternal(fsn, src, iip, logRetryCache));
        }
Ejemplo n.º 23
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.º 24
0
        /// <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));
        }
Ejemplo n.º 25
0
        /// <exception cref="System.Exception"/>
        private void TestTruncate(long newLength, long expectedDiff, long expectedUsage)
        {
            // before doing the real truncation, make sure the computation is correct
            INodesInPath iip      = fsdir.GetINodesInPath4Write(file.ToString());
            INodeFile    fileNode = iip.GetLastINode().AsFile();

            fileNode.RecordModification(iip.GetLatestSnapshotId(), true);
            long diff = fileNode.ComputeQuotaDeltaForTruncate(newLength);

            NUnit.Framework.Assert.AreEqual(expectedDiff, diff);
            // do the real truncation
            dfs.Truncate(file, newLength);
            // wait for truncate to finish
            TestFileTruncate.CheckBlockRecovery(file, dfs);
            INodeDirectory dirNode   = fsdir.GetINode4Write(dir.ToString()).AsDirectory();
            long           spaceUsed = dirNode.GetDirectoryWithQuotaFeature().GetSpaceConsumed().GetStorageSpace
                                           ();
            long diskUsed = dirNode.GetDirectoryWithQuotaFeature().GetSpaceConsumed().GetTypeSpaces
                                ().Get(StorageType.Disk);

            NUnit.Framework.Assert.AreEqual(expectedUsage, spaceUsed);
            NUnit.Framework.Assert.AreEqual(expectedUsage, diskUsed);
        }
Ejemplo n.º 26
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.º 27
0
        /// <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);
        }
Ejemplo n.º 28
0
        /// <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));
        }
Ejemplo n.º 29
0
        /// <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));
        }
Ejemplo n.º 30
0
        /// <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
                                               );
        }