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;
57 #include <linux/lustre_ha.h>
59 int target_handle_connect(struct ptlrpc_request *req);
60 int target_handle_disconnect(struct ptlrpc_request *req);
61 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
62 struct obd_uuid *cluuid);
63 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
64 struct obd_uuid *cluuid, struct recovd_obd *recovd,
65 ptlrpc_recovery_cb_t recover);
66 int client_obd_disconnect(struct lustre_handle *conn);
67 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
68 int client_obd_cleanup(struct obd_device * obddev);
69 struct client_obd *client_conn2cli(struct lustre_handle *conn);
70 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid);
72 int target_revoke_connection(struct recovd_data *rd, int phase);
74 int obd_self_statfs(struct obd_device *dev, struct statfs *sfs);
79 struct task_struct *l_owner;
80 struct semaphore l_sem;
84 void l_lock_init(struct lustre_lock *);
85 void l_lock(struct lustre_lock *);
86 void l_unlock(struct lustre_lock *);
87 int l_has_lock(struct lustre_lock *);
89 #define CB_PHASE_START 12
90 #define CB_PHASE_FINISH 13
92 /* This list head doesn't need to be locked, because it's only manipulated by
93 * one thread at a time. */
95 struct list_head brw_desc_head; /* list of ptlrpc_bulk_desc */
96 wait_queue_head_t brw_waitq;
97 atomic_t brw_refcount;
100 int (*brw_callback)(struct obd_brw_set *, int phase);
106 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
107 struct obd_ucred *cred);
108 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
109 struct obd_ucred *cred);
110 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
111 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode);
112 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
113 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
114 int lustre_fsync(struct file *file);
116 static inline void l_dput(struct dentry *de)
118 if (!de || IS_ERR(de))
120 shrink_dcache_parent(de);
121 LASSERT(atomic_read(&de->d_count) > 0);
125 static inline void ll_sleep(int t)
127 set_current_state(TASK_INTERRUPTIBLE);
128 schedule_timeout(t * HZ);
129 set_current_state(TASK_RUNNING);
133 /* FIXME: This needs to validate pointers and cookies */
134 static inline void *lustre_handle2object(struct lustre_handle *handle)
137 return (void *)(unsigned long)(handle->addr);
141 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
143 handle->addr = (__u64)(unsigned long)object;
146 #include <linux/portals_lib.h>
151 #define OBD_IOCTL_VERSION 0x00010001
153 struct obd_ioctl_data {
155 uint32_t ioc_version;
162 struct obdo ioc_obdo1;
163 struct obdo ioc_obdo2;
168 uint32_t ____padding;
170 /* buffers the kernel will treat as user pointers */
176 /* two inline buffers */
177 uint32_t ioc_inllen1;
179 uint32_t ioc_inllen2;
181 uint32_t ioc_inllen3;
187 struct obd_ioctl_hdr {
189 uint32_t ioc_version;
192 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
194 int len = size_round(sizeof(struct obd_ioctl_data));
195 len += size_round(data->ioc_inllen1);
196 len += size_round(data->ioc_inllen2);
197 len += size_round(data->ioc_inllen3);
202 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
204 if (data->ioc_len > (1<<30)) {
205 printk("OBD ioctl: ioc_len larger than 1<<30\n");
208 if (data->ioc_inllen1 > (1<<30)) {
209 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
212 if (data->ioc_inllen2 > (1<<30)) {
213 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
217 if (data->ioc_inllen3 > (1<<30)) {
218 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
221 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
222 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
225 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
226 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
229 if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
230 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
233 if (data->ioc_pbuf1 && !data->ioc_plen1) {
234 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
237 if (data->ioc_pbuf2 && !data->ioc_plen2) {
238 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
242 if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
243 printk("OBD ioctl: inllen1 set but NULL pointer\n");
246 if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
247 printk("OBD ioctl: inllen2 set but NULL pointer\n");
250 if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
251 printk("OBD ioctl: inllen3 set but NULL pointer\n");
255 if (data->ioc_plen1 && !data->ioc_pbuf1) {
256 printk("OBD ioctl: plen1 set but NULL pointer\n");
259 if (data->ioc_plen2 && !data->ioc_pbuf2) {
260 printk("OBD ioctl: plen2 set but NULL pointer\n");
263 if (obd_ioctl_packlen(data) != data->ioc_len ) {
264 printk("OBD ioctl: packlen exceeds ioc_len\n");
268 if (data->ioc_inllen1 &&
269 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
270 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
273 if (data->ioc_inllen2 &&
274 data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
275 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
278 if (data->ioc_inllen3 &&
279 data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
280 + data->ioc_inllen3 - 1] != '\0') {
281 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
289 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
293 struct obd_ioctl_data *overlay;
294 data->ioc_len = obd_ioctl_packlen(data);
295 data->ioc_version = OBD_IOCTL_VERSION;
297 if (*pbuf && data->ioc_len > max)
300 *pbuf = malloc(data->ioc_len);
304 overlay = (struct obd_ioctl_data *)*pbuf;
305 memcpy(*pbuf, data, sizeof(*data));
307 ptr = overlay->ioc_bulk;
308 if (data->ioc_inlbuf1)
309 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
310 if (data->ioc_inlbuf2)
311 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
312 if (data->ioc_inlbuf3)
313 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
314 if (obd_ioctl_is_invalid(overlay))
320 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
324 struct obd_ioctl_data *overlay;
328 overlay = (struct obd_ioctl_data *)pbuf;
330 /* Preserve the caller's buffer pointers */
331 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
332 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
333 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
335 memcpy(data, pbuf, sizeof(*data));
337 ptr = overlay->ioc_bulk;
338 if (data->ioc_inlbuf1)
339 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
340 if (data->ioc_inlbuf2)
341 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
342 if (data->ioc_inlbuf3)
343 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
349 #include <linux/obd_support.h>
351 /* buffer MUST be at least the size of obd_ioctl_hdr */
352 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
354 struct obd_ioctl_hdr hdr;
355 struct obd_ioctl_data *data;
359 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
365 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
366 printk("OBD: version mismatch kernel vs application\n");
370 if (hdr.ioc_len > 8192) {
371 printk("OBD: user buffer exceeds 8192 max buffer\n");
375 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
376 printk("OBD: user buffer too small for ioctl\n");
380 OBD_ALLOC(*buf, hdr.ioc_len);
382 CERROR("Cannot allocate control buffer of len %d\n",
387 data = (struct obd_ioctl_data *)*buf;
389 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
395 if (obd_ioctl_is_invalid(data)) {
396 printk("OBD: ioctl not correctly formatted\n");
400 if (data->ioc_inllen1) {
401 data->ioc_inlbuf1 = &data->ioc_bulk[0];
404 if (data->ioc_inllen2) {
405 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
406 size_round(data->ioc_inllen1);
409 if (data->ioc_inllen3) {
410 data->ioc_inlbuf3 = &data->ioc_bulk[0] +
411 size_round(data->ioc_inllen1) +
412 size_round(data->ioc_inllen2);
420 #define OBD_IOC_CREATE _IOR ('f', 101, long)
421 #define OBD_IOC_SETUP _IOW ('f', 102, long)
422 #define OBD_IOC_CLEANUP _IO ('f', 103 )
423 #define OBD_IOC_DESTROY _IOW ('f', 104, long)
424 #define OBD_IOC_PREALLOCATE _IOWR('f', 105, long)
426 #define OBD_IOC_SETATTR _IOW ('f', 107, long)
427 #define OBD_IOC_GETATTR _IOR ('f', 108, long)
428 #define OBD_IOC_READ _IOWR('f', 109, long)
429 #define OBD_IOC_WRITE _IOWR('f', 110, long)
430 #define OBD_IOC_CONNECT _IOR ('f', 111, long)
431 #define OBD_IOC_DISCONNECT _IOW ('f', 112, long)
432 #define OBD_IOC_STATFS _IOWR('f', 113, long)
433 #define OBD_IOC_SYNC _IOR ('f', 114, long)
434 #define OBD_IOC_READ2 _IOWR('f', 115, long)
435 #define OBD_IOC_FORMAT _IOWR('f', 116, long)
436 #define OBD_IOC_PARTITION _IOWR('f', 117, long)
437 #define OBD_IOC_ATTACH _IOWR('f', 118, long)
438 #define OBD_IOC_DETACH _IOWR('f', 119, long)
439 #define OBD_IOC_COPY _IOWR('f', 120, long)
440 #define OBD_IOC_MIGR _IOWR('f', 121, long)
441 #define OBD_IOC_PUNCH _IOWR('f', 122, long)
442 #define OBD_IOC_DEVICE _IOWR('f', 123, long)
443 #define OBD_IOC_MODULE_DEBUG _IOWR('f', 124, long)
444 #define OBD_IOC_BRW_READ _IOWR('f', 125, long)
445 #define OBD_IOC_BRW_WRITE _IOWR('f', 126, long)
446 #define OBD_IOC_NAME2DEV _IOWR('f', 127, long)
447 #define OBD_IOC_NEWDEV _IOWR('f', 128, long)
448 #define OBD_IOC_LIST _IOWR('f', 129, long)
449 #define OBD_IOC_UUID2DEV _IOWR('f', 130, long)
451 #define OBD_IOC_RECOVD_NEWCONN _IOWR('f', 131, long)
452 #define OBD_IOC_LOV_SET_CONFIG _IOWR('f', 132, long)
453 #define OBD_IOC_LOV_GET_CONFIG _IOWR('f', 133, long)
454 #define OBD_IOC_LOV_CONFIG OBD_IOC_LOV_SET_CONFIG
456 #define OBD_IOC_OPEN _IOWR('f', 134, long)
457 #define OBD_IOC_CLOSE _IOWR('f', 135, long)
459 #define OBD_IOC_RECOVD_FAILCONN _IOWR('f', 136, long)
461 #define OBD_IOC_DEC_FS_USE_COUNT _IO ('f', 139 )
462 #define OBD_IOC_NO_TRANSNO _IOW ('f', 140, long)
463 #define OBD_IOC_SET_READONLY _IOW ('f', 141, long)
465 #define OBD_GET_VERSION _IOWR ('f', 144, long)
467 #define ECHO_IOC_GET_STRIPE _IOWR('f', 200, long)
468 #define ECHO_IOC_SET_STRIPE _IOWR('f', 201, long)
469 #define ECHO_IOC_ENQUEUE _IOWR('f', 202, long)
470 #define ECHO_IOC_CANCEL _IOWR('f', 203, long)
474 * l_wait_event is a flexible sleeping function, permitting simple caller
475 * configuration of interrupt and timeout sensitivity along with actions to
476 * be performed in the event of either exception.
478 * Common usage looks like this:
480 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
481 * intr_handler, callback_data);
482 * rc = l_wait_event(waitq, condition, &lwi);
484 * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
485 * interrupt-only variants, respectively.)
487 * If a timeout is specified, the timeout_handler will be invoked in the event
488 * that the timeout expires before the process is awakened. (Note that any
489 * waking of the process will restart the timeout, even if the condition is
490 * not satisfied and the process immediately returns to sleep. This might be
491 * considered a bug.) If the timeout_handler returns non-zero, l_wait_event
492 * will return -ETIMEDOUT and the caller will continue. If the handler returns
493 * zero instead, the process will go back to sleep until it is awakened by the
494 * waitq or some similar mechanism, or an interrupt occurs (if the caller has
495 * asked for interrupts to be detected). The timeout will only fire once, so
496 * callers should take care that a timeout_handler which returns zero will take
497 * future steps to awaken the process. N.B. that these steps must include
498 * making the provided condition become true.
500 * If the interrupt flag (lwi_signals) is non-zero, then the process will be
501 * interruptible, and will be awakened by any "killable" signal (SIGTERM,
502 * SIGKILL or SIGINT). If a timeout is also specified, then the process will
503 * only become interruptible _after_ the timeout has expired, though it can be
504 * awakened by a signal that was delivered before the timeout and is still
505 * pending when the timeout expires. If a timeout is not specified, the process
506 * will be interruptible at all times during l_wait_event.
511 int (*lwi_on_timeout)(void *);
513 int (*lwi_on_signal)(void *); /* XXX return is ignored for now */
517 #define LWI_TIMEOUT(time, cb, data) \
518 ((struct l_wait_info) { \
520 lwi_on_timeout: cb, \
524 #define LWI_INTR(cb, data) \
525 ((struct l_wait_info) { \
531 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
532 ((struct l_wait_info) { \
534 lwi_on_timeout: time_cb, \
536 lwi_on_signal: sig_cb, \
540 /* XXX this should be one mask-check */
541 #define l_killable_pending(task) \
542 (sigismember(&(task->pending.signal), SIGKILL) || \
543 sigismember(&(task->pending.signal), SIGINT) || \
544 sigismember(&(task->pending.signal), SIGTERM))
546 #define __l_wait_event(wq, condition, info, ret) \
548 wait_queue_t __wait; \
550 int __timed_out = 0; \
551 init_waitqueue_entry(&__wait, current); \
553 add_wait_queue(&wq, &__wait); \
554 if (info->lwi_signals && !info->lwi_timeout) \
555 __state = TASK_INTERRUPTIBLE; \
557 __state = TASK_UNINTERRUPTIBLE; \
559 set_current_state(__state); \
562 if (__state == TASK_INTERRUPTIBLE && l_killable_pending(current)) {\
563 if (info->lwi_on_signal) \
564 info->lwi_on_signal(info->lwi_cb_data); \
568 if (info->lwi_timeout && !__timed_out) { \
569 if (schedule_timeout(info->lwi_timeout) == 0) { \
571 if (!info->lwi_on_timeout || \
572 info->lwi_on_timeout(info->lwi_cb_data)) { \
576 /* We'll take signals after a timeout. */ \
577 if (info->lwi_signals) { \
578 __state = TASK_INTERRUPTIBLE; \
579 /* Check for a pending interrupt. */ \
580 if (info->lwi_signals && l_killable_pending(current)) {\
581 if (info->lwi_on_signal) \
582 info->lwi_on_signal(info->lwi_cb_data); \
592 current->state = TASK_RUNNING; \
593 remove_wait_queue(&wq, &__wait); \
596 #define l_wait_event(wq, condition, info) \
599 struct l_wait_info *__info = (info); \
601 __l_wait_event(wq, condition, __info, __ret); \
605 #endif /* _LUSTRE_LIB_H */