From patchwork Tue Sep 11 06:59:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 146412 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp3319575ljw; Tue, 11 Sep 2018 00:10:12 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZGBPwwjf6mChgddzM3Wks7GYcJAj7oeEY5PHB73CcYrPbLOz7168jyDsK48tqtnikDrgnx X-Received: by 2002:a05:6402:1348:: with SMTP id y8mr26494979edw.222.1536649812483; Tue, 11 Sep 2018 00:10:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536649812; cv=none; d=google.com; s=arc-20160816; b=0sYb94KDXKat+35R9MBO3IORoydgWt0fpA6Jgs9rJqj7lo1nXdt3ea3F+iTHbfEyUn Xnyb2q4UCK7tgZWSK9rUYA+0ns/6+/xlpbcWmwQULuKS/XGjy6/4M14LaIFL8shFg93X BMeVtK5zJAwTKYdFafMx7mhVbbNwuy3mbZcZAAhzmUEr7ZF6vApN34SdBC24gnbgPUTB +wBEnjJ+yegITEEic+i2+2PMzWiaoasmQizHUm7SuQQ5aU9o7sr1am/xZshCxrSW3eQk 87/d2MfX/yfSo/JIZdeLzTdTcSoPKy+YMecwAP8HlMM8FkbIKP2dEGF8DuYlfa0Lf36V sYKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:message-id :date:to:from:dkim-signature; bh=6xyOXqM+2tiS+2VQCDWEqEq0V9sB2bS2faqKqCG64h8=; b=zgOFMgdnbdw6U1wbghOLmOVL94pJ8D6qgZ9vlUS39ViduLywqxl+WixVPkU2KBGH0e fueJR4FWWxcP3MCbG1vQyfzmxWPWo6OeOgHLWIPhlvKtEbDVEe6Ku9zRu2YxZwadEJpy QXPtR3ukUVf5YE/EA5MFjfqxD02Nd32j9ZBhoauvd87yK5IHyg9fwb/mYAKGEHtMOebp OyPA35esSRKdO8D8gmw1eyrsLZ9dPeGkuoagWMX1Cp6DprTAFwoYIos/J34O0Zva0eJA UjO8RppjKLlGzKYv5Vhuup7v80TmPdt+rVSZ+4ehffGtR1mdJNnJvIB4WfD10YtbOj5K 41fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=QtfZ98lw; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id s13-v6si2010097edh.327.2018.09.11.00.10.12; Tue, 11 Sep 2018 00:10:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=QtfZ98lw; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id D753BC21E70; Tue, 11 Sep 2018 07:06:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id DA38EC21EFC; Tue, 11 Sep 2018 07:00:52 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 74ACFC21EC9; Tue, 11 Sep 2018 07:00:23 +0000 (UTC) Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by lists.denx.de (Postfix) with ESMTPS id E2782C21EFC for ; Tue, 11 Sep 2018 07:00:17 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id d19-v6so11767638pgv.1 for ; Tue, 11 Sep 2018 00:00:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4kbxUPVWkedb8CcqEPt5F1V4tG0kf9B6Ax7XOaTDTyU=; b=QtfZ98lw0FM47AX6q9sHjaojV47aV2BTGd65T9VuWbBnQEQbToUsqsSCHRRc5OuCZ1 2hN2UwpoBCIVZimBTErhjoggie06vOAlR1vRTg9NTqr1sTh8XP2jHdmqZbTIAqQ2GBAZ pOKfc2k7S8a+kQ4JZYp4o72t5uRfgpYYGrkp0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4kbxUPVWkedb8CcqEPt5F1V4tG0kf9B6Ax7XOaTDTyU=; b=jqJRz04pMnpWBt1QRS2+50FqLysoKv8ruuvgcZ38hS3L+ej8YPslZgwH7pXa3Pzh5J WHcEbYhsZCxeIIktxB5Nym/BEeOtdC7fI905TgPf4t0AIDG7gjQNegttc43V1xeGXLSa yX1lOs5UCTjo3syLS/njH91H9xvNxEVCuHANL7oBB1ldKQjaoJApg4TAP4BRiBvs/aLW QROVu2fYdBDwXWycN61oxb670FTsA6re1nvuL6LVKKg2dd+zsCsfKdPKZ2YNJzJ/0MRv 0/ruQTLL1rzko5QWpaLrEQxTDAFWOVLISg0AK6+Ln9k6kOVD63bejkKBoSI2hKaSuDfQ f+Dg== X-Gm-Message-State: APzg51COliiN49I9u6z8q0TthWK6/oZsqLkrz24f6pgJhgQTu0xgG7Jw can3Wt/Fq+svaE3PHtV8cA9ZyA== X-Received: by 2002:a63:4826:: with SMTP id v38-v6mr26767573pga.379.1536649211327; Tue, 11 Sep 2018 00:00:11 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id o21-v6sm26008596pfi.165.2018.09.11.00.00.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 00:00:10 -0700 (PDT) From: "Akashi, Takahiro" To: trini@konsulko.com Date: Tue, 11 Sep 2018 15:59:14 +0900 Message-Id: <20180911065922.19141-19-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911065922.19141-1-takahiro.akashi@linaro.org> References: <20180911065922.19141-1-takahiro.akashi@linaro.org> Cc: u-boot@lists.denx.de, xypron.glpk@gmx.de, agraf@suse.de Subject: [U-Boot] [PATCH v3 18/26] fs: fat: support unlink X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: AKASHI Takahiro In this patch, unlink support is added to FAT file system. A directory can be deleted only if it is empty. In this implementation, only a directory entry for a short file name will be removed. So entries for a long file name can and should be reclaimed with fsck. Signed-off-by: AKASHI Takahiro --- fs/fat/fat_write.c | 132 +++++++++++++++++++++++++++++++++++++++++++++ fs/fs.c | 3 +- include/fat.h | 1 + 3 files changed, 135 insertions(+), 1 deletion(-) diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 035469f31c8d..6d3d2d1abb04 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -1184,6 +1184,138 @@ int file_fat_write(const char *filename, void *buffer, loff_t offset, return file_fat_write_at(filename, offset, buffer, maxsize, actwrite); } +static int fat_dir_entries(fat_itr *itr) +{ + fat_itr *dirs; + fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata; + /* for FATBUFSIZE */ + int count; + + dirs = malloc_cache_aligned(sizeof(fat_itr)); + if (!dirs) { + debug("Error: allocating memory\n"); + count = -ENOMEM; + goto exit; + } + + /* duplicate fsdata */ + fat_itr_child(dirs, itr); + fsdata = *dirs->fsdata; + + /* allocate local fat buffer */ + fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE); + if (!fsdata.fatbuf) { + debug("Error: allocating memory\n"); + count = -ENOMEM; + goto exit; + } + fsdata.fatbufnum = -1; + dirs->fsdata = &fsdata; + + for (count = 0; fat_itr_next(dirs); count++) + ; + +exit: + free(fsdata.fatbuf); + free(dirs); + return count; +} + +static int delete_dentry(fat_itr *itr) +{ + fsdata *mydata = itr->fsdata; + dir_entry *dentptr = itr->dent; + + /* free cluster blocks */ + clear_fatent(mydata, START(dentptr)); + if (flush_dirty_fat_buffer(mydata) < 0) { + printf("Error: flush fat buffer\n"); + return -EIO; + } + + /* + * update a directory entry + * TODO: + * - long file name support + * - find and mark the "new" first invalid entry as name[0]=0x00 + */ + memset(dentptr, 0, sizeof(*dentptr)); + dentptr->name[0] = 0xe5; + + if (set_cluster(mydata, itr->clust, itr->block, + mydata->clust_size * mydata->sect_size) != 0) { + printf("error: writing directory entry\n"); + return -EIO; + } + + return 0; +} + +int fat_unlink(const char *filename) +{ + fsdata fsdata = { .fatbuf = NULL, }; + fat_itr *itr = NULL; + int n_entries, ret; + char *filename_copy, *dirname, *basename; + + filename_copy = strdup(filename); + split_filename(filename_copy, &dirname, &basename); + + if (!strcmp(dirname, "/") && !strcmp(basename, "")) { + printf("Error: cannot remove root\n"); + ret = -EINVAL; + goto exit; + } + + itr = malloc_cache_aligned(sizeof(fat_itr)); + if (!itr) { + printf("Error: allocating memory\n"); + return -ENOMEM; + } + + ret = fat_itr_root(itr, &fsdata); + if (ret) + goto exit; + + total_sector = fsdata.total_sect; + + ret = fat_itr_resolve(itr, dirname, TYPE_DIR); + if (ret) { + printf("%s: doesn't exist (%d)\n", dirname, ret); + ret = -ENOENT; + goto exit; + } + + if (!find_directory_entry(itr, basename)) { + printf("%s: doesn't exist\n", basename); + ret = -ENOENT; + goto exit; + } + + if (fat_itr_isdir(itr)) { + n_entries = fat_dir_entries(itr); + if (n_entries < 0) { + ret = n_entries; + goto exit; + } + if (n_entries > 2) { + printf("Error: directory is not empty: %d\n", + n_entries); + ret = -EINVAL; + goto exit; + } + } + + ret = delete_dentry(itr); + +exit: + free(fsdata.fatbuf); + free(itr); + free(filename_copy); + + return ret; +} + int fat_mkdir(const char *new_dirname) { dir_entry *retdent; diff --git a/fs/fs.c b/fs/fs.c index ba9a65166c70..adae98d021ee 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -170,16 +170,17 @@ static struct fstype_info fstypes[] = { .read = fat_read_file, #ifdef CONFIG_FAT_WRITE .write = file_fat_write, + .unlink = fat_unlink, .mkdir = fat_mkdir, #else .write = fs_write_unsupported, + .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, #endif .uuid = fs_uuid_unsupported, .opendir = fat_opendir, .readdir = fat_readdir, .closedir = fat_closedir, - .unlink = fs_unlink_unsupported, }, #endif #ifdef CONFIG_FS_EXT4 diff --git a/include/fat.h b/include/fat.h index 97460a3cdff1..ca92a735b4fd 100644 --- a/include/fat.h +++ b/include/fat.h @@ -205,6 +205,7 @@ int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, int fat_opendir(const char *filename, struct fs_dir_stream **dirsp); int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp); void fat_closedir(struct fs_dir_stream *dirs); +int fat_unlink(const char *filename); int fat_mkdir(const char *dirname); void fat_close(void); #endif /* CONFIG_FS_FAT */