diff options
Diffstat (limited to 'xlators/performance/write-behind/src/write-behind.c')
| -rw-r--r-- | xlators/performance/write-behind/src/write-behind.c | 3513 |
1 files changed, 1586 insertions, 1927 deletions
diff --git a/xlators/performance/write-behind/src/write-behind.c b/xlators/performance/write-behind/src/write-behind.c index 10e8ec4b1..95c5921c6 100644 --- a/xlators/performance/write-behind/src/write-behind.c +++ b/xlators/performance/write-behind/src/write-behind.c @@ -1,24 +1,13 @@ /* - Copyright (c) 2006-2009 Z RESEARCH, Inc. <http://www.zresearch.com> + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part of GlusterFS. - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. */ -/*TODO: check for non null wb_file_data before getting wb_file */ - #ifndef _CONFIG_H #define _CONFIG_H @@ -34,2333 +23,1989 @@ #include "compat-errno.h" #include "common-utils.h" #include "call-stub.h" +#include "statedump.h" +#include "defaults.h" +#include "write-behind-mem-types.h" + +#define MAX_VECTOR_COUNT 8 +#define WB_AGGREGATE_SIZE 131072 /* 128 KB */ +#define WB_WINDOW_SIZE 1048576 /* 1MB */ -#define MAX_VECTOR_COUNT 8 -#define WB_AGGREGATE_SIZE 131072 /* 128 KB */ -#define WB_WINDOW_SIZE 1048576 /* 1MB */ - typedef struct list_head list_head_t; struct wb_conf; -struct wb_page; -struct wb_file; - - -typedef struct wb_file { - int disabled; - uint64_t disable_till; - size_t window_size; - int32_t refcount; - int32_t op_ret; - int32_t op_errno; - list_head_t request; - list_head_t passive_requests; - fd_t *fd; +struct wb_inode; + +typedef struct wb_inode { + ssize_t window_conf; + ssize_t window_current; + ssize_t transit; /* size of data stack_wound, and yet + to be fulfilled (wb_fulfill_cbk). + used for trickling_writes + */ + + list_head_t all; /* All requests, from enqueue() till destroy(). + Used only for resetting generation + number when empty. + */ + list_head_t todo; /* Work to do (i.e, STACK_WIND to server). + Once we STACK_WIND, the entry is taken + off the list. If it is non-sync write, + then we continue to track it via @liability + or @temptation depending on the status + of its writeback. + */ + list_head_t liability; /* Non-sync writes which are lied + (STACK_UNWIND'ed to caller) but ack + from server not yet complete. This + is the "liability" which we hold, and + must guarantee that dependent operations + which arrive later (which overlap, etc.) + are issued only after their dependencies + in this list are "fulfilled". + + Server acks for entries in this list + shrinks the window. + + The sum total of all req->write_size + of entries in this list must be kept less + than the permitted window size. + */ + list_head_t temptation; /* Operations for which we are tempted + to 'lie' (write-behind), but temporarily + holding off (because of insufficient + window capacity, etc.) + + This is the list to look at to grow + the window (in __wb_pick_unwinds()). + + Entries typically get chosen from + write-behind from this list, and therefore + get "upgraded" to the "liability" list. + */ + list_head_t wip; /* List of write calls in progress, SYNC or non-SYNC + which are currently STACK_WIND'ed towards the server. + This is for guaranteeing that no two overlapping + writes are in progress at the same time. Modules + like eager-lock in AFR depend on this behavior. + */ + uint64_t gen; /* Liability generation number. Represents + the current 'state' of liability. Every + new addition to the liability list bumps + the generation number. + + a newly arrived request is only required + to perform causal checks against the entries + in the liability list which were present + at the time of its addition. the generation + number at the time of its addition is stored + in the request and used during checks. + + the liability list can grow while the request + waits in the todo list waiting for its + dependent operations to complete. however + it is not of the request's concern to depend + itself on those new entries which arrived + after it arrived (i.e, those that have a + liability generation higher than itself) + */ gf_lock_t lock; xlator_t *this; -}wb_file_t; +} wb_inode_t; typedef struct wb_request { - list_head_t list; - list_head_t winds; - list_head_t unwinds; - list_head_t other_requests; - call_stub_t *stub; - int32_t refcount; - wb_file_t *file; - union { - struct { - char write_behind; - char stack_wound; - char got_reply; - }write_request; - - struct { - char marked_for_resume; - }other_requests; - }flags; + list_head_t all; + list_head_t todo; + list_head_t lie; /* either in @liability or @temptation */ + list_head_t winds; + list_head_t unwinds; + list_head_t wip; + + call_stub_t *stub; + + ssize_t write_size; /* currently held size + (after collapsing) */ + size_t orig_size; /* size which arrived with the request. + This is the size by which we grow + the window when unwinding the frame. + */ + size_t total_size; /* valid only in @head in wb_fulfill(). + This is the size with which we perform + STACK_WIND to server and therefore the + amount by which we shrink the window. + */ + + int op_ret; + int op_errno; + + int32_t refcount; + wb_inode_t *wb_inode; + glusterfs_fop_t fop; + gf_lkowner_t lk_owner; + struct iobref *iobref; + uint64_t gen; /* inode liability state at the time of + request arrival */ + + fd_t *fd; + struct { + size_t size; /* 0 size == till infinity */ + off_t off; + int append:1; /* offset is invalid. only one + outstanding append at a time */ + int tempted:1; /* true only for non-sync writes */ + int lied:1; /* sin committed */ + int fulfilled:1; /* got server acknowledgement */ + int go:1; /* enough aggregating, good to go */ + } ordering; } wb_request_t; -struct wb_conf { - uint64_t aggregate_size; - uint64_t window_size; - uint64_t disable_till; - gf_boolean_t enable_O_SYNC; - gf_boolean_t flush_behind; - gf_boolean_t enable_trickling_writes; -}; +typedef struct wb_conf { + uint64_t aggregate_size; + uint64_t window_size; + gf_boolean_t flush_behind; + gf_boolean_t trickling_writes; + gf_boolean_t strict_write_ordering; + gf_boolean_t strict_O_DIRECT; +} wb_conf_t; -typedef struct wb_local { - list_head_t winds; - struct wb_file *file; - wb_request_t *request; - int op_ret; - int op_errno; - call_frame_t *frame; - int32_t reply_count; -} wb_local_t; +void +wb_process_queue (wb_inode_t *wb_inode); -typedef struct wb_conf wb_conf_t; -typedef struct wb_page wb_page_t; +wb_inode_t * +__wb_inode_ctx_get (xlator_t *this, inode_t *inode) +{ + uint64_t value = 0; + wb_inode_t *wb_inode = NULL; + __inode_ctx_get (inode, this, &value); + wb_inode = (wb_inode_t *)(unsigned long) value; -int32_t -wb_process_queue (call_frame_t *frame, wb_file_t *file, char flush_all); + return wb_inode; +} -ssize_t -wb_sync (call_frame_t *frame, wb_file_t *file, list_head_t *winds); -ssize_t -__wb_mark_winds (list_head_t *list, list_head_t *winds, size_t aggregate_size, - char wind_all, char enable_trickling_writes); +wb_inode_t * +wb_inode_ctx_get (xlator_t *this, inode_t *inode) +{ + wb_inode_t *wb_inode = NULL; + GF_VALIDATE_OR_GOTO ("write-behind", this, out); + GF_VALIDATE_OR_GOTO (this->name, inode, out); -static void -__wb_request_unref (wb_request_t *this) -{ - if (this->refcount <= 0) { - gf_log ("wb-request", GF_LOG_DEBUG, - "refcount(%d) is <= 0", this->refcount); - return; + LOCK (&inode->lock); + { + wb_inode = __wb_inode_ctx_get (this, inode); } + UNLOCK (&inode->lock); +out: + return wb_inode; +} - this->refcount--; - if (this->refcount == 0) { - list_del_init (&this->list); - if (this->stub && this->stub->fop == GF_FOP_WRITE) { - call_stub_destroy (this->stub); + +gf_boolean_t +wb_fd_err (fd_t *fd, xlator_t *this, int32_t *op_errno) +{ + gf_boolean_t err = _gf_false; + uint64_t value = 0; + int32_t tmp = 0; + + if (fd_ctx_get (fd, this, &value) == 0) { + if (op_errno) { + tmp = value; + *op_errno = tmp; } - FREE (this); + err = _gf_true; } + + return err; } -static void -wb_request_unref (wb_request_t *this) +/* + Below is a succinct explanation of the code deciding whether two regions + overlap, from Pavan <tcp@gluster.com>. + + For any two ranges to be non-overlapping, either the end of the first + range is lesser than the start of the second, or vice versa. Example - + + <---------> <--------------> + p q x y + + ( q < x ) or (y < p) = > No overlap. + + To check for *overlap*, we can negate this (using de morgan's laws), and + it becomes - + + (q >= x ) and (y >= p) + + Either that, or you write the negation using - + + if (! ((q < x) or (y < p)) ) { + "Overlap" + } +*/ + +gf_boolean_t +wb_requests_overlap (wb_request_t *req1, wb_request_t *req2) { - wb_file_t *file = NULL; - if (this == NULL) { - gf_log ("wb-request", GF_LOG_DEBUG, - "request is NULL"); - return; - } - - file = this->file; - LOCK (&file->lock); - { - __wb_request_unref (this); - } - UNLOCK (&file->lock); + uint64_t r1_start = 0; + uint64_t r1_end = 0; + uint64_t r2_start = 0; + uint64_t r2_end = 0; + enum _gf_boolean do_overlap = 0; + + r1_start = req1->ordering.off; + if (req1->ordering.size) + r1_end = r1_start + req1->ordering.size - 1; + else + r1_end = ULLONG_MAX; + + r2_start = req2->ordering.off; + if (req2->ordering.size) + r2_end = r2_start + req2->ordering.size - 1; + else + r2_end = ULLONG_MAX; + + do_overlap = ((r1_end >= r2_start) && (r2_end >= r1_start)); + + return do_overlap; } -static wb_request_t * -__wb_request_ref (wb_request_t *this) +gf_boolean_t +wb_requests_conflict (wb_request_t *lie, wb_request_t *req) { - if (this->refcount < 0) { - gf_log ("wb-request", GF_LOG_DEBUG, - "refcount(%d) is < 0", this->refcount); - return NULL; - } + wb_conf_t *conf = NULL; + + conf = req->wb_inode->this->private; + + if (lie == req) + /* request cannot conflict with itself */ + return _gf_false; - this->refcount++; - return this; + if (lie->gen >= req->gen) + /* this liability entry was behind + us in the todo list */ + return _gf_false; + + if (lie->ordering.append) + /* all modifications wait for the completion + of outstanding append */ + return _gf_true; + + if (conf->strict_write_ordering) + /* We are sure (lie->gen < req->gen) by now. So + skip overlap check if strict write ordering is + requested and always return "conflict" against a + lower generation lie. */ + return _gf_true; + + return wb_requests_overlap (lie, req); } -wb_request_t * -wb_request_ref (wb_request_t *this) +gf_boolean_t +wb_liability_has_conflict (wb_inode_t *wb_inode, wb_request_t *req) { - wb_file_t *file = NULL; - if (this == NULL) { - gf_log ("wb-request", GF_LOG_DEBUG, - "request is NULL"); - return NULL; + wb_request_t *each = NULL; + + list_for_each_entry (each, &wb_inode->liability, lie) { + if (wb_requests_conflict (each, req)) + return _gf_true; } - file = this->file; - LOCK (&file->lock); - { - this = __wb_request_ref (this); + return _gf_false; +} + + +gf_boolean_t +wb_wip_has_conflict (wb_inode_t *wb_inode, wb_request_t *req) +{ + wb_request_t *each = NULL; + + if (req->stub->fop != GF_FOP_WRITE) + /* non-writes fundamentally never conflict with WIP requests */ + return _gf_false; + + list_for_each_entry (each, &wb_inode->wip, wip) { + if (each == req) + /* request never conflicts with itself, + though this condition should never occur. + */ + continue; + + if (wb_requests_overlap (each, req)) + return _gf_true; } - UNLOCK (&file->lock); - return this; + return _gf_false; } -wb_request_t * -wb_enqueue (wb_file_t *file, call_stub_t *stub) +static int +__wb_request_unref (wb_request_t *req) { - wb_request_t *request = NULL; - call_frame_t *frame = NULL; - wb_local_t *local = NULL; - struct iovec *vector = NULL; - int32_t count = 0; - - request = CALLOC (1, sizeof (*request)); - if (request == NULL) { + int ret = -1; + wb_inode_t *wb_inode = NULL; + + wb_inode = req->wb_inode; + + if (req->refcount <= 0) { + gf_log ("wb-request", GF_LOG_WARNING, + "refcount(%d) is <= 0", req->refcount); goto out; } - INIT_LIST_HEAD (&request->list); - INIT_LIST_HEAD (&request->winds); - INIT_LIST_HEAD (&request->unwinds); - INIT_LIST_HEAD (&request->other_requests); + ret = --req->refcount; + if (req->refcount == 0) { + list_del_init (&req->todo); + list_del_init (&req->lie); + list_del_init (&req->wip); - request->stub = stub; - request->file = file; + list_del_init (&req->all); + if (list_empty (&wb_inode->all)) { + wb_inode->gen = 0; + /* in case of accounting errors? */ + wb_inode->window_current = 0; + } - frame = stub->frame; - local = frame->local; - if (local) { - local->request = request; - } + list_del_init (&req->winds); + list_del_init (&req->unwinds); - if (stub->fop == GF_FOP_WRITE) { - vector = stub->args.writev.vector; - count = stub->args.writev.count; + if (req->stub && req->ordering.tempted) { + call_stub_destroy (req->stub); + req->stub = NULL; + } /* else we would have call_resume()'ed */ + + if (req->iobref) + iobref_unref (req->iobref); - frame = stub->frame; - local = frame->local; - local->op_ret = iov_length (vector, count); - local->op_errno = 0; + if (req->fd) + fd_unref (req->fd); + + GF_FREE (req); } +out: + return ret; +} - LOCK (&file->lock); + +static int +wb_request_unref (wb_request_t *req) +{ + wb_inode_t *wb_inode = NULL; + int ret = -1; + + GF_VALIDATE_OR_GOTO ("write-behind", req, out); + + wb_inode = req->wb_inode; + + LOCK (&wb_inode->lock); { - list_add_tail (&request->list, &file->request); - if (stub->fop == GF_FOP_WRITE) { - /* reference for stack winding */ - __wb_request_ref (request); - - /* reference for stack unwinding */ - __wb_request_ref (request); - } else { - /*reference for resuming */ - __wb_request_ref (request); - } + ret = __wb_request_unref (req); } - UNLOCK (&file->lock); + UNLOCK (&wb_inode->lock); out: - return request; + return ret; } -wb_file_t * -wb_file_create (xlator_t *this, fd_t *fd) +static wb_request_t * +__wb_request_ref (wb_request_t *req) { - wb_file_t *file = NULL; - wb_conf_t *conf = this->private; + GF_VALIDATE_OR_GOTO ("write-behind", req, out); - file = CALLOC (1, sizeof (*file)); - if (file == NULL) { + if (req->refcount < 0) { + gf_log ("wb-request", GF_LOG_WARNING, + "refcount(%d) is < 0", req->refcount); + req = NULL; goto out; } - INIT_LIST_HEAD (&file->request); - INIT_LIST_HEAD (&file->passive_requests); - - /* - fd_ref() not required, file should never decide the existance of - an fd - */ - file->fd= fd; - file->disable_till = conf->disable_till; - file->this = this; - file->refcount = 1; - - fd_ctx_set (fd, this, (uint64_t)(long)file); + req->refcount++; out: - return file; + return req; } -void -wb_file_destroy (wb_file_t *file) + +wb_request_t * +wb_request_ref (wb_request_t *req) { - int32_t refcount = 0; + wb_inode_t *wb_inode = NULL; - LOCK (&file->lock); - { - refcount = --file->refcount; - } - UNLOCK (&file->lock); + GF_VALIDATE_OR_GOTO ("write-behind", req, out); - if (!refcount){ - LOCK_DESTROY (&file->lock); - FREE (file); + wb_inode = req->wb_inode; + LOCK (&wb_inode->lock); + { + req = __wb_request_ref (req); } + UNLOCK (&wb_inode->lock); - return; +out: + return req; } -int32_t -wb_sync_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno, struct stat *stbuf) +gf_boolean_t +wb_enqueue_common (wb_inode_t *wb_inode, call_stub_t *stub, int tempted) { - wb_local_t *local = NULL; - list_head_t *winds = NULL; - wb_file_t *file = NULL; - wb_request_t *request = NULL, *dummy = NULL; - wb_local_t *per_request_local = NULL; - int32_t ret = -1; - fd_t *fd = NULL; + wb_request_t *req = NULL; + GF_VALIDATE_OR_GOTO ("write-behind", wb_inode, out); + GF_VALIDATE_OR_GOTO (wb_inode->this->name, stub, out); - local = frame->local; - winds = &local->winds; - file = local->file; + req = GF_CALLOC (1, sizeof (*req), gf_wb_mt_wb_request_t); + if (!req) + goto out; - LOCK (&file->lock); - { - list_for_each_entry_safe (request, dummy, winds, winds) { - request->flags.write_request.got_reply = 1; + INIT_LIST_HEAD (&req->all); + INIT_LIST_HEAD (&req->todo); + INIT_LIST_HEAD (&req->lie); + INIT_LIST_HEAD (&req->winds); + INIT_LIST_HEAD (&req->unwinds); + INIT_LIST_HEAD (&req->wip); - if (!request->flags.write_request.write_behind - && (op_ret == -1)) { - per_request_local = request->stub->frame->local; - per_request_local->op_ret = op_ret; - per_request_local->op_errno = op_errno; - } + req->stub = stub; + req->wb_inode = wb_inode; + req->fop = stub->fop; + req->ordering.tempted = tempted; - __wb_request_unref (request); - } - - if (op_ret == -1) { - file->op_ret = op_ret; - file->op_errno = op_errno; - } - fd = file->fd; - } - UNLOCK (&file->lock); + if (stub->fop == GF_FOP_WRITE) { + req->write_size = iov_length (stub->args.vector, + stub->args.count); - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - LOCK (&file->lock); - { - file->op_ret = -1; - file->op_errno = ENOMEM; - } - UNLOCK (&file->lock); + /* req->write_size can change as we collapse + small writes. But the window needs to grow + only by how much we acknowledge the app. so + copy the original size in orig_size for the + purpose of accounting. + */ + req->orig_size = req->write_size; + + /* Let's be optimistic that we can + lie about it + */ + req->op_ret = req->write_size; + req->op_errno = 0; + + if (stub->args.fd->flags & O_APPEND) + req->ordering.append = 1; } - /* safe place to do fd_unref */ - fd_unref (fd); + req->lk_owner = stub->frame->root->lk_owner; - STACK_DESTROY (frame->root); + switch (stub->fop) { + case GF_FOP_WRITE: + req->ordering.off = stub->args.offset; + req->ordering.size = req->write_size; - return 0; -} + req->fd = fd_ref (stub->args.fd); + break; + case GF_FOP_READ: + req->ordering.off = stub->args.offset; + req->ordering.size = stub->args.size; -ssize_t -wb_sync (call_frame_t *frame, wb_file_t *file, list_head_t *winds) -{ - wb_request_t *dummy = NULL, *request = NULL; - wb_request_t *first_request = NULL, *next = NULL; - size_t total_count = 0, count = 0; - size_t copied = 0; - call_frame_t *sync_frame = NULL; - struct iobref *iobref = NULL; - wb_local_t *local = NULL; - struct iovec *vector = NULL; - ssize_t current_size = 0, bytes = 0; - size_t bytecount = 0; - wb_conf_t *conf = NULL; - fd_t *fd = NULL; - - conf = file->this->private; - list_for_each_entry (request, winds, winds) { - total_count += request->stub->args.writev.count; - if (total_count > 0) { - break; - } - } + req->fd = fd_ref (stub->args.fd); - if (total_count == 0) { - goto out; - } - - list_for_each_entry_safe (request, dummy, winds, winds) { - if (!vector) { - vector = MALLOC (VECTORSIZE (MAX_VECTOR_COUNT)); - if (vector == NULL) { - bytes = -1; - goto out; - } - - iobref = iobref_new (); - if (iobref == NULL) { - bytes = -1; - goto out; - } - - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - bytes = -1; - goto out; - } - - INIT_LIST_HEAD (&local->winds); - - first_request = request; - current_size = 0; - } + break; + case GF_FOP_TRUNCATE: + req->ordering.off = stub->args.offset; + req->ordering.size = 0; /* till infinity */ + break; + case GF_FOP_FTRUNCATE: + req->ordering.off = stub->args.offset; + req->ordering.size = 0; /* till infinity */ - count += request->stub->args.writev.count; - bytecount = VECTORSIZE (request->stub->args.writev.count); - memcpy (((char *)vector)+copied, - request->stub->args.writev.vector, - bytecount); - copied += bytecount; - - current_size += iov_length (request->stub->args.writev.vector, - request->stub->args.writev.count); - - if (request->stub->args.writev.iobref) { - iobref_merge (iobref, - request->stub->args.writev.iobref); - } + req->fd = fd_ref (stub->args.fd); - next = NULL; - if (request->winds.next != winds) { - next = list_entry (request->winds.next, - wb_request_t, winds); - } + break; + default: + break; + } - list_del_init (&request->winds); - list_add_tail (&request->winds, &local->winds); + LOCK (&wb_inode->lock); + { + list_add_tail (&req->all, &wb_inode->all); - if ((!next) - || ((count + next->stub->args.writev.count) - > MAX_VECTOR_COUNT) - || ((current_size + iov_length (next->stub->args.writev.vector, - next->stub->args.writev.count)) - > conf->aggregate_size)) - { - sync_frame = copy_frame (frame); - if (sync_frame == NULL) { - bytes = -1; - goto out; - } - - sync_frame->local = local; - local->file = file; - - LOCK (&file->lock); - { - fd = file->fd; - } - UNLOCK (&file->lock); - - fd_ref (fd); - - bytes += current_size; - STACK_WIND (sync_frame, - wb_sync_cbk, - FIRST_CHILD(sync_frame->this), - FIRST_CHILD(sync_frame->this)->fops->writev, - fd, vector, - count, - first_request->stub->args.writev.off, - iobref); - - iobref_unref (iobref); - FREE (vector); - first_request = NULL; - iobref = NULL; - vector = NULL; - sync_frame = NULL; - local = NULL; - copied = count = 0; - } + req->gen = wb_inode->gen; + + list_add_tail (&req->todo, &wb_inode->todo); + __wb_request_ref (req); /* for wind */ + + if (req->ordering.tempted) { + list_add_tail (&req->lie, &wb_inode->temptation); + __wb_request_ref (req); /* for unwind */ + } } + UNLOCK (&wb_inode->lock); out: - if (sync_frame != NULL) { - sync_frame->local = NULL; - STACK_DESTROY (sync_frame->root); - } + if (!req) + return _gf_false; - if (local != NULL) { - FREE (local); - } + return _gf_true; +} - if (iobref != NULL) { - iobref_unref (iobref); - } - if (vector != NULL) { - FREE (vector); - } +gf_boolean_t +wb_enqueue (wb_inode_t *wb_inode, call_stub_t *stub) +{ + return wb_enqueue_common (wb_inode, stub, 0); +} + - return bytes; +gf_boolean_t +wb_enqueue_tempted (wb_inode_t *wb_inode, call_stub_t *stub) +{ + return wb_enqueue_common (wb_inode, stub, 1); } -int32_t -wb_stat_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno, struct stat *buf) +wb_inode_t * +__wb_inode_create (xlator_t *this, inode_t *inode) { - wb_local_t *local = NULL; - wb_request_t *request = NULL; - call_frame_t *process_frame = NULL; - wb_file_t *file = NULL; - int32_t ret = -1; - fd_t *fd = NULL; - - local = frame->local; - file = local->file; - - request = local->request; - if (request) { - process_frame = copy_frame (frame); - if (process_frame == NULL) { - op_ret = -1; - op_errno = ENOMEM; - } - } + wb_inode_t *wb_inode = NULL; + wb_conf_t *conf = NULL; - STACK_UNWIND (frame, op_ret, op_errno, buf); + GF_VALIDATE_OR_GOTO (this->name, inode, out); - if (request != NULL) { - wb_request_unref (request); - } + conf = this->private; - if (process_frame != NULL) { - ret = wb_process_queue (process_frame, file, 0); - if ((ret == -1) && (errno == ENOMEM) && (file != NULL)) { - LOCK (&file->lock); - { - file->op_ret = -1; - file->op_errno = ENOMEM; - } - UNLOCK (&file->lock); - } + wb_inode = GF_CALLOC (1, sizeof (*wb_inode), gf_wb_mt_wb_inode_t); + if (!wb_inode) + goto out; - STACK_DESTROY (process_frame->root); - } + INIT_LIST_HEAD (&wb_inode->all); + INIT_LIST_HEAD (&wb_inode->todo); + INIT_LIST_HEAD (&wb_inode->liability); + INIT_LIST_HEAD (&wb_inode->temptation); + INIT_LIST_HEAD (&wb_inode->wip); - if (file) { - LOCK (&file->lock); - { - fd = file->fd; - } - UNLOCK (&file->lock); + wb_inode->this = this; - fd_unref (fd); - } + wb_inode->window_conf = conf->window_size; - return 0; -} + LOCK_INIT (&wb_inode->lock); + __inode_ctx_put (inode, this, (uint64_t)(unsigned long)wb_inode); -static int32_t -wb_stat_helper (call_frame_t *frame, xlator_t *this, loc_t *loc) -{ - STACK_WIND (frame, wb_stat_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->stat, - loc); - return 0; +out: + return wb_inode; } -int32_t -wb_stat (call_frame_t *frame, xlator_t *this, loc_t *loc) +wb_inode_t * +wb_inode_create (xlator_t *this, inode_t *inode) { - wb_file_t *file = NULL; - fd_t *iter_fd = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if (loc->inode) { - /* FIXME: fd_lookup extends life of fd till stat returns */ - iter_fd = fd_lookup (loc->inode, frame->root->pid); - if (iter_fd) { - if (!fd_ctx_get (iter_fd, this, &tmp_file)) { - file = (wb_file_t *)(long)tmp_file; - } else { - fd_unref (iter_fd); - iter_fd = NULL; - } - } - } + wb_inode_t *wb_inode = NULL; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - - if (iter_fd != NULL) { - fd_unref (iter_fd); - } - return 0; + GF_VALIDATE_OR_GOTO (this->name, inode, out); + + LOCK (&inode->lock); + { + wb_inode = __wb_inode_ctx_get (this, inode); + if (!wb_inode) + wb_inode = __wb_inode_create (this, inode); } + UNLOCK (&inode->lock); - local->file = file; +out: + return wb_inode; +} - frame->local = local; - if (file) { - stub = fop_stat_stub (frame, wb_stat_helper, loc); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); +void +wb_inode_destroy (wb_inode_t *wb_inode) +{ + GF_VALIDATE_OR_GOTO ("write-behind", wb_inode, out); - if (iter_fd != NULL) { - fd_unref (iter_fd); - } + LOCK_DESTROY (&wb_inode->lock); + GF_FREE (wb_inode); +out: + return; +} - return 0; - } - - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - - if (iter_fd != NULL) { - fd_unref (iter_fd); - } - return 0; - } - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); +void +__wb_fulfill_request (wb_request_t *req) +{ + wb_inode_t *wb_inode = NULL; - if (iter_fd != NULL) { - fd_unref (iter_fd); - } - return 0; - } + wb_inode = req->wb_inode; - } else { - STACK_WIND (frame, wb_stat_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->stat, - loc); - } + req->ordering.fulfilled = 1; + wb_inode->window_current -= req->total_size; + wb_inode->transit -= req->total_size; - return 0; + if (!req->ordering.lied) { + /* TODO: fail the req->frame with error if + necessary + */ + } + + __wb_request_unref (req); } -int32_t -wb_fstat_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno, struct stat *buf) +void +wb_head_done (wb_request_t *head) { - wb_local_t *local = NULL; - wb_request_t *request = NULL; - wb_file_t *file = NULL; - int32_t ret = -1; - - local = frame->local; - file = local->file; - - request = local->request; - if (request) { - wb_request_unref (request); - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - op_ret = -1; - op_errno = ENOMEM; - } - } - - STACK_UNWIND (frame, op_ret, op_errno, buf); - - return 0; + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; + wb_inode_t *wb_inode = NULL; + + wb_inode = head->wb_inode; + + LOCK (&wb_inode->lock); + { + list_for_each_entry_safe (req, tmp, &head->winds, winds) { + __wb_fulfill_request (req); + } + __wb_fulfill_request (head); + } + UNLOCK (&wb_inode->lock); } -int32_t -wb_fstat_helper (call_frame_t *frame, xlator_t *this, fd_t *fd) +void +wb_fulfill_err (wb_request_t *head, int op_errno) { - STACK_WIND (frame, - wb_fstat_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->fstat, - fd); - return 0; + wb_inode_t *wb_inode; + wb_request_t *req; + + wb_inode = head->wb_inode; + + /* for all future requests yet to arrive */ + fd_ctx_set (head->fd, THIS, op_errno); + + LOCK (&wb_inode->lock); + { + /* for all requests already arrived */ + list_for_each_entry (req, &wb_inode->all, all) { + if (req->fd != head->fd) + continue; + req->op_ret = -1; + req->op_errno = op_errno; + } + } + UNLOCK (&wb_inode->lock); } -int32_t -wb_fstat (call_frame_t *frame, xlator_t *this, fd_t *fd) +int +wb_fulfill_cbk (call_frame_t *frame, void *cookie, xlator_t *this, + int32_t op_ret, int32_t op_errno, struct iatt *prebuf, + struct iatt *postbuf, dict_t *xdata) { - wb_file_t *file = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); - - STACK_UNWIND (frame, -1, EBADFD, NULL); - return 0; - } + wb_inode_t *wb_inode = NULL; + wb_request_t *head = NULL; - file = (wb_file_t *)(long)tmp_file; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + head = frame->local; + frame->local = NULL; - local->file = file; + wb_inode = head->wb_inode; - frame->local = local; + if (op_ret == -1) { + wb_fulfill_err (head, op_errno); + } else if (op_ret < head->total_size) { + /* + * We've encountered a short write, for whatever reason. + * Set an EIO error for the next fop. This should be + * valid for writev or flush (close). + * + * TODO: Retry the write so we can potentially capture + * a real error condition (i.e., ENOSPC). + */ + wb_fulfill_err (head, EIO); + } - if (file) { - stub = fop_fstat_stub (frame, wb_fstat_helper, fd); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } - - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } + wb_head_done (head); - /* - FIXME:should the request queue be emptied in case of error? - */ - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); + wb_process_queue (wb_inode); - return 0; - } - } else { - STACK_WIND (frame, - wb_fstat_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->fstat, - fd); - } + STACK_DESTROY (frame->root); return 0; } -int32_t -wb_truncate_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno, struct stat *buf) +#define WB_IOV_LOAD(vec, cnt, req, head) do { \ + memcpy (&vec[cnt], req->stub->args.vector, \ + (req->stub->args.count * sizeof(vec[0]))); \ + cnt += req->stub->args.count; \ + head->total_size += req->write_size; \ + } while (0) + + +int +wb_fulfill_head (wb_inode_t *wb_inode, wb_request_t *head) { - wb_local_t *local = NULL; - wb_request_t *request = NULL; - wb_file_t *file = NULL; - call_frame_t *process_frame = NULL; - int32_t ret = -1; - fd_t *fd = NULL; - - local = frame->local; - file = local->file; - request = local->request; - - if (request) { - process_frame = copy_frame (frame); - if (process_frame == NULL) { - op_ret = -1; - op_errno = ENOMEM; - } - } + struct iovec vector[MAX_VECTOR_COUNT]; + int count = 0; + wb_request_t *req = NULL; + call_frame_t *frame = NULL; + gf_boolean_t fderr = _gf_false; + xlator_t *this = NULL; - STACK_UNWIND (frame, op_ret, op_errno, buf); + this = THIS; - if (request) { - wb_request_unref (request); - } + /* make sure head->total_size is updated before we run into any + * errors + */ - if (process_frame != NULL) { - ret = wb_process_queue (process_frame, file, 0); - if ((ret == -1) && (errno == ENOMEM) && (file != NULL)) { - LOCK (&file->lock); - { - file->op_ret = -1; - file->op_errno = ENOMEM; - } - UNLOCK (&file->lock); - } + WB_IOV_LOAD (vector, count, head, head); + + list_for_each_entry (req, &head->winds, winds) { + WB_IOV_LOAD (vector, count, req, head); + + iobref_merge (head->stub->args.iobref, + req->stub->args.iobref); + } - STACK_DESTROY (process_frame->root); + if (wb_fd_err (head->fd, this, NULL)) { + fderr = _gf_true; + goto err; } - if (file) { - LOCK (&file->lock); - { - fd = file->fd; - } - UNLOCK (&file->lock); + frame = create_frame (wb_inode->this, wb_inode->this->ctx->pool); + if (!frame) + goto err; - fd_unref (fd); + frame->root->lk_owner = head->lk_owner; + frame->local = head; + + LOCK (&wb_inode->lock); + { + wb_inode->transit += head->total_size; + } + UNLOCK (&wb_inode->lock); + + STACK_WIND (frame, wb_fulfill_cbk, FIRST_CHILD (frame->this), + FIRST_CHILD (frame->this)->fops->writev, + head->fd, vector, count, + head->stub->args.offset, + head->stub->args.flags, + head->stub->args.iobref, NULL); + + return 0; +err: + if (!fderr) { + /* frame creation failure */ + fderr = ENOMEM; + wb_fulfill_err (head, fderr); } - return 0; + wb_head_done (head); + + return fderr; } -static int32_t -wb_truncate_helper (call_frame_t *frame, xlator_t *this, loc_t *loc, - off_t offset) -{ - STACK_WIND (frame, - wb_truncate_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->truncate, - loc, - offset); +#define NEXT_HEAD(head, req) do { \ + if (head) \ + ret |= wb_fulfill_head (wb_inode, head); \ + head = req; \ + expected_offset = req->stub->args.offset + \ + req->write_size; \ + curr_aggregate = 0; \ + vector_count = 0; \ + } while (0) - return 0; + +int +wb_fulfill (wb_inode_t *wb_inode, list_head_t *liabilities) +{ + wb_request_t *req = NULL; + wb_request_t *head = NULL; + wb_request_t *tmp = NULL; + wb_conf_t *conf = NULL; + off_t expected_offset = 0; + size_t curr_aggregate = 0; + size_t vector_count = 0; + int ret = 0; + + conf = wb_inode->this->private; + + list_for_each_entry_safe (req, tmp, liabilities, winds) { + list_del_init (&req->winds); + + if (!head) { + NEXT_HEAD (head, req); + continue; + } + + if (req->fd != head->fd) { + NEXT_HEAD (head, req); + continue; + } + + if (!is_same_lkowner (&req->lk_owner, &head->lk_owner)) { + NEXT_HEAD (head, req); + continue; + } + + if (expected_offset != req->stub->args.offset) { + NEXT_HEAD (head, req); + continue; + } + + if ((curr_aggregate + req->write_size) > conf->aggregate_size) { + NEXT_HEAD (head, req); + continue; + } + + if (vector_count + req->stub->args.count > + MAX_VECTOR_COUNT) { + NEXT_HEAD (head, req); + continue; + } + + list_add_tail (&req->winds, &head->winds); + curr_aggregate += req->write_size; + vector_count += req->stub->args.count; + } + + if (head) + ret |= wb_fulfill_head (wb_inode, head); + + return ret; } -int32_t -wb_truncate (call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset) +void +wb_do_unwinds (wb_inode_t *wb_inode, list_head_t *lies) { - wb_file_t *file = NULL; - fd_t *iter_fd = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if (loc->inode) - { - /* - FIXME: fd_lookup extends life of fd till the execution of - truncate_cbk - */ - iter_fd = fd_lookup (loc->inode, frame->root->pid); - if (iter_fd) { - if (!fd_ctx_get (iter_fd, this, &tmp_file)){ - file = (wb_file_t *)(long)tmp_file; - } else { - fd_unref (iter_fd); - } - } - } - - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; + call_frame_t *frame = NULL; + struct iatt buf = {0, }; - local->file = file; - - frame->local = local; - if (file) { - stub = fop_truncate_stub (frame, wb_truncate_helper, loc, - offset); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + list_for_each_entry_safe (req, tmp, lies, unwinds) { + frame = req->stub->frame; - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } - - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } - } else { - STACK_WIND (frame, - wb_truncate_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->truncate, - loc, - offset); + STACK_UNWIND_STRICT (writev, frame, req->op_ret, req->op_errno, + &buf, &buf, NULL); /* :O */ + req->stub->frame = NULL; + + list_del_init (&req->unwinds); + wb_request_unref (req); } - return 0; + return; } -int32_t -wb_ftruncate_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno, struct stat *buf) +void +__wb_pick_unwinds (wb_inode_t *wb_inode, list_head_t *lies) { - wb_local_t *local = NULL; - wb_request_t *request = NULL; - wb_file_t *file = NULL; - int32_t ret = -1; - - local = frame->local; - file = local->file; - request = local->request; - - if (request) { - wb_request_unref (request); - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - op_ret = -1; - op_errno = ENOMEM; - } - } + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; - STACK_UNWIND (frame, op_ret, op_errno, buf); + list_for_each_entry_safe (req, tmp, &wb_inode->temptation, lie) { + if (!req->ordering.fulfilled && + wb_inode->window_current > wb_inode->window_conf) + continue; - return 0; -} + list_del_init (&req->lie); + list_move_tail (&req->unwinds, lies); + wb_inode->window_current += req->orig_size; -static int32_t -wb_ftruncate_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, - off_t offset) -{ - STACK_WIND (frame, - wb_ftruncate_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->ftruncate, - fd, - offset); - return 0; + if (!req->ordering.fulfilled) { + /* burden increased */ + list_add_tail (&req->lie, &wb_inode->liability); + + req->ordering.lied = 1; + + wb_inode->gen++; + } + } + + return; } - -int32_t -wb_ftruncate (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset) + +int +__wb_collapse_small_writes (wb_request_t *holder, wb_request_t *req) { - wb_file_t *file = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); - - STACK_UNWIND (frame, -1, EBADFD, NULL); - return 0; - } + char *ptr = NULL; + struct iobuf *iobuf = NULL; + struct iobref *iobref = NULL; + int ret = -1; + ssize_t required_size = 0; + size_t holder_len = 0; + size_t req_len = 0; + + if (!holder->iobref) { + holder_len = iov_length (holder->stub->args.vector, + holder->stub->args.count); + req_len = iov_length (req->stub->args.vector, + req->stub->args.count); + + required_size = max ((THIS->ctx->page_size), + (holder_len + req_len)); + iobuf = iobuf_get2 (req->wb_inode->this->ctx->iobuf_pool, + required_size); + if (iobuf == NULL) { + goto out; + } + + iobref = iobref_new (); + if (iobref == NULL) { + iobuf_unref (iobuf); + goto out; + } + + ret = iobref_add (iobref, iobuf); + if (ret != 0) { + iobuf_unref (iobuf); + iobref_unref (iobref); + gf_log (req->wb_inode->this->name, GF_LOG_WARNING, + "cannot add iobuf (%p) into iobref (%p)", + iobuf, iobref); + goto out; + } - file = (wb_file_t *)(long)tmp_file; + iov_unload (iobuf->ptr, holder->stub->args.vector, + holder->stub->args.count); + holder->stub->args.vector[0].iov_base = iobuf->ptr; + holder->stub->args.count = 1; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + iobref_unref (holder->stub->args.iobref); + holder->stub->args.iobref = iobref; - local->file = file; + iobuf_unref (iobuf); - frame->local = local; + holder->iobref = iobref_ref (iobref); + } - if (file) { - stub = fop_ftruncate_stub (frame, wb_ftruncate_helper, fd, - offset); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + ptr = holder->stub->args.vector[0].iov_base + holder->write_size; - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } + iov_unload (ptr, req->stub->args.vector, + req->stub->args.count); - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } - } else { - STACK_WIND (frame, - wb_ftruncate_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->ftruncate, - fd, - offset); - } + holder->stub->args.vector[0].iov_len += req->write_size; + holder->write_size += req->write_size; + holder->ordering.size += req->write_size; - return 0; + ret = 0; +out: + return ret; } -int32_t -wb_utimens_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno, struct stat *buf) +void +__wb_preprocess_winds (wb_inode_t *wb_inode) { - wb_local_t *local = NULL; - wb_request_t *request = NULL; - call_frame_t *process_frame = NULL; - wb_file_t *file = NULL; - int32_t ret = -1; - fd_t *fd = NULL; - - local = frame->local; - file = local->file; - request = local->request; - - if (request) { - process_frame = copy_frame (frame); - if (process_frame == NULL) { - op_ret = -1; - op_errno = ENOMEM; + off_t offset_expected = 0; + ssize_t space_left = 0; + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; + wb_request_t *holder = NULL; + wb_conf_t *conf = NULL; + int ret = 0; + ssize_t page_size = 0; + + /* With asynchronous IO from a VM guest (as a file), there + can be two sequential writes happening in two regions + of the file. But individual (broken down) IO requests + can arrive interleaved. + + TODO: cycle for each such sequence sifting + through the interleaved ops + */ + + page_size = wb_inode->this->ctx->page_size; + conf = wb_inode->this->private; + + list_for_each_entry_safe (req, tmp, &wb_inode->todo, todo) { + if (!req->ordering.tempted) { + if (holder) { + if (wb_requests_conflict (holder, req)) + /* do not hold on write if a + dependent write is in queue */ + holder->ordering.go = 1; + } + /* collapse only non-sync writes */ + continue; + } else if (!holder) { + /* holder is always a non-sync write */ + holder = req; + continue; + } + + offset_expected = holder->stub->args.offset + + holder->write_size; + + if (req->stub->args.offset != offset_expected) { + holder->ordering.go = 1; + holder = req; + continue; + } + + if (!is_same_lkowner (&req->lk_owner, &holder->lk_owner)) { + holder->ordering.go = 1; + holder = req; + continue; + } + + if (req->fd != holder->fd) { + holder->ordering.go = 1; + holder = req; + continue; } - } - STACK_UNWIND (frame, op_ret, op_errno, buf); + space_left = page_size - holder->write_size; - if (request) { - wb_request_unref (request); - } + if (space_left < req->write_size) { + holder->ordering.go = 1; + holder = req; + continue; + } - if (request && (process_frame != NULL)) { - ret = wb_process_queue (process_frame, file, 0); - if ((ret == -1) && (errno == ENOMEM) && (file != NULL)) { - LOCK (&file->lock); - { - file->op_ret = -1; - file->op_errno = ENOMEM; - } - UNLOCK (&file->lock); - } + ret = __wb_collapse_small_writes (holder, req); + if (ret) + continue; - STACK_DESTROY (process_frame->root); - } + /* collapsed request is as good as wound + (from its p.o.v) + */ + list_del_init (&req->todo); + __wb_fulfill_request (req); - if (file) { - LOCK (&file->lock); - { - fd = file->fd; - } - UNLOCK (&file->lock); + /* Only the last @holder in queue which - fd_unref (fd); - } + - does not have any non-buffered-writes following it + - has not yet filled its capacity - return 0; -} + does not get its 'go' set, in anticipation of the arrival + of consecutive smaller writes. + */ + } + /* but if trickling writes are enabled, then do not hold back + writes if there are no outstanding requests + */ -static int32_t -wb_utimens_helper (call_frame_t *frame, xlator_t *this, loc_t *loc, - struct timespec tv[2]) -{ - STACK_WIND (frame, - wb_utimens_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->utimens, - loc, - tv); + if (conf->trickling_writes && !wb_inode->transit && holder) + holder->ordering.go = 1; - return 0; + return; } -int32_t -wb_utimens (call_frame_t *frame, xlator_t *this, loc_t *loc, - struct timespec tv[2]) +void +__wb_pick_winds (wb_inode_t *wb_inode, list_head_t *tasks, + list_head_t *liabilities) { - wb_file_t *file = NULL; - fd_t *iter_fd = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if (loc->inode) { - /* - FIXME: fd_lookup extends life of fd till the execution - of wb_utimens_cbk - */ - iter_fd = fd_lookup (loc->inode, frame->root->pid); - if (iter_fd) { - if (!fd_ctx_get (iter_fd, this, &tmp_file)) { - file = (wb_file_t *)(long)tmp_file; - } else { - fd_unref (iter_fd); - } - } + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; - } + list_for_each_entry_safe (req, tmp, &wb_inode->todo, todo) { + if (wb_liability_has_conflict (wb_inode, req)) + continue; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + if (req->ordering.tempted && !req->ordering.go) + /* wait some more */ + continue; - local->file = file; + if (req->stub->fop == GF_FOP_WRITE) { + if (wb_wip_has_conflict (wb_inode, req)) + continue; - frame->local = local; + list_add_tail (&req->wip, &wb_inode->wip); - if (file) { - stub = fop_utimens_stub (frame, wb_utimens_helper, loc, tv); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + if (!req->ordering.tempted) + /* unrefed in wb_writev_cbk */ + req->stub->frame->local = + __wb_request_ref (req); + } - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } + list_del_init (&req->todo); - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } - } else { - STACK_WIND (frame, - wb_utimens_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->utimens, - loc, - tv); - } - - return 0; + if (req->ordering.tempted) + list_add_tail (&req->winds, liabilities); + else + list_add_tail (&req->winds, tasks); + } } -int32_t -wb_open_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno, fd_t *fd) + +void +wb_do_winds (wb_inode_t *wb_inode, list_head_t *tasks) { - long flags = 0; - wb_file_t *file = NULL; - wb_conf_t *conf = this->private; - - if (op_ret != -1) { - file = wb_file_create (this, fd); - if (file == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, fd); - return 0; - } + wb_request_t *req = NULL; + wb_request_t *tmp = NULL; - /* - If mandatory locking has been enabled on this file, - we disable caching on it - */ - - if ((fd->inode->st_mode & S_ISGID) - && !(fd->inode->st_mode & S_IXGRP)) - file->disabled = 1; - - /* If O_DIRECT then, we disable chaching */ - if (frame->local) { - flags = (long)frame->local; - if (((flags & O_DIRECT) == O_DIRECT) - || ((flags & O_ACCMODE) == O_RDONLY) - || (((flags & O_SYNC) == O_SYNC) - && conf->enable_O_SYNC == _gf_true)) { - file->window_size = 0; - } - } + list_for_each_entry_safe (req, tmp, tasks, winds) { + list_del_init (&req->winds); - LOCK_INIT (&file->lock); - } - - frame->local = NULL; + call_resume (req->stub); - STACK_UNWIND (frame, op_ret, op_errno, fd); - return 0; + wb_request_unref (req); + } } -int32_t -wb_open (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags, - fd_t *fd) +void +wb_process_queue (wb_inode_t *wb_inode) { - frame->local = (void *)(long)flags; + list_head_t tasks = {0, }; + list_head_t lies = {0, }; + list_head_t liabilities = {0, }; + int retry = 0; - STACK_WIND (frame, - wb_open_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->open, - loc, flags, fd); - return 0; -} + INIT_LIST_HEAD (&tasks); + INIT_LIST_HEAD (&lies); + INIT_LIST_HEAD (&liabilities); + do { + LOCK (&wb_inode->lock); + { + __wb_preprocess_winds (wb_inode); + + __wb_pick_winds (wb_inode, &tasks, &liabilities); + + __wb_pick_unwinds (wb_inode, &lies); -int32_t -wb_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno, fd_t *fd, inode_t *inode, - struct stat *buf) -{ - long flags = 0; - wb_file_t *file = NULL; - wb_conf_t *conf = this->private; - - if (op_ret != -1) { - file = wb_file_create (this, fd); - if (file == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, fd, inode, buf); - return 0; } - /* - * If mandatory locking has been enabled on this file, - * we disable caching on it + UNLOCK (&wb_inode->lock); + + wb_do_unwinds (wb_inode, &lies); + + wb_do_winds (wb_inode, &tasks); + + /* fd might've been marked bad due to previous errors. + * Since, caller of wb_process_queue might be the last fop on + * inode, make sure we keep processing request queue, till there + * are no requests left. */ - if ((fd->inode->st_mode & S_ISGID) - && !(fd->inode->st_mode & S_IXGRP)) - file->disabled = 1; - - /* If O_DIRECT then, we disable chaching */ - if (frame->local) { - flags = (long)frame->local; - if (((flags & O_DIRECT) == O_DIRECT) - || ((flags & O_ACCMODE) == O_RDONLY) - || (((flags & O_SYNC) == O_SYNC) - && (conf->enable_O_SYNC == _gf_true))) { - file->window_size = 0; - } - } + retry = wb_fulfill (wb_inode, &liabilities); + } while (retry); - LOCK_INIT (&file->lock); - } - - frame->local = NULL; - - STACK_UNWIND (frame, op_ret, op_errno, fd, inode, buf); - return 0; + return; } -int32_t -wb_create (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags, - mode_t mode, fd_t *fd) +int +wb_writev_cbk (call_frame_t *frame, void *cookie, xlator_t *this, + int32_t op_ret, int32_t op_errno, + struct iatt *prebuf, struct iatt *postbuf, dict_t *xdata) { - frame->local = (void *)(long)flags; + wb_request_t *req = NULL; + wb_inode_t *wb_inode; - STACK_WIND (frame, - wb_create_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->create, - loc, flags, mode, fd); - return 0; + req = frame->local; + frame->local = NULL; + wb_inode = req->wb_inode; + + wb_request_unref (req); + + /* requests could be pending while this was in progress */ + wb_process_queue(wb_inode); + + STACK_UNWIND_STRICT (writev, frame, op_ret, op_errno, prebuf, postbuf, + xdata); + return 0; } -size_t -__wb_mark_wind_all (list_head_t *list, list_head_t *winds) +int +wb_writev_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, + struct iovec *vector, int32_t count, off_t offset, + uint32_t flags, struct iobref *iobref, dict_t *xdata) { - wb_request_t *request = NULL; - size_t size = 0; - struct iovec *vector = NULL; - int32_t count = 0; - char first_request = 1; - off_t offset_expected = 0; - size_t length = 0; + STACK_WIND (frame, wb_writev_cbk, + FIRST_CHILD (this), FIRST_CHILD (this)->fops->writev, + fd, vector, count, offset, flags, iobref, xdata); + return 0; +} - list_for_each_entry (request, list, list) - { - if ((request->stub == NULL) - || (request->stub->fop != GF_FOP_WRITE)) { - break; - } - vector = request->stub->args.writev.vector; - count = request->stub->args.writev.count; - if (!request->flags.write_request.stack_wound) { - if (first_request) { - first_request = 0; - offset_expected = request->stub->args.writev.off; - } - - if (request->stub->args.writev.off != offset_expected) { - break; - } - - length = iov_length (vector, count); - size += length; - offset_expected += length; - - request->flags.write_request.stack_wound = 1; - list_add_tail (&request->winds, winds); - } +int +wb_writev (call_frame_t *frame, xlator_t *this, fd_t *fd, struct iovec *vector, + int32_t count, off_t offset, uint32_t flags, struct iobref *iobref, + dict_t *xdata) +{ + wb_inode_t *wb_inode = NULL; + wb_conf_t *conf = NULL; + gf_boolean_t wb_disabled = 0; + call_stub_t *stub = NULL; + int ret = -1; + int32_t op_errno = EINVAL; + int o_direct = O_DIRECT; + + conf = this->private; + + if (wb_fd_err (fd, this, &op_errno)) { + goto unwind; } - - return size; -} + wb_inode = wb_inode_create (this, fd->inode); + if (!wb_inode) { + op_errno = ENOMEM; + goto unwind; + } -void -__wb_can_wind (list_head_t *list, size_t aggregate_conf, - char *other_fop_in_queue, char *non_contiguous_writes, - char *incomplete_writes, char *enough_data_aggregated) -{ - wb_request_t *request = NULL; - size_t aggregate_current = 0, length = 0; - struct iovec *vector = NULL; - int32_t count = 0; - char first_request = 1; - off_t offset_expected = 0; + if (!conf->strict_O_DIRECT) + o_direct = 0; - list_for_each_entry (request, list, list) - { - if ((request->stub == NULL) - || (request->stub->fop != GF_FOP_WRITE)) { - if (request->stub && other_fop_in_queue) { - *other_fop_in_queue = 1; - } - break; - } + if (fd->flags & (O_SYNC|O_DSYNC|o_direct)) + wb_disabled = 1; - if (request->flags.write_request.stack_wound - && !request->flags.write_request.got_reply - && (incomplete_writes != NULL)) { - *incomplete_writes = 1; - break; - } + if (flags & (O_SYNC|O_DSYNC|o_direct)) + wb_disabled = 1; - vector = request->stub->args.writev.vector; - count = request->stub->args.writev.count; - if (!request->flags.write_request.stack_wound) { - if (first_request) { - first_request = 0; - offset_expected = request->stub->args.writev.off; - } - - if (offset_expected != request->stub->args.writev.off) { - if (non_contiguous_writes) { - *non_contiguous_writes = 1; - } - break; - } - - length = iov_length (vector, count); - aggregate_current += length; - offset_expected += length; - - if (aggregate_current >= aggregate_conf) { - if (enough_data_aggregated != NULL) { - *enough_data_aggregated = 1; - } - break; - } - } + if (wb_disabled) + stub = fop_writev_stub (frame, wb_writev_helper, fd, vector, + count, offset, flags, iobref, xdata); + else + stub = fop_writev_stub (frame, NULL, fd, vector, count, offset, + flags, iobref, xdata); + if (!stub) { + op_errno = ENOMEM; + goto unwind; } - return; -} + if (wb_disabled) + ret = wb_enqueue (wb_inode, stub); + else + ret = wb_enqueue_tempted (wb_inode, stub); + if (!ret) { + op_errno = ENOMEM; + goto unwind; + } -ssize_t -__wb_mark_winds (list_head_t *list, list_head_t *winds, size_t aggregate_conf, - char wind_all, char enable_trickling_writes) -{ - size_t size = 0; - char other_fop_in_queue = 0; - char incomplete_writes = 1; - char non_contiguous_writes = 0; - char enough_data_aggregated = 0; - char *trickling_writes = NULL; - - if (enable_trickling_writes) { - trickling_writes = &incomplete_writes; - } + wb_process_queue (wb_inode); - if (!wind_all) { - __wb_can_wind (list, aggregate_conf, &other_fop_in_queue, - &non_contiguous_writes, trickling_writes, - &enough_data_aggregated); - } + return 0; + +unwind: + STACK_UNWIND_STRICT (writev, frame, -1, op_errno, NULL, NULL, NULL); - if ((!incomplete_writes) || (wind_all) || (non_contiguous_writes) - || (other_fop_in_queue) || (enough_data_aggregated)) { - size = __wb_mark_wind_all (list, winds); - } + if (stub) + call_stub_destroy (stub); - return size; + return 0; } -char -__wb_can_unwind (list_head_t *list, size_t window_conf, - size_t *window_current_ptr) +int +wb_readv_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, + off_t offset, uint32_t flags, dict_t *xdata) { - wb_request_t *request = NULL; - size_t window_current = 0; - struct iovec *vector = NULL; - int32_t count = 0; - char can_unwind = 1; + STACK_WIND (frame, default_readv_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->readv, fd, size, offset, flags, + xdata); + return 0; +} - list_for_each_entry (request, list, list) - { - if ((request->stub == NULL) - || (request->stub->fop != GF_FOP_WRITE)) { - continue; - } - vector = request->stub->args.writev.vector; - count = request->stub->args.writev.count; +int +wb_readv (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, + off_t offset, uint32_t flags, dict_t *xdata) +{ + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - if (request->flags.write_request.write_behind - && !request->flags.write_request.got_reply) - { - window_current += iov_length (vector, count); - if (window_current > window_conf) { - can_unwind = 0; - break; - } - } - } + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) + goto noqueue; - if (can_unwind && (window_current_ptr != NULL)) { - *window_current_ptr = window_current; - } - - return can_unwind; -} + stub = fop_readv_stub (frame, wb_readv_helper, fd, size, + offset, flags, xdata); + if (!stub) + goto unwind; + if (!wb_enqueue (wb_inode, stub)) + goto unwind; -size_t -__wb_mark_unwind_till (list_head_t *list, list_head_t *unwinds, size_t size) -{ - size_t written_behind = 0; - wb_request_t *request = NULL; - struct iovec *vector = NULL; - int32_t count = 0; + wb_process_queue (wb_inode); - list_for_each_entry (request, list, list) - { - if ((request->stub == NULL) - || (request->stub->fop != GF_FOP_WRITE)) { - continue; - } + return 0; - vector = request->stub->args.writev.vector; - count = request->stub->args.writev.count; - - if (written_behind <= size) { - if (!request->flags.write_request.write_behind) { - written_behind += iov_length (vector, count); - request->flags.write_request.write_behind = 1; - list_add_tail (&request->unwinds, unwinds); - } - } else { - break; - } - } +unwind: + STACK_UNWIND_STRICT (readv, frame, -1, ENOMEM, NULL, 0, NULL, NULL, + NULL); + return 0; - return written_behind; +noqueue: + STACK_WIND (frame, default_readv_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->readv, fd, size, offset, flags, + xdata); + return 0; } -void -__wb_mark_unwinds (list_head_t *list, list_head_t *unwinds, size_t window_conf) +int +wb_flush_bg_cbk (call_frame_t *frame, void *cookie, xlator_t *this, + int32_t op_ret, int32_t op_errno, dict_t *xdata) { - size_t window_current = 0; - - if (__wb_can_unwind (list, window_conf, &window_current)) { - __wb_mark_unwind_till (list, unwinds, - window_conf - window_current); - } + STACK_DESTROY (frame->root); + return 0; } -uint32_t -__wb_get_other_requests (list_head_t *list, list_head_t *other_requests) +int +wb_flush_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata) { - wb_request_t *request = NULL; - uint32_t count = 0; - list_for_each_entry (request, list, list) { - if ((request->stub == NULL) - || (request->stub->fop == GF_FOP_WRITE)) { - break; - } - - if (!request->flags.other_requests.marked_for_resume) { - request->flags.other_requests.marked_for_resume = 1; - list_add_tail (&request->other_requests, - other_requests); - count++; - - /* lets handle one at a time */ - break; - } - } + wb_conf_t *conf = NULL; + wb_inode_t *wb_inode = NULL; + call_frame_t *bg_frame = NULL; + int32_t op_errno = 0; + int op_ret = 0; - return count; + conf = this->private; + + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) { + op_ret = -1; + op_errno = EINVAL; + goto unwind; + } + + if (wb_fd_err (fd, this, &op_errno)) { + op_ret = -1; + goto unwind; + } + + if (conf->flush_behind) + goto flushbehind; + + STACK_WIND (frame, default_flush_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->flush, fd, xdata); + return 0; + +flushbehind: + bg_frame = copy_frame (frame); + if (!bg_frame) { + op_ret = -1; + op_errno = ENOMEM; + goto unwind; + } + + STACK_WIND (bg_frame, wb_flush_bg_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->flush, fd, xdata); + /* fall through */ +unwind: + STACK_UNWIND_STRICT (flush, frame, op_ret, op_errno, NULL); + + return 0; } -int32_t -wb_stack_unwind (list_head_t *unwinds) +int +wb_flush (call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata) { - struct stat buf = {0,}; - wb_request_t *request = NULL, *dummy = NULL; - call_frame_t *frame = NULL; - wb_local_t *local = NULL; + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - list_for_each_entry_safe (request, dummy, unwinds, unwinds) - { - frame = request->stub->frame; - local = frame->local; + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) + goto noqueue; - STACK_UNWIND (frame, local->op_ret, local->op_errno, &buf); + stub = fop_flush_stub (frame, wb_flush_helper, fd, xdata); + if (!stub) + goto unwind; - wb_request_unref (request); - } + if (!wb_enqueue (wb_inode, stub)) + goto unwind; + + wb_process_queue (wb_inode); return 0; -} +unwind: + STACK_UNWIND_STRICT (flush, frame, -1, ENOMEM, NULL); -int32_t -wb_resume_other_requests (call_frame_t *frame, wb_file_t *file, - list_head_t *other_requests) -{ - int32_t ret = 0; - wb_request_t *request = NULL, *dummy = NULL; - int32_t fops_removed = 0; - char wind = 0; - call_stub_t *stub = NULL; + return 0; - if (list_empty (other_requests)) { - goto out; - } +noqueue: + STACK_WIND (frame, default_flush_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->flush, fd, xdata); + return 0; +} - list_for_each_entry_safe (request, dummy, other_requests, - other_requests) { - wind = request->stub->wind; - stub = request->stub; - - LOCK (&file->lock); - { - request->stub = NULL; - } - UNLOCK (&file->lock); - - if (!wind) { - wb_request_unref (request); - fops_removed++; - } - - call_resume (stub); - } - if (fops_removed > 0) { - ret = wb_process_queue (frame, file, 0); - } - -out: - return ret; + +int +wb_fsync_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, + int32_t datasync, dict_t *xdata) +{ + STACK_WIND (frame, default_fsync_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fsync, fd, datasync, xdata); + return 0; } -int32_t -wb_do_ops (call_frame_t *frame, wb_file_t *file, list_head_t *winds, - list_head_t *unwinds, list_head_t *other_requests) +int +wb_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync, + dict_t *xdata) { - int32_t ret = -1; + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; + int32_t op_errno = EINVAL; - ret = wb_stack_unwind (unwinds); - if (ret == -1) { - goto out; - } + if (wb_fd_err (fd, this, &op_errno)) + goto unwind; - ret = wb_sync (frame, file, winds); - if (ret == -1) { - goto out; - } + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) + goto noqueue; - ret = wb_resume_other_requests (frame, file, other_requests); + stub = fop_fsync_stub (frame, wb_fsync_helper, fd, datasync, xdata); + if (!stub) + goto unwind; -out: - return ret; -} + if (!wb_enqueue (wb_inode, stub)) + goto unwind; + wb_process_queue (wb_inode); -/* this procedure assumes that write requests have only one vector to write */ -void -__wb_collapse_write_bufs (list_head_t *requests, size_t page_size) -{ + return 0; - off_t offset_expected = 0; - size_t space_left = 0, length = 0, *iov_len = NULL; - char *ptr = NULL, first_request = 1; - wb_request_t *request = NULL, *tmp = NULL; - - list_for_each_entry_safe (request, tmp, requests, list) { - if ((request->stub->fop != GF_FOP_WRITE) - || (request->flags.write_request.stack_wound)) { - space_left = 0; - ptr = NULL; - first_request = 1; - continue; - } +unwind: + STACK_UNWIND_STRICT (fsync, frame, -1, op_errno, NULL, NULL, NULL); - if (request->flags.write_request.write_behind) { - length = iov_length (request->stub->args.writev.vector, - request->stub->args.writev.count); - - if (first_request) { - first_request = 0; - offset_expected = request->stub->args.writev.off; - } - - if (request->stub->args.writev.off != offset_expected) { - offset_expected = request->stub->args.writev.off + length; - space_left = page_size - length; - ptr = request->stub->args.writev.vector[0].iov_base - + length; - iov_len = &request->stub->args.writev.vector[0].iov_len; - continue; - } - - if (space_left >= length) { - iov_unload (ptr, - request->stub->args.writev.vector, - request->stub->args.writev.count); - space_left -= length; - ptr += length; - *iov_len = *iov_len + length; - - list_move_tail (&request->list, - &request->file->passive_requests); - - __wb_request_unref (request); - } else { - space_left = page_size - length; - ptr = request->stub->args.writev.vector[0].iov_base - + length; - iov_len = &request->stub->args.writev.vector[0].iov_len; - } - } else { - break; - } + return 0; - offset_expected += length; - } +noqueue: + STACK_WIND (frame, default_fsync_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fsync, fd, datasync, xdata); + return 0; } -int32_t -wb_process_queue (call_frame_t *frame, wb_file_t *file, char flush_all) +int +wb_stat_helper (call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata) { - list_head_t winds, unwinds, other_requests; - size_t size = 0; - wb_conf_t *conf = file->this->private; - uint32_t count = 0; - int32_t ret = -1; - - INIT_LIST_HEAD (&winds); - INIT_LIST_HEAD (&unwinds); - INIT_LIST_HEAD (&other_requests); - - if (file == NULL) { - errno = EINVAL; - goto out; - } + STACK_WIND (frame, default_stat_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->stat, loc, xdata); + return 0; +} - size = conf->aggregate_size; - LOCK (&file->lock); - { - /* - * make sure requests are marked for unwinding and adjacent - * continguous write buffers (each of size less than that of - * an iobuf) are packed properly so that iobufs are filled to - * their maximum capacity, before calling __wb_mark_winds. - */ - __wb_mark_unwinds (&file->request, &unwinds, conf->window_size); - __wb_collapse_write_bufs (&file->request, - file->this->ctx->page_size); +int +wb_stat (call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata) +{ + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - count = __wb_get_other_requests (&file->request, - &other_requests); - if (count == 0) { - __wb_mark_winds (&file->request, &winds, size, - flush_all, - conf->enable_trickling_writes); - } + wb_inode = wb_inode_ctx_get (this, loc->inode); + if (!wb_inode) + goto noqueue; - } - UNLOCK (&file->lock); + stub = fop_stat_stub (frame, wb_stat_helper, loc, xdata); + if (!stub) + goto unwind; - ret = wb_do_ops (frame, file, &winds, &unwinds, &other_requests); + if (!wb_enqueue (wb_inode, stub)) + goto unwind; -out: - return ret; -} + wb_process_queue (wb_inode); + return 0; -int32_t -wb_writev_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno, struct stat *stbuf) -{ - STACK_UNWIND (frame, op_ret, op_errno, stbuf); +unwind: + STACK_UNWIND_STRICT (stat, frame, -1, ENOMEM, NULL, NULL); + + if (stub) + call_stub_destroy (stub); return 0; + +noqueue: + STACK_WIND (frame, default_stat_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->stat, loc, xdata); + return 0; } -int32_t -wb_writev (call_frame_t *frame, xlator_t *this, fd_t *fd, struct iovec *vector, - int32_t count, off_t offset, struct iobref *iobref) +int +wb_fstat_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata) { - wb_file_t *file = NULL; - char wb_disabled = 0; - call_frame_t *process_frame = NULL; - size_t size = 0; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_local_t *local = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - int32_t op_ret = -1, op_errno = -1; - - if (vector != NULL) - size = iov_length (vector, count); - - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); - - STACK_UNWIND (frame, -1, EBADFD, NULL); - return 0; - } - - file = (wb_file_t *)(long)tmp_file; - if (!file) { - gf_log (this->name, GF_LOG_DEBUG, - "wb_file not found for fd %p", fd); - STACK_UNWIND (frame, -1, EBADFD, NULL); - return 0; - } + STACK_WIND (frame, default_fstat_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fstat, fd, xdata); + return 0; +} - LOCK (&file->lock); - { - op_ret = file->op_ret; - op_errno = file->op_errno; - - file->op_ret = 0; - - if ((op_ret == 0) && (file->disabled || file->disable_till)) { - if (size > file->disable_till) { - file->disable_till = 0; - } else { - file->disable_till -= size; - } - wb_disabled = 1; - } - } - UNLOCK (&file->lock); - if (op_ret == -1) { - STACK_UNWIND (frame, op_ret, op_errno, NULL); - return 0; - } +int +wb_fstat (call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata) +{ + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - if (wb_disabled) { - STACK_WIND (frame, wb_writev_cbk, - FIRST_CHILD (frame->this), - FIRST_CHILD (frame->this)->fops->writev, - fd, vector, count, offset, iobref); - return 0; - } - process_frame = copy_frame (frame); - if (process_frame == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) + goto noqueue; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - STACK_DESTROY (process_frame->root); - return 0; - } + stub = fop_fstat_stub (frame, wb_fstat_helper, fd, xdata); + if (!stub) + goto unwind; - frame->local = local; - local->file = file; + if (!wb_enqueue (wb_inode, stub)) + goto unwind; - stub = fop_writev_stub (frame, NULL, fd, vector, count, offset, - iobref); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); + wb_process_queue (wb_inode); - STACK_DESTROY (process_frame->root); - return 0; - } + return 0; - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); +unwind: + STACK_UNWIND_STRICT (fstat, frame, -1, ENOMEM, NULL, NULL); - STACK_DESTROY (process_frame->root); + if (stub) call_stub_destroy (stub); - return 0; - } - - ret = wb_process_queue (process_frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); + return 0; - STACK_DESTROY (process_frame->root); - call_stub_destroy (stub); - } +noqueue: + STACK_WIND (frame, default_fstat_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fstat, fd, xdata); + return 0; +} - STACK_DESTROY (process_frame->root); +int +wb_truncate_helper (call_frame_t *frame, xlator_t *this, loc_t *loc, + off_t offset, dict_t *xdata) +{ + STACK_WIND (frame, default_truncate_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->truncate, loc, offset, xdata); return 0; } -int32_t -wb_readv_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno, struct iovec *vector, int32_t count, - struct stat *stbuf, struct iobref *iobref) +int +wb_truncate (call_frame_t *frame, xlator_t *this, loc_t *loc, off_t offset, + dict_t *xdata) { - wb_local_t *local = NULL; - wb_file_t *file = NULL; - wb_request_t *request = NULL; - int32_t ret = 0; - - local = frame->local; - file = local->file; - request = local->request; - - if (request) { - wb_request_unref (request); - - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL, -1, NULL, NULL); - return 0; - } - } + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; + + wb_inode = wb_inode_create (this, loc->inode); + if (!wb_inode) + goto unwind; + + stub = fop_truncate_stub (frame, wb_truncate_helper, loc, + offset, xdata); + if (!stub) + goto unwind; - STACK_UNWIND (frame, op_ret, op_errno, vector, count, stbuf, iobref); + if (!wb_enqueue (wb_inode, stub)) + goto unwind; + + wb_process_queue (wb_inode); + + return 0; + +unwind: + STACK_UNWIND_STRICT (truncate, frame, -1, ENOMEM, NULL, NULL, NULL); + + if (stub) + call_stub_destroy (stub); return 0; } -static int32_t -wb_readv_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, - off_t offset) +int +wb_ftruncate_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, + off_t offset, dict_t *xdata) { - STACK_WIND (frame, - wb_readv_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->readv, - fd, size, offset); - + STACK_WIND (frame, default_ftruncate_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->ftruncate, fd, offset, xdata); return 0; } -int32_t -wb_readv (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, - off_t offset) +int +wb_ftruncate (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset, + dict_t *xdata) { - wb_file_t *file = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - int32_t ret = -1; - wb_request_t *request = NULL; - - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); - - STACK_UNWIND (frame, -1, EBADFD, NULL); - return 0; + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; + int32_t op_errno = 0; + + wb_inode = wb_inode_create (this, fd->inode); + if (!wb_inode) { + op_errno = ENOMEM; + goto unwind; } - file = (wb_file_t *)(long)tmp_file; + if (wb_fd_err (fd, this, &op_errno)) + goto unwind; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - return 0; + stub = fop_ftruncate_stub (frame, wb_ftruncate_helper, fd, + offset, xdata); + if (!stub) { + op_errno = ENOMEM; + goto unwind; } - local->file = file; - - frame->local = local; - if (file) { - stub = fop_readv_stub (frame, wb_readv_helper, fd, size, - offset); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + if (!wb_enqueue (wb_inode, stub)) { + op_errno = ENOMEM; + goto unwind; + } - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } + wb_process_queue (wb_inode); - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - call_stub_destroy (stub); - return 0; - } + return 0; - } else { - STACK_WIND (frame, - wb_readv_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->readv, - fd, size, offset); - } +unwind: + STACK_UNWIND_STRICT (ftruncate, frame, -1, op_errno, NULL, NULL, NULL); + if (stub) + call_stub_destroy (stub); return 0; } -int32_t -wb_ffr_bg_cbk (call_frame_t *frame, void *cookie, xlator_t *this, - int32_t op_ret, int32_t op_errno) +int +wb_setattr_helper (call_frame_t *frame, xlator_t *this, loc_t *loc, + struct iatt *stbuf, int32_t valid, dict_t *xdata) { - STACK_DESTROY (frame->root); + STACK_WIND (frame, default_setattr_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->setattr, loc, stbuf, valid, xdata); return 0; } -int32_t -wb_ffr_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno) +int +wb_setattr (call_frame_t *frame, xlator_t *this, loc_t *loc, + struct iatt *stbuf, int32_t valid, dict_t *xdata) { - wb_local_t *local = NULL; - wb_file_t *file = NULL; - wb_conf_t *conf = NULL; - char unwind = 0; - int32_t ret = -1; - int disabled = 0; - int64_t disable_till = 0; + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - conf = this->private; - local = frame->local; - file = local->file; + wb_inode = wb_inode_ctx_get (this, loc->inode); + if (!wb_inode) + goto noqueue; - LOCK (&file->lock); - { - disabled = file->disabled; - disable_till = file->disable_till; - } - UNLOCK (&file->lock); - - if (conf->flush_behind - && (!disabled) && (disable_till == 0)) { - unwind = 1; - } else { - local->reply_count++; - /* - without flush-behind, unwind should wait for replies of - writes queued before and the flush - */ - if (local->reply_count == 2) { - unwind = 1; - } - } + stub = fop_setattr_stub (frame, wb_setattr_helper, loc, stbuf, + valid, xdata); + if (!stub) + goto unwind; - if (unwind) { - LOCK (&file->lock); - { - if (file->op_ret == -1) { - op_ret = file->op_ret; - op_errno = file->op_errno; + if (!wb_enqueue (wb_inode, stub)) + goto unwind; - file->op_ret = 0; - } - } - UNLOCK (&file->lock); + wb_process_queue (wb_inode); - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - op_ret = -1; - op_errno = ENOMEM; - } - - STACK_UNWIND (frame, op_ret, op_errno); - } + return 0; +unwind: + STACK_UNWIND_STRICT (setattr, frame, -1, ENOMEM, NULL, NULL, NULL); + if (stub) + call_stub_destroy (stub); + return 0; + +noqueue: + STACK_WIND (frame, default_setattr_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->setattr, loc, stbuf, valid, xdata); return 0; } -int32_t -wb_flush (call_frame_t *frame, xlator_t *this, fd_t *fd) +int +wb_fsetattr_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, + struct iatt *stbuf, int32_t valid, dict_t *xdata) { - wb_conf_t *conf = NULL; - wb_file_t *file = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - call_frame_t *process_frame = NULL; - wb_local_t *tmp_local = NULL; - wb_request_t *request = NULL; - int32_t ret = 0; - int disabled = 0; - int64_t disable_till = 0; + STACK_WIND (frame, default_fsetattr_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fsetattr, fd, stbuf, valid, xdata); + return 0; +} - conf = this->private; - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); +int +wb_fsetattr (call_frame_t *frame, xlator_t *this, fd_t *fd, + struct iatt *stbuf, int32_t valid, dict_t *xdata) +{ + wb_inode_t *wb_inode = NULL; + call_stub_t *stub = NULL; - STACK_UNWIND (frame, -1, EBADFD); - return 0; - } + wb_inode = wb_inode_ctx_get (this, fd->inode); + if (!wb_inode) + goto noqueue; - file = (wb_file_t *)(long)tmp_file; + stub = fop_fsetattr_stub (frame, wb_fsetattr_helper, fd, stbuf, + valid, xdata); + if (!stub) + goto unwind; - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM, NULL); - return 0; - } + if (!wb_enqueue (wb_inode, stub)) + goto unwind; - local->file = file; + wb_process_queue (wb_inode); - frame->local = local; - stub = fop_flush_cbk_stub (frame, wb_ffr_cbk, 0, 0); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - return 0; - } + return 0; +unwind: + STACK_UNWIND_STRICT (fsetattr, frame, -1, ENOMEM, NULL, NULL, NULL); - process_frame = copy_frame (frame); - if (process_frame == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); + if (stub) call_stub_destroy (stub); - return 0; - } + return 0; - LOCK (&file->lock); - { - disabled = file->disabled; - disable_till = file->disable_till; - } - UNLOCK (&file->lock); - - if (conf->flush_behind - && (!disabled) && (disable_till == 0)) { - tmp_local = CALLOC (1, sizeof (*local)); - if (tmp_local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - - STACK_DESTROY (process_frame->root); - call_stub_destroy (stub); - return 0; - } - tmp_local->file = file; +noqueue: + STACK_WIND (frame, default_fsetattr_cbk, FIRST_CHILD(this), + FIRST_CHILD(this)->fops->fsetattr, fd, stbuf, valid, xdata); + return 0; +} - process_frame->local = tmp_local; - } - fd_ref (fd); +int +wb_forget (xlator_t *this, inode_t *inode) +{ + uint64_t tmp = 0; + wb_inode_t *wb_inode = NULL; - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); + inode_ctx_del (inode, this, &tmp); - fd_unref (fd); - call_stub_destroy (stub); - STACK_DESTROY (process_frame->root); - return 0; - } + wb_inode = (wb_inode_t *)(long)tmp; - ret = wb_process_queue (process_frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM); + if (!wb_inode) + return 0; - fd_unref (fd); - call_stub_destroy (stub); - STACK_DESTROY (process_frame->root); - return 0; - } - - if (conf->flush_behind - && (!disabled) && (disable_till == 0)) { - STACK_WIND (process_frame, - wb_ffr_bg_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->flush, - fd); - } else { - STACK_WIND (frame, - wb_ffr_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->flush, - fd); - STACK_DESTROY (process_frame->root); - } + GF_ASSERT (list_empty (&wb_inode->todo)); + GF_ASSERT (list_empty (&wb_inode->liability)); + GF_ASSERT (list_empty (&wb_inode->temptation)); - fd_unref (fd); + GF_FREE (wb_inode); return 0; } -static int32_t -wb_fsync_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret, - int32_t op_errno) +int +wb_release (xlator_t *this, fd_t *fd) { - wb_local_t *local = NULL; - wb_file_t *file = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; + uint64_t tmp = 0; - local = frame->local; - file = local->file; - request = local->request; + fd_ctx_del (fd, this, &tmp); - LOCK (&file->lock); - { - if (file->op_ret == -1) { - op_ret = file->op_ret; - op_errno = file->op_errno; + return 0; +} - file->op_ret = 0; - } - } - UNLOCK (&file->lock); - - if (request) { - wb_request_unref (request); - ret = wb_process_queue (frame, file, 0); - if ((ret == -1) && (errno == ENOMEM)) { - op_ret = -1; - op_errno = ENOMEM; - } - } - STACK_UNWIND (frame, op_ret, op_errno); - - return 0; +int +wb_priv_dump (xlator_t *this) +{ + wb_conf_t *conf = NULL; + char key_prefix[GF_DUMP_MAX_BUF_LEN] = {0, }; + int ret = -1; + + GF_VALIDATE_OR_GOTO ("write-behind", this, out); + + conf = this->private; + GF_VALIDATE_OR_GOTO (this->name, conf, out); + + gf_proc_dump_build_key (key_prefix, "xlator.performance.write-behind", + "priv"); + + gf_proc_dump_add_section (key_prefix); + + gf_proc_dump_write ("aggregate_size", "%d", conf->aggregate_size); + gf_proc_dump_write ("window_size", "%d", conf->window_size); + gf_proc_dump_write ("flush_behind", "%d", conf->flush_behind); + gf_proc_dump_write ("trickling_writes", "%d", conf->trickling_writes); + + ret = 0; +out: + return ret; } -static int32_t -wb_fsync_helper (call_frame_t *frame, xlator_t *this, fd_t *fd, - int32_t datasync) +void +__wb_dump_requests (struct list_head *head, char *prefix) { - STACK_WIND (frame, - wb_fsync_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->fsync, - fd, datasync); - return 0; + char key[GF_DUMP_MAX_BUF_LEN] = {0, }; + char key_prefix[GF_DUMP_MAX_BUF_LEN] = {0, }, flag = 0; + wb_request_t *req = NULL; + + list_for_each_entry (req, head, all) { + gf_proc_dump_build_key (key_prefix, key, + (char *)gf_fop_list[req->fop]); + + gf_proc_dump_add_section(key_prefix); + + gf_proc_dump_write ("request-ptr", "%p", req); + + gf_proc_dump_write ("refcount", "%d", req->refcount); + + if (list_empty (&req->todo)) + gf_proc_dump_write ("wound", "yes"); + else + gf_proc_dump_write ("wound", "no"); + + if (req->fop == GF_FOP_WRITE) { + gf_proc_dump_write ("size", "%"GF_PRI_SIZET, + req->write_size); + + gf_proc_dump_write ("offset", "%"PRId64, + req->stub->args.offset); + + flag = req->ordering.lied; + gf_proc_dump_write ("lied", "%d", flag); + + flag = req->ordering.append; + gf_proc_dump_write ("append", "%d", flag); + + flag = req->ordering.fulfilled; + gf_proc_dump_write ("fulfilled", "%d", flag); + + flag = req->ordering.go; + gf_proc_dump_write ("go", "%d", flag); + } + } } -int32_t -wb_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync) +int +wb_inode_dump (xlator_t *this, inode_t *inode) { - wb_file_t *file = NULL; - wb_local_t *local = NULL; - uint64_t tmp_file = 0; - call_stub_t *stub = NULL; - wb_request_t *request = NULL; - int32_t ret = -1; - - if ((!S_ISDIR (fd->inode->st_mode)) - && fd_ctx_get (fd, this, &tmp_file)) { - gf_log (this->name, GF_LOG_DEBUG, "write behind file pointer is" - " not stored in context of fd(%p), returning EBADFD", - fd); - - STACK_UNWIND (frame, -1, EBADFD); - return 0; + wb_inode_t *wb_inode = NULL; + int32_t ret = -1; + char *path = NULL; + char key_prefix[GF_DUMP_MAX_BUF_LEN] = {0, }; + char uuid_str[64] = {0,}; + + if ((inode == NULL) || (this == NULL)) { + ret = 0; + goto out; } - file = (wb_file_t *)(long)tmp_file; + wb_inode = wb_inode_ctx_get (this, inode); + if (wb_inode == NULL) { + ret = 0; + goto out; + } + + gf_proc_dump_build_key (key_prefix, "xlator.performance.write-behind", + "wb_inode"); + + gf_proc_dump_add_section (key_prefix); - local = CALLOC (1, sizeof (*local)); - if (local == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - return 0; + __inode_path (inode, NULL, &path); + if (path != NULL) { + gf_proc_dump_write ("path", "%s", path); + GF_FREE (path); } - local->file = file; + gf_proc_dump_write ("inode", "%p", inode); - frame->local = local; + gf_proc_dump_write ("window_conf", "%"GF_PRI_SIZET, + wb_inode->window_conf); + + gf_proc_dump_write ("window_current", "%"GF_PRI_SIZET, + wb_inode->window_current); - if (file) { - stub = fop_fsync_stub (frame, wb_fsync_helper, fd, datasync); - if (stub == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - return 0; - } - - request = wb_enqueue (file, stub); - if (request == NULL) { - STACK_UNWIND (frame, -1, ENOMEM); - call_stub_destroy (stub); - return 0; - } - ret = wb_process_queue (frame, file, 1); - if ((ret == -1) && (errno == ENOMEM)) { - STACK_UNWIND (frame, -1, ENOMEM); - call_stub_destroy (stub); - return 0; + ret = TRY_LOCK (&wb_inode->lock); + if (!ret) + { + if (!list_empty (&wb_inode->all)) { + __wb_dump_requests (&wb_inode->all, key_prefix); } - - } else { - STACK_WIND (frame, - wb_fsync_cbk, - FIRST_CHILD(this), - FIRST_CHILD(this)->fops->fsync, - fd, datasync); + UNLOCK (&wb_inode->lock); } - return 0; + if (ret && wb_inode) + gf_proc_dump_write ("Unable to dump the inode information", + "(Lock acquisition failed) %p (gfid: %s)", + wb_inode, + uuid_utoa_r (inode->gfid, uuid_str)); + ret = 0; +out: + return ret; } -int32_t -wb_release (xlator_t *this, fd_t *fd) +int +mem_acct_init (xlator_t *this) { - uint64_t file_ptr = 0; - wb_file_t *file = NULL; + int ret = -1; - fd_ctx_get (fd, this, &file_ptr); - file = (wb_file_t *) (long) file_ptr; + if (!this) { + goto out; + } - LOCK (&file->lock); - { - assert (list_empty (&file->request)); + ret = xlator_mem_acct_init (this, gf_wb_mt_end + 1); + + if (ret != 0) { + gf_log (this->name, GF_LOG_ERROR, "Memory accounting init" + "failed"); } - UNLOCK (&file->lock); - wb_file_destroy (file); +out: + return ret; +} - return 0; + +int +reconfigure (xlator_t *this, dict_t *options) +{ + wb_conf_t *conf = NULL; + int ret = -1; + + conf = this->private; + + GF_OPTION_RECONF ("cache-size", conf->window_size, options, size, out); + + GF_OPTION_RECONF ("flush-behind", conf->flush_behind, options, bool, + out); + + GF_OPTION_RECONF ("trickling-writes", conf->trickling_writes, options, + bool, out); + + GF_OPTION_RECONF ("strict-O_DIRECT", conf->strict_O_DIRECT, options, + bool, out); + + GF_OPTION_RECONF ("strict-write-ordering", conf->strict_write_ordering, + options, bool, out); + ret = 0; +out: + return ret; } -int32_t +int32_t init (xlator_t *this) { - dict_t *options = NULL; - wb_conf_t *conf = NULL; - char *str = NULL; - int32_t ret = -1; + wb_conf_t *conf = NULL; + int32_t ret = -1; if ((this->children == NULL) || this->children->next) { gf_log (this->name, GF_LOG_ERROR, "FATAL: write-behind (%s) not configured with exactly " - "one child", - this->name); - return -1; + "one child", this->name); + goto out; } if (this->parents == NULL) { gf_log (this->name, GF_LOG_WARNING, - "dangling volume. check volfile"); + "dangling volume. check volfilex"); } - - options = this->options; - conf = CALLOC (1, sizeof (*conf)); + conf = GF_CALLOC (1, sizeof (*conf), gf_wb_mt_wb_conf_t); if (conf == NULL) { - gf_log (this->name, GF_LOG_ERROR, - "FATAL: Out of memory"); - return -1; - } - - conf->enable_O_SYNC = _gf_false; - ret = dict_get_str (options, "enable-O_SYNC", - &str); - if (ret == 0) { - ret = gf_string2boolean (str, - &conf->enable_O_SYNC); - if (ret == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'enable-O_SYNC' takes only boolean arguments"); - return -1; - } + goto out; } /* configure 'options aggregate-size <size>' */ conf->aggregate_size = WB_AGGREGATE_SIZE; - conf->disable_till = 1; - ret = dict_get_str (options, "disable-for-first-nbytes", - &str); - if (ret == 0) { - ret = gf_string2bytesize (str, - &conf->disable_till); - if (ret != 0) { - gf_log (this->name, GF_LOG_ERROR, - "invalid number format \"%s\" of \"option " - "disable-for-first-nbytes\"", - str); - return -1; - } - } - gf_log (this->name, GF_LOG_DEBUG, - "disabling write-behind for first %"PRIu64" bytes", - conf->disable_till); - /* configure 'option window-size <size>' */ - conf->window_size = WB_WINDOW_SIZE; - ret = dict_get_str (options, "cache-size", - &str); - if (ret == 0) { - ret = gf_string2bytesize (str, - &conf->window_size); - if (ret != 0) { - gf_log (this->name, GF_LOG_ERROR, - "invalid number format \"%s\" of \"option " - "window-size\"", - str); - FREE (conf); - return -1; - } - } + GF_OPTION_INIT ("cache-size", conf->window_size, size, out); if (!conf->window_size && conf->aggregate_size) { gf_log (this->name, GF_LOG_WARNING, @@ -2373,64 +2018,54 @@ init (xlator_t *this) if (conf->window_size < conf->aggregate_size) { gf_log (this->name, GF_LOG_ERROR, "aggregate-size(%"PRIu64") cannot be more than " - "window-size" - "(%"PRIu64")", conf->window_size, conf->aggregate_size); - FREE (conf); - return -1; + "window-size(%"PRIu64")", conf->aggregate_size, + conf->window_size); + goto out; } /* configure 'option flush-behind <on/off>' */ - conf->flush_behind = 0; - ret = dict_get_str (options, "flush-behind", - &str); - if (ret == 0) { - ret = gf_string2boolean (str, - &conf->flush_behind); - if (ret == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'flush-behind' takes only boolean arguments"); - return -1; - } + GF_OPTION_INIT ("flush-behind", conf->flush_behind, bool, out); - if (conf->flush_behind) { - gf_log (this->name, GF_LOG_DEBUG, - "enabling flush-behind"); - } - } + GF_OPTION_INIT ("trickling-writes", conf->trickling_writes, bool, out); - conf->enable_trickling_writes = _gf_true; - ret = dict_get_str (options, "enable-trickling-writes", - &str); - if (ret == 0) { - ret = gf_string2boolean (str, - &conf->enable_trickling_writes); - if (ret == -1) { - gf_log (this->name, GF_LOG_ERROR, - "'enable-trickling_writes' takes only boolean" - " arguments"); - return -1; - } - } + GF_OPTION_INIT ("strict-O_DIRECT", conf->strict_O_DIRECT, bool, out); + + GF_OPTION_INIT ("strict-write-ordering", conf->strict_write_ordering, + bool, out); this->private = conf; - return 0; + ret = 0; + +out: + if (ret) { + GF_FREE (conf); + } + return ret; } void fini (xlator_t *this) { - wb_conf_t *conf = this->private; + wb_conf_t *conf = NULL; + + GF_VALIDATE_OR_GOTO ("write-behind", this, out); - FREE (conf); + conf = this->private; + if (!conf) { + goto out; + } + + this->private = NULL; + GF_FREE (conf); + +out: return; } struct xlator_fops fops = { .writev = wb_writev, - .open = wb_open, - .create = wb_create, .readv = wb_readv, .flush = wb_flush, .fsync = wb_fsync, @@ -2438,32 +2073,56 @@ struct xlator_fops fops = { .fstat = wb_fstat, .truncate = wb_truncate, .ftruncate = wb_ftruncate, - .utimens = wb_utimens, + .setattr = wb_setattr, + .fsetattr = wb_fsetattr, }; -struct xlator_mops mops = { -}; struct xlator_cbks cbks = { + .forget = wb_forget, .release = wb_release }; + +struct xlator_dumpops dumpops = { + .priv = wb_priv_dump, + .inodectx = wb_inode_dump, +}; + + struct volume_options options[] = { - { .key = {"flush-behind"}, - .type = GF_OPTION_TYPE_BOOL - }, - { .key = {"cache-size", "window-size"}, - .type = GF_OPTION_TYPE_SIZET, - .min = 512 * GF_UNIT_KB, - .max = 1 * GF_UNIT_GB + { .key = {"flush-behind"}, + .type = GF_OPTION_TYPE_BOOL, + .default_value = "on", + .description = "If this option is set ON, instructs write-behind " + "translator to perform flush in background, by " + "returning success (or any errors, if any of " + "previous writes were failed) to application even " + "before flush FOP is sent to backend filesystem. " }, - { .key = {"disable-for-first-nbytes"}, + { .key = {"cache-size", "window-size"}, .type = GF_OPTION_TYPE_SIZET, - .min = 1, - .max = 1 * GF_UNIT_MB, + .min = 512 * GF_UNIT_KB, + .max = 1 * GF_UNIT_GB, + .default_value = "1MB", + .description = "Size of the write-behind buffer for a single file " + "(inode)." }, - { .key = {"enable-O_SYNC"}, + { .key = {"trickling-writes"}, .type = GF_OPTION_TYPE_BOOL, - }, + .default_value = "on", + }, + { .key = {"strict-O_DIRECT"}, + .type = GF_OPTION_TYPE_BOOL, + .default_value = "off", + .description = "This option when set to off, ignores the " + "O_DIRECT flag." + }, + { .key = {"strict-write-ordering"}, + .type = GF_OPTION_TYPE_BOOL, + .default_value = "off", + .description = "Do not let later writes overtake earlier writes even " + "if they do not overlap", + }, { .key = {NULL} }, }; |
