Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * (C) 2001 Clemson University and The University of Chicago
0004  * (C) 2011 Omnibond Systems
0005  *
0006  * Changes by Acxiom Corporation to implement generic service_operation()
0007  * function, Copyright Acxiom Corporation, 2005.
0008  *
0009  * See COPYING in top-level directory.
0010  */
0011 
0012 /*
0013  *  In-kernel waitqueue operations.
0014  */
0015 
0016 #include "protocol.h"
0017 #include "orangefs-kernel.h"
0018 #include "orangefs-bufmap.h"
0019 
0020 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
0021         long timeout,
0022         int flags)
0023             __acquires(op->lock);
0024 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
0025     __releases(op->lock);
0026 
0027 /*
0028  * What we do in this function is to walk the list of operations that are
0029  * present in the request queue and mark them as purged.
0030  * NOTE: This is called from the device close after client-core has
0031  * guaranteed that no new operations could appear on the list since the
0032  * client-core is anyway going to exit.
0033  */
0034 void purge_waiting_ops(void)
0035 {
0036     struct orangefs_kernel_op_s *op, *tmp;
0037 
0038     spin_lock(&orangefs_request_list_lock);
0039     list_for_each_entry_safe(op, tmp, &orangefs_request_list, list) {
0040         gossip_debug(GOSSIP_WAIT_DEBUG,
0041                  "pvfs2-client-core: purging op tag %llu %s\n",
0042                  llu(op->tag),
0043                  get_opname_string(op));
0044         set_op_state_purged(op);
0045         gossip_debug(GOSSIP_DEV_DEBUG,
0046                  "%s: op:%s: op_state:%d: process:%s:\n",
0047                  __func__,
0048                  get_opname_string(op),
0049                  op->op_state,
0050                  current->comm);
0051     }
0052     spin_unlock(&orangefs_request_list_lock);
0053 }
0054 
0055 /*
0056  * submits a ORANGEFS operation and waits for it to complete
0057  *
0058  * Note op->downcall.status will contain the status of the operation (in
0059  * errno format), whether provided by pvfs2-client or a result of failure to
0060  * service the operation.  If the caller wishes to distinguish, then
0061  * op->state can be checked to see if it was serviced or not.
0062  *
0063  * Returns contents of op->downcall.status for convenience
0064  */
0065 int service_operation(struct orangefs_kernel_op_s *op,
0066               const char *op_name,
0067               int flags)
0068 {
0069     long timeout = MAX_SCHEDULE_TIMEOUT;
0070     int ret = 0;
0071 
0072     DEFINE_WAIT(wait_entry);
0073 
0074     op->upcall.tgid = current->tgid;
0075     op->upcall.pid = current->pid;
0076 
0077 retry_servicing:
0078     op->downcall.status = 0;
0079     gossip_debug(GOSSIP_WAIT_DEBUG,
0080              "%s: %s op:%p: process:%s: pid:%d:\n",
0081              __func__,
0082              op_name,
0083              op,
0084              current->comm,
0085              current->pid);
0086 
0087     /*
0088      * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
0089      * acquiring the request_mutex because we're servicing a
0090      * high priority remount operation and the request_mutex is
0091      * already taken.
0092      */
0093     if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
0094         if (flags & ORANGEFS_OP_INTERRUPTIBLE)
0095             ret = mutex_lock_interruptible(&orangefs_request_mutex);
0096         else
0097             ret = mutex_lock_killable(&orangefs_request_mutex);
0098         /*
0099          * check to see if we were interrupted while waiting for
0100          * mutex
0101          */
0102         if (ret < 0) {
0103             op->downcall.status = ret;
0104             gossip_debug(GOSSIP_WAIT_DEBUG,
0105                      "%s: service_operation interrupted.\n",
0106                      __func__);
0107             return ret;
0108         }
0109     }
0110 
0111     /* queue up the operation */
0112     spin_lock(&orangefs_request_list_lock);
0113     spin_lock(&op->lock);
0114     set_op_state_waiting(op);
0115     gossip_debug(GOSSIP_DEV_DEBUG,
0116              "%s: op:%s: op_state:%d: process:%s:\n",
0117              __func__,
0118              get_opname_string(op),
0119              op->op_state,
0120              current->comm);
0121     /* add high priority remount op to the front of the line. */
0122     if (flags & ORANGEFS_OP_PRIORITY)
0123         list_add(&op->list, &orangefs_request_list);
0124     else
0125         list_add_tail(&op->list, &orangefs_request_list);
0126     spin_unlock(&op->lock);
0127     wake_up_interruptible(&orangefs_request_list_waitq);
0128     if (!__is_daemon_in_service()) {
0129         gossip_debug(GOSSIP_WAIT_DEBUG,
0130                  "%s:client core is NOT in service.\n",
0131                  __func__);
0132         /*
0133          * Don't wait for the userspace component to return if
0134          * the filesystem is being umounted anyway.
0135          */
0136         if (op->upcall.type == ORANGEFS_VFS_OP_FS_UMOUNT)
0137             timeout = 0;
0138         else
0139             timeout = op_timeout_secs * HZ;
0140     }
0141     spin_unlock(&orangefs_request_list_lock);
0142 
0143     if (!(flags & ORANGEFS_OP_NO_MUTEX))
0144         mutex_unlock(&orangefs_request_mutex);
0145 
0146     ret = wait_for_matching_downcall(op, timeout, flags);
0147     gossip_debug(GOSSIP_WAIT_DEBUG,
0148              "%s: wait_for_matching_downcall returned %d for %p\n",
0149              __func__,
0150              ret,
0151              op);
0152 
0153     /* got matching downcall; make sure status is in errno format */
0154     if (!ret) {
0155         spin_unlock(&op->lock);
0156         op->downcall.status =
0157             orangefs_normalize_to_errno(op->downcall.status);
0158         ret = op->downcall.status;
0159         goto out;
0160     }
0161 
0162     /* failed to get matching downcall */
0163     if (ret == -ETIMEDOUT) {
0164         gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
0165                __func__,
0166                op_name);
0167     }
0168 
0169     /*
0170      * remove a waiting op from the request list or
0171      * remove an in-progress op from the in-progress list.
0172      */
0173     orangefs_clean_up_interrupted_operation(op);
0174 
0175     op->downcall.status = ret;
0176     /* retry if operation has not been serviced and if requested */
0177     if (ret == -EAGAIN) {
0178         op->attempts++;
0179         timeout = op_timeout_secs * HZ;
0180         gossip_debug(GOSSIP_WAIT_DEBUG,
0181                  "orangefs: tag %llu (%s)"
0182                  " -- operation to be retried (%d attempt)\n",
0183                  llu(op->tag),
0184                  op_name,
0185                  op->attempts);
0186 
0187         /*
0188          * io ops (ops that use the shared memory buffer) have
0189          * to be returned to their caller for a retry. Other ops
0190          * can just be recycled here.
0191          */
0192         if (!op->uses_shared_memory)
0193             goto retry_servicing;
0194     }
0195 
0196 out:
0197     gossip_debug(GOSSIP_WAIT_DEBUG,
0198              "%s: %s returning: %d for %p.\n",
0199              __func__,
0200              op_name,
0201              ret,
0202              op);
0203     return ret;
0204 }
0205 
0206 /* This can get called on an I/O op if it had a bad service_operation. */
0207 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
0208 {
0209     u64 tag = op->tag;
0210     if (!op_state_in_progress(op))
0211         return false;
0212 
0213     op->slot_to_free = op->upcall.req.io.buf_index;
0214     memset(&op->upcall, 0, sizeof(op->upcall));
0215     memset(&op->downcall, 0, sizeof(op->downcall));
0216     op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
0217     op->upcall.req.cancel.op_tag = tag;
0218     op->downcall.type = ORANGEFS_VFS_OP_INVALID;
0219     op->downcall.status = -1;
0220     orangefs_new_tag(op);
0221 
0222     spin_lock(&orangefs_request_list_lock);
0223     /* orangefs_request_list_lock is enough of a barrier here */
0224     if (!__is_daemon_in_service()) {
0225         spin_unlock(&orangefs_request_list_lock);
0226         return false;
0227     }
0228     spin_lock(&op->lock);
0229     set_op_state_waiting(op);
0230     gossip_debug(GOSSIP_DEV_DEBUG,
0231              "%s: op:%s: op_state:%d: process:%s:\n",
0232              __func__,
0233              get_opname_string(op),
0234              op->op_state,
0235              current->comm);
0236     list_add(&op->list, &orangefs_request_list);
0237     spin_unlock(&op->lock);
0238     spin_unlock(&orangefs_request_list_lock);
0239 
0240     gossip_debug(GOSSIP_WAIT_DEBUG,
0241              "Attempting ORANGEFS operation cancellation of tag %llu\n",
0242              llu(tag));
0243     return true;
0244 }
0245 
0246 /*
0247  * Change an op to the "given up" state and remove it from its list.
0248  */
0249 static void
0250     orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
0251         __releases(op->lock)
0252 {
0253     /*
0254      * handle interrupted cases depending on what state we were in when
0255      * the interruption is detected.
0256      *
0257      * Called with op->lock held.
0258      */
0259 
0260     /*
0261      * List manipulation code elsewhere will ignore ops that
0262      * have been given up upon.
0263      */
0264     op->op_state |= OP_VFS_STATE_GIVEN_UP;
0265 
0266     if (list_empty(&op->list)) {
0267         /* caught copying to/from daemon */
0268         BUG_ON(op_state_serviced(op));
0269         spin_unlock(&op->lock);
0270         wait_for_completion(&op->waitq);
0271     } else if (op_state_waiting(op)) {
0272         /*
0273          * upcall hasn't been read; remove op from upcall request
0274          * list.
0275          */
0276         spin_unlock(&op->lock);
0277         spin_lock(&orangefs_request_list_lock);
0278         list_del_init(&op->list);
0279         spin_unlock(&orangefs_request_list_lock);
0280         gossip_debug(GOSSIP_WAIT_DEBUG,
0281                  "Interrupted: Removed op %p from request_list\n",
0282                  op);
0283     } else if (op_state_in_progress(op)) {
0284         /* op must be removed from the in progress htable */
0285         spin_unlock(&op->lock);
0286         spin_lock(&orangefs_htable_ops_in_progress_lock);
0287         list_del_init(&op->list);
0288         spin_unlock(&orangefs_htable_ops_in_progress_lock);
0289         gossip_debug(GOSSIP_WAIT_DEBUG,
0290                  "Interrupted: Removed op %p"
0291                  " from htable_ops_in_progress\n",
0292                  op);
0293     } else {
0294         spin_unlock(&op->lock);
0295         gossip_err("interrupted operation is in a weird state 0x%x\n",
0296                op->op_state);
0297     }
0298     reinit_completion(&op->waitq);
0299 }
0300 
0301 /*
0302  * Sleeps on waitqueue waiting for matching downcall.
0303  * If client-core finishes servicing, then we are good to go.
0304  * else if client-core exits, we get woken up here, and retry with a timeout
0305  *
0306  * When this call returns to the caller, the specified op will no
0307  * longer be in either the in_progress hash table or on the request list.
0308  *
0309  * Returns 0 on success and -errno on failure
0310  * Errors are:
0311  * EAGAIN in case we want the caller to requeue and try again..
0312  * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
0313  * operation since client-core seems to be exiting too often
0314  * or if we were interrupted.
0315  *
0316  * Returns with op->lock taken.
0317  */
0318 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
0319         long timeout,
0320         int flags)
0321             __acquires(op->lock)
0322 {
0323     long n;
0324     int writeback = flags & ORANGEFS_OP_WRITEBACK,
0325         interruptible = flags & ORANGEFS_OP_INTERRUPTIBLE;
0326 
0327     /*
0328      * There's a "schedule_timeout" inside of these wait
0329      * primitives, during which the op is out of the hands of the
0330      * user process that needs something done and is being
0331      * manipulated by the client-core process.
0332      */
0333     if (writeback)
0334         n = wait_for_completion_io_timeout(&op->waitq, timeout);
0335     else if (!writeback && interruptible)
0336         n = wait_for_completion_interruptible_timeout(&op->waitq,
0337                                       timeout);
0338     else /* !writeback && !interruptible but compiler complains */
0339         n = wait_for_completion_killable_timeout(&op->waitq, timeout);
0340 
0341     spin_lock(&op->lock);
0342 
0343     if (op_state_serviced(op))
0344         return 0;
0345 
0346     if (unlikely(n < 0)) {
0347         gossip_debug(GOSSIP_WAIT_DEBUG,
0348                  "%s: operation interrupted, tag %llu, %p\n",
0349                  __func__,
0350                  llu(op->tag),
0351                  op);
0352         return -EINTR;
0353     }
0354     if (op_state_purged(op)) {
0355         gossip_debug(GOSSIP_WAIT_DEBUG,
0356                  "%s: operation purged, tag %llu, %p, %d\n",
0357                  __func__,
0358                  llu(op->tag),
0359                  op,
0360                  op->attempts);
0361         return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
0362              -EAGAIN :
0363              -EIO;
0364     }
0365     /* must have timed out, then... */
0366     gossip_debug(GOSSIP_WAIT_DEBUG,
0367              "%s: operation timed out, tag %llu, %p, %d)\n",
0368              __func__,
0369              llu(op->tag),
0370              op,
0371              op->attempts);
0372     return -ETIMEDOUT;
0373 }