Received: by 10.223.185.111 with SMTP id b44csp1643379wrg; Sat, 10 Mar 2018 10:26:18 -0800 (PST) X-Google-Smtp-Source: AG47ELsEtFZ0xVHMzXK+EPW6rdXp+9tt+YAc5/Ocl4HfEM2vKNsGLgQMYqTdKHrgDsmyHPaBjIAI X-Received: by 2002:a17:902:7f0c:: with SMTP id d12-v6mr2708632plm.350.1520706378401; Sat, 10 Mar 2018 10:26:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520706378; cv=none; d=google.com; s=arc-20160816; b=C+836ixWUjIvAgM04qfv0zgkd7yP9gxD/TTmfwXxjbKPeFI/yfm2P/KSVKPlCxdW7Z Z1dd20lPzTMSnhl25KpvxDi8UxqSxZFLwSX3sKBj9OWjv77gas+pzjyeG/lLB9+Fk2vW BKSCI9zkv1tbYQjlTOQhVi3pnBCdnIiXrSeQ/S87ZKm9CIvWZ9p0tVWd0LyvhN0sBcCL ui05rYNSzueDVVhUQ/ew8fD/HDNaW0mNnmO7PuHM9RwFk2RDXvLWfWXh0MKs52rzzlNH dAJw9MqcdZZCKYX8BGZL5oAHbm/meQcg5xyBcLO6X7t2UfNw80nHjpYJq0myTyRyTNp8 5cxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Y+M9z+OT5Akc0ZtqHYzxjpLv0kr9GZoO8tAK3lm6e2o=; b=z2cfnEbPfaaDdpndsjT921uoPJ/01ohmZ8auz3izifwmfTEQHgUcZOmBoJCfJgAgFH N3uMyQ6VMiUXGuzz6mIjsLA7L/1qAptBv6Hwb33s/TtNOl3OYDejyYbUrLKY9WXOvuvN 5YhmmQlWvc24fHisnTYqkXio+WlFGl18MaXAPR6LLBH61P9aK2dmjGzp1Dbthr/ZNyBV 2wvqifQqy3FM84RIRpc1m/6zB4I3Rf4Qt4fGsDrPLxEHOJWvIEv57sGzZ3fP9V4xy69J P1LOVzpBSrvRQPqUEWK0emVJaVWm33ylaTZMrVu/OXRGTq0WQ/UGhkAfRekxTpGa6UTo VQ6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@eng.ucsd.edu header.s=google header.b=OO2e4j9T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b61-v6si3041086plc.385.2018.03.10.10.26.03; Sat, 10 Mar 2018 10:26:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@eng.ucsd.edu header.s=google header.b=OO2e4j9T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932913AbeCJSVq (ORCPT + 99 others); Sat, 10 Mar 2018 13:21:46 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:41911 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932892AbeCJSVo (ORCPT ); Sat, 10 Mar 2018 13:21:44 -0500 Received: by mail-pg0-f68.google.com with SMTP id w17so768456pgq.8 for ; Sat, 10 Mar 2018 10:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eng.ucsd.edu; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Y+M9z+OT5Akc0ZtqHYzxjpLv0kr9GZoO8tAK3lm6e2o=; b=OO2e4j9TwaugZFnkrCX5KxLYBBqaN9LIwAnTT7VvwhvFyIHXx9dAc/GmpfqnSQHm2C bTtHXlUeJGhlpQ+yjSll4E1E22JqVU06rN6KcxJVOB8Qp3qJbVL/r7KttTtIkUy8Hyvg AebKpoLAo7t7yBpzJVz08RVGVau0deiLv4HnQ= 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=Y+M9z+OT5Akc0ZtqHYzxjpLv0kr9GZoO8tAK3lm6e2o=; b=gToMeOU5NXg7YDCqY8taPrrB6th/qWR7xZoTi3XnUekRqeyQeJYBPc4DA8+Xu/y11L bco/Zkjvz8+yUigi9BtwHL+lYpMezeTDcv8VC/90/ni43CZ/uLo/+8a3mLyvC3aldl1E 1aT59kwFzE1o2WPhI2q0dDyWP38tpQ433lGiRZ+o++YQpNwMd98CN6LEnTHRyQBbYrvn /yCFwz8C0TfpdhSolWF0eux15c/iBVKwG0wUHulcXwYNZybNJXMQfrM8s2PNQ3lw7qm2 62OyHIfKY0n7G5x6qg6wFmtxDaljXem22MkfUrMvmWo5vG+1yqZ9LNSsrvH9WQ3aD57E xbnQ== X-Gm-Message-State: AElRT7G14qTZBT7mDlaQSHGzPGczdkWqGebRy09+BfpZ4axnmOssLLv0 c2FiNLSgMwvRMWokcvHFl9Ztpw== X-Received: by 10.101.93.82 with SMTP id e18mr2171558pgt.371.1520706103422; Sat, 10 Mar 2018 10:21:43 -0800 (PST) Received: from brienza-desktop.8.8.4.4 (andxu.ucsd.edu. [132.239.17.134]) by smtp.gmail.com with ESMTPSA id h80sm9210167pfj.181.2018.03.10.10.21.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 10 Mar 2018 10:21:42 -0800 (PST) From: Andiry Xu To: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvdimm@lists.01.org Cc: dan.j.williams@intel.com, andy.rudoff@intel.com, coughlan@redhat.com, swanson@cs.ucsd.edu, david@fromorbit.com, jack@suse.com, swhiteho@redhat.com, miklos@szeredi.hu, andiry.xu@gmail.com, Andiry Xu Subject: [RFC v2 72/83] File operation: fallocate. Date: Sat, 10 Mar 2018 10:18:53 -0800 Message-Id: <1520705944-6723-73-git-send-email-jix024@eng.ucsd.edu> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520705944-6723-1-git-send-email-jix024@eng.ucsd.edu> References: <1520705944-6723-1-git-send-email-jix024@eng.ucsd.edu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andiry Xu Fallocate works similar as writes, allocating zeroed blocked for the holes in the request region. Signed-off-by: Andiry Xu --- fs/nova/file.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/nova/nova.h | 5 ++ 2 files changed, 153 insertions(+) diff --git a/fs/nova/file.c b/fs/nova/file.c index b94a9a3..a6b5bd3 100644 --- a/fs/nova/file.c +++ b/fs/nova/file.c @@ -113,6 +113,153 @@ static int nova_open(struct inode *inode, struct file *filp) return generic_file_open(inode, filp); } +static long nova_fallocate(struct file *file, int mode, loff_t offset, + loff_t len) +{ + struct inode *inode = file->f_path.dentry->d_inode; + struct super_block *sb = inode->i_sb; + struct nova_inode_info *si = NOVA_I(inode); + struct nova_inode_info_header *sih = &si->header; + struct nova_inode *pi; + struct nova_file_write_entry *entry; + struct nova_file_write_item *entry_item; + struct list_head item_head; + struct nova_inode_update update; + unsigned long start_blk, num_blocks, ent_blks = 0; + unsigned long total_blocks = 0; + unsigned long blocknr = 0; + unsigned long blockoff; + loff_t new_size; + long ret = 0; + int inplace = 0; + int blocksize_mask; + int allocated = 0; + timing_t fallocate_time; + u64 epoch_id; + u32 time; + + /* + * Fallocate does not make much sence for CoW, + * but we still support it for DAX-mmap purpose. + */ + + /* We only support the FALLOC_FL_KEEP_SIZE mode */ + if (mode & ~FALLOC_FL_KEEP_SIZE) + return -EOPNOTSUPP; + + if (S_ISDIR(inode->i_mode)) + return -ENODEV; + + INIT_LIST_HEAD(&item_head); + new_size = len + offset; + if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size > inode->i_size) { + ret = inode_newsize_ok(inode, new_size); + if (ret) + return ret; + } else { + new_size = inode->i_size; + } + + nova_dbgv("%s: inode %lu, offset %lld, count %lld, mode 0x%x\n", + __func__, inode->i_ino, offset, len, mode); + + NOVA_START_TIMING(fallocate_t, fallocate_time); + inode_lock(inode); + sih_lock(sih); + + pi = nova_get_inode(sb, inode); + if (!pi) { + ret = -EACCES; + goto out; + } + + inode->i_mtime = inode->i_ctime = current_time(inode); + time = current_time(inode).tv_sec; + + blocksize_mask = sb->s_blocksize - 1; + start_blk = offset >> sb->s_blocksize_bits; + blockoff = offset & blocksize_mask; + num_blocks = (blockoff + len + blocksize_mask) >> sb->s_blocksize_bits; + + epoch_id = nova_get_epoch_id(sb); + update.tail = sih->log_tail; + while (num_blocks > 0) { + ent_blks = nova_check_existing_entry(sb, inode, num_blocks, + start_blk, &entry, + 1, epoch_id, &inplace); + + if (entry && inplace) { + if (entry->size < new_size) { + /* Update existing entry */ + entry->size = new_size; + nova_persist_entry(entry); + } + allocated = ent_blks; + goto next; + } + + /* Allocate zeroed blocks to fill hole */ + allocated = nova_new_data_blocks(sb, sih, &blocknr, start_blk, + ent_blks, ALLOC_INIT_ZERO, ANY_CPU, + ALLOC_FROM_HEAD); + nova_dbgv("%s: alloc %d blocks @ %lu\n", __func__, + allocated, blocknr); + + if (allocated <= 0) { + nova_dbg("%s alloc %lu blocks failed!, %d\n", + __func__, ent_blks, allocated); + ret = allocated; + goto out; + } + + entry_item = nova_alloc_file_write_item(sb); + if (!entry_item) { + ret = -ENOMEM; + goto out; + } + + /* Handle hole fill write */ + nova_init_file_write_item(sb, sih, entry_item, epoch_id, + start_blk, allocated, blocknr, + time, new_size); + + list_add_tail(&entry_item->list, &item_head); + + total_blocks += allocated; +next: + num_blocks -= allocated; + start_blk += allocated; + } + + ret = nova_commit_writes_to_log(sb, pi, inode, + &item_head, total_blocks, 1); + if (ret < 0) { + nova_err(sb, "commit to log failed\n"); + goto out; + } + + if (ret || (mode & FALLOC_FL_KEEP_SIZE)) { + pi->i_flags |= cpu_to_le32(NOVA_EOFBLOCKS_FL); + sih->i_flags |= cpu_to_le32(NOVA_EOFBLOCKS_FL); + } + + if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size > inode->i_size) { + inode->i_size = new_size; + sih->i_size = new_size; + } + + nova_persist_inode(pi); + +out: + if (ret < 0) + nova_cleanup_incomplete_write(sb, sih, &item_head, 1); + + sih_unlock(sih); + inode_unlock(inode); + NOVA_END_TIMING(fallocate_t, fallocate_time); + return ret; +} + static ssize_t do_dax_mapping_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) @@ -477,6 +624,7 @@ const struct file_operations nova_dax_file_operations = { .open = nova_open, .fsync = nova_fsync, .flush = nova_flush, + .fallocate = nova_fallocate, }; const struct inode_operations nova_file_inode_operations = { diff --git a/fs/nova/nova.h b/fs/nova/nova.h index 6392bb3..ab9e8f3 100644 --- a/fs/nova/nova.h +++ b/fs/nova/nova.h @@ -477,6 +477,11 @@ void nova_init_file_write_item(struct super_block *sb, struct nova_inode_info_header *sih, struct nova_file_write_item *item, u64 epoch_id, u64 pgoff, int num_pages, u64 blocknr, u32 time, u64 file_size); +unsigned long nova_check_existing_entry(struct super_block *sb, + struct inode *inode, unsigned long num_blocks, unsigned long start_blk, + struct nova_file_write_entry **ret_entry, + int check_next, u64 epoch_id, + int *inplace); ssize_t nova_inplace_file_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos); ssize_t do_nova_inplace_file_write(struct file *filp, const char __user *buf, -- 2.7.4