Add basic file data support
cover Add basic file data support
Add basic file data read/write support to rpdfs, plus a few small cleanups and a debug function. This implementation will deadlock with multiple nodes and needs much more work.
Valerie Aurora (8):
rpdfs: fix compiler warning in place.h
rpdfs: add rpdfs_file_llseek
rpdfs: remove unused ino member of rpdfs_inode_info
rpdfs: add inode change debugging routine
rpdfs: add basic file data initialization
rpdfs: add file data allocation and lookup routines
rpdfs: add rpdfs_write_iter/rpdfs_read_iter
rpdfs: add read_folio, dirty_folio, write_begin, write_end
fs/rpdfs/Makefile | 1 + fs/rpdfs/data.c | 500 ++++++++++++++++++++++++++++++++++++++++ fs/rpdfs/data.h | 11 + fs/rpdfs/file.c | 44 ++++ fs/rpdfs/format-block.h | 6 +- fs/rpdfs/inode.c | 47 ++++ fs/rpdfs/inode.h | 2 +- fs/rpdfs/place.h | 2 + 8 files changed, 609 insertions(+), 4 deletions(-) create mode 100644 fs/rpdfs/data.c create mode 100644 fs/rpdfs/data.h
Signature · 1 line
2.49.0
PATCH 1/8 rpdfs: fix compiler warning in place.h
Add a header file to place.h to stop gcc from complaining about undeclared byteswap functions.
--- fs/rpdfs/place.h | 2 ++ 1 file changed, 2 insertions(+)
fs/rpdfs/place.h +2 -0
@@ -2,6 +2,8 @@#ifndef RPDFS_PLACE_H #define RPDFS_PLACE_H +#include <asm/byteorder.h> +#include "format-msg.h" #define RPF "%llx%016llx"
Signature · 1 line
2.49.0
Zach Brown
AppliedRe: [PATCH 1/8] rpdfs: fix compiler warning in place.h
Quoted 2 lines · Valerie Aurora
Add a header file to
place.hto stop gcc from complaining about undeclared byteswap functions.
Applied, thanks!
- z
PATCH 2/8 rpdfs: add rpdfs_file_llseek
In reply to Valerie AuroraUse generic_file_llseek to implement file seek.
--- fs/rpdfs/file.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/rpdfs/file.c +6 -0
@@ -5,10 +5,16 @@#include "file.h" #include "inode.h" +static loff_t rpdfs_file_llseek (struct file *file,loff_t offset,int whence) +{ +return generic_file_llseek (file, offset, whence); +} +const struct inode_operations rpdfs_file_iops = { .getattr = rpdfs_getattr, .setattr = rpdfs_setattr, };const struct file_operations rpdfs_file_fops = { + .llseek = rpdfs_file_llseek, };
Signature · 1 line
2.49.0
Re: [PATCH 2/8] rpdfs: add rpdfs_file_llseek
fs/rpdfs/file.c line 11
Quoted 6 lines · Valerie Aurora
Use
generic_file_llseekto implement file seek.fs/rpdfs/file.c +4
-0+static loff_t rpdfs_file_llseek (struct file *file,loff_t offset,int whence) +{ +return generic_file_llseek (file, offset, whence); +}
This needs to refresh the inode. It'd use the same pattern as getattr of acquiring a block handle with the inode around its operation.
- z
PATCH 3/8 rpdfs: remove unused ino member of rpdfs_inode_info
In reply to Valerie AuroraThe ino field in rpdfs_inode_info is superseded by the ig.ino field and no longer necessary.
--- fs/rpdfs/inode.h | 1 - 1 file changed, 1 deletion(-)
fs/rpdfs/inode.h +0 -1
@@ -11,7 +11,6 @@ struct rpdfs_transaction;#include "txn.h" struct rpdfs_inode_info { -u64 ino;/* updating vfs inode when wcount is older than block contents */ seqlock_t refresh_seqlock;
Signature · 1 line
2.49.0
Zach Brown
AppliedRe: [PATCH 3/8] rpdfs: remove unused ino member of rpdfs_inode_info
Quoted 2 lines · Valerie Aurora
The ino field in
rpdfs_inode_infois superseded by the ig.ino field and no longer necessary.
Applied, thanks!
- z
PATCH 4/8 rpdfs: add inode change debugging routine
In reply to Valerie Auroraprint_inode_change() can be added to copy_vfs_inode_to_rinode() to log what has changed since the last inode update.
--- fs/rpdfs/inode.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
fs/rpdfs/inode.c +38 -0
@@ -82,6 +82,44 @@ static void copy_rinode_to_vfs_inode(struct inode *inode, struct rpdfs_inode *ri ri->xattr_creates = rinode->xattr_creates; } +__always_unused +static inline void print_diff64 (char *name,u64 new,u64 old) +{ + old =le64_to_cpu (old); + +if (old != new) +rpdfs_prd ("%s: %llu -> %llu (%lld)" , name, old, new, new - old); +} + +__always_unused +static void print_inode_change (struct inode *inode,struct rpdfs_inode *rinode) +{ +struct rpdfs_inode_info *ri =RPDFS_I (inode); +struct timespec64 ts; + +rpdfs_prd ("copying inode %lu to rinode" , inode->i_ino); + +print_diff64 ("ino" , ri->ig.ino, rinode->ig.ino); +print_diff64 ("gen" , ri->ig.gen, rinode->ig.gen); +print_diff64 ("i_size" ,i_size_read (inode), rinode->size); +print_diff64 ("nlink" , inode->i_nlink, rinode->nlink); +print_diff64 ("uid" ,i_uid_read (inode), rinode->uid); +print_diff64 ("gid" ,i_gid_read (inode), rinode->gid); +print_diff64 ("mode" , inode->i_mode, rinode->mode); + + ts =inode_get_atime (inode); +print_diff64 ("atime" ,timespec64_to_ns (&ts), rinode->atime_nsec); + ts =inode_get_ctime (inode); +print_diff64 ("ctime" ,timespec64_to_ns (&ts), rinode->atime_nsec); + ts =inode_get_mtime (inode); +print_diff64 ("mtime" ,timespec64_to_ns (&ts), rinode->atime_nsec); +print_diff64 ("crtime" , ri->crtime_nsec, rinode->crtime_nsec); + +print_diff64 ("dirents" , ri->dirents.ref.bnr, rinode->dirents.ref.bnr); +print_diff64 ("xattrs" , ri->xattrs.ref.bnr, rinode->xattrs.ref.bnr); +print_diff64 ("xattr_creates" , ri->xattr_creates, rinode->xattr_creates); +} +static __le64 cpu_ts64_to_le64_ns (struct timespec64 ts) {return cpu_to_le64 (timespec64_to_ns (&ts));
Signature · 1 line
2.49.0
Re: [PATCH 4/8] rpdfs: add inode change debugging routine
fs/rpdfs/inode.c static void copy_rinode_to_vfs_inode(struct inode *inode, struct rpdfs_inode *ri · line 88
Quoted 6 lines · Valerie Aurora
print_inode_change()can be added tocopy_vfs_inode_to_rinode()to log what has changed since the last inode update.fs/rpdfs/inode.c +3
-0+static inline void print_diff64 (char *name,u64 new,u64 old) +{ + old =le64_to_cpu (old);
Hmm, not building with sparse bitwise checking (C=2)? There a few more of these in the rest of the series but I won't call them out.
With that fixed this'd be fine.
- z
PATCH 5/8 rpdfs: add basic file data initialization
In reply to Valerie AuroraInitialize the file data root, plus a few simple routines to calculate mapping block levels. Rename the field in the inode from "data" to "data_root" to avoid confusion with the "data" member of struct rpdfs_block_handle.
--- fs/rpdfs/Makefile | 1 + fs/rpdfs/data.c | 90 +++++++++++++++++++++++++++++++++++++++++ fs/rpdfs/data.h | 11 +++++ fs/rpdfs/format-block.h | 6 +-- fs/rpdfs/inode.c | 8 ++++ fs/rpdfs/inode.h | 1 + 6 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 fs/rpdfs/data.c create mode 100644 fs/rpdfs/data.h
fs/rpdfs/Makefile +1 -0
@@ -12,6 +12,7 @@ rpdfs-y := balloc.o \ block.o \ btree.o \ btree_txn.o \ + data.o \ dir.o \ file.o \ ht.o \
fs/rpdfs/data.c new file +90 -0
90 lines not on this page · raw message
fs/rpdfs/data.h new file +11 -0
@@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef RPDFS_DATA_H +#define RPDFS_DATA_H + +#include "format-block.h" + +void rpdfs_data_root_init (struct rpdfs_data_root *data); + +extern const struct address_space_operations rpdfs_aops; + +#endif
fs/rpdfs/format-block.h +3 -3
@@ -6,7 +6,7 @@#include <linux/align.h> #define RPDFS_BLOCK_SHIFT 12 -#define RPDFS_BLOCK_SIZE (1 << RPDFS_BLOCK_SHIFT) +#define RPDFS_BLOCK_SIZE (1ULL << RPDFS_BLOCK_SHIFT)#define RPDFS_BLOCK_MASK (RPDFS_BLOCK_SIZE - 1ULL)struct rpdfs_block_ref {
@@ -138,7 +138,7 @@ struct rpdfs_data_root {#define RPDFS_DATA_REFS_PER_BLK_SHIFT const_ilog2(RPDFS_DATA_REFS_PER_BLK) -struct rpdfs_indirect_block { +struct rpdfs_map_block {struct rpdfs_block_ref refs[RPDFS_DATA_REFS_PER_BLK ]; };
@@ -168,7 +168,7 @@ struct rpdfs_inode {__le64 crtime_nsec;struct rpdfs_btree_root dirents;struct rpdfs_btree_root xattrs; -struct rpdfs_data_root data; +struct rpdfs_data_root data_root; };#define RPDFS_ROOT_INO 1
fs/rpdfs/inode.c +8 -0
@@ -9,6 +9,7 @@#include "btree.h" #include "compare.h" +#include "data.h" #include "dir.h" #include "file.h" #include "inode.h"
@@ -80,6 +81,8 @@ static void copy_rinode_to_vfs_inode(struct inode *inode, struct rpdfs_inode *ri ri->xattrs = rinode->xattrs; ri->xattr_creates = rinode->xattr_creates; + + ri->data_root = rinode->data_root; }__always_unused
@@ -118,6 +121,8 @@ static void print_inode_change(struct inode *inode, struct rpdfs_inode *rinode)print_diff64 ("dirents" , ri->dirents.ref.bnr, rinode->dirents.ref.bnr);print_diff64 ("xattrs" , ri->xattrs.ref.bnr, rinode->xattrs.ref.bnr);print_diff64 ("xattr_creates" , ri->xattr_creates, rinode->xattr_creates); +print_diff64 ("data_root.height" , ri->data_root.height, rinode->data_root.height); +print_diff64 ("data_root.ref.bnr" , ri->data_root.ref.bnr, rinode->data_root.ref.bnr); }static __le64 cpu_ts64_to_le64_ns (struct timespec64 ts)
@@ -145,6 +150,8 @@ static void copy_vfs_inode_to_rinode(struct rpdfs_inode *rinode, struct inode *i rinode->xattrs = ri->xattrs; rinode->xattr_creates = ri->xattr_creates; + + rinode->data_root = ri->data_root; }/*
@@ -342,6 +349,7 @@ struct inode *rpdfs_new_inode(struct super_block *sb, struct rpdfs_ino_gen *ig)rpdfs_btree_root_init (&ri->dirents);rpdfs_btree_root_init (&ri->xattrs); +rpdfs_data_root_init (&ri->data_root); ts =inode_set_ctime_current (inode);inode_set_mtime_to_ts (inode, ts);
fs/rpdfs/inode.h +1 -0
@@ -25,6 +25,7 @@ struct rpdfs_inode_info {struct rpdfs_ino_gen ig;struct rpdfs_btree_root dirents;struct rpdfs_btree_root xattrs; +struct rpdfs_data_root data_root;struct inode vfs_inode; };
Signature · 1 line
2.49.0
Re: [PATCH 5/8] rpdfs: add basic file data initialization
Quoted 10 lines · Valerie Aurora
Initialize the file data root, plus a few simple routines to calculate mapping block levels. Rename the field in the inode from "data" to "
data_root" to avoid confusion with the "data" member of structrpdfs_block_handle.fs/rpdfs/data.c new file +5
-0+/* +* Calculate the index of the block reference for this logical block +* within an mapping block at this level (1 = pointers to data blocks). +*/ +static u32 calc_ref_ind (u64 lblk,int level)
fs/rpdfs/data.c line 63
Quoted 10 lines
fs/rpdfs/data.c new file +10
-0+{ +u32 ind; +int i; + +BUG_ON (level <1 ); + +for (i =1 ; i < level; i++) + lblk >>=RPDFS_DATA_REFS_PER_BLK_SHIFT ; + + ind = lblk & (RPDFS_DATA_REFS_PER_BLK -1ULL );
This could be a much smaller ">> ((level - 1) * shift) & mask". And let's have a specific _MASK define instead of the -1 by hand.
fs/rpdfs/data.c line 82
Quoted 11 lines
fs/rpdfs/data.c new file +11
-0+static u8 height_from_lblk (u64 lblk) +{ +u8 height =2 ; + +if (lblk ==0 ) +return 1 ; + +while (lblk >>=RPDFS_DATA_REFS_PER_BLK_SHIFT ) + height++; + +return height;
Similarly, this could be something involving "fls() / _SHIFT" while being careful with the various +1/-1 fenceposts.
- z
PATCH 6/8 rpdfs: add file data allocation and lookup routines
In reply to Valerie AuroraAdd routines to lookup and allocate file data and mapping blocks.
--- fs/rpdfs/data.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+)
fs/rpdfs/data.c +220 -0
@@ -88,3 +88,223 @@ void rpdfs_data_root_init(struct rpdfs_data_root *data) data->ref.bnr =0 ; data->ref.alloc_counter =0 ; } + +static int alloc_block_ref (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct rpdfs_block_ref *ref,struct rpdfs_block_handle **blk_hnd) +{ +int ret; + + ret =rpdfs_txn_acquire_alloc (rfi, txn, blk_hnd); +if (ret <0 ) + goto out; + +memset ((*blk_hnd)->data,0 ,RPDFS_BLOCK_SIZE ); + + ref->bnr =cpu_to_le64 ((*blk_hnd)->bnr); + ref->alloc_counter =0 ;/* XXX */ +out: +rpdfs_prd ("ret %d bnr %llu" , ret, ref->bnr); +return ret; +} + +static int grow_height (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct inode *inode,struct rpdfs_block_handle *inode_hnd,int height) +{ +struct rpdfs_inode_info *ri =RPDFS_I (inode); +struct rpdfs_block_handle *hnd =NULL ; +struct rpdfs_block_ref *ref; +struct rpdfs_map_block *iblk; +struct rpdfs_data_root *dr; +struct rpdfs_block_ref orig_root; +u8 level; +int ret; + + dr = &ri->data_root; + +rpdfs_prd ("dr->height %u goal height %u" , dr->height, height); + + ret =0 ; +if (height > dr->height) { +/* start at root and fill in till we get to existing tree */ + level = height; + orig_root = dr->ref;/* save current root of tree */ + ref = &dr->ref; + +while (level-- > dr->height) { +rpdfs_prd ("allocing block at level %d" , level +1 ); + hnd =NULL ; + ret =alloc_block_ref (rfi, txn, &dr->ref, &hnd); +if (ret <0 ) + goto out; + iblk = hnd->data; +/* growing height will always index old data to 0 */ + ref = &iblk->refs[0 ]; +rpdfs_block_release (rfi, &hnd); + } +rpdfs_prd ("grafting old height %d tree to new %d height tree" , + dr->height, height); +/* now put existing tree in mapping block */ + *ref = orig_root; +/* and update the height in the data root in the inode */ + ri->data_root.height = height; + } +out: +return ret; +} + +/* +* Return an array of block references in mapping blocks, beginning with +* the one containing the requested offset. If it is a write, allocate +* the mapping blocks for that file data offset if necessary. If it is a +* read for an unallocated but valid offset, return a null range. Actual +* data block allocation occurs in the caller. This is so we don't have +* to traverse the mapping blocks for every data block access. +* +* This function is only called after checking that a read is from a +* valid range of the file. +* +* XXX: Takes write access on map blocks on write even when it only +* needs to read them. Redescend with write when we know we have to +* allocate? +*/ +static int get_or_alloc_map_blk (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct inode *inode,struct rpdfs_block_handle *inode_hnd, +u64 lblk,rbaf_t rbaf,struct rpdfs_block_handle **hnd_ret, +struct rpdfs_block_ref **refs_ret,int *nr_ret) +{ +struct rpdfs_inode_info *ri =RPDFS_I (inode); +struct rpdfs_block_handle *parent_hnd; +struct rpdfs_block_handle *blk_hnd =NULL ; +struct rpdfs_block_ref *refs; +struct rpdfs_map_block *iblk; +struct rpdfs_data_root *dr; +rbaf_t map_rbaf; +u8 min_height; +u8 level; +u8 ind; +u64 bnr; +int write; +int nr; +int ret; + + map_rbaf = rbaf &RBAF_WRITE ?RBAF_WRITE :0 ; + write = rbaf &RBAF_WRITE ?1 :0 ; + + dr = &ri->data_root; + min_height =height_from_lblk (lblk); + +rpdfs_prd ("ino %llu dr->height %u dr->ref.bnr %llu lblk %llu lblk height %u map_rbaf %x write %d" , + ri->ig.ino, dr->height, dr->ref.bnr, lblk, min_height, map_rbaf, write); + +/* grow the height of existing data, if any */ +if ((min_height >1 ) && + (min_height > dr->height)) { + ret =grow_height (rfi, txn, inode, inode_hnd, min_height); +if (ret <0 ) + goto out; + } + +/* start with the root of the mapping tree in the inode */ + parent_hnd = inode_hnd; + refs = &dr->ref; + bnr =le64_to_cpu (dr->ref.bnr); + ind =0 ; + nr =1 ; + ret =0 ; + + level = dr->height; + +/* allocate all map blocks but not the data block itself */ +while (level-- >1 ) { +rpdfs_prd ("level %d bnr %llu" , level +1 , bnr); + +if (bnr ==0 && !write) { +rpdfs_prd ("read of unallocated block" ); +break ; + } + +if (bnr) { +/* TODO: be smarter about needing write */ + ret =rpdfs_block_acquire (rfi, txn, bnr, &blk_hnd, map_rbaf); +if (ret <0 ) + goto out; + }else { + + ret =alloc_block_ref (rfi, txn, refs, &blk_hnd); +if (ret <0 ) + goto out;/* XXX do error/recovery now */ + } + + iblk = blk_hnd->data; + +/* look up next block reference */ + ind =calc_ref_ind (lblk, level); + bnr =le64_to_cpu (iblk->refs[ind].bnr); + +if (parent_hnd != inode_hnd) +rpdfs_block_release (rfi, &parent_hnd); + + parent_hnd = blk_hnd; + refs = &iblk->refs[ind]; + nr =RPDFS_DATA_REFS_PER_BLK - ind; + blk_hnd =NULL ; + }; + +/* XXX make this a struct? */ + *hnd_ret = parent_hnd; + *refs_ret = refs; + *nr_ret = nr; + +if (dr->height < min_height) + dr->height = min_height; + +out: +rpdfs_prd ("dr->height %u bnr %llu ind %d refs %p nr %d" , + dr->height, *refs_ret ? (*refs_ret)[0 ].bnr :0 , ind, refs_ret, *nr_ret); +return ret; +} + +static int get_or_alloc_file_block (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct inode *inode,struct rpdfs_block_handle *inode_hnd, +u64 lblk,rbaf_t rbaf,struct rpdfs_block_handle **hnd_ret) +{ +struct rpdfs_block_handle *parent_hnd =NULL ; +struct rpdfs_block_handle *blk_hnd =NULL ; +struct rpdfs_block_ref *refs; +int write; +int nr; +u64 bnr; +int ret; + + write = rbaf &RBAF_WRITE ?1 :0 ; + +rpdfs_prd ("ino %llu lblk %llu rbaf %x" ,rpdfs_inode_ino (inode), lblk, rbaf); + + ret =get_or_alloc_map_blk (rfi, txn, inode, inode_hnd, lblk, rbaf, &parent_hnd, &refs, &nr); +if (ret <0 ) + goto out; + + bnr =le64_to_cpu (refs[0 ].bnr); + +if (bnr ==0 ) { +/* read from/write to unallocated range */ +if (write) { + ret =alloc_block_ref (rfi, txn, refs, &blk_hnd); +if (ret <0 ) + goto out; + } + }else { + ret =rpdfs_block_acquire (rfi, txn, bnr, &blk_hnd, rbaf); +if (ret <0 ) + goto out; + } + +if (parent_hnd != inode_hnd) +rpdfs_block_release (rfi, &parent_hnd); + + *hnd_ret = blk_hnd; +out: +rpdfs_prd ("ret %d ino %llu lblk %llu bnr %llu *hnd_ret %p" , + ret,rpdfs_inode_ino (inode), lblk, bnr, *hnd_ret); +return ret; +}
Signature · 1 line
2.49.0
Re: [PATCH 6/8] rpdfs: add file data allocation and lookup routines
Quoted 9 lines · Valerie Aurora
Add routines to lookup and allocate file data and mapping blocks.
fs/rpdfs/data.c +7
-0+static int alloc_block_ref (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct rpdfs_block_ref *ref,struct rpdfs_block_handle **blk_hnd) +{ +int ret; + + ret =rpdfs_txn_acquire_alloc (rfi, txn, blk_hnd); +if (ret <0 )
fs/rpdfs/data.c void rpdfs_data_root_init(struct rpdfs_data_root *data) · line 108
Quoted 10 lines
fs/rpdfs/data.c +10
-0+ goto out; + +memset ((*blk_hnd)->data,0 ,RPDFS_BLOCK_SIZE ); + + ref->bnr =cpu_to_le64 ((*blk_hnd)->bnr); + ref->alloc_counter =0 ;/* XXX */ +out: +rpdfs_prd ("ret %d bnr %llu" , ret, ref->bnr); +return ret; +}
This needs to set the place for the new blocks. Maybe something like _PLACE_DATA_MAP and _PLACE_DATA_BLOCK that are after the metadata place definitions. The place offset could be the lblk for the data blocks and some expression of level and lblk for the mapping blocks.
Quoted 33 lines
fs/rpdfs/data.c +33
-0+static int grow_height (struct rpdfs_fs_info *rfi,struct rpdfs_transaction *txn, +struct inode *inode,struct rpdfs_block_handle *inode_hnd,int height) +{ +struct rpdfs_inode_info *ri =RPDFS_I (inode); +struct rpdfs_block_handle *hnd =NULL ; +struct rpdfs_block_ref *ref; +struct rpdfs_map_block *iblk; +struct rpdfs_data_root *dr; +struct rpdfs_block_ref orig_root; +u8 level; +int ret; + + dr = &ri->data_root; + +rpdfs_prd ("dr->height %u goal height %u" , dr->height, height); + + ret =0 ; +if (height > dr->height) { +/* start at root and fill in till we get to existing tree */ + level = height; + orig_root = dr->ref;/* save current root of tree */ + ref = &dr->ref; + +while (level-- > dr->height) { +rpdfs_prd ("allocing block at level %d" , level +1 ); + hnd =NULL ; + ret =alloc_block_ref (rfi, txn, &dr->ref, &hnd); +if (ret <0 ) + goto out; + iblk = hnd->data; +/* growing height will always index old data to 0 */ + ref = &iblk->refs[0 ]; +rpdfs_block_release (rfi, &hnd);
fs/rpdfs/data.c void rpdfs_data_root_init(struct rpdfs_data_root *data) · line 152
Quoted 10 lines
fs/rpdfs/data.c +10
-0+ } +rpdfs_prd ("grafting old height %d tree to new %d height tree" , + dr->height, height); +/* now put existing tree in mapping block */ + *ref = orig_root; +/* and update the height in the data root in the inode */ + ri->data_root.height = height; + } +out: +return ret;
You can lose most of this by always operating on the root instead of juggling local temporary variables.
while (ri->data_root.height < height) {
alloc(&ref);
...
iblk->refs[0] = ri->data_root->ref;
ri->data_root->ref = ref;
ri->data_root->height++;
}Quoted 3 lines
+ * XXX: Takes write access on map blocks on write even when it only
+ * needs to read them. Redescend with write when we know we have to
+ * allocate?
Yeah, we don't want to be dirtying parent mapping blocks when streaming writes. It shouldn't have to redescend, though, just release and acquire the parent block. It could save the ref to the parent.
- z
PATCH 7/8 rpdfs: add rpdfs_write_iter/rpdfs_read_iter
In reply to Valerie AuroraImplement write_iter/read_iter with generic functions.
--- fs/rpdfs/file.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
fs/rpdfs/file.c +38 -0
@@ -1,15 +1,51 @@/* SPDX-License-Identifier: GPL-2.0 */ +#include <linux/kernel.h> #include <linux/fs.h> +#include <linux/slab.h> +#include "inode.h" #include "file.h" #include "inode.h" +#include "pr.h" static loff_t rpdfs_file_llseek (struct file *file,loff_t offset,int whence) {return generic_file_llseek (file, offset, whence); } +static ssize_t rpdfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file); +int ret; + +rpdfs_prd ("ino %llu count %lu pos %lu" , +rpdfs_inode_ino (inode),iov_iter_count (from), (unsigned long ) iocb->ki_pos); + + ret =generic_write_checks (iocb, from); +if (ret <=0 ) + goto out; + + ret =generic_perform_write (iocb, from); +out: +return ret; +} + +static ssize_t rpdfs_file_read_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file); +int ret; + +rpdfs_prd ("ino %llu count %lu pos %lu" , +rpdfs_inode_ino (inode),iov_iter_count (from), (unsigned long ) iocb->ki_pos); + + ret =generic_file_read_iter (iocb, from); + +return ret; +} +const struct inode_operations rpdfs_file_iops = { .getattr = rpdfs_getattr, .setattr = rpdfs_setattr,
@@ -17,4 +53,6 @@ const struct inode_operations rpdfs_file_iops = {const struct file_operations rpdfs_file_fops = { .llseek = rpdfs_file_llseek, + .write_iter = rpdfs_file_write_iter, + .read_iter = rpdfs_file_read_iter, };
Signature · 1 line
2.49.0
Re: [PATCH 7/8] rpdfs: add rpdfs_write_iter/rpdfs_read_iter
Quoted 9 lines · Valerie Aurora
Implement write_iter/read_iter with generic functions.
fs/rpdfs/file.c +7
-0+static ssize_t rpdfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file); +int ret; + + rpdfs_prd("ino %llu count %lu pos %lu",
fs/rpdfs/file.c line 33
Quoted 10 lines
fs/rpdfs/file.c +10
-0+ rpdfs_inode_ino(inode), iov_iter_count(from), (unsigned long) iocb->ki_pos); + + ret =generic_write_checks (iocb, from); +if (ret <=0 ) + goto out; + + ret =generic_perform_write (iocb, from); +out: +return ret; +}
This needs to refresh the inode and serialize with both local and remote writers. generic_file_write_iter() shows the pattern, but we need to serialize with the network inside of the inode_lock(). We'd copy it and nest our network locking inside the inode_lock().
This is where the block cache offering "locking" comes in. Today we'd get the inode block handle here, but we can't hold that across all of the _perform_write because that could make an enormous transaction. We should add the call that pins/unpins write mode on the inode block while it can be dirtied and written as many times as needed.
- z
PATCH 8/8 rpdfs: add read_folio, dirty_folio, write_begin, write_end
In reply to Valerie AuroraThis commit completes basic file data read/write support. Currently it will deadlock with multiple clients, so much more work is needed.
--- fs/rpdfs/data.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++ fs/rpdfs/inode.c | 1 + 2 files changed, 191 insertions(+)
fs/rpdfs/data.c +190 -0
@@ -308,3 +308,193 @@ static int get_or_alloc_file_block(struct rpdfs_fs_info *rfi, struct rpdfs_trans ret, rpdfs_inode_ino(inode), lblk, bnr, *hnd_ret);return ret; } + +/* +* We have to avoid a potential deadlock between the kernel's lock on +* each page and our cache consistency algorithm. The order of +* acquisition on a read or write or similar operation is: +* +* 1. kernel grabs page lock, calls file system routine +* 2. local node attempts to get access to a block +* +* But invalidation of a page has this order: +* +* 1. remote node requests exclusive access to a block +* 2. local node receives cache invalidate message and tries to get page lock +* +* If the local node is trying to get access to a block while another +* node tries to get exclusive access, we could end up with: +* +* 1. local node holds page lock, can't get access to block +* 2. local node attempts to service invalidate request but can't get page lock +* +* The solution is to drop the page lock while getting access to blocks, +* then re-acquire the lock and handle any changes that occurred while +* it was unlocked. Generally this looks like returning +* AOP_TRUNCATED_PAGE if the page is gone. +* +* Note that readahead() should satisfy most read requests +* asynchronously, leaving us to do any remaining synchronous requests +* in read_folio(). +*/ + +/* +* TODO: cannot block on block acquisition while holding the folio lock. +*/ +static int rpdfs_read_folio (struct file *file,struct folio *folio) +{ +struct inode *inode = folio->mapping->host; +struct rpdfs_fs_info *rfi =RPDFS_INODE_FS (inode); +loff_t pos =folio_pos (folio); +size_t len =folio_size (folio); +struct rpdfs_block_handle *inode_hnd =NULL ; +struct rpdfs_block_handle *blk_hnd =NULL ; +u64 lblk; +int ret; + + lblk =lblk_from_offset (pos); + +rpdfs_prd ("ino %llu lblk %llu pos %lld len %lu" , +rpdfs_inode_ino (inode), lblk, pos, len); + +/* we turn off atime always, inode will not be written */ + ret =rpdfs_inode_acquire (rfi,NULL , inode, &inode_hnd,0 ); +if (ret <0 ) + goto out; + + ret =get_or_alloc_file_block (rfi,NULL , inode, inode_hnd, lblk,0 , &blk_hnd); +if (ret <0 ) + goto out; + +/* copy the data into the actual block */ +if (blk_hnd) +memcpy (folio_address (folio), blk_hnd->data, len); +else +memset (folio_address (folio),0 , len); + +folio_mark_uptodate (folio); +out: +folio_unlock (folio); + +rpdfs_block_release (rfi, &inode_hnd); +rpdfs_block_release (rfi, &blk_hnd); + +rpdfs_prd ("ret %d" , ret); + +return ret; +} + +static bool rpdfs_dirty_folio (struct address_space *mapping,struct folio *folio) +{ +rpdfs_prd ("ino %lu index %lu" , mapping->host->i_ino, folio->index); + +return filemap_dirty_folio (folio_mapping (folio), folio); +} + +/* +* Do whatever preparation is necessary before the VFS copies the data +* from the user buffer into the folio pages. Must return a folio. +* +* Called before a write starts with the page lock held. Due to this, we +* can't do anything that blocks on acquisition of a block. Right now it +* just gets the folio. In the future it might check quotas, file system +* error state, etc. +*/ +static int rpdfs_write_begin (struct file *file,struct address_space *mapping, +loff_t pos,unsigned len, +struct folio **foliop,void **fsdata) +{ +struct folio *folio; +int ret; + +rpdfs_prd ("ino %lu pos %lld len %u" , mapping->host->i_ino, pos, len); + + folio =__filemap_get_folio (mapping, pos >>PAGE_SHIFT ,FGP_WRITEBEGIN , +mapping_gfp_mask (mapping)); +if (IS_ERR (folio)) { + ret =PTR_ERR (folio); + goto out; + } + + *foliop = folio; + ret =0 ; +out: +return ret; +} + +/* +* Called when the data for a write has been copied in memory, with page +* lock held. Here we actually do the write. +* +* To avoid deadlocking with another thread/node that has exclusive +* access to a block we need and is also attempting to get the page lock +* on this page, we first do a non-blocking lookup of the blocks we +* need. If that fails, we drop the page lock, do a blocking +* acquisition, and when that succeeds, return AOP_TRUNCATE_PAGE to +* request a retry. +*/ +static int rpdfs_write_end (struct file *file,struct address_space *mapping, +loff_t pos,unsigned len,unsigned copied, +struct folio *folio,void *fsdata) +{ +struct inode *inode = folio->mapping->host; +struct rpdfs_fs_info *rfi =RPDFS_INODE_FS (inode); +struct rpdfs_inode_info *ri =RPDFS_I (inode); +struct rpdfs_block_handle *inode_hnd =NULL ; +struct rpdfs_block_handle *blk_hnd =NULL ; +struct rpdfs_transaction txn =RPDFS_INIT_TXN ; +rbaf_t rbaf; +u64 lblk; +int ret; +unsigned offset =offset_in_folio (folio, pos); + +rpdfs_prd ("ino %llu pos %lld len %u offset %u" , ri->ig.ino, pos, len, offset); + + lblk =lblk_from_offset (pos); + +/* XXX use pos/len to figure out when it is an overwrite */ + rbaf =RBAF_WRITE ; + + ret =rpdfs_inode_acquire (rfi, &txn, inode, &inode_hnd,RBAF_WRITE ); +if (ret <0 ) + goto out; + + ret =get_or_alloc_file_block (rfi, &txn, inode, inode_hnd, lblk, rbaf, &blk_hnd); +if (ret <0 ) + goto out; + +/* copy the data into the actual block */ +memcpy (blk_hnd->data + offset,folio_address (folio) + offset, len); + +rpdfs_prd ("copied %c len %d to %c" , *((char *)folio_address (folio) + offset), len, + *((char *) blk_hnd->data + offset)); + +if ((pos + len) > inode->i_size) +i_size_write (inode, pos + len); + +folio_mark_uptodate (folio); +folio_mark_dirty (folio); +folio_unlock (folio); + +file_update_time (file); +mark_inode_dirty (inode);/* TODO: not always necessary */ + +/* finalize changes to the inode and block */ +rpdfs_inode_update (rfi, inode, inode_hnd); +rpdfs_block_release (rfi, &inode_hnd); +rpdfs_block_release (rfi, &blk_hnd); + + ret = len; +out: +rpdfs_txn_finish (rfi, &txn); + +rpdfs_prd ("i_size %lld ret %d" ,i_size_read (inode), ret); +return ret; +} + +const struct address_space_operations rpdfs_aops = { + .read_folio = rpdfs_read_folio, + .dirty_folio = rpdfs_dirty_folio, + .write_begin = rpdfs_write_begin, + .write_end = rpdfs_write_end, +};
fs/rpdfs/inode.c +1 -0
@@ -164,6 +164,7 @@ void rpdfs_inode_init_ops(struct inode *inode)case S_IFREG : inode->i_op = &rpdfs_file_iops; inode->i_fop = &rpdfs_file_fops; + inode->i_mapping->a_ops = &rpdfs_aops;break ;case S_IFDIR : inode->i_op = &rpdfs_dir_iops;
Signature · 1 line
2.49.0
Re: [PATCH 8/8] rpdfs: add read_folio, dirty_folio, write_begin, write_end
Quoted 5 lines · Valerie Aurora
This commit completes basic file data read/write support. Currently it will deadlock with multiple clients, so much more work is needed.
+/*
+ * TODO: cannot block on block acquisition while holding the folio lock.
There's not much to it.
ret = acquire(, RBAF_NONBLOCK_MODE);
if (ret == -EAGAIN) {
folio_unlock(folio);
ret = acquire(, 0);
if (ret == 0)
release(&hnd);
ret = AOP_TRUNCATED_PAGE;
goto out;
}The caller will retry and this time it'll be able to acquire the block without waiting.
Quoted 9 lines
+/*
+ * Do whatever preparation is necessary before the VFS copies the data
+ * from the user buffer into the folio pages. Must return a folio.
+ *
+ * Called before a write starts with the page lock held. Due to this, we
+ * can't do anything that blocks on acquisition of a block. Right now it
+ * just gets the folio. In the future it might check quotas, file system
+ * error state, etc.
+ */
It's not called with the page lock held. That's what the __filemap_get_folio() is doing. FGP_WRITEBGIN includes FGP_LOCK. We have the freedom to place the page lock in whatever order we need.
(holy moly is it hard to type FGP_ without typing GFP_)
Most of what's in write_end should be brought up in to write_begin. Interestingly, I don't think we need to communicate the txn to write_end. write_begin just needs to make sure that all our dirty blocks for the change (inode, mapping, data blocks) are all in one atomic write. write_end just needs to do the vfs work. Ala generic_write_end().
fs/rpdfs/data.c static int get_or_alloc_file_block(struct rpdfs_fs_info *rfi, struct rpdfs_trans · line 467
Quoted 2 lines
fs/rpdfs/data.c +2
-0+/* copy the data into the actual block */ +memcpy (blk_hnd->data + offset,folio_address (folio) + offset, len);
Let's do this by wiring up writepages and having our network IO reference the page.
fs/rpdfs/data.c static int get_or_alloc_file_block(struct rpdfs_fs_info *rfi, struct rpdfs_trans · line 492
Quoted 2 lines
fs/rpdfs/data.c +2
-0+rpdfs_prd ("i_size %lld ret %d" ,i_size_read (inode), ret); +return ret;
I'm hoping we can get away with not causing errors in write_end. It'd avoid having to unwind the mapping and folio insertions that we did in write_begin. (Poke around ext2_write_failed()).
- z