{"record":{"id":"5a976e33608cd9ec","repo":"apache/hadoop","slug":"cannot-create-directory-curdir","errorCode":null,"errorMessage":"Cannot create directory {curDir}","messagePattern":"Cannot create directory (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java","lineNumber":447,"sourceCode":"     * Derived storage is responsible for setting specific storage values and\n     * writing the version file to disk.\n     * \n     * @throws IOException\n     */\n    public void clearDirectory() throws IOException {\n      File curDir = this.getCurrentDir();\n      if (curDir == null) {\n        // if the directory is null, there is nothing to do.\n        return;\n      }\n      if (curDir.exists()) {\n        File[] files = FileUtil.listFiles(curDir);\n        LOG.info(\"Will remove files: {}\", Arrays.toString(files));\n        if (!(FileUtil.fullyDelete(curDir)))\n          throw new IOException(\"Cannot remove current directory: \" + curDir);\n      }\n      if (!curDir.mkdirs()) {\n        throw new IOException(\"Cannot create directory \" + curDir);\n      }\n      if (permission != null) {\n        try {\n          Set<PosixFilePermission> permissions =\n              PosixFilePermissions.fromString(permission.toString());\n          Files.setPosixFilePermissions(curDir.toPath(), permissions);\n        } catch (UnsupportedOperationException uoe) {\n          // Default to FileUtil for non posix file systems\n          FileUtil.setPermission(curDir, permission);\n        }\n      }\n    }\n\n    /**\n     * Directory {@code current} contains latest files defining\n     * the file system meta-data.\n     * \n     * @return the directory path","sourceCodeStart":429,"sourceCodeEnd":465,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java#L429-L465","documentation":"The second guard in Storage.StorageDirectory.clearDirectory(): after successfully deleting current/, curDir.mkdirs() must recreate it, and failure throws IOException('Cannot create directory <curDir>'). This is a plain filesystem failure — the parent must be writable and the path must be free.","triggerScenarios":"Format/rollback/finalize where current/ was deleted but recreation fails: no write permission on the parent directory, a non-directory file occupies the path, the filesystem is read-only, or the disk is full.","commonSituations":"Storage dir owned by root while the daemon runs as hdfs; a leftover file named 'current'; read-only mount after disk errors; full data disk on a DataNode.","solutions":["Check parent directory permissions/ownership for the daemon user (chown/chmod the storage root)","Remove any non-directory file occupying the current path","Free space and confirm the mount is read-write (df -h, mount), then retry the operation"],"exampleFix":"# before\ndf -h /dfs/nn   # 100% full, or root-owned\n\n# after\nsudo chown -R hdfs:hdfs /dfs/nn && sudo chmod 755 /dfs/nn\ndf -h /dfs/nn   # space available\nsudo -u hdfs hdfs namenode -format","handlingStrategy":"validation","validationCode":"File curDir = sd.getCurrentDir();\nFile parent = curDir.getParentFile();\nif (!parent.canWrite()) throw new IOException(\"No write permission on \" + parent);\nif (curDir.exists() && !curDir.isDirectory()) throw new IOException(curDir + \" is a file\");\nif (parent.getUsableSpace() < MIN_FREE_BYTES) throw new IOException(\"Insufficient space under \" + parent);\nsd.clearDirectory();","typeGuard":null,"tryCatchPattern":"try {\n  sd.clearDirectory();\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"Cannot create directory\")) {\n    throw new IOException(\"Fix permissions/space on parent of \" + sd.getCurrentDir(), e);\n  } else throw e;\n}","preventionTips":["Pre-flight format scripts: check parent ownership, writability, and free space (df) first","Keep storage mounts dedicated and monitored for fill/read-only transitions","Ensure no regular file can shadow the 'current' path"],"tags":["hdfs","storage","format","io","permissions","disk-full"],"backgroundTag":"cannot-create-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}