]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/obdclass/class_obd.c
Staging: lustre: obdclass: class_obd: Declare as static
[karo-tx-linux.git] / drivers / staging / lustre / lustre / obdclass / class_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38 # include <linux/atomic.h>
39
40 #include "../include/obd_support.h"
41 #include "../include/obd_class.h"
42 #include "../../include/linux/lnet/lnetctl.h"
43 #include "../include/lustre_debug.h"
44 #include "../include/lprocfs_status.h"
45 #include "../include/lustre/lustre_build_version.h"
46 #include <linux/list.h>
47 #include "../include/cl_object.h"
48 #include "llog_internal.h"
49
50
51 struct obd_device *obd_devs[MAX_OBD_DEVICES];
52 EXPORT_SYMBOL(obd_devs);
53 struct list_head obd_types;
54 DEFINE_RWLOCK(obd_dev_lock);
55
56 /* The following are visible and mutable through /proc/sys/lustre/. */
57 unsigned int obd_debug_peer_on_timeout;
58 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
59 unsigned int obd_dump_on_timeout;
60 EXPORT_SYMBOL(obd_dump_on_timeout);
61 unsigned int obd_dump_on_eviction;
62 EXPORT_SYMBOL(obd_dump_on_eviction);
63 unsigned int obd_max_dirty_pages = 256;
64 EXPORT_SYMBOL(obd_max_dirty_pages);
65 atomic_t obd_dirty_pages;
66 EXPORT_SYMBOL(obd_dirty_pages);
67 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT;   /* seconds */
68 EXPORT_SYMBOL(obd_timeout);
69 unsigned int obd_timeout_set;
70 EXPORT_SYMBOL(obd_timeout_set);
71 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
72 unsigned int at_min;
73 EXPORT_SYMBOL(at_min);
74 unsigned int at_max = 600;
75 EXPORT_SYMBOL(at_max);
76 unsigned int at_history = 600;
77 EXPORT_SYMBOL(at_history);
78 int at_early_margin = 5;
79 EXPORT_SYMBOL(at_early_margin);
80 int at_extra = 30;
81 EXPORT_SYMBOL(at_extra);
82
83 atomic_t obd_dirty_transit_pages;
84 EXPORT_SYMBOL(obd_dirty_transit_pages);
85
86 char obd_jobid_var[JOBSTATS_JOBID_VAR_MAX_LEN + 1] = JOBSTATS_DISABLE;
87 EXPORT_SYMBOL(obd_jobid_var);
88
89 char obd_jobid_node[JOBSTATS_JOBID_SIZE + 1];
90
91 /* Get jobid of current process from stored variable or calculate
92  * it from pid and user_id.
93  *
94  * Historically this was also done by reading the environment variable
95  * stored in between the "env_start" & "env_end" of task struct.
96  * This is now deprecated.
97  */
98 int lustre_get_jobid(char *jobid)
99 {
100         memset(jobid, 0, JOBSTATS_JOBID_SIZE);
101         /* Jobstats isn't enabled */
102         if (strcmp(obd_jobid_var, JOBSTATS_DISABLE) == 0)
103                 return 0;
104
105         /* Use process name + fsuid as jobid */
106         if (strcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID) == 0) {
107                 snprintf(jobid, JOBSTATS_JOBID_SIZE, "%s.%u",
108                          current_comm(),
109                          from_kuid(&init_user_ns, current_fsuid()));
110                 return 0;
111         }
112
113         /* Whole node dedicated to single job */
114         if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) == 0) {
115                 strcpy(jobid, obd_jobid_node);
116                 return 0;
117         }
118
119         return -ENOENT;
120 }
121 EXPORT_SYMBOL(lustre_get_jobid);
122
123 static inline void obd_data2conn(struct lustre_handle *conn,
124                                  struct obd_ioctl_data *data)
125 {
126         memset(conn, 0, sizeof(*conn));
127         conn->cookie = data->ioc_cookie;
128 }
129
130 static inline void obd_conn2data(struct obd_ioctl_data *data,
131                                  struct lustre_handle *conn)
132 {
133         data->ioc_cookie = conn->cookie;
134 }
135
136 static int class_resolve_dev_name(__u32 len, const char *name)
137 {
138         int rc;
139         int dev;
140
141         if (!len || !name) {
142                 CERROR("No name passed,!\n");
143                 rc = -EINVAL;
144                 goto out;
145         }
146         if (name[len - 1] != 0) {
147                 CERROR("Name not nul terminated!\n");
148                 rc = -EINVAL;
149                 goto out;
150         }
151
152         CDEBUG(D_IOCTL, "device name %s\n", name);
153         dev = class_name2dev(name);
154         if (dev == -1) {
155                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
156                 rc = -EINVAL;
157                 goto out;
158         }
159
160         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
161         rc = dev;
162
163 out:
164         return rc;
165 }
166
167 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
168 {
169         char *buf = NULL;
170         struct obd_ioctl_data *data;
171         struct libcfs_debug_ioctl_data *debug_data;
172         struct obd_device *obd = NULL;
173         int err = 0, len = 0;
174
175         /* only for debugging */
176         if (cmd == LIBCFS_IOC_DEBUG_MASK) {
177                 debug_data = (struct libcfs_debug_ioctl_data *)arg;
178                 libcfs_subsystem_debug = debug_data->subs;
179                 libcfs_debug = debug_data->debug;
180                 return 0;
181         }
182
183         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
184         if (obd_ioctl_getdata(&buf, &len, (void *)arg)) {
185                 CERROR("OBD ioctl: data error\n");
186                 return -EINVAL;
187         }
188         data = (struct obd_ioctl_data *)buf;
189
190         switch (cmd) {
191         case OBD_IOC_PROCESS_CFG: {
192                 struct lustre_cfg *lcfg;
193
194                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
195                         CERROR("No config buffer passed!\n");
196                         err = -EINVAL;
197                         goto out;
198                 }
199                 lcfg = kzalloc(data->ioc_plen1, GFP_NOFS);
200                 if (!lcfg) {
201                         err = -ENOMEM;
202                         goto out;
203                 }
204                 err = copy_from_user(lcfg, data->ioc_pbuf1,
205                                          data->ioc_plen1);
206                 if (!err)
207                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
208                 if (!err)
209                         err = class_process_config(lcfg);
210
211                 kfree(lcfg);
212                 goto out;
213         }
214
215         case OBD_GET_VERSION:
216                 if (!data->ioc_inlbuf1) {
217                         CERROR("No buffer passed in ioctl\n");
218                         err = -EINVAL;
219                         goto out;
220                 }
221
222                 if (strlen(BUILD_VERSION) + 1 > data->ioc_inllen1) {
223                         CERROR("ioctl buffer too small to hold version\n");
224                         err = -EINVAL;
225                         goto out;
226                 }
227
228                 memcpy(data->ioc_bulk, BUILD_VERSION,
229                        strlen(BUILD_VERSION) + 1);
230
231                 err = obd_ioctl_popdata((void *)arg, data, len);
232                 if (err)
233                         err = -EFAULT;
234                 goto out;
235
236         case OBD_IOC_NAME2DEV: {
237                 /* Resolve a device name.  This does not change the
238                  * currently selected device.
239                  */
240                 int dev;
241
242                 dev = class_resolve_dev_name(data->ioc_inllen1,
243                                              data->ioc_inlbuf1);
244                 data->ioc_dev = dev;
245                 if (dev < 0) {
246                         err = -EINVAL;
247                         goto out;
248                 }
249
250                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
251                 if (err)
252                         err = -EFAULT;
253                 goto out;
254         }
255
256         case OBD_IOC_UUID2DEV: {
257                 /* Resolve a device uuid.  This does not change the
258                  * currently selected device.
259                  */
260                 int dev;
261                 struct obd_uuid uuid;
262
263                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
264                         CERROR("No UUID passed!\n");
265                         err = -EINVAL;
266                         goto out;
267                 }
268                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
269                         CERROR("UUID not NUL terminated!\n");
270                         err = -EINVAL;
271                         goto out;
272                 }
273
274                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
275                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
276                 dev = class_uuid2dev(&uuid);
277                 data->ioc_dev = dev;
278                 if (dev == -1) {
279                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
280                                data->ioc_inlbuf1);
281                         err = -EINVAL;
282                         goto out;
283                 }
284
285                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
286                        dev);
287                 err = obd_ioctl_popdata((void *)arg, data, sizeof(*data));
288                 if (err)
289                         err = -EFAULT;
290                 goto out;
291         }
292
293         case OBD_IOC_CLOSE_UUID: {
294                 CDEBUG(D_IOCTL, "closing all connections to uuid %s (NOOP)\n",
295                        data->ioc_inlbuf1);
296                 err = 0;
297                 goto out;
298         }
299
300         case OBD_IOC_GETDEVICE: {
301                 int     index = data->ioc_count;
302                 char    *status, *str;
303
304                 if (!data->ioc_inlbuf1) {
305                         CERROR("No buffer passed in ioctl\n");
306                         err = -EINVAL;
307                         goto out;
308                 }
309                 if (data->ioc_inllen1 < 128) {
310                         CERROR("ioctl buffer too small to hold version\n");
311                         err = -EINVAL;
312                         goto out;
313                 }
314
315                 obd = class_num2obd(index);
316                 if (!obd) {
317                         err = -ENOENT;
318                         goto out;
319                 }
320
321                 if (obd->obd_stopping)
322                         status = "ST";
323                 else if (obd->obd_set_up)
324                         status = "UP";
325                 else if (obd->obd_attached)
326                         status = "AT";
327                 else
328                         status = "--";
329                 str = (char *)data->ioc_bulk;
330                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
331                          (int)index, status, obd->obd_type->typ_name,
332                          obd->obd_name, obd->obd_uuid.uuid,
333                          atomic_read(&obd->obd_refcount));
334                 err = obd_ioctl_popdata((void *)arg, data, len);
335
336                 err = 0;
337                 goto out;
338         }
339
340         }
341
342         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
343                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL) {
344                         err = -EINVAL;
345                         goto out;
346                 }
347                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME) {
348                         err = -EINVAL;
349                         goto out;
350                 }
351                 obd = class_name2obd(data->ioc_inlbuf4);
352         } else if (data->ioc_dev < class_devno_max()) {
353                 obd = class_num2obd(data->ioc_dev);
354         } else {
355                 CERROR("OBD ioctl: No device\n");
356                 err = -EINVAL;
357                 goto out;
358         }
359
360         if (obd == NULL) {
361                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
362                 err = -EINVAL;
363                 goto out;
364         }
365         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
366
367         if (!obd->obd_set_up || obd->obd_stopping) {
368                 CERROR("OBD ioctl: device not setup %d\n", data->ioc_dev);
369                 err = -EINVAL;
370                 goto out;
371         }
372
373         switch (cmd) {
374         case OBD_IOC_NO_TRANSNO: {
375                 if (!obd->obd_attached) {
376                         CERROR("Device %d not attached\n", obd->obd_minor);
377                         err = -ENODEV;
378                         goto out;
379                 }
380                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
381                        obd->obd_name);
382                 obd->obd_no_transno = 1;
383                 err = 0;
384                 goto out;
385         }
386
387         default: {
388                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
389                 if (err)
390                         goto out;
391
392                 err = obd_ioctl_popdata((void *)arg, data, len);
393                 if (err)
394                         err = -EFAULT;
395                 goto out;
396         }
397         }
398
399  out:
400         if (buf)
401                 obd_ioctl_freedata(buf, len);
402         return err;
403 } /* class_handle_ioctl */
404
405 #define OBD_INIT_CHECK
406 static int obd_init_checks(void)
407 {
408         __u64 u64val, div64val;
409         char buf[64];
410         int len, ret = 0;
411
412         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", "%llu", "%lld", "%#llx");
413
414         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
415
416         u64val = OBD_OBJECT_EOF;
417         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
418         if (u64val != OBD_OBJECT_EOF) {
419                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
420                        u64val, (int)sizeof(u64val));
421                 ret = -EINVAL;
422         }
423         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
424         if (len != 18) {
425                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
426                 ret = -EINVAL;
427         }
428
429         div64val = OBD_OBJECT_EOF;
430         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
431         if (u64val != OBD_OBJECT_EOF) {
432                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
433                        u64val, (int)sizeof(u64val));
434                 ret = -EOVERFLOW;
435         }
436         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
437                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
438                        u64val, (int)sizeof(u64val));
439                 return -EOVERFLOW;
440         }
441         if (do_div(div64val, 256) != (u64val & 255)) {
442                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
443                 return -EOVERFLOW;
444         }
445         if (u64val >> 8 != div64val) {
446                 CERROR("do_div(%#llx,256) %llu != %llu\n",
447                        u64val, div64val, u64val >> 8);
448                 return -EOVERFLOW;
449         }
450         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
451         if (len != 18) {
452                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
453                 ret = -EINVAL;
454         }
455         len = snprintf(buf, sizeof(buf), "%llu", u64val);
456         if (len != 20) {
457                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
458                 ret = -EINVAL;
459         }
460         len = snprintf(buf, sizeof(buf), "%lld", u64val);
461         if (len != 2) {
462                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
463                 ret = -EINVAL;
464         }
465         if ((u64val & ~CFS_PAGE_MASK) >= PAGE_CACHE_SIZE) {
466                 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
467                       (__u64)PAGE_CACHE_SIZE);
468                 ret = -EINVAL;
469         }
470
471         return ret;
472 }
473
474 extern int class_procfs_init(void);
475 extern int class_procfs_clean(void);
476
477 static int __init init_obdclass(void)
478 {
479         int i, err;
480         int lustre_register_fs(void);
481
482         LCONSOLE_INFO("Lustre: Build Version: "BUILD_VERSION"\n");
483
484         spin_lock_init(&obd_types_lock);
485         obd_zombie_impexp_init();
486
487         err = obd_init_checks();
488         if (err == -EOVERFLOW)
489                 return err;
490
491         class_init_uuidlist();
492         err = class_handle_init();
493         if (err)
494                 return err;
495
496         INIT_LIST_HEAD(&obd_types);
497
498         err = misc_register(&obd_psdev);
499         if (err) {
500                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
501                 return err;
502         }
503
504         /* This struct is already zeroed for us (static global) */
505         for (i = 0; i < class_devno_max(); i++)
506                 obd_devs[i] = NULL;
507
508         /* Default the dirty page cache cap to 1/2 of system memory.
509          * For clients with less memory, a larger fraction is needed
510          * for other purposes (mostly for BGL). */
511         if (totalram_pages <= 512 << (20 - PAGE_CACHE_SHIFT))
512                 obd_max_dirty_pages = totalram_pages / 4;
513         else
514                 obd_max_dirty_pages = totalram_pages / 2;
515
516         err = obd_init_caches();
517         if (err)
518                 return err;
519
520         err = class_procfs_init();
521         if (err)
522                 return err;
523
524         err = obd_sysctl_init();
525         if (err)
526                 return err;
527
528         err = lu_global_init();
529         if (err)
530                 return err;
531
532         err = cl_global_init();
533         if (err != 0)
534                 return err;
535
536
537         err = llog_info_init();
538         if (err)
539                 return err;
540
541         err = lustre_register_fs();
542
543         return err;
544 }
545
546 /* liblustre doesn't call cleanup_obdclass, apparently.  we carry on in this
547  * ifdef to the end of the file to cover module and versioning goo.*/
548 static void cleanup_obdclass(void)
549 {
550         int i;
551         int lustre_unregister_fs(void);
552
553         lustre_unregister_fs();
554
555         misc_deregister(&obd_psdev);
556         for (i = 0; i < class_devno_max(); i++) {
557                 struct obd_device *obd = class_num2obd(i);
558                 if (obd && obd->obd_set_up &&
559                     OBT(obd) && OBP(obd, detach)) {
560                         /* XXX should this call generic detach otherwise? */
561                         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
562                         OBP(obd, detach)(obd);
563                 }
564         }
565         llog_info_fini();
566         cl_global_fini();
567         lu_global_fini();
568
569         obd_cleanup_caches();
570
571         class_procfs_clean();
572
573         class_handle_cleanup();
574         class_exit_uuidlist();
575         obd_zombie_impexp_stop();
576 }
577
578 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
579 MODULE_DESCRIPTION("Lustre Class Driver Build Version: " BUILD_VERSION);
580 MODULE_LICENSE("GPL");
581 MODULE_VERSION(LUSTRE_VERSION_STRING);
582
583 module_init(init_obdclass);
584 module_exit(cleanup_obdclass);