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