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