Whamcloud - gitweb
* Chain granted locks off the export.
[fs/lustre-release.git] / lustre / include / linux / lustre_lib.h
index 4d053cd..2b3ff7a 100644 (file)
@@ -60,6 +60,27 @@ void l_unlock(struct lustre_lock *);
 
 
 /* page.c */
+#define CB_PHASE_START   12
+#define CB_PHASE_FINISH  13
+
+/*
+ * io_cb_data: io callback data merged into one struct to simplify
+ *   memory managment. This may be turn out to be too simple.
+ */
+struct io_cb_data;
+typedef int (*brw_callback_t)(struct io_cb_data *, int err, int phase);
+
+struct io_cb_data {
+        wait_queue_head_t waitq;
+        atomic_t refcount;
+        int complete;
+        int err;
+        struct ptlrpc_bulk_desc *desc;
+        brw_callback_t    cb;
+};
+
+int ll_sync_io_cb(struct io_cb_data *data, int err, int phase);
+struct  io_cb_data *ll_init_cb(void);
 inline void lustre_put_page(struct page *page);
 struct page *lustre_get_page_read(struct inode *dir, unsigned long index);
 struct page *lustre_get_page_write(struct inode *dir, unsigned long index);
@@ -71,11 +92,6 @@ void set_page_dirty(struct page *page);
 struct obd_run_ctxt;
 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new);
 void pop_ctxt(struct obd_run_ctxt *saved);
-#ifdef OBD_CTXT_DEBUG
-#define OBD_SET_CTXT_MAGIC(ctxt) (ctxt)->magic = OBD_RUN_CTXT_MAGIC
-#else
-#define OBD_SET_CTXT_MAGIC(ctxt) do {} while(0)
-#endif
 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
@@ -397,77 +413,139 @@ static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
 
 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 133      )
 
+/*
+ * l_wait_event is a flexible sleeping function, permitting simple caller
+ * configuration of interrupt and timeout sensitivity along with actions to
+ * be performed in the event of either exception.
+ *
+ * Common usage looks like this:
+ * 
+ * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
+ *                                           intr_handler, callback_data);
+ * rc = l_wait_event(waitq, condition, &lwi);
+ *
+ * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
+ * interrupt-only variants, respectively.)
+ *
+ * If a timeout is specified, the timeout_handler will be invoked in the event
+ * that the timeout expires before the process is awakened.  (Note that any
+ * waking of the process will restart the timeout, even if the condition is
+ * not satisfied and the process immediately returns to sleep.  This might be
+ * considered a bug.)  If the timeout_handler returns non-zero, l_wait_event
+ * will return -ETIMEDOUT and the caller will continue.  If the handler returns
+ * zero instead, the process will go back to sleep until it is awakened by the
+ * waitq or some similar mechanism, or an interrupt occurs (if the caller has
+ * asked for interrupts to be detected).  The timeout will only fire once, so
+ * callers should take care that a timeout_handler which returns zero will take
+ * future steps to awaken the process.  N.B. that these steps must include making
+ * the provided condition become true.
+ *
+ * If the interrupt flag (lwi_signals) is non-zero, then the process will be
+ * interruptible, and will be awakened by any "killable" signal (SIGTERM,
+ * SIGKILL or SIGINT).  If a timeout is also specified, then the process will
+ * only become interruptible _after_ the timeout has expired, though it can be
+ * awakened by a signal that was delivered before the timeout and is still
+ * pending when the timeout expires.  If a timeout is not specified, the process
+ * will be interruptible at all times during l_wait_event.
+ */
 
+struct l_wait_info {
+        long   lwi_timeout;
+        int  (*lwi_on_timeout)(void *);
+        long   lwi_signals;
+        int  (*lwi_on_signal)(void *); /* XXX return is ignored for now */
+        void  *lwi_cb_data;
+};
 
+#define LWI_TIMEOUT(time, cb, data)                                             \
+((struct l_wait_info) {                                                         \
+        lwi_timeout:    time,                                                   \
+        lwi_on_timeout: cb,                                                     \
+        lwi_cb_data:    data                                                    \
+})
 
+#define LWI_INTR(cb, data)                                                      \
+((struct l_wait_info) {                                                         \
+        lwi_signals:   1,                                                       \
+        lwi_on_signal: cb,                                                      \
+        lwi_cb_data:   data                                                     \
+})
+
+#define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)                           \
+((struct l_wait_info) {                                                         \
+        lwi_timeout:    time,                                                   \
+        lwi_on_timeout: time_cb,                                                \
+        lwi_signals:    1,                                                      \
+        lwi_on_signal:  sig_cb,                                                 \
+        lwi_cb_data:    data                                                    \
+})
 
 /* XXX this should be one mask-check */
-#define l_killable_pending(task)                                               \
-(sigismember(&(task->pending.signal), SIGKILL) ||                              \
- sigismember(&(task->pending.signal), SIGINT) ||                               \
+#define l_killable_pending(task)                                                \
+(sigismember(&(task->pending.signal), SIGKILL) ||                               \
+ sigismember(&(task->pending.signal), SIGINT) ||                                \
  sigismember(&(task->pending.signal), SIGTERM))
 
-/*
- * Like wait_event_interruptible, but we're only interruptible by KILL, INT, or
- * TERM.
- *
- * XXXshaver These are going away soon, I hope.
- */
-#define __l_wait_event_killable(wq, condition, ret)                          \
-do {                                                                         \
-        wait_queue_t __wait;                                                 \
-        init_waitqueue_entry(&__wait, current);                              \
-                                                                             \
-        add_wait_queue(&wq, &__wait);                                        \
-        for (;;) {                                                           \
-                set_current_state(TASK_INTERRUPTIBLE);                       \
-                if (condition)                                               \
-                        break;                                               \
-                if (!signal_pending(current) ||                              \
-                    !l_killable_pending(current)) {                          \
-                        schedule();                                          \
-                        continue;                                            \
-                }                                                            \
-                ret = -ERESTARTSYS;                                          \
-                break;                                                       \
-        }                                                                    \
-        current->state = TASK_RUNNING;                                       \
-        remove_wait_queue(&wq, &__wait);                                     \
-} while(0)
-
-#define l_wait_event_killable(wq, condition)                            \
-({                                                                      \
-        int __ret = 0;                                                  \
-        if (!(condition))                                               \
-                __l_wait_event_killable(wq, condition, __ret);          \
-        __ret;                                                          \
-})
-
-#define __l_wait_event_timeout(wq, condition, timeout, ret)                   \
-do {                                                                          \
-        wait_queue_t __wait;                                                  \
-        init_waitqueue_entry(&__wait, current);                               \
-                                                                              \
-        add_wait_queue(&wq, &__wait);                                         \
-        for (;;) {                                                            \
-                set_current_state(TASK_INTERRUPTIBLE);                        \
-                if (condition)                                                \
-                        break;                                                \
-                if (timeout)                                                  \
-                        schedule_timeout(timeout);                            \
-                else                                                          \
-                        schedule();                                           \
-        }                                                                     \
-        current->state = TASK_RUNNING;                                        \
-        remove_wait_queue(&wq, &__wait);                                      \
+#define __l_wait_event(wq, condition, info, ret)                                \
+do {                                                                            \
+        wait_queue_t __wait;                                                    \
+        long __state;                                                           \
+        int __timed_out = 0;                                                    \
+        init_waitqueue_entry(&__wait, current);                                 \
+                                                                                \
+        add_wait_queue(&wq, &__wait);                                           \
+        if (info->lwi_signals && !info->lwi_timeout)                            \
+            __state = TASK_INTERRUPTIBLE;                                       \
+        else                                                                    \
+            __state = TASK_UNINTERRUPTIBLE;                                     \
+        for (;;) {                                                              \
+            set_current_state(__state);                                         \
+            if (condition)                                                      \
+                    break;                                                      \
+            if (__state == TASK_INTERRUPTIBLE && l_killable_pending(current)) { \
+                CERROR("lwe: interrupt\n");                                     \
+                if (info->lwi_on_signal)                                        \
+                        info->lwi_on_signal(info->lwi_cb_data);                 \
+                ret = -EINTR;                                                   \
+                break;                                                          \
+            }                                                                   \
+            if (info->lwi_timeout && !__timed_out) {                            \
+                if (schedule_timeout(info->lwi_timeout) == 0) {                 \
+                    CERROR("lwe: timeout\n");                                   \
+                    __timed_out = 1;                                            \
+                    if (!info->lwi_on_timeout ||                                \
+                        info->lwi_on_timeout(info->lwi_cb_data)) {              \
+                        ret = -ETIMEDOUT;                                       \
+                        break;                                                  \
+                    }                                                           \
+                    /* We'll take signals after a timeout. */                   \
+                    if (info->lwi_signals) {                                    \
+                        __state = TASK_INTERRUPTIBLE;                           \
+                        /* Check for a pending interrupt. */                    \
+                        if (info->lwi_signals && l_killable_pending(current)) { \
+                            CERROR("lwe: pending interrupt\n");                 \
+                            if (info->lwi_on_signal)                            \
+                                info->lwi_on_signal(info->lwi_cb_data);         \
+                            ret = -EINTR;                                       \
+                            break;                                              \
+                        }                                                       \
+                    }                                                           \
+                }                                                               \
+            } else {                                                            \
+                schedule();                                                     \
+            }                                                                   \
+        }                                                                       \
+        current->state = TASK_RUNNING;                                          \
+        remove_wait_queue(&wq, &__wait);                                        \
 } while(0)
 
-#define l_wait_event_timeout(wq, condition, timeout)                          \
-({                                                                            \
-        int __ret = 0;                                                        \
-        if (!(condition))                                                     \
-                __l_wait_event_timeout(wq, condition, timeout, __ret);        \
-        __ret;                                                                \
+#define l_wait_event(wq, condition, info)                                       \
+({                                                                              \
+        int __ret = 0;                                                          \
+        struct l_wait_info *__info = (info);                                    \
+        if (!(condition))                                                       \
+                __l_wait_event(wq, condition, __info, __ret);                   \
+        __ret;                                                                  \
 })
 
 #endif /* _LUSTRE_LIB_H */