GNU Linux-libre 4.9.337-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 "../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 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                 err = copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1);
184                 if (!err)
185                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
186                 if (!err)
187                         err = class_process_config(lcfg);
188
189                 kfree(lcfg);
190                 goto out;
191         }
192
193         case OBD_GET_VERSION:
194                 if (!data->ioc_inlbuf1) {
195                         CERROR("No buffer passed in ioctl\n");
196                         err = -EINVAL;
197                         goto out;
198                 }
199
200                 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
201                         CERROR("ioctl buffer too small to hold version\n");
202                         err = -EINVAL;
203                         goto out;
204                 }
205
206                 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
207                        strlen(LUSTRE_VERSION_STRING) + 1);
208
209                 err = obd_ioctl_popdata((void __user *)arg, data, len);
210                 if (err)
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                 err = obd_ioctl_popdata((void __user *)arg, data,
229                                         sizeof(*data));
230                 if (err)
231                         err = -EFAULT;
232                 goto out;
233         }
234
235         case OBD_IOC_UUID2DEV: {
236                 /* Resolve a device uuid.  This does not change the
237                  * currently selected device.
238                  */
239                 int dev;
240                 struct obd_uuid uuid;
241
242                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
243                         CERROR("No UUID passed!\n");
244                         err = -EINVAL;
245                         goto out;
246                 }
247                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
248                         CERROR("UUID not NUL terminated!\n");
249                         err = -EINVAL;
250                         goto out;
251                 }
252
253                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
254                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
255                 dev = class_uuid2dev(&uuid);
256                 data->ioc_dev = dev;
257                 if (dev == -1) {
258                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
259                                data->ioc_inlbuf1);
260                         err = -EINVAL;
261                         goto out;
262                 }
263
264                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
265                        dev);
266                 err = obd_ioctl_popdata((void __user *)arg, data,
267                                         sizeof(*data));
268                 if (err)
269                         err = -EFAULT;
270                 goto out;
271         }
272
273         case OBD_IOC_GETDEVICE: {
274                 int     index = data->ioc_count;
275                 char    *status, *str;
276
277                 if (!data->ioc_inlbuf1) {
278                         CERROR("No buffer passed in ioctl\n");
279                         err = -EINVAL;
280                         goto out;
281                 }
282                 if (data->ioc_inllen1 < 128) {
283                         CERROR("ioctl buffer too small to hold version\n");
284                         err = -EINVAL;
285                         goto out;
286                 }
287
288                 obd = class_num2obd(index);
289                 if (!obd) {
290                         err = -ENOENT;
291                         goto out;
292                 }
293
294                 if (obd->obd_stopping)
295                         status = "ST";
296                 else if (obd->obd_set_up)
297                         status = "UP";
298                 else if (obd->obd_attached)
299                         status = "AT";
300                 else
301                         status = "--";
302                 str = (char *)data->ioc_bulk;
303                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
304                          (int)index, status, obd->obd_type->typ_name,
305                          obd->obd_name, obd->obd_uuid.uuid,
306                          atomic_read(&obd->obd_refcount));
307                 err = obd_ioctl_popdata((void __user *)arg, data, len);
308
309                 err = 0;
310                 goto out;
311         }
312         }
313
314         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
315                 if (data->ioc_inllen4 <= 0 || !data->ioc_inlbuf4) {
316                         err = -EINVAL;
317                         goto out;
318                 }
319                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME) {
320                         err = -EINVAL;
321                         goto out;
322                 }
323                 obd = class_name2obd(data->ioc_inlbuf4);
324         } else if (data->ioc_dev < class_devno_max()) {
325                 obd = class_num2obd(data->ioc_dev);
326         } else {
327                 CERROR("OBD ioctl: No device\n");
328                 err = -EINVAL;
329                 goto out;
330         }
331
332         if (!obd) {
333                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
334                 err = -EINVAL;
335                 goto out;
336         }
337         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
338
339         if (!obd->obd_set_up || obd->obd_stopping) {
340                 CERROR("OBD ioctl: device not setup %d\n", data->ioc_dev);
341                 err = -EINVAL;
342                 goto out;
343         }
344
345         switch (cmd) {
346         case OBD_IOC_NO_TRANSNO: {
347                 if (!obd->obd_attached) {
348                         CERROR("Device %d not attached\n", obd->obd_minor);
349                         err = -ENODEV;
350                         goto out;
351                 }
352                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
353                        obd->obd_name);
354                 obd->obd_no_transno = 1;
355                 err = 0;
356                 goto out;
357         }
358
359         default: {
360                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
361                 if (err)
362                         goto out;
363
364                 err = obd_ioctl_popdata((void __user *)arg, data, len);
365                 if (err)
366                         err = -EFAULT;
367                 goto out;
368         }
369         }
370
371  out:
372         if (buf)
373                 obd_ioctl_freedata(buf, len);
374         return err;
375 } /* class_handle_ioctl */
376
377 #define OBD_INIT_CHECK
378 static int obd_init_checks(void)
379 {
380         __u64 u64val, div64val;
381         char buf[64];
382         int len, ret = 0;
383
384         CDEBUG(D_INFO, "LPU64=%s, LPD64=%s, LPX64=%s\n", "%llu", "%lld", "%#llx");
385
386         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
387
388         u64val = OBD_OBJECT_EOF;
389         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
390         if (u64val != OBD_OBJECT_EOF) {
391                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
392                        u64val, (int)sizeof(u64val));
393                 ret = -EINVAL;
394         }
395         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
396         if (len != 18) {
397                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
398                 ret = -EINVAL;
399         }
400
401         div64val = OBD_OBJECT_EOF;
402         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
403         if (u64val != OBD_OBJECT_EOF) {
404                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
405                        u64val, (int)sizeof(u64val));
406                 ret = -EOVERFLOW;
407         }
408         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
409                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
410                        u64val, (int)sizeof(u64val));
411                 return -EOVERFLOW;
412         }
413         if (do_div(div64val, 256) != (u64val & 255)) {
414                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
415                 return -EOVERFLOW;
416         }
417         if (u64val >> 8 != div64val) {
418                 CERROR("do_div(%#llx,256) %llu != %llu\n",
419                        u64val, div64val, u64val >> 8);
420                 return -EOVERFLOW;
421         }
422         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
423         if (len != 18) {
424                 CWARN("LPX64 wrong length! strlen(%s)=%d != 18\n", buf, len);
425                 ret = -EINVAL;
426         }
427         len = snprintf(buf, sizeof(buf), "%llu", u64val);
428         if (len != 20) {
429                 CWARN("LPU64 wrong length! strlen(%s)=%d != 20\n", buf, len);
430                 ret = -EINVAL;
431         }
432         len = snprintf(buf, sizeof(buf), "%lld", u64val);
433         if (len != 2) {
434                 CWARN("LPD64 wrong length! strlen(%s)=%d != 2\n", buf, len);
435                 ret = -EINVAL;
436         }
437         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
438                 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
439                       (__u64)PAGE_SIZE);
440                 ret = -EINVAL;
441         }
442
443         return ret;
444 }
445
446 static int __init obdclass_init(void)
447 {
448         int i, err;
449
450         LCONSOLE_INFO("Lustre: Build Version: " LUSTRE_VERSION_STRING "\n");
451
452         spin_lock_init(&obd_types_lock);
453         obd_zombie_impexp_init();
454
455         err = obd_init_checks();
456         if (err == -EOVERFLOW)
457                 return err;
458
459         class_init_uuidlist();
460         err = class_handle_init();
461         if (err)
462                 return err;
463
464         INIT_LIST_HEAD(&obd_types);
465
466         err = misc_register(&obd_psdev);
467         if (err) {
468                 CERROR("cannot register %d err %d\n", OBD_DEV_MINOR, err);
469                 return err;
470         }
471
472         /* This struct is already zeroed for us (static global) */
473         for (i = 0; i < class_devno_max(); i++)
474                 obd_devs[i] = NULL;
475
476         /* Default the dirty page cache cap to 1/2 of system memory.
477          * For clients with less memory, a larger fraction is needed
478          * for other purposes (mostly for BGL).
479          */
480         if (totalram_pages <= 512 << (20 - PAGE_SHIFT))
481                 obd_max_dirty_pages = totalram_pages / 4;
482         else
483                 obd_max_dirty_pages = totalram_pages / 2;
484
485         err = obd_init_caches();
486         if (err)
487                 return err;
488
489         err = class_procfs_init();
490         if (err)
491                 return err;
492
493         err = obd_sysctl_init();
494         if (err)
495                 return err;
496
497         err = lu_global_init();
498         if (err)
499                 return err;
500
501         err = cl_global_init();
502         if (err != 0)
503                 return err;
504
505         err = llog_info_init();
506         if (err)
507                 return err;
508
509         err = lustre_register_fs();
510
511         return err;
512 }
513
514 static void obdclass_exit(void)
515 {
516         lustre_unregister_fs();
517
518         misc_deregister(&obd_psdev);
519         llog_info_fini();
520         cl_global_fini();
521         lu_global_fini();
522
523         obd_cleanup_caches();
524
525         class_procfs_clean();
526
527         class_handle_cleanup();
528         class_exit_uuidlist();
529         obd_zombie_impexp_stop();
530 }
531
532 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
533 MODULE_DESCRIPTION("Lustre Class Driver");
534 MODULE_VERSION(LUSTRE_VERSION_STRING);
535 MODULE_LICENSE("GPL");
536
537 module_init(obdclass_init);
538 module_exit(obdclass_exit);