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/portals_lib.h>
34 #include <linux/lustre_idl.h>
38 struct ptlrpc_request;
40 int target_handle_connect(struct ptlrpc_request *req);
41 int target_handle_disconnect(struct ptlrpc_request *req);
42 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
44 int client_obd_disconnect(struct lustre_handle *conn);
45 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
46 int client_obd_cleanup(struct obd_device * obddev);
47 struct client_obd *client_conn2cli(struct lustre_handle *conn);
52 struct task_struct *l_owner;
53 struct semaphore l_sem;
57 void l_lock_init(struct lustre_lock *);
58 void l_lock(struct lustre_lock *);
59 void l_unlock(struct lustre_lock *);
63 #define CB_PHASE_START 12
64 #define CB_PHASE_FINISH 13
66 wait_queue_head_t waitq;
70 struct ptlrpc_bulk_desc *desc;
72 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase);
73 struct io_cb_data *ll_init_cb(void);
74 inline void lustre_put_page(struct page *page);
75 struct page *lustre_get_page_read(struct inode *dir, unsigned long index);
76 struct page *lustre_get_page_write(struct inode *dir, unsigned long index);
77 int lustre_commit_write(struct page *page, unsigned from, unsigned to);
78 void set_page_clean(struct page *page);
79 void set_page_dirty(struct page *page);
83 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new);
84 void pop_ctxt(struct obd_run_ctxt *saved);
86 #define OBD_SET_CTXT_MAGIC(ctxt) (ctxt)->magic = OBD_RUN_CTXT_MAGIC
88 #define OBD_SET_CTXT_MAGIC(ctxt) do {} while(0)
90 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
91 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
92 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
93 int lustre_fsync(struct file *file);
95 static inline void l_dput(struct dentry *de)
97 if (!de || IS_ERR(de))
99 shrink_dcache_parent(de);
103 static inline void ll_sleep(int t)
105 set_current_state(TASK_INTERRUPTIBLE);
106 schedule_timeout(t * HZ);
107 set_current_state(TASK_RUNNING);
111 /* FIXME: This needs to validate pointers and cookies */
112 static inline void *lustre_handle2object(struct lustre_handle *handle)
115 return (void *)(unsigned long)(handle->addr);
119 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
121 handle->addr = (__u64)(unsigned long)object;
126 void obd_statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
127 void obd_statfs_unpack(struct obd_statfs *osfs, struct statfs *sfs);
129 #include <linux/portals_lib.h>
134 #define OBD_IOCTL_VERSION 0x00010001
136 struct obd_ioctl_data {
138 uint32_t ioc_version;
145 struct obdo ioc_obdo1;
146 struct obdo ioc_obdo2;
151 uint32_t ____padding;
153 /* buffers the kernel will treat as user pointers */
159 /* two inline buffers */
160 uint32_t ioc_inllen1;
162 uint32_t ioc_inllen2;
164 uint32_t ioc_inllen3;
170 struct obd_ioctl_hdr {
172 uint32_t ioc_version;
175 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
177 int len = size_round(sizeof(struct obd_ioctl_data));
178 len += size_round(data->ioc_inllen1);
179 len += size_round(data->ioc_inllen2);
180 len += size_round(data->ioc_inllen3);
185 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
187 if (data->ioc_len > (1<<30)) {
188 printk("OBD ioctl: ioc_len larger than 1<<30\n");
191 if (data->ioc_inllen1 > (1<<30)) {
192 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
195 if (data->ioc_inllen2 > (1<<30)) {
196 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
200 if (data->ioc_inllen3 > (1<<30)) {
201 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
204 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
205 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
208 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
209 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
212 if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
213 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
216 if (data->ioc_pbuf1 && !data->ioc_plen1) {
217 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
220 if (data->ioc_pbuf2 && !data->ioc_plen2) {
221 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
225 if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
226 printk("OBD ioctl: inllen1 set but NULL pointer\n");
229 if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
230 printk("OBD ioctl: inllen2 set but NULL pointer\n");
233 if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
234 printk("OBD ioctl: inllen3 set but NULL pointer\n");
238 if (data->ioc_plen1 && !data->ioc_pbuf1) {
239 printk("OBD ioctl: plen1 set but NULL pointer\n");
242 if (data->ioc_plen2 && !data->ioc_pbuf2) {
243 printk("OBD ioctl: plen2 set but NULL pointer\n");
246 if (obd_ioctl_packlen(data) != data->ioc_len ) {
247 printk("OBD ioctl: packlen exceeds ioc_len\n");
251 if (data->ioc_inllen1 &&
252 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
253 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
256 if (data->ioc_inllen2 &&
257 data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
258 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
261 if (data->ioc_inllen3 &&
262 data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
263 + data->ioc_inllen3 - 1] != '\0') {
264 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
272 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
276 struct obd_ioctl_data *overlay;
277 data->ioc_len = obd_ioctl_packlen(data);
278 data->ioc_version = OBD_IOCTL_VERSION;
280 if (*pbuf && obd_ioctl_packlen(data) > max)
283 *pbuf = malloc(data->ioc_len);
287 overlay = (struct obd_ioctl_data *)*pbuf;
288 memcpy(*pbuf, data, sizeof(*data));
290 ptr = overlay->ioc_bulk;
291 if (data->ioc_inlbuf1)
292 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
293 if (data->ioc_inlbuf2)
294 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
295 if (data->ioc_inlbuf3)
296 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
297 if (obd_ioctl_is_invalid(overlay))
305 #include <linux/obd_support.h>
307 /* buffer MUST be at least the size of obd_ioctl_hdr */
308 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
310 struct obd_ioctl_hdr hdr;
311 struct obd_ioctl_data *data;
316 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
322 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
323 printk("OBD: version mismatch kernel vs application\n");
327 if (hdr.ioc_len > 8192) {
328 printk("OBD: user buffer exceeds 8192 max buffer\n");
332 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
333 printk("OBD: user buffer too small for ioctl\n");
337 OBD_ALLOC(*buf, hdr.ioc_len);
339 CERROR("Cannot allocate control buffer of len %d\n",
344 data = (struct obd_ioctl_data *)*buf;
346 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
352 if (obd_ioctl_is_invalid(data)) {
353 printk("OBD: ioctl not correctly formatted\n");
357 if (data->ioc_inllen1) {
358 data->ioc_inlbuf1 = &data->ioc_bulk[0];
361 if (data->ioc_inllen2) {
362 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
365 if (data->ioc_inllen3) {
366 data->ioc_inlbuf3 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1) +
367 size_round(data->ioc_inllen2);
375 #define OBD_IOC_CREATE _IOR ('f', 101, long)
376 #define OBD_IOC_SETUP _IOW ('f', 102, long)
377 #define OBD_IOC_CLEANUP _IO ('f', 103 )
378 #define OBD_IOC_DESTROY _IOW ('f', 104, long)
379 #define OBD_IOC_PREALLOCATE _IOWR('f', 105, long)
380 #define OBD_IOC_DEC_USE_COUNT _IO ('f', 106 )
381 #define OBD_IOC_SETATTR _IOW ('f', 107, long)
382 #define OBD_IOC_GETATTR _IOR ('f', 108, long)
383 #define OBD_IOC_READ _IOWR('f', 109, long)
384 #define OBD_IOC_WRITE _IOWR('f', 110, long)
385 #define OBD_IOC_CONNECT _IOR ('f', 111, long)
386 #define OBD_IOC_DISCONNECT _IOW ('f', 112, long)
387 #define OBD_IOC_STATFS _IOWR('f', 113, long)
388 #define OBD_IOC_SYNC _IOR ('f', 114, long)
389 #define OBD_IOC_READ2 _IOWR('f', 115, long)
390 #define OBD_IOC_FORMAT _IOWR('f', 116, long)
391 #define OBD_IOC_PARTITION _IOWR('f', 117, long)
392 #define OBD_IOC_ATTACH _IOWR('f', 118, long)
393 #define OBD_IOC_DETACH _IOWR('f', 119, long)
394 #define OBD_IOC_COPY _IOWR('f', 120, long)
395 #define OBD_IOC_MIGR _IOWR('f', 121, long)
396 #define OBD_IOC_PUNCH _IOWR('f', 122, long)
397 #define OBD_IOC_DEVICE _IOWR('f', 123, long)
398 #define OBD_IOC_MODULE_DEBUG _IOWR('f', 124, long)
399 #define OBD_IOC_BRW_READ _IOWR('f', 125, long)
400 #define OBD_IOC_BRW_WRITE _IOWR('f', 126, long)
401 #define OBD_IOC_NAME2DEV _IOWR('f', 127, long)
402 #define OBD_IOC_NEWDEV _IOWR('f', 128, long)
403 #define OBD_IOC_LIST _IOWR('f', 129, long)
404 #define OBD_IOC_UUID2DEV _IOWR('f', 130, long)
406 #define OBD_IOC_RECOVD_NEWCONN _IOWR('f', 131, long)
407 #define OBD_IOC_LOV_CONFIG _IOWR('f', 132, long)
409 #define OBD_IOC_DEC_FS_USE_COUNT _IO ('f', 133 )
413 int (*lwi_on_timeout)(void *);
415 int (*lwi_on_signal)(void *); /* XXX return is ignored for now */
419 #define LWI_TIMEOUT(time, cb, data) \
420 ((struct l_wait_info) { \
422 lwi_on_timeout: cb, \
426 #define LWI_INTR(signals, cb, data) \
427 ((struct l_wait_info) { \
428 lwi_signals: signals, \
433 #define LWI_TIMEOUT_INTR(time, time_cb, signals, sig_cb, data) \
434 ((struct l_wait_info) { \
436 lwi_on_timeout: time_cb, \
437 lwi_signals: signals, \
438 lwi_on_signal: sig_cb, \
442 /* XXX this should be one mask-check */
443 #define l_killable_pending(task) \
444 (sigismember(&(task->pending.signal), SIGKILL) || \
445 sigismember(&(task->pending.signal), SIGINT) || \
446 sigismember(&(task->pending.signal), SIGTERM))
448 #define __l_wait_event(wq, condition, info, ret) \
450 wait_queue_t __wait; \
452 init_waitqueue_entry(&__wait, current); \
454 add_wait_queue(&wq, &__wait); \
455 __state = TASK_UNINTERRUPTIBLE; \
457 set_current_state(__state); \
460 /* We only become INTERRUPTIBLE if a timeout has fired, and \
461 * the caller has given us some signals to care about. \
463 * XXXshaver we should check against info->wli_signals here, \
464 * XXXshaver instead of just using l_killable_pending, perhaps. \
466 if (__state == TASK_INTERRUPTIBLE && \
467 l_killable_pending(current)) { \
468 if (info->lwi_on_signal) \
469 info->lwi_on_signal(info->lwi_cb_data); \
473 if (info->lwi_timeout) { \
474 if (schedule_timeout(info->lwi_timeout) == 0) { \
475 /* We'll take signals only after a timeout. */ \
476 if (info->lwi_signals) \
477 __state = TASK_INTERRUPTIBLE; \
478 if (info->lwi_on_timeout && \
479 info->lwi_on_timeout(info->lwi_cb_data)) { \
488 current->state = TASK_RUNNING; \
489 remove_wait_queue(&wq, &__wait); \
492 #define l_wait_event(wq, condition, info) \
495 struct l_wait_info *__info = (info); \
497 __l_wait_event(wq, condition, __info, __ret); \
501 #endif /* _LUSTRE_LIB_H */