1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Basic Lustre library routines.
31 # include <asm/semaphore.h>
33 #include <linux/types.h>
34 #include <linux/portals_lib.h>
35 #include <linux/kp30.h> /* XXX just for LASSERT! */
36 #include <linux/lustre_idl.h>
39 #if BITS_PER_LONG > 32
52 struct ptlrpc_request;
56 #include <linux/lustre_ha.h>
58 int target_handle_connect(struct ptlrpc_request *req);
59 int target_handle_disconnect(struct ptlrpc_request *req);
60 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
61 obd_uuid_t cluuid, struct recovd_obd *recovd,
62 ptlrpc_recovery_cb_t recover);
63 int client_obd_disconnect(struct lustre_handle *conn);
64 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
65 int client_obd_cleanup(struct obd_device * obddev);
66 struct client_obd *client_conn2cli(struct lustre_handle *conn);
68 int target_revoke_connection(struct recovd_data *rd, int phase);
73 struct task_struct *l_owner;
74 struct semaphore l_sem;
78 void l_lock_init(struct lustre_lock *);
79 void l_lock(struct lustre_lock *);
80 void l_unlock(struct lustre_lock *);
81 int l_has_lock(struct lustre_lock *);
83 #define CB_PHASE_START 12
84 #define CB_PHASE_FINISH 13
86 /* This list head doesn't need to be locked, because it's only manipulated by
87 * one thread at a time. */
89 struct list_head brw_desc_head; /* list of ptlrpc_bulk_desc */
90 wait_queue_head_t brw_waitq;
91 atomic_t brw_refcount;
94 int (*brw_callback)(struct obd_brw_set *, int phase);
100 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
101 struct obd_ucred *cred);
102 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
103 struct obd_ucred *cred);
104 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
105 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode);
106 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
107 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
108 int lustre_fsync(struct file *file);
110 static inline void l_dput(struct dentry *de)
112 if (!de || IS_ERR(de))
114 shrink_dcache_parent(de);
115 LASSERT(atomic_read(&de->d_count) > 0);
119 static inline void ll_sleep(int t)
121 set_current_state(TASK_INTERRUPTIBLE);
122 schedule_timeout(t * HZ);
123 set_current_state(TASK_RUNNING);
127 /* FIXME: This needs to validate pointers and cookies */
128 static inline void *lustre_handle2object(struct lustre_handle *handle)
131 return (void *)(unsigned long)(handle->addr);
135 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
137 handle->addr = (__u64)(unsigned long)object;
142 void statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
143 void statfs_unpack(struct statfs *sfs, struct obd_statfs *osfs);
144 void obd_statfs_pack(struct obd_statfs *tgt, struct obd_statfs *src);
146 obd_statfs_unpack(struct obd_statfs *tgt, struct obd_statfs *src)
148 obd_statfs_pack(tgt, src);
151 #include <linux/portals_lib.h>
156 #define OBD_IOCTL_VERSION 0x00010001
158 struct obd_ioctl_data {
160 uint32_t ioc_version;
167 struct obdo ioc_obdo1;
168 struct obdo ioc_obdo2;
173 uint32_t ____padding;
175 /* buffers the kernel will treat as user pointers */
181 /* two inline buffers */
182 uint32_t ioc_inllen1;
184 uint32_t ioc_inllen2;
186 uint32_t ioc_inllen3;
192 struct obd_ioctl_hdr {
194 uint32_t ioc_version;
197 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
199 int len = size_round(sizeof(struct obd_ioctl_data));
200 len += size_round(data->ioc_inllen1);
201 len += size_round(data->ioc_inllen2);
202 len += size_round(data->ioc_inllen3);
207 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
209 if (data->ioc_len > (1<<30)) {
210 printk("OBD ioctl: ioc_len larger than 1<<30\n");
213 if (data->ioc_inllen1 > (1<<30)) {
214 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
217 if (data->ioc_inllen2 > (1<<30)) {
218 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
222 if (data->ioc_inllen3 > (1<<30)) {
223 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
226 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
227 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
230 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
231 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
234 if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
235 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
238 if (data->ioc_pbuf1 && !data->ioc_plen1) {
239 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
242 if (data->ioc_pbuf2 && !data->ioc_plen2) {
243 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
247 if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
248 printk("OBD ioctl: inllen1 set but NULL pointer\n");
251 if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
252 printk("OBD ioctl: inllen2 set but NULL pointer\n");
255 if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
256 printk("OBD ioctl: inllen3 set but NULL pointer\n");
260 if (data->ioc_plen1 && !data->ioc_pbuf1) {
261 printk("OBD ioctl: plen1 set but NULL pointer\n");
264 if (data->ioc_plen2 && !data->ioc_pbuf2) {
265 printk("OBD ioctl: plen2 set but NULL pointer\n");
268 if (obd_ioctl_packlen(data) != data->ioc_len ) {
269 printk("OBD ioctl: packlen exceeds ioc_len\n");
273 if (data->ioc_inllen1 &&
274 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
275 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
278 if (data->ioc_inllen2 &&
279 data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
280 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
283 if (data->ioc_inllen3 &&
284 data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
285 + data->ioc_inllen3 - 1] != '\0') {
286 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
294 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
298 struct obd_ioctl_data *overlay;
299 data->ioc_len = obd_ioctl_packlen(data);
300 data->ioc_version = OBD_IOCTL_VERSION;
302 if (*pbuf && data->ioc_len > max)
305 *pbuf = malloc(data->ioc_len);
309 overlay = (struct obd_ioctl_data *)*pbuf;
310 memcpy(*pbuf, data, sizeof(*data));
312 ptr = overlay->ioc_bulk;
313 if (data->ioc_inlbuf1)
314 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
315 if (data->ioc_inlbuf2)
316 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
317 if (data->ioc_inlbuf3)
318 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
319 if (obd_ioctl_is_invalid(overlay))
325 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
329 struct obd_ioctl_data *overlay;
333 overlay = (struct obd_ioctl_data *)pbuf;
335 /* Preserve the caller's buffer pointers */
336 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
337 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
338 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
340 memcpy(data, pbuf, sizeof(*data));
342 ptr = overlay->ioc_bulk;
343 if (data->ioc_inlbuf1)
344 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
345 if (data->ioc_inlbuf2)
346 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
347 if (data->ioc_inlbuf3)
348 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
354 #include <linux/obd_support.h>
356 /* buffer MUST be at least the size of obd_ioctl_hdr */
357 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
359 struct obd_ioctl_hdr hdr;
360 struct obd_ioctl_data *data;
364 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
370 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
371 printk("OBD: version mismatch kernel vs application\n");
375 if (hdr.ioc_len > 8192) {
376 printk("OBD: user buffer exceeds 8192 max buffer\n");
380 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
381 printk("OBD: user buffer too small for ioctl\n");
385 OBD_ALLOC(*buf, hdr.ioc_len);
387 CERROR("Cannot allocate control buffer of len %d\n",
392 data = (struct obd_ioctl_data *)*buf;
394 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
400 if (obd_ioctl_is_invalid(data)) {
401 printk("OBD: ioctl not correctly formatted\n");
405 if (data->ioc_inllen1) {
406 data->ioc_inlbuf1 = &data->ioc_bulk[0];
409 if (data->ioc_inllen2) {
410 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
413 if (data->ioc_inllen3) {
414 data->ioc_inlbuf3 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1) +
415 size_round(data->ioc_inllen2);
423 #define OBD_IOC_CREATE _IOR ('f', 101, long)
424 #define OBD_IOC_SETUP _IOW ('f', 102, long)
425 #define OBD_IOC_CLEANUP _IO ('f', 103 )
426 #define OBD_IOC_DESTROY _IOW ('f', 104, long)
427 #define OBD_IOC_PREALLOCATE _IOWR('f', 105, long)
428 #define OBD_IOC_DEC_USE_COUNT _IO ('f', 106 )
429 #define OBD_IOC_SETATTR _IOW ('f', 107, long)
430 #define OBD_IOC_GETATTR _IOR ('f', 108, long)
431 #define OBD_IOC_READ _IOWR('f', 109, long)
432 #define OBD_IOC_WRITE _IOWR('f', 110, long)
433 #define OBD_IOC_CONNECT _IOR ('f', 111, long)
434 #define OBD_IOC_DISCONNECT _IOW ('f', 112, long)
435 #define OBD_IOC_STATFS _IOWR('f', 113, long)
436 #define OBD_IOC_SYNC _IOR ('f', 114, long)
437 #define OBD_IOC_READ2 _IOWR('f', 115, long)
438 #define OBD_IOC_FORMAT _IOWR('f', 116, long)
439 #define OBD_IOC_PARTITION _IOWR('f', 117, long)
440 #define OBD_IOC_ATTACH _IOWR('f', 118, long)
441 #define OBD_IOC_DETACH _IOWR('f', 119, long)
442 #define OBD_IOC_COPY _IOWR('f', 120, long)
443 #define OBD_IOC_MIGR _IOWR('f', 121, long)
444 #define OBD_IOC_PUNCH _IOWR('f', 122, long)
445 #define OBD_IOC_DEVICE _IOWR('f', 123, long)
446 #define OBD_IOC_MODULE_DEBUG _IOWR('f', 124, long)
447 #define OBD_IOC_BRW_READ _IOWR('f', 125, long)
448 #define OBD_IOC_BRW_WRITE _IOWR('f', 126, long)
449 #define OBD_IOC_NAME2DEV _IOWR('f', 127, long)
450 #define OBD_IOC_NEWDEV _IOWR('f', 128, long)
451 #define OBD_IOC_LIST _IOWR('f', 129, long)
452 #define OBD_IOC_UUID2DEV _IOWR('f', 130, long)
454 #define OBD_IOC_RECOVD_NEWCONN _IOWR('f', 131, long)
455 #define OBD_IOC_LOV_SET_CONFIG _IOWR('f', 132, long)
456 #define OBD_IOC_LOV_GET_CONFIG _IOWR('f', 133, long)
457 #define OBD_IOC_LOV_CONFIG OBD_IOC_LOV_SET_CONFIG
459 #define OBD_IOC_OPEN _IOWR('f', 134, long)
460 #define OBD_IOC_CLOSE _IOWR('f', 135, long)
462 #define OBD_IOC_RECOVD_FAILCONN _IOWR('f', 136, long)
464 #define OBD_IOC_DEC_FS_USE_COUNT _IO ('f', 139 )
466 #define OBD_GET_VERSION _IOWR ('f', 144, long)
469 * l_wait_event is a flexible sleeping function, permitting simple caller
470 * configuration of interrupt and timeout sensitivity along with actions to
471 * be performed in the event of either exception.
473 * Common usage looks like this:
475 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
476 * intr_handler, callback_data);
477 * rc = l_wait_event(waitq, condition, &lwi);
479 * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
480 * interrupt-only variants, respectively.)
482 * If a timeout is specified, the timeout_handler will be invoked in the event
483 * that the timeout expires before the process is awakened. (Note that any
484 * waking of the process will restart the timeout, even if the condition is
485 * not satisfied and the process immediately returns to sleep. This might be
486 * considered a bug.) If the timeout_handler returns non-zero, l_wait_event
487 * will return -ETIMEDOUT and the caller will continue. If the handler returns
488 * zero instead, the process will go back to sleep until it is awakened by the
489 * waitq or some similar mechanism, or an interrupt occurs (if the caller has
490 * asked for interrupts to be detected). The timeout will only fire once, so
491 * callers should take care that a timeout_handler which returns zero will take
492 * future steps to awaken the process. N.B. that these steps must include
493 * making the provided condition become true.
495 * If the interrupt flag (lwi_signals) is non-zero, then the process will be
496 * interruptible, and will be awakened by any "killable" signal (SIGTERM,
497 * SIGKILL or SIGINT). If a timeout is also specified, then the process will
498 * only become interruptible _after_ the timeout has expired, though it can be
499 * awakened by a signal that was delivered before the timeout and is still
500 * pending when the timeout expires. If a timeout is not specified, the process
501 * will be interruptible at all times during l_wait_event.
506 int (*lwi_on_timeout)(void *);
508 int (*lwi_on_signal)(void *); /* XXX return is ignored for now */
512 #define LWI_TIMEOUT(time, cb, data) \
513 ((struct l_wait_info) { \
515 lwi_on_timeout: cb, \
519 #define LWI_INTR(cb, data) \
520 ((struct l_wait_info) { \
526 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
527 ((struct l_wait_info) { \
529 lwi_on_timeout: time_cb, \
531 lwi_on_signal: sig_cb, \
535 /* XXX this should be one mask-check */
536 #define l_killable_pending(task) \
537 (sigismember(&(task->pending.signal), SIGKILL) || \
538 sigismember(&(task->pending.signal), SIGINT) || \
539 sigismember(&(task->pending.signal), SIGTERM))
541 #define __l_wait_event(wq, condition, info, ret) \
543 wait_queue_t __wait; \
545 int __timed_out = 0; \
546 init_waitqueue_entry(&__wait, current); \
548 add_wait_queue(&wq, &__wait); \
549 if (info->lwi_signals && !info->lwi_timeout) \
550 __state = TASK_INTERRUPTIBLE; \
552 __state = TASK_UNINTERRUPTIBLE; \
554 set_current_state(__state); \
557 if (__state == TASK_INTERRUPTIBLE && l_killable_pending(current)) {\
558 if (info->lwi_on_signal) \
559 info->lwi_on_signal(info->lwi_cb_data); \
563 if (info->lwi_timeout && !__timed_out) { \
564 if (schedule_timeout(info->lwi_timeout) == 0) { \
566 if (!info->lwi_on_timeout || \
567 info->lwi_on_timeout(info->lwi_cb_data)) { \
571 /* We'll take signals after a timeout. */ \
572 if (info->lwi_signals) { \
573 __state = TASK_INTERRUPTIBLE; \
574 /* Check for a pending interrupt. */ \
575 if (info->lwi_signals && l_killable_pending(current)) {\
576 if (info->lwi_on_signal) \
577 info->lwi_on_signal(info->lwi_cb_data); \
587 current->state = TASK_RUNNING; \
588 remove_wait_queue(&wq, &__wait); \
591 #define l_wait_event(wq, condition, info) \
594 struct l_wait_info *__info = (info); \
596 __l_wait_event(wq, condition, __info, __ret); \
600 #endif /* _LUSTRE_LIB_H */