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