Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9D968C433F5 for ; Thu, 9 Dec 2021 01:21:31 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4J8bpd6C5pz307C for ; Thu, 9 Dec 2021 12:21:29 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.ozlabs.org; s=201707; t=1639012889; bh=KEjeKIDz2jd1Bgf2lTFVJbKZTfXuUAY/DsDcj20NZ/A=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=BW2H6xZIKOwlA8pVmi82Bc1Fihfg5u1yiAmfIqBuFnQbaccdcfRonN7nA40pr4pPf qPA6J+fLphhN/EShUOFeE8McJE1wvgrwb1E90BaU9BSazuPR25yS0iTfl4kOHO6+vx z4U/dkwyjppJ5bMR+9J37kZyfv+YBIDBOhKR22iSt/e7QeTvZ1wNGQ0faJWpwUOERJ Zs97wO0JHqp7PWAprZxP9fi9pL3nE/xQEIQzSd+b0HFraOFTGDwYmh/+18RE9yimcM QqfrHH/JhnsulZET9M+9So35Z/MR9RtvV4ewMqoL9qPlvjK2cXHQ0v3VHZRQoV0Plx 2/6Uizn3jSNhA== Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=flex--zhangkelvin.bounces.google.com (client-ip=2607:f8b0:4864:20::74a; helo=mail-qk1-x74a.google.com; envelope-from=3efqxyqskc50we7kdhbisfkdlldib.9ljifkru-bolcpifpqp.lwi78p.lod@flex--zhangkelvin.bounces.google.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20210112 header.b=nlfJHz69; dkim-atps=neutral Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4J8bpY63q0z2yPs for ; Thu, 9 Dec 2021 12:21:23 +1100 (AEDT) Received: by mail-qk1-x74a.google.com with SMTP id bm33-20020a05620a19a100b0046f1b6f3526so5228459qkb.21 for ; Wed, 08 Dec 2021 17:21:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KEjeKIDz2jd1Bgf2lTFVJbKZTfXuUAY/DsDcj20NZ/A=; b=pHZqcW9whAQbADtekH7KchAjEt3BHuZrfj9SZezPCNOC+sZmvQxoclPmNS4zVQaGcr 6SVIYjNq2Q1TMbHp40qKJXB5Y38K4rrJJypJB6VH/PQLtKBq8y+kLivPNhkV6ZS68ndH URThgAatfiEyDVVI4wpQO3rpqvOx02WrkpHUhGJfBEfVu5H2mSBAiotxu5ktd5zbHtn/ RuYULzumdNG2i4/PSuxSdeGU+eT6w2X7rd/j5FsQNjCb68tw3S0mLxh/mh+Fo1EYx/VB dLOuJV/l9XYTi3ZB5CYnDUdFvpjrbdOm8WsWIKjdmcAR2n+iSKK9GqFqaTjmvR7NWIIs PH2w== X-Gm-Message-State: AOAM5307RwXkkM4dxBzJPbp6HXMFrrgIWWnR6jTI8EWabFW96y+7lRna tqq09lAFTGspA4qdfGYupWHZzVnbyWVQjKSuHkqu2ykh/9qZfx/21YVZzjueB4KAG2zH5Kq0Hel JapXRAL982SkvD0XsoM/l+0p5DUFm9ROU3GL5xro13AYvo62iNXkAsvtw4iykuDg802lj2LY98X 8SgStuCgE= X-Google-Smtp-Source: ABdhPJxC3VD4LJ5Ldfxd9eiP+sX/V14LY+bqNrJ7VhqHh6sR5woYodNHklpBkGozs2Q5rOAi+hXpadqx35CUoH4/1Q== X-Received: from zhangkelvin-big.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:1f4a]) (user=zhangkelvin job=sendgmr) by 2002:a05:622a:210:: with SMTP id b16mr13122177qtx.613.1639012880313; Wed, 08 Dec 2021 17:21:20 -0800 (PST) Date: Wed, 8 Dec 2021 17:21:16 -0800 In-Reply-To: <20211209004659.4161847-1-zhangkelvin@google.com> Message-Id: <20211209012118.4175351-1-zhangkelvin@google.com> Mime-Version: 1.0 References: <20211209004659.4161847-1-zhangkelvin@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH v1] Add API to get full pathname of an inode To: linux-erofs mailing list , Miao Xie , Fang Wei Content-Type: text/plain; charset="UTF-8" X-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Kelvin Zhang via Linux-erofs Reply-To: Kelvin Zhang Cc: Kelvin Zhang Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" Change-Id: Iba99e590f051638285d1d1949311a08f5a5f1a82 Signed-off-by: Kelvin Zhang --- dump/main.c | 70 ++----------------------- include/erofs/internal.h | 5 ++ lib/namei.c | 108 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 67 deletions(-) diff --git a/dump/main.c b/dump/main.c index b7560ec..bcaaa21 100644 --- a/dump/main.c +++ b/dump/main.c @@ -11,6 +11,7 @@ #include "erofs/print.h" #include "erofs/inode.h" #include "erofs/io.h" +#include "erofs/internal.h" #ifdef HAVE_LIBUUID #include @@ -357,71 +358,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid) return 0; } -static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid, - erofs_nid_t target, char *path, unsigned int pos) -{ - int err; - erofs_off_t offset; - char buf[EROFS_BLKSIZ]; - struct erofs_inode inode = { .nid = nid }; - - path[pos++] = '/'; - if (target == sbi.root_nid) - return 0; - - err = erofs_read_inode_from_disk(&inode); - if (err) { - erofs_err("read inode failed @ nid %llu", nid | 0ULL); - return err; - } - - offset = 0; - while (offset < inode.i_size) { - erofs_off_t maxsize = min_t(erofs_off_t, - inode.i_size - offset, EROFS_BLKSIZ); - struct erofs_dirent *de = (void *)buf; - struct erofs_dirent *end; - unsigned int nameoff; - - err = erofs_pread(&inode, buf, maxsize, offset); - if (err) - return err; - - nameoff = le16_to_cpu(de->nameoff); - end = (void *)buf + nameoff; - while (de < end) { - const char *dname; - int len; - - nameoff = le16_to_cpu(de->nameoff); - dname = (char *)buf + nameoff; - len = erofs_checkdirent(de, end, maxsize, dname); - if (len < 0) - return len; - - if (de->nid == target) { - memcpy(path + pos, dname, len); - path[pos + len] = '\0'; - return 0; - } - - if (de->file_type == EROFS_FT_DIR && - de->nid != parent_nid && - de->nid != nid) { - memcpy(path + pos, dname, len); - err = erofs_get_pathname(de->nid, nid, - target, path, pos + len); - if (!err) - return 0; - memset(path + pos, 0, len); - } - ++de; - } - offset += maxsize; - } - return -1; -} - static int erofsdump_map_blocks(struct erofs_inode *inode, struct erofs_map_blocks *map, int flags) { @@ -461,12 +397,12 @@ static void erofsdump_show_fileinfo(bool show_extent) return; } - err = erofs_get_pathname(sbi.root_nid, sbi.root_nid, - inode.nid, path, 0); + err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1); if (err < 0) { erofs_err("file path not found @ nid %llu", inode.nid | 0ULL); return; } + printf("Path: %s\n", path); strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime)); diff --git a/include/erofs/internal.h b/include/erofs/internal.h index a68de32..632461e 100644 --- a/include/erofs/internal.h +++ b/include/erofs/internal.h @@ -305,6 +305,11 @@ int erofs_read_superblock(void); int erofs_read_inode_from_disk(struct erofs_inode *vi); int erofs_ilookup(const char *path, struct erofs_inode *vi); int erofs_read_inode_from_disk(struct erofs_inode *vi); +// Get full path name of an inode, return 0 if success +int erofs_get_inode_name(const struct erofs_sb_info* sbi, + erofs_nid_t target, + char* output_buffer, + size_t capacity); /* data.c */ int erofs_pread(struct erofs_inode *inode, char *buf, diff --git a/lib/namei.c b/lib/namei.c index 7377e74..ee76e82 100644 --- a/lib/namei.c +++ b/lib/namei.c @@ -274,3 +274,111 @@ int erofs_ilookup(const char *path, struct erofs_inode *vi) vi->nid = nd.nid; return erofs_read_inode_from_disk(vi); } + +static inline int erofs_checkdirent(const struct erofs_dirent* de, + const struct erofs_dirent* last_de, + u32 maxsize, + const char* dname) { + int dname_len; + unsigned int nameoff = le16_to_cpu(de->nameoff); + if (nameoff < sizeof(struct erofs_dirent) || nameoff >= PAGE_SIZE) { + erofs_err("invalid de[0].nameoff %u @ nid %llu", nameoff, de->nid | 0ULL); + return -EFSCORRUPTED; + } + dname_len = (de + 1 >= last_de) ? strnlen(dname, maxsize - nameoff) + : le16_to_cpu(de[1].nameoff) - nameoff; + /* a corrupted entry is found */ + if (nameoff + dname_len > maxsize || dname_len > EROFS_NAME_LEN) { + erofs_err("bogus dirent @ nid %llu", le64_to_cpu(de->nid) | 0ULL); + DBG_BUGON(1); + return -EFSCORRUPTED; + } + if (de->file_type >= EROFS_FT_MAX) { + erofs_err("invalid file type %llu", de->nid); + return -EFSCORRUPTED; + } + return dname_len; +} + +int erofs_get_pathname(const struct erofs_sb_info* sbi, + erofs_nid_t nid, + erofs_nid_t parent_nid, + erofs_nid_t target, + char* path, + size_t capacity) { + if (capacity <= 1) { + return -2; + } + int err; + erofs_off_t offset; + char buf[EROFS_BLKSIZ]; + struct erofs_inode inode = { .nid = nid }; + + path[0] = '/'; + if (target == sbi->root_nid) + return 0; + + err = erofs_read_inode_from_disk(&inode); + if (err) { + erofs_err("read inode failed @ nid %llu", nid | 0ULL); + return err; + } + + offset = 0; + while (offset < inode.i_size) { + erofs_off_t maxsize = min_t(erofs_off_t, + inode.i_size - offset, EROFS_BLKSIZ); + struct erofs_dirent *de = (void *)buf; + struct erofs_dirent *end; + unsigned int nameoff; + + err = erofs_pread(&inode, buf, maxsize, offset); + if (err) + return err; + + nameoff = le16_to_cpu(de->nameoff); + end = (void *)buf + nameoff; + while (de < end) { + const char *dname; + int len; + + nameoff = le16_to_cpu(de->nameoff); + dname = (char *)buf + nameoff; + len = erofs_checkdirent(de, end, maxsize, dname); + if (len < 0) + return len; + // First char is '/', last char is '\0' + // so usable capacity is |capacity| - 2 + if (len > capacity - 2) { + len = capacity - 2; + } + if (de->nid == target) { + memcpy(path + 1, dname, len); + path[1 + len] = '\0'; + return 0; + } + + if (de->file_type == EROFS_FT_DIR && + de->nid != parent_nid && + de->nid != nid) { + memcpy(path + 1, dname, len); + err = erofs_get_pathname(sbi, de->nid, nid, + target, path + 1 + len, capacity - 1 - len); + if (!err) + return 0; + memset(path + 1, 0, len); + } + ++de; + } + offset += maxsize; + } + return -1; +} + +int erofs_get_inode_name(const struct erofs_sb_info* sbi, + erofs_nid_t target, + char* path, + size_t capacity) { + return erofs_get_pathname(sbi, sbi->root_nid, sbi->root_nid, target, path, capacity); +} + -- 2.34.1.173.g76aa8bc2d0-goog