From patchwork Thu Dec 5 08:01:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Pang X-Patchwork-Id: 13894817 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.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (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 5EF67E77171 for ; Thu, 5 Dec 2024 08:01:59 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1tJ6o9-0002yB-9u; Thu, 05 Dec 2024 08:01:58 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tJ6o8-0002xe-Ie for linux-f2fs-devel@lists.sourceforge.net; Thu, 05 Dec 2024 08:01:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=3eMlSAULgoo0XpQdaR1ooZz9GNiNy/RVWekpYrnkZv8=; b=FFrn6FpGxIda1rdQqtOplfEEdb iSGLnoL642jXn4kYwvwAeYIP5W6UZMUsDgyVJMvCddEOjJkqF2UYvHrwfKD93pyECxtIBxe77U8NF pxQV0yhsKbR7UzcsEUpEdegLU1/YFgDdXVYL5ExZ3211ebZ+s+Ura/TM5lcSAjfHH7fM=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=3eMlSAULgoo0XpQdaR1ooZz9GNiNy/RVWekpYrnkZv8=; b=C rHKKLHsepunX+Hl3KbmfO/8xKFsLV1IQugtEHuVqWbutUqMMFMcYZaI5t0Jys6WkkjSsReRMuMOJ1 EPiZQjKww57szCSvvbw3jqnsUnBH/027gTQl6jO9SIcYPkbSS36n9ER1mj7x+u5VAQDb4sv24pUJi KB79XgFkOBnBsvZ4=; Received: from mail-pf1-f169.google.com ([209.85.210.169]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1tJ6o8-0006fr-LO for linux-f2fs-devel@lists.sourceforge.net; Thu, 05 Dec 2024 08:01:57 +0000 Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7253bc4d25eso530593b3a.0 for ; Thu, 05 Dec 2024 00:01:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733385711; x=1733990511; darn=lists.sourceforge.net; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3eMlSAULgoo0XpQdaR1ooZz9GNiNy/RVWekpYrnkZv8=; b=lc+C2LvOy0MqnH7Sqp6smNlzgK7jSvE3JpFXastoziQTyfb88+I7B+ZwDZrYJlAv6T 2VZsdXHa1ZDZ3fEHTpTQx9H3WpsJF3MTpwhC+EU//cCK3c6w+lDdQXSHfh+ufHp1O0eT njFIkcvJQYdLCqQ1B9rU6WoPKP7XE7XvoGkdw5Ompb39lQWDGZ8OLQwpdvCx/xEW4PRB 24cauh7xBJafH0emKHruFXhNlRAhOvZ96Cee+wPloEG4oUJXRKdQxL3sB70RywLZLSZB 4yC84lvVxc/Rzs4IwhCGm8TZRirJdAuK1kghehkoeGw4PMVBYoAfiw/Ix3q4vRNxzovz LSFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733385711; x=1733990511; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3eMlSAULgoo0XpQdaR1ooZz9GNiNy/RVWekpYrnkZv8=; b=XLsz2iC5OxB1UiDd/E1L/b4R1ETaV0ZYmWbhxNX4pTcIrY72QVm89JfkTxXB8JgmYw rjCbhVW0eGSibL7acP6C3mVgRC4Oabi5YwuD9IAbcJ4/4cJ2abrpO22A7RtnTA7RYpZt UMTPPndkgxfv10TlG5vroI5gyIOJMsmCabxsvvZ/Ac7yagvwI6JJ5rPmzmqS4QCbwuMd r8P1qCB5ChRINmbU9e2oFlLt6FMtxEJfujdhj0SSIjw723Xf3BKisGxecr8AZZBqwzOR tDex3ocaKZFDStkev6XC8eutVyBMjbM8Zdi03VkiWb0VVeTaaIW9rhGSMwWRqGrdrl6t HcXw== X-Forwarded-Encrypted: i=1; AJvYcCU++s6l3AR4+jNAZf5zTwoRgv0rQc+/a+rV78/s8uOwMexrk3HfYqr/zpNrzMU3/r4y/Uflq9G4Zki7drQkCX4Q@lists.sourceforge.net X-Gm-Message-State: AOJu0Yw9TZoSf/+TlsVRY5t3t9E+gHZGZR0LE81UyyeqE9rg1ri2sOyJ EzKrlS3QN589Y5ZCED7Ie0cLDux1rGvLU9me28dwz2/kappwyCyV9KLWALnH X-Gm-Gg: ASbGncv/7gnho+QkVD2BVOwy4TdAt0lmVikgzionzxUemxgkP4vLENXqp51hLRUkHnZ LmOOAmopA+NXjk7OeemPJE62iAgBuqqOFOfIblPf4K9DBCXKAgsZ5iypoGRwc83Ytu/qDMvnSZz t28PfRXEmWOZzXdVtTTOPz4ecisKlXULoezCZ2bKNUzW9uLdbXf54CihLeyzjaaH1WVZJwM7yFp fKptNGbsGoXxf0jcRdj7GjfK1rYfNkny6JpyzC1+HxqqJIgG1nDieGm7g4E11w7En6kPPVSrIWr Dc4cw4RGTpJZ5MzIJEQUgaMo/u8= X-Google-Smtp-Source: AGHT+IHh8bvZGQKYeDY1Ysj24MvCSDYgu0i6OSK1z8Z7lj7NwI5NsQCHyiOBZ5KenIFigQOx1PyEng== X-Received: by 2002:a17:902:ec88:b0:215:a034:3bae with SMTP id d9443c01a7336-215f3ca19fdmr34292605ad.18.1733385710773; Thu, 05 Dec 2024 00:01:50 -0800 (PST) Received: from lu-HP-ProDesk-680-G6-PCI-Microtower-PC.mioffice.cn ([43.224.245.228]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-215f8e61ef7sm7177905ad.95.2024.12.05.00.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 00:01:50 -0800 (PST) From: Lu Pang To: chao@kernel.org Date: Thu, 5 Dec 2024 16:01:08 +0800 Message-ID: <20241205080109.43552-1-panglu2022@gmail.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Headers-End: 1tJ6o8-0006fr-LO Subject: [f2fs-dev] [PATCH] libf2fs_io: change lseek & read/write to pread/pwrite X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Lu Pang , jaegeuk@kernel.org, linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net Most of read() and write() are performed with lseek(), combining two operations into one can slightly improve the performance. Signed-off-by: Lu Pang --- lib/libf2fs_io.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c index 520ae03..6388ace 100644 --- a/lib/libf2fs_io.c +++ b/lib/libf2fs_io.c @@ -279,11 +279,7 @@ static int dcache_io_read(long entry, __u64 offset, off_t blk) if (fd < 0) return fd; - if (lseek(fd, offset, SEEK_SET) < 0) { - MSG(0, "\n lseek fail.\n"); - return -1; - } - if (read(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE) < 0) { + if (pread(fd, dcache_buf + entry * F2FS_BLKSIZE, F2FS_BLKSIZE, (off_t)offset) < 0) { MSG(0, "\n read() fail.\n"); return -1; } @@ -393,9 +389,7 @@ int dev_read_version(void *buf, __u64 offset, size_t len) { if (c.sparse_mode) return 0; - if (lseek(c.kd, (off_t)offset, SEEK_SET) < 0) - return -1; - if (read(c.kd, buf, len) < 0) + if (pread(c.kd, buf, len, (off_t)offset) < 0) return -1; return 0; } @@ -535,9 +529,7 @@ int dev_read(void *buf, __u64 offset, size_t len) fd = __get_device_fd(&offset); if (fd < 0) return fd; - if (lseek(fd, (off_t)offset, SEEK_SET) < 0) - return -1; - if (read(fd, buf, len) < 0) + if (pread(fd, buf, len, (off_t)offset) < 0) return -1; return 0; } @@ -615,8 +607,6 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint) if (fd < 0) return fd; - if (lseek(fd, (off_t)offset, SEEK_SET) < 0) - return -1; #if ! defined(__MINGW32__) if (c.need_whint && (c.whint != whint)) { @@ -629,7 +619,7 @@ static int __dev_write(void *buf, __u64 offset, size_t len, enum rw_hint whint) } #endif - if (write(fd, buf, len) < 0) + if (pwrite(fd, buf, len, (off_t)offset) < 0) return -1; c.need_fsync = true; @@ -663,9 +653,7 @@ int dev_write_block(void *buf, __u64 blk_addr, enum rw_hint whint) int dev_write_dump(void *buf, __u64 offset, size_t len) { - if (lseek(c.dump_fd, (off_t)offset, SEEK_SET) < 0) - return -1; - if (write(c.dump_fd, buf, len) < 0) + if (pwrite(c.dump_fd, buf, len, (off_t)offset) < 0) return -1; return 0; }