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