Received: by 10.223.185.111 with SMTP id b44csp1645965wrg; Sat, 10 Mar 2018 10:30:11 -0800 (PST) X-Google-Smtp-Source: AG47ELtX0vUJDwMT0wWGBxautmi2fiM+lqIQ0I3zdG+spjMHU4IrcHAHlzIy7SMkIFeYEdpQ8sxW X-Received: by 10.98.155.194 with SMTP id e63mr2646424pfk.95.1520706611090; Sat, 10 Mar 2018 10:30:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520706611; cv=none; d=google.com; s=arc-20160816; b=nfeKEp3OOIgMVCdZPIHM5vwdgwcZIILEBT4ZADh4kNl2+ygE3A1/UmYf9IRk2nY4Fe SvKwOiB9/EmqMYP5+ccGoCwszhRKN9h14pKiO2mOnFEmZF4FSLCbuD7lAsUX5ZjoWaaD q9kWObU7mPvwnG43NjpsS5qG0Bz9yN08U+A+b7yCGygf7Kyaf3Jr8MRbkx3tTyHXVuDO UTrY8NqPLmjsKTM05AyAENoXHj0v5Mq6C3ZT+2Xjss+CNMCJgkmGPVuK5N1tjhGGt8N4 EceKJCh/CztxPtTHhoPQBg9Z7N/L8Y1AB/hs/zPOOaGOVxGK3syN4vYOwjVLfKibdP+J 7vfA== 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=x7hxdKU6yWhvP20AEDX8pHRQ48mQy3WjYG4Q3B/8Dlg=; b=jT07+Ssr3p3PbZ6Ycbp5AqIunblYeK1CjReBYIx2Y1kypEHgW5REz31gQK5YLL8sTb mdKFSKnFhWAwTKVfWtH744zwU+ox38siCXp/ca9UghjGv09BjFtf+Xm/E2TCfNtCwM2K 6hS97lUZkJbfMb8qd7ssopkS+IbMShId5BlHwstcL79PKVp4RYZCfmA4CrZ5Cdd0/onL IsQvXfwtHBJMoc/TkizTBuiFNTo2B+rXPd66yjnmdwOaamdsDyBwpSn6gR68cIvx14Zr aw++9u2jvEDp+C4mR99Cu0v+Eof2exP3lf4L7578J9ydOu79jK7A5926K+bKnHf98mDm ZOSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@eng.ucsd.edu header.s=google header.b=c3LR06wa; 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 33-v6si3094692plg.227.2018.03.10.10.29.56; Sat, 10 Mar 2018 10:30:11 -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=c3LR06wa; 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 S1751512AbeCJS3H (ORCPT + 99 others); Sat, 10 Mar 2018 13:29:07 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:46210 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932769AbeCJSVc (ORCPT ); Sat, 10 Mar 2018 13:21:32 -0500 Received: by mail-pg0-f68.google.com with SMTP id r26so4828745pgv.13 for ; Sat, 10 Mar 2018 10:21:32 -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=x7hxdKU6yWhvP20AEDX8pHRQ48mQy3WjYG4Q3B/8Dlg=; b=c3LR06waSc5Ux2n3YuMlOuwC7xQsrX/A3IVJnkDoXJUvpDVXc9L/5jPQUjn4HN+FNa w9Bk5xJLFO15rojHXaj8aKkzSJV02PpyBcOIt4IKFsPzN1+ZD3vy91wa9rcJoxY+m5s0 5jxhT6ZT1xdgpG3GuqDgXOh91lVWYqm/hQ2Mw= 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=x7hxdKU6yWhvP20AEDX8pHRQ48mQy3WjYG4Q3B/8Dlg=; b=QB0XhZseiDhSQ1xgXhCyDTbdsvpXgWNfak1rQDT+oaeyTWM0g/wyzf6id/M3FH6bUl 8Tr15KfLohzvf4odszwqaVgdg7wVZzR2FwE/bb+p869JBUaL05+EMp+nP9AYbTSFx0i2 /3gSs6koMFjswkKbj5cCQvPZgr1OOeRyxkEn0Zs5Ba94FQlIoFXppyQ7NrIlovLv2jMP a/KS5kKGOfvVZUxzOuFvguCNfuUiu9HEx4npNu6DM4bXCIQBu0LV+VkR+31ISfyCahz3 XGBpTW4sHEn60VOZxLa//3dJ1DNWFeRfLGNfwnVJM3jkrUZLGsXwkjdm2tB35HYnGZTE O+aw== X-Gm-Message-State: AElRT7HW0d8j53Ma7WBE1nxDmPQ5cHTwDIyGBeEfisONPufankOFW6r2 eZchqxydJeVLbsNFo8CHcONwFw== X-Received: by 10.99.157.142 with SMTP id i136mr2284498pgd.14.1520706092317; Sat, 10 Mar 2018 10:21:32 -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.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 10 Mar 2018 10:21:31 -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 63/83] File operation: llseek. Date: Sat, 10 Mar 2018 10:18:44 -0800 Message-Id: <1520705944-6723-64-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 Search the file radix tree to find hold or data. Signed-off-by: Andiry Xu --- fs/nova/file.c | 47 +++++++++++++++++++++++ fs/nova/inode.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/nova/inode.h | 1 + fs/nova/nova.h | 1 + 4 files changed, 162 insertions(+) diff --git a/fs/nova/file.c b/fs/nova/file.c index b46d4bd..ecaf20a 100644 --- a/fs/nova/file.c +++ b/fs/nova/file.c @@ -23,6 +23,53 @@ #include "nova.h" #include "inode.h" +static loff_t nova_llseek(struct file *file, loff_t offset, int origin) +{ + struct inode *inode = file->f_path.dentry->d_inode; + struct nova_inode_info *si = NOVA_I(inode); + struct nova_inode_info_header *sih = &si->header; + int retval; + + if (origin != SEEK_DATA && origin != SEEK_HOLE) + return generic_file_llseek(file, offset, origin); + + sih_lock_shared(sih); + switch (origin) { + case SEEK_DATA: + retval = nova_find_region(inode, &offset, 0); + if (retval) { + sih_unlock_shared(sih); + return retval; + } + break; + case SEEK_HOLE: + retval = nova_find_region(inode, &offset, 1); + if (retval) { + sih_unlock_shared(sih); + return retval; + } + break; + } + + if ((offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) || + offset > inode->i_sb->s_maxbytes) { + sih_unlock_shared(sih); + return -ENXIO; + } + + if (offset != file->f_pos) { + file->f_pos = offset; + file->f_version = 0; + } + + sih_unlock_shared(sih); + return offset; +} + + +const struct file_operations nova_dax_file_operations = { + .llseek = nova_llseek, +}; const struct inode_operations nova_file_inode_operations = { .setattr = nova_notify_change, diff --git a/fs/nova/inode.c b/fs/nova/inode.c index 6fcc5e7..a6d74cb 100644 --- a/fs/nova/inode.c +++ b/fs/nova/inode.c @@ -193,6 +193,52 @@ static void nova_truncate_file_blocks(struct inode *inode, loff_t start, } +/* search the radix tree to find hole or data + * in the specified range + * Input: + * first_blocknr: first block in the specified range + * last_blocknr: last_blocknr in the specified range + * @data_found: indicates whether data blocks were found + * @hole_found: indicates whether a hole was found + * hole: whether we are looking for a hole or data + */ +static int nova_lookup_hole_in_range(struct super_block *sb, + struct nova_inode_info_header *sih, + unsigned long first_blocknr, unsigned long last_blocknr, + int *data_found, int *hole_found, int hole) +{ + struct nova_file_write_entry *entry; + unsigned long blocks = 0; + unsigned long pgoff, old_pgoff; + + pgoff = first_blocknr; + while (pgoff <= last_blocknr) { + old_pgoff = pgoff; + entry = radix_tree_lookup(&sih->tree, pgoff); + if (entry) { + *data_found = 1; + if (!hole) + goto done; + pgoff++; + } else { + *hole_found = 1; + entry = nova_find_next_entry(sb, sih, pgoff); + pgoff++; + if (entry) { + pgoff = pgoff > entry->pgoff ? + pgoff : entry->pgoff; + if (pgoff > last_blocknr) + pgoff = last_blocknr + 1; + } + } + + if (!*hole_found || !hole) + blocks += pgoff - old_pgoff; + } +done: + return blocks; +} + /* copy persistent state to struct inode */ static int nova_read_inode(struct super_block *sb, struct inode *inode, u64 pi_addr) @@ -232,6 +278,7 @@ static int nova_read_inode(struct super_block *sb, struct inode *inode, switch (inode->i_mode & S_IFMT) { case S_IFREG: inode->i_op = &nova_file_inode_operations; + inode->i_fop = &nova_dax_file_operations; break; case S_IFDIR: inode->i_op = &nova_dir_inode_operations; @@ -929,6 +976,7 @@ struct inode *nova_new_vfs_inode(enum nova_new_inode_type type, case TYPE_CREATE: inode->i_op = &nova_file_inode_operations; inode->i_mapping->a_ops = &nova_aops_dax; + inode->i_fop = &nova_dax_file_operations; break; case TYPE_MKNOD: init_special_inode(inode, mode, rdev); @@ -1170,6 +1218,71 @@ int nova_notify_change(struct dentry *dentry, struct iattr *attr) return ret; } +/* + * find the file offset for SEEK_DATA/SEEK_HOLE + */ +unsigned long nova_find_region(struct inode *inode, loff_t *offset, int hole) +{ + struct nova_inode_info *si = NOVA_I(inode); + struct nova_inode_info_header *sih = &si->header; + unsigned int data_bits = blk_type_to_shift[sih->i_blk_type]; + unsigned long first_blocknr, last_blocknr; + unsigned long blocks = 0, offset_in_block; + int data_found = 0, hole_found = 0; + + if (*offset >= inode->i_size) + return -ENXIO; + + if (!inode->i_blocks || !sih->i_size) { + if (hole) + return inode->i_size; + else + return -ENXIO; + } + + offset_in_block = *offset & ((1UL << data_bits) - 1); + + first_blocknr = *offset >> data_bits; + last_blocknr = inode->i_size >> data_bits; + + nova_dbgv("find_region offset %llx, first_blocknr %lx, last_blocknr %lx hole %d\n", + *offset, first_blocknr, last_blocknr, hole); + + blocks = nova_lookup_hole_in_range(inode->i_sb, sih, + first_blocknr, last_blocknr, &data_found, &hole_found, hole); + + /* Searching data but only hole found till the end */ + if (!hole && !data_found && hole_found) + return -ENXIO; + + if (data_found && !hole_found) { + /* Searching data but we are already into them */ + if (hole) + /* Searching hole but only data found, go to the end */ + *offset = inode->i_size; + return 0; + } + + /* Searching for hole, hole found and starting inside an hole */ + if (hole && hole_found && !blocks) { + /* we found data after it */ + if (!data_found) + /* last hole */ + *offset = inode->i_size; + return 0; + } + + if (offset_in_block) { + blocks--; + *offset += (blocks << data_bits) + + ((1 << data_bits) - offset_in_block); + } else { + *offset += blocks << data_bits; + } + + return 0; +} + static ssize_t nova_direct_IO(struct kiocb *iocb, struct iov_iter *iter) { /* DAX does not support direct IO */ diff --git a/fs/nova/inode.h b/fs/nova/inode.h index 48403cf..693aa90 100644 --- a/fs/nova/inode.h +++ b/fs/nova/inode.h @@ -264,6 +264,7 @@ int nova_delete_file_tree(struct super_block *sb, struct nova_inode_info_header *sih, unsigned long start_blocknr, unsigned long last_blocknr, bool delete_nvmm, bool delete_dead, u64 epoch_id); +unsigned long nova_find_region(struct inode *inode, loff_t *offset, int hole); extern void nova_evict_inode(struct inode *inode); extern int nova_write_inode(struct inode *inode, struct writeback_control *wbc); extern void nova_dirty_inode(struct inode *inode, int flags); diff --git a/fs/nova/nova.h b/fs/nova/nova.h index 601e082..b2831f6 100644 --- a/fs/nova/nova.h +++ b/fs/nova/nova.h @@ -485,6 +485,7 @@ int nova_remove_dentry(struct dentry *dentry, int dec_link, struct nova_inode_update *update, u64 epoch_id); /* file.c */ +extern const struct file_operations nova_dax_file_operations; extern const struct inode_operations nova_file_inode_operations; /* namei.c */ -- 2.7.4