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.
30 # include <sys/types.h>
32 # include <asm/semaphore.h>
33 # include <linux/rwsem.h>
34 # include <linux/sched.h>
35 # include <linux/signal.h>
36 # include <linux/types.h>
38 #include <linux/kp30.h>
39 #include <linux/lustre_idl.h>
40 #include <linux/lustre_cfg.h>
43 #if BITS_PER_LONG > 32
44 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
45 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
46 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
48 # define LI_POISON ((int)0x5a5a5a5a)
49 # define LL_POISON ((long)0x5a5a5a5a)
50 # define LP_POISON ((void *)(long)0x5a5a5a5a)
55 /* x86_64 has 64bit longs and defines u64 as long long */
56 #if BITS_PER_LONG > 32 && !defined(__x86_64__)
67 /* lustre_id output helper macros */
68 #define DLID4 "%lu/%lu/%lu/%lu"
71 (unsigned long)(id)->li_fid.lf_id, \
72 (unsigned long)(id)->li_fid.lf_group, \
73 (unsigned long)(id)->li_stc.u.e3s.l3s_ino, \
74 (unsigned long)(id)->li_stc.u.e3s.l3s_gen
77 struct ptlrpc_request;
81 #include <linux/lustre_ha.h>
82 #include <linux/lustre_net.h>
83 #include <linux/lustre_compat25.h>
84 #include <linux/lvfs.h>
86 int target_handle_connect(struct ptlrpc_request *req);
87 int target_handle_disconnect(struct ptlrpc_request *req);
88 void target_destroy_export(struct obd_export *exp);
89 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
90 struct obd_uuid *cluuid, int);
91 int target_handle_ping(struct ptlrpc_request *req);
92 void target_cancel_recovery_timer(struct obd_device *obd);
94 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 * HZ / 2) /* *waves hands* */
95 void target_start_recovery_timer(struct obd_device *obd);
96 int target_start_recovery_thread(struct obd_device *obd,
97 svc_handler_t handler);
98 void target_stop_recovery_thread(struct obd_device *obd);
99 void target_cleanup_recovery(struct obd_device *obd);
100 int target_queue_recovery_request(struct ptlrpc_request *req,
101 struct obd_device *obd);
102 int target_queue_final_reply(struct ptlrpc_request *req, int rc);
103 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
107 int client_sanobd_setup(struct obd_device *obddev, obd_count len, void *buf);
108 struct client_obd *client_conn2cli(struct lustre_handle *conn);
110 struct mdc_open_data;
111 struct obd_client_handle {
112 struct lustre_handle och_fh;
113 struct llog_cookie och_cookie;
114 struct mdc_open_data *och_mod;
117 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
120 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
121 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
126 struct task_struct *l_owner;
127 struct semaphore l_sem;
131 void l_lock_init(struct lustre_lock *);
132 void l_lock(struct lustre_lock *);
133 void l_unlock(struct lustre_lock *);
134 int l_has_lock(struct lustre_lock *);
140 #define OBD_IOCTL_VERSION 0x00010004
142 struct obd_ioctl_data {
144 uint32_t ioc_version;
150 struct obdo ioc_obdo1;
151 struct obdo ioc_obdo2;
156 uint32_t ioc_command;
162 /* buffers the kernel will treat as user pointers */
168 /* inline buffers for various arguments */
169 uint32_t ioc_inllen1;
171 uint32_t ioc_inllen2;
173 uint32_t ioc_inllen3;
175 uint32_t ioc_inllen4;
181 struct obd_ioctl_hdr {
183 uint32_t ioc_version;
186 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
188 int len = size_round(sizeof(struct obd_ioctl_data));
189 len += size_round(data->ioc_inllen1);
190 len += size_round(data->ioc_inllen2);
191 len += size_round(data->ioc_inllen3);
192 len += size_round(data->ioc_inllen4);
197 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
199 if (data->ioc_len > (1<<30)) {
200 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
203 if (data->ioc_inllen1 > (1<<30)) {
204 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
207 if (data->ioc_inllen2 > (1<<30)) {
208 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
211 if (data->ioc_inllen3 > (1<<30)) {
212 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
215 if (data->ioc_inllen4 > (1<<30)) {
216 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
219 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
220 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
223 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
224 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
227 if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
228 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
231 if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
232 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
235 if (data->ioc_pbuf1 && !data->ioc_plen1) {
236 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
239 if (data->ioc_pbuf2 && !data->ioc_plen2) {
240 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
243 if (data->ioc_plen1 && !data->ioc_pbuf1) {
244 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
247 if (data->ioc_plen2 && !data->ioc_pbuf2) {
248 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
251 if (obd_ioctl_packlen(data) != data->ioc_len) {
252 CERROR("OBD ioctl: packlen exceeds ioc_len (%d != %d)\n",
253 obd_ioctl_packlen(data), data->ioc_len);
260 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
264 struct obd_ioctl_data *overlay;
265 data->ioc_len = obd_ioctl_packlen(data);
266 data->ioc_version = OBD_IOCTL_VERSION;
268 if (*pbuf && data->ioc_len > max)
271 *pbuf = malloc(data->ioc_len);
275 overlay = (struct obd_ioctl_data *)*pbuf;
276 memcpy(*pbuf, data, sizeof(*data));
278 ptr = overlay->ioc_bulk;
279 if (data->ioc_inlbuf1)
280 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
281 if (data->ioc_inlbuf2)
282 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
283 if (data->ioc_inlbuf3)
284 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
285 if (data->ioc_inlbuf4)
286 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
287 if (obd_ioctl_is_invalid(overlay))
293 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
297 struct obd_ioctl_data *overlay;
301 overlay = (struct obd_ioctl_data *)pbuf;
303 /* Preserve the caller's buffer pointers */
304 overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
305 overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
306 overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
307 overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
309 memcpy(data, pbuf, sizeof(*data));
311 ptr = overlay->ioc_bulk;
312 if (data->ioc_inlbuf1)
313 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
314 if (data->ioc_inlbuf2)
315 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
316 if (data->ioc_inlbuf3)
317 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
318 if (data->ioc_inlbuf4)
319 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
325 #include <linux/obd_support.h>
327 /* buffer MUST be at least the size of obd_ioctl_hdr */
328 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
330 struct obd_ioctl_hdr hdr;
331 struct obd_ioctl_data *data;
336 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
342 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
343 CERROR("Version mismatch kernel vs application\n");
347 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
348 CERROR("User buffer len %d exceeds %d max buffer\n",
349 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
353 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
354 CERROR("OBD: user buffer too small for ioctl\n");
358 /* XXX allocate this more intelligently, using kmalloc when
360 OBD_VMALLOC(*buf, hdr.ioc_len);
362 CERROR("Cannot allocate control buffer of len %d\n",
367 data = (struct obd_ioctl_data *)*buf;
369 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
375 if (obd_ioctl_is_invalid(data)) {
376 CERROR("ioctl not correctly formatted\n");
380 if (data->ioc_inllen1) {
381 data->ioc_inlbuf1 = &data->ioc_bulk[0];
382 offset += size_round(data->ioc_inllen1);
385 if (data->ioc_inllen2) {
386 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
387 offset += size_round(data->ioc_inllen2);
390 if (data->ioc_inllen3) {
391 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
392 offset += size_round(data->ioc_inllen3);
395 if (data->ioc_inllen4) {
396 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
403 static inline void obd_ioctl_freedata(char *buf, int len)
412 #define OBD_IOC_CREATE _IOR ('f', 101, long)
413 #define OBD_IOC_DESTROY _IOW ('f', 104, long)
414 #define OBD_IOC_PREALLOCATE _IOWR('f', 105, long)
416 #define OBD_IOC_SETATTR _IOW ('f', 107, long)
417 #define OBD_IOC_GETATTR _IOR ('f', 108, long)
418 #define OBD_IOC_READ _IOWR('f', 109, long)
419 #define OBD_IOC_WRITE _IOWR('f', 110, long)
422 #define OBD_IOC_STATFS _IOWR('f', 113, long)
423 #define OBD_IOC_SYNC _IOR ('f', 114, long)
424 #define OBD_IOC_READ2 _IOWR('f', 115, long)
425 #define OBD_IOC_FORMAT _IOWR('f', 116, long)
426 #define OBD_IOC_PARTITION _IOWR('f', 117, long)
427 #define OBD_IOC_COPY _IOWR('f', 120, long)
428 #define OBD_IOC_MIGR _IOWR('f', 121, long)
429 #define OBD_IOC_PUNCH _IOWR('f', 122, long)
431 #define OBD_IOC_MODULE_DEBUG _IOWR('f', 124, long)
432 #define OBD_IOC_BRW_READ _IOWR('f', 125, long)
433 #define OBD_IOC_BRW_WRITE _IOWR('f', 126, long)
434 #define OBD_IOC_NAME2DEV _IOWR('f', 127, long)
435 #define OBD_IOC_UUID2DEV _IOWR('f', 130, long)
437 #define OBD_IOC_LOV_GET_CONFIG _IOWR('f', 132, long)
438 #define OBD_IOC_CLIENT_RECOVER _IOW ('f', 133, long)
440 #define OBD_IOC_PING _IOWR('f', 135, long)
442 #define OBD_IOC_DEC_FS_USE_COUNT _IO ('f', 139 )
443 #define OBD_IOC_NO_TRANSNO _IOW ('f', 140, long)
444 #define OBD_IOC_SET_READONLY _IOW ('f', 141, long)
445 #define OBD_IOC_ABORT_RECOVERY _IOR ('f', 142, long)
446 #define OBD_IOC_ROOT_SQUASH _IOWR('f', 143, long)
448 #define OBD_GET_VERSION _IOWR ('f', 144, long)
450 #define OBD_IOC_CLOSE_UUID _IOWR ('f', 147, long)
452 #define OBD_IOC_LOV_SETSTRIPE _IOW ('f', 154, long)
453 #define OBD_IOC_LOV_GETSTRIPE _IOW ('f', 155, long)
454 #define OBD_IOC_LOV_SETEA _IOW ('f', 156, long)
456 #define OBD_IOC_MOUNTOPT _IOWR('f', 170, long)
458 #define OBD_IOC_RECORD _IOWR('f', 180, long)
459 #define OBD_IOC_ENDRECORD _IOWR('f', 181, long)
460 #define OBD_IOC_PARSE _IOWR('f', 182, long)
461 #define OBD_IOC_DORECORD _IOWR('f', 183, long)
462 #define OBD_IOC_PROCESS_CFG _IOWR('f', 184, long)
463 #define OBD_IOC_DUMP_LOG _IOWR('f', 185, long)
464 #define OBD_IOC_CLEAR_LOG _IOWR('f', 186, long)
466 #define OBD_IOC_CATLOGLIST _IOWR('f', 190, long)
467 #define OBD_IOC_LLOG_INFO _IOWR('f', 191, long)
468 #define OBD_IOC_LLOG_PRINT _IOWR('f', 192, long)
469 #define OBD_IOC_LLOG_CANCEL _IOWR('f', 193, long)
470 #define OBD_IOC_LLOG_REMOVE _IOWR('f', 194, long)
471 #define OBD_IOC_LLOG_CHECK _IOWR('f', 195, long)
472 #define OBD_IOC_LLOG_CATINFO _IOWR('f', 196, long)
474 #define ECHO_IOC_GET_STRIPE _IOWR('f', 200, long)
475 #define ECHO_IOC_SET_STRIPE _IOWR('f', 201, long)
476 #define ECHO_IOC_ENQUEUE _IOWR('f', 202, long)
477 #define ECHO_IOC_CANCEL _IOWR('f', 203, long)
479 #define OBD_IOC_CMOBD_SYNC _IOWR('f', 210, long)
481 #define OBD_IOC_COBD_CON _IOWR('f', 220, long)
482 #define OBD_IOC_COBD_COFF _IOWR('f', 221, long)
483 #define OBD_IOC_COBD_CFLUSH _IOWR('f', 222, long)
485 #define OBD_IOC_SMFS_SNAP_ADD _IOWR('f', 230, long)
487 /* XXX _IOWR('f', 250, long) has been defined in
488 * portals/include/linux/kp30.h for debug, don't use it
491 /* Until such time as we get_info the per-stripe maximum from the OST,
492 * we define this to be 2T - 4k, which is the ext3 maxbytes. */
493 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
495 #define CHECKSUM_BULK 0
496 #define POISON_BULK 0
499 static inline void ost_checksum(obd_count *cksum, void *addr, int len)
501 unsigned char *ptr = (unsigned char *)addr;
504 /* very stupid, but means I don't have to think about byte order */
508 *cksum = (*cksum << 2) + sum;
512 static inline int ll_insecure_random_int(void)
516 return (int)(t.tv_usec);
520 * l_wait_event is a flexible sleeping function, permitting simple caller
521 * configuration of interrupt and timeout sensitivity along with actions to
522 * be performed in the event of either exception.
524 * Common usage looks like this:
526 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
527 * intr_handler, callback_data);
528 * rc = l_wait_event(waitq, condition, &lwi);
530 * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
531 * interrupt-only variants, respectively.)
533 * If a timeout is specified, the timeout_handler will be invoked in the event
534 * that the timeout expires before the process is awakened. (Note that any
535 * waking of the process will restart the timeout, even if the condition is
536 * not satisfied and the process immediately returns to sleep. This might be
537 * considered a bug.) If the timeout_handler returns non-zero, l_wait_event
538 * will return -ETIMEDOUT and the caller will continue. If the handler returns
539 * zero instead, the process will go back to sleep until it is awakened by the
540 * waitq or some similar mechanism, or an interrupt occurs (if the caller has
541 * asked for interrupts to be detected). The timeout will only fire once, so
542 * callers should take care that a timeout_handler which returns zero will take
543 * future steps to awaken the process. N.B. that these steps must include
544 * making the provided condition become true.
546 * If the interrupt flag (lwi_signals) is non-zero, then the process will be
547 * interruptible, and will be awakened by any "killable" signal (SIGTERM,
548 * SIGKILL or SIGINT). If a timeout is also specified, then the process will
549 * only become interruptible _after_ the timeout has expired, though it can be
550 * awakened by a signal that was delivered before the timeout and is still
551 * pending when the timeout expires. If a timeout is not specified, the process
552 * will be interruptible at all times during l_wait_event.
557 int (*lwi_on_timeout)(void *);
559 void (*lwi_on_signal)(void *);
563 #define LWI_TIMEOUT(time, cb, data) \
564 ((struct l_wait_info) { \
566 lwi_on_timeout: cb, \
570 #define LWI_INTR(cb, data) \
571 ((struct l_wait_info) { \
577 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
578 ((struct l_wait_info) { \
580 lwi_on_timeout: time_cb, \
582 lwi_on_signal: sig_cb, \
586 #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | \
587 sigmask(SIGTERM) | sigmask(SIGQUIT) | \
591 static inline sigset_t l_w_e_set_sigs(int sigs)
594 unsigned long irqflags;
596 SIGNAL_MASK_LOCK(current, irqflags);
597 old = current->blocked;
598 siginitsetinv(¤t->blocked, sigs);
600 SIGNAL_MASK_UNLOCK(current, irqflags);
605 #define __l_wait_event(wq, condition, info, ret, excl) \
607 wait_queue_t __wait; \
608 int __timed_out = 0; \
609 unsigned long irqflags; \
612 init_waitqueue_entry(&__wait, current); \
614 add_wait_queue_exclusive(&wq, &__wait); \
616 add_wait_queue(&wq, &__wait); \
618 /* Block all signals (just the non-fatal ones if no timeout). */ \
619 if (info->lwi_signals && !info->lwi_timeout) \
620 blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS); \
622 blocked = l_w_e_set_sigs(0); \
625 set_current_state(TASK_INTERRUPTIBLE); \
628 if (signal_pending(current)) { \
629 if (info->lwi_on_signal) \
630 info->lwi_on_signal(info->lwi_cb_data); \
634 if (info->lwi_timeout && !__timed_out) { \
635 if (schedule_timeout(info->lwi_timeout) == 0) { \
637 if (!info->lwi_on_timeout || \
638 info->lwi_on_timeout(info->lwi_cb_data)) { \
642 /* We'll take signals after a timeout. */ \
643 if (info->lwi_signals) \
644 (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS); \
651 SIGNAL_MASK_LOCK(current, irqflags); \
652 current->blocked = blocked; \
654 SIGNAL_MASK_UNLOCK(current, irqflags); \
656 current->state = TASK_RUNNING; \
657 remove_wait_queue(&wq, &__wait); \
660 #else /* !__KERNEL__ */
661 #define __l_wait_event(wq, condition, info, ret, excl) \
663 int timeout = info->lwi_timeout, elapse; \
664 int __timed_out = 0; \
671 if (liblustre_wait_event(timeout)) { \
674 elapse = (int) (time(NULL) - last); \
683 if (info->lwi_timeout && !__timed_out) { \
685 if (info->lwi_on_timeout == NULL || \
686 info->lwi_on_timeout(info->lwi_cb_data)) { \
694 #endif /* __KERNEL__ */
696 #define l_wait_event(wq, condition, info) \
699 struct l_wait_info *__info = (info); \
701 __l_wait_event(wq, condition, __info, __ret, 0); \
705 #define l_wait_event_exclusive(wq, condition, info) \
708 struct l_wait_info *__info = (info); \
710 __l_wait_event(wq, condition, __info, __ret, 1); \
714 #endif /* _LUSTRE_LIB_H */
716 #define LMD_MAGIC 0xbdacbdac
718 #define lmd_bad_magic(LMDP) \
720 struct lustre_mount_data *_lmd__ = (LMDP); \
723 CERROR("Missing mount data: " \
724 "check that /sbin/mount.lustre is installed.\n");\
726 } else if (_lmd__->lmd_magic != LMD_MAGIC) { \
727 CERROR("Invalid mount data (%#x != %#x): " \
728 "check that /sbin/mount.lustre is installed\n", \
729 _lmd__->lmd_magic, LMD_MAGIC); \