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>
32 #include <linux/kp30.h> /* XXX just for LASSERT! */
34 #include <linux/portals_lib.h>
35 #include <linux/lustre_idl.h>
39 struct ptlrpc_request;
43 int target_handle_connect(struct ptlrpc_request *req);
44 int target_handle_disconnect(struct ptlrpc_request *req);
45 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
47 int client_obd_disconnect(struct lustre_handle *conn);
48 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
49 int client_obd_cleanup(struct obd_device * obddev);
50 struct client_obd *client_conn2cli(struct lustre_handle *conn);
52 int target_revoke_connection(struct recovd_data *rd, int phase);
57 struct task_struct *l_owner;
58 struct semaphore l_sem;
62 void l_lock_init(struct lustre_lock *);
63 void l_lock(struct lustre_lock *);
64 void l_unlock(struct lustre_lock *);
68 #define CB_PHASE_START 12
69 #define CB_PHASE_FINISH 13
72 * io_cb_data: io callback data merged into one struct to simplify
73 * memory managment. This may be turn out to be too simple.
76 typedef int (*brw_callback_t)(struct io_cb_data *, int err, int phase);
79 wait_queue_head_t waitq;
83 struct ptlrpc_bulk_desc *desc;
87 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase);
88 struct io_cb_data *ll_init_cb(void);
89 inline void lustre_put_page(struct page *page);
90 struct page *lustre_get_page_read(struct inode *dir, unsigned long index);
91 struct page *lustre_get_page_write(struct inode *dir, unsigned long index);
92 int lustre_commit_write(struct page *page, unsigned from, unsigned to);
93 void set_page_clean(struct page *page);
94 void set_page_dirty(struct page *page);
98 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new);
99 void pop_ctxt(struct obd_run_ctxt *saved);
100 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
101 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
102 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
103 int lustre_fsync(struct file *file);
105 static inline void l_dput(struct dentry *de)
107 if (!de || IS_ERR(de))
109 shrink_dcache_parent(de);
110 LASSERT(atomic_read(&de->d_count) > 0);
114 static inline void ll_sleep(int t)
116 set_current_state(TASK_INTERRUPTIBLE);
117 schedule_timeout(t * HZ);
118 set_current_state(TASK_RUNNING);
122 /* FIXME: This needs to validate pointers and cookies */
123 static inline void *lustre_handle2object(struct lustre_handle *handle)
126 return (void *)(unsigned long)(handle->addr);
130 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
132 handle->addr = (__u64)(unsigned long)object;
137 void obd_statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
138 void obd_statfs_unpack(struct obd_statfs *osfs, struct statfs *sfs);
140 #include <linux/portals_lib.h>
145 #define OBD_IOCTL_VERSION 0x00010001
147 struct obd_ioctl_data {
149 uint32_t ioc_version;
156 struct obdo ioc_obdo1;
157 struct obdo ioc_obdo2;
162 uint32_t ____padding;
164 /* buffers the kernel will treat as user pointers */
170 /* two inline buffers */
171 uint32_t ioc_inllen1;
173 uint32_t ioc_inllen2;
175 uint32_t ioc_inllen3;
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);
196 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
198 if (data->ioc_len > (1<<30)) {
199 printk("OBD ioctl: ioc_len larger than 1<<30\n");
202 if (data->ioc_inllen1 > (1<<30)) {
203 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
206 if (data->ioc_inllen2 > (1<<30)) {
207 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
211 if (data->ioc_inllen3 > (1<<30)) {
212 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
215 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
216 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
219 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
220 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
223 if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
224 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
227 if (data->ioc_pbuf1 && !data->ioc_plen1) {
228 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
231 if (data->ioc_pbuf2 && !data->ioc_plen2) {
232 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
236 if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
237 printk("OBD ioctl: inllen1 set but NULL pointer\n");
240 if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
241 printk("OBD ioctl: inllen2 set but NULL pointer\n");
244 if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
245 printk("OBD ioctl: inllen3 set but NULL pointer\n");
249 if (data->ioc_plen1 && !data->ioc_pbuf1) {
250 printk("OBD ioctl: plen1 set but NULL pointer\n");
253 if (data->ioc_plen2 && !data->ioc_pbuf2) {
254 printk("OBD ioctl: plen2 set but NULL pointer\n");
257 if (obd_ioctl_packlen(data) != data->ioc_len ) {
258 printk("OBD ioctl: packlen exceeds ioc_len\n");
262 if (data->ioc_inllen1 &&
263 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
264 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
267 if (data->ioc_inllen2 &&
268 data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
269 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
272 if (data->ioc_inllen3 &&
273 data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
274 + data->ioc_inllen3 - 1] != '\0') {
275 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
283 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
287 struct obd_ioctl_data *overlay;
288 data->ioc_len = obd_ioctl_packlen(data);
289 data->ioc_version = OBD_IOCTL_VERSION;
291 if (*pbuf && obd_ioctl_packlen(data) > max)
294 *pbuf = malloc(data->ioc_len);
298 overlay = (struct obd_ioctl_data *)*pbuf;
299 memcpy(*pbuf, data, sizeof(*data));
301 ptr = overlay->ioc_bulk;
302 if (data->ioc_inlbuf1)
303 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
304 if (data->ioc_inlbuf2)
305 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
306 if (data->ioc_inlbuf3)
307 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
308 if (obd_ioctl_is_invalid(overlay))
316 #include <linux/obd_support.h>
318 /* buffer MUST be at least the size of obd_ioctl_hdr */
319 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
321 struct obd_ioctl_hdr hdr;
322 struct obd_ioctl_data *data;
327 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
333 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
334 printk("OBD: version mismatch kernel vs application\n");
338 if (hdr.ioc_len > 8192) {
339 printk("OBD: user buffer exceeds 8192 max buffer\n");
343 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
344 printk("OBD: user buffer too small for ioctl\n");
348 OBD_ALLOC(*buf, hdr.ioc_len);
350 CERROR("Cannot allocate control buffer of len %d\n",
355 data = (struct obd_ioctl_data *)*buf;
357 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
363 if (obd_ioctl_is_invalid(data)) {
364 printk("OBD: ioctl not correctly formatted\n");
368 if (data->ioc_inllen1) {
369 data->ioc_inlbuf1 = &data->ioc_bulk[0];
372 if (data->ioc_inllen2) {
373 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
376 if (data->ioc_inllen3) {
377 data->ioc_inlbuf3 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1) +
378 size_round(data->ioc_inllen2);
386 #define OBD_IOC_CREATE _IOR ('f', 101, long)
387 #define OBD_IOC_SETUP _IOW ('f', 102, long)
388 #define OBD_IOC_CLEANUP _IO ('f', 103 )
389 #define OBD_IOC_DESTROY _IOW ('f', 104, long)
390 #define OBD_IOC_PREALLOCATE _IOWR('f', 105, long)
391 #define OBD_IOC_DEC_USE_COUNT _IO ('f', 106 )
392 #define OBD_IOC_SETATTR _IOW ('f', 107, long)
393 #define OBD_IOC_GETATTR _IOR ('f', 108, long)
394 #define OBD_IOC_READ _IOWR('f', 109, long)
395 #define OBD_IOC_WRITE _IOWR('f', 110, long)
396 #define OBD_IOC_CONNECT _IOR ('f', 111, long)
397 #define OBD_IOC_DISCONNECT _IOW ('f', 112, long)
398 #define OBD_IOC_STATFS _IOWR('f', 113, long)
399 #define OBD_IOC_SYNC _IOR ('f', 114, long)
400 #define OBD_IOC_READ2 _IOWR('f', 115, long)
401 #define OBD_IOC_FORMAT _IOWR('f', 116, long)
402 #define OBD_IOC_PARTITION _IOWR('f', 117, long)
403 #define OBD_IOC_ATTACH _IOWR('f', 118, long)
404 #define OBD_IOC_DETACH _IOWR('f', 119, long)
405 #define OBD_IOC_COPY _IOWR('f', 120, long)
406 #define OBD_IOC_MIGR _IOWR('f', 121, long)
407 #define OBD_IOC_PUNCH _IOWR('f', 122, long)
408 #define OBD_IOC_DEVICE _IOWR('f', 123, long)
409 #define OBD_IOC_MODULE_DEBUG _IOWR('f', 124, long)
410 #define OBD_IOC_BRW_READ _IOWR('f', 125, long)
411 #define OBD_IOC_BRW_WRITE _IOWR('f', 126, long)
412 #define OBD_IOC_NAME2DEV _IOWR('f', 127, long)
413 #define OBD_IOC_NEWDEV _IOWR('f', 128, long)
414 #define OBD_IOC_LIST _IOWR('f', 129, long)
415 #define OBD_IOC_UUID2DEV _IOWR('f', 130, long)
417 #define OBD_IOC_RECOVD_NEWCONN _IOWR('f', 131, long)
418 #define OBD_IOC_LOV_CONFIG _IOWR('f', 132, long)
420 #define OBD_IOC_DEC_FS_USE_COUNT _IO ('f', 133 )
423 * l_wait_event is a flexible sleeping function, permitting simple caller
424 * configuration of interrupt and timeout sensitivity along with actions to
425 * be performed in the event of either exception.
427 * Common usage looks like this:
429 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
430 * intr_handler, callback_data);
431 * rc = l_wait_event(waitq, condition, &lwi);
433 * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
434 * interrupt-only variants, respectively.)
436 * If a timeout is specified, the timeout_handler will be invoked in the event
437 * that the timeout expires before the process is awakened. (Note that any
438 * waking of the process will restart the timeout, even if the condition is
439 * not satisfied and the process immediately returns to sleep. This might be
440 * considered a bug.) If the timeout_handler returns non-zero, l_wait_event
441 * will return -ETIMEDOUT and the caller will continue. If the handler returns
442 * zero instead, the process will go back to sleep until it is awakened by the
443 * waitq or some similar mechanism, or an interrupt occurs (if the caller has
444 * asked for interrupts to be detected). The timeout will only fire once, so
445 * callers should take care that a timeout_handler which returns zero will take
446 * future steps to awaken the process. N.B. that these steps must include making
447 * the provided condition become true.
449 * If the interrupt flag (lwi_signals) is non-zero, then the process will be
450 * interruptible, and will be awakened by any "killable" signal (SIGTERM,
451 * SIGKILL or SIGINT). If a timeout is also specified, then the process will
452 * only become interruptible _after_ the timeout has expired, though it can be
453 * awakened by a signal that was delivered before the timeout and is still
454 * pending when the timeout expires. If a timeout is not specified, the process
455 * will be interruptible at all times during l_wait_event.
460 int (*lwi_on_timeout)(void *);
462 int (*lwi_on_signal)(void *); /* XXX return is ignored for now */
466 #define LWI_TIMEOUT(time, cb, data) \
467 ((struct l_wait_info) { \
469 lwi_on_timeout: cb, \
473 #define LWI_INTR(cb, data) \
474 ((struct l_wait_info) { \
480 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
481 ((struct l_wait_info) { \
483 lwi_on_timeout: time_cb, \
485 lwi_on_signal: sig_cb, \
489 /* XXX this should be one mask-check */
490 #define l_killable_pending(task) \
491 (sigismember(&(task->pending.signal), SIGKILL) || \
492 sigismember(&(task->pending.signal), SIGINT) || \
493 sigismember(&(task->pending.signal), SIGTERM))
495 #define __l_wait_event(wq, condition, info, ret) \
497 wait_queue_t __wait; \
499 int __timed_out = 0; \
500 init_waitqueue_entry(&__wait, current); \
502 add_wait_queue(&wq, &__wait); \
503 if (info->lwi_signals && !info->lwi_timeout) \
504 __state = TASK_INTERRUPTIBLE; \
506 __state = TASK_UNINTERRUPTIBLE; \
508 set_current_state(__state); \
511 if (__state == TASK_INTERRUPTIBLE && l_killable_pending(current)) { \
512 CERROR("lwe: interrupt\n"); \
513 if (info->lwi_on_signal) \
514 info->lwi_on_signal(info->lwi_cb_data); \
518 if (info->lwi_timeout && !__timed_out) { \
519 if (schedule_timeout(info->lwi_timeout) == 0) { \
520 CERROR("lwe: timeout\n"); \
522 if (!info->lwi_on_timeout || \
523 info->lwi_on_timeout(info->lwi_cb_data)) { \
527 /* We'll take signals after a timeout. */ \
528 if (info->lwi_signals) { \
529 __state = TASK_INTERRUPTIBLE; \
530 /* Check for a pending interrupt. */ \
531 if (info->lwi_signals && l_killable_pending(current)) { \
532 CERROR("lwe: pending interrupt\n"); \
533 if (info->lwi_on_signal) \
534 info->lwi_on_signal(info->lwi_cb_data); \
544 current->state = TASK_RUNNING; \
545 remove_wait_queue(&wq, &__wait); \
548 #define l_wait_event(wq, condition, info) \
551 struct l_wait_info *__info = (info); \
553 __l_wait_event(wq, condition, __info, __ret); \
557 #endif /* _LUSTRE_LIB_H */