History

fuse: allow filesystems to have precise control over data cache

On networked filesystems file data can be changed externally. FUSE provides notification messages for filesystem to inform kernel that metadata or data region of a file needs to be invalidated in local page cache. That provides the basis for filesystem implementations to invalidate kernel cache explicitly based on observed filesystem-specific events. FUSE has also "automatic" invalidation mode(*) when the kernel automatically invalidates data cache of a file if it sees mtime change. It also automatically invalidates whole data cache of a file if it sees file size being changed. The automatic mode has corresponding capability - FUSE_AUTO_INVAL_DATA. However, due to probably historical reason, that capability controls only whether mtime change should be resulting in automatic invalidation or not. A change in file size always results in invalidating whole data cache of a file irregardless of whether FUSE_AUTO_INVAL_DATA was negotiated(+). The filesystem I write[1] represents data arrays stored in networked database as local files suitable for mmap. It is read-only filesystem - changes to data are committed externally via database interfaces and the filesystem only glues data into contiguous file streams suitable for mmap and traditional array processing. The files are big - starting from hundreds gigabytes and more. The files change regularly, and frequently by data being appended to their end. The size of files thus changes frequently. If a file was accessed locally and some part of its data got into page cache, we want that data to stay cached unless there is memory pressure, or unless corresponding part of the file was actually changed. However current FUSE behaviour - when it sees file size change - is to invalidate the whole file. The data cache of the file is thus completely lost even on small size change, and despite that the filesystem server is careful to accurately translate database changes into FUSE invalidation messages to kernel. Let's fix it: if a filesystem, through new FUSE_EXPLICIT_INVAL_DATA capability, indicates to kernel that it is fully responsible for data cache invalidation, then the kernel won't invalidate files data cache on size change and only truncate that cache to new size in case the size decreased. (*) see 72d0d248ca "fuse: add FUSE_AUTO_INVAL_DATA init flag", eed2179efe "fuse: invalidate inode mapping if mtime changes" (+) in writeback mode the kernel does not invalidate data cache on file size change, but neither it allows the filesystem to set the size due to external event (see 8373200b12 "fuse: Trust kernel i_size only") [1] https://lab.nexedi.com/kirr/wendelin.core/blob/a50f1d9f/wcfs/wcfs.go#L20

Kirill Smelkov committed 7 years ago

Commit ad2ba641 parent f2294483 files · +14 −3Browse files

Unified · Split

3 files
  1. fs/fuse/fuse_i.h +3 −0
  2. fs/fuse/inode.c +5 −2
  3. include/uapi/linux/fuse.h +6 −1
fs/fuse/fuse_i.h+3−0
@@ -694,6 +694,9 @@ struct fuse_conn { · 693 unmodified lines
694 /** Use enhanced/automatic page cache invalidation. */694 /** Use enhanced/automatic page cache invalidation. */
695 unsigned auto_inval_data:1;695 unsigned auto_inval_data:1;
696 696
697 /** Filesystem is fully reponsible for page cache invalidation. */
698 unsigned explicit_inval_data:1;
699
697 /** Does the filesystem support readdirplus? */700 /** Does the filesystem support readdirplus? */
698 unsigned do_readdirplus:1;701 unsigned do_readdirplus:1;
699 702
fs/fuse/inode.c+5−2
@@ -237,7 +237,8 @@ void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, · 236 unmodified lines
237 237
238 if (oldsize != attr->size) {238 if (oldsize != attr->size) {
239 truncate_pagecache(inode, attr->size);239 truncate_pagecache(inode, attr->size);
240 inval = true;240 if (!fc->explicit_inval_data)
241 inval = true;
241 } else if (fc->auto_inval_data) {242 } else if (fc->auto_inval_data) {
242 struct timespec64 new_mtime = {243 struct timespec64 new_mtime = {
243 .tv_sec = attr->mtime,244 .tv_sec = attr->mtime,
@@ -912,6 +913,8 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) · 668 unmodified lines
912 fc->dont_mask = 1;913 fc->dont_mask = 1;
913 if (arg->flags & FUSE_AUTO_INVAL_DATA)914 if (arg->flags & FUSE_AUTO_INVAL_DATA)
914 fc->auto_inval_data = 1;915 fc->auto_inval_data = 1;
916 else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
917 fc->explicit_inval_data = 1;
915 if (arg->flags & FUSE_DO_READDIRPLUS) {918 if (arg->flags & FUSE_DO_READDIRPLUS) {
916 fc->do_readdirplus = 1;919 fc->do_readdirplus = 1;
917 if (arg->flags & FUSE_READDIRPLUS_AUTO)920 if (arg->flags & FUSE_READDIRPLUS_AUTO)
@@ -973,7 +976,7 @@ static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) · 55 unmodified lines
973 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |976 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
974 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |977 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
975 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |978 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
976 FUSE_NO_OPENDIR_SUPPORT;979 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA;
977 req->in.h.opcode = FUSE_INIT;980 req->in.h.opcode = FUSE_INIT;
978 req->in.numargs = 1;981 req->in.numargs = 1;
979 req->in.args[0].size = sizeof(*arg);982 req->in.args[0].size = sizeof(*arg);
include/uapi/linux/fuse.h+6−1
@@ -125,6 +125,9 @@ · 124 unmodified lines
125 *125 *
126 * 7.29126 * 7.29
127 * - add FUSE_NO_OPENDIR_SUPPORT flag127 * - add FUSE_NO_OPENDIR_SUPPORT flag
128 *
129 * 7.30
130 * - add FUSE_EXPLICIT_INVAL_DATA
128 */131 */
129 132
130#ifndef _LINUX_FUSE_H133#ifndef _LINUX_FUSE_H
@@ -160,7 +163,7 @@ · 29 unmodified lines
160#define FUSE_KERNEL_VERSION 7163#define FUSE_KERNEL_VERSION 7
161 164
162/** Minor version number of this interface */165/** Minor version number of this interface */
163#define FUSE_KERNEL_MINOR_VERSION 29166#define FUSE_KERNEL_MINOR_VERSION 30
164 167
165/** The node ID of the root inode */168/** The node ID of the root inode */
166#define FUSE_ROOT_ID 1169#define FUSE_ROOT_ID 1
@@ -263,6 +266,7 @@ struct fuse_file_lock { · 96 unmodified lines
263 * FUSE_MAX_PAGES: init_out.max_pages contains the max number of req pages266 * FUSE_MAX_PAGES: init_out.max_pages contains the max number of req pages
264 * FUSE_CACHE_SYMLINKS: cache READLINK responses267 * FUSE_CACHE_SYMLINKS: cache READLINK responses
265 * FUSE_NO_OPENDIR_SUPPORT: kernel supports zero-message opendir268 * FUSE_NO_OPENDIR_SUPPORT: kernel supports zero-message opendir
269 * FUSE_EXPLICIT_INVAL_DATA: only invalidate cached pages on explicit request
266 */270 */
267#define FUSE_ASYNC_READ (1 << 0)271#define FUSE_ASYNC_READ (1 << 0)
268#define FUSE_POSIX_LOCKS (1 << 1)272#define FUSE_POSIX_LOCKS (1 << 1)
@@ -289,6 +293,7 @@ struct fuse_file_lock { · 20 unmodified lines
289#define FUSE_MAX_PAGES (1 << 22)293#define FUSE_MAX_PAGES (1 << 22)
290#define FUSE_CACHE_SYMLINKS (1 << 23)294#define FUSE_CACHE_SYMLINKS (1 << 23)
291#define FUSE_NO_OPENDIR_SUPPORT (1 << 24)295#define FUSE_NO_OPENDIR_SUPPORT (1 << 24)
296#define FUSE_EXPLICIT_INVAL_DATA (1 << 25)
292 297
293/**298/**
294 * CUSE INIT request/reply flags299 * CUSE INIT request/reply flags