Whamcloud - gitweb
LU-12355 llite: totalram_pages changed to atomic_long_t
[fs/lustre-release.git] / 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, 2017, 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
35 #include <linux/miscdevice.h>
36 #include <linux/user_namespace.h>
37 #ifdef HAVE_UIDGID_HEADER
38 # include <linux/uidgid.h>
39 #endif
40 #include <linux/atomic.h>
41 #include <linux/list.h>
42
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <uapi/linux/lnet/lnetctl.h>
46 #include <lustre_debug.h>
47 #include <lustre_kernelcomm.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <dt_object.h>
52 # include <md_object.h>
53 #endif /* HAVE_SERVER_SUPPORT */
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include "llog_internal.h"
56
57 #ifdef CONFIG_PROC_FS
58 static __u64 obd_max_alloc;
59 #else
60 __u64 obd_max_alloc;
61 #endif
62
63 static DEFINE_SPINLOCK(obd_updatemax_lock);
64
65 /* The following are visible and mutable through /proc/sys/lustre/. */
66 unsigned int obd_debug_peer_on_timeout;
67 EXPORT_SYMBOL(obd_debug_peer_on_timeout);
68 unsigned int obd_dump_on_timeout;
69 EXPORT_SYMBOL(obd_dump_on_timeout);
70 unsigned int obd_dump_on_eviction;
71 EXPORT_SYMBOL(obd_dump_on_eviction);
72 unsigned int obd_lbug_on_eviction;
73 EXPORT_SYMBOL(obd_lbug_on_eviction);
74 unsigned long obd_max_dirty_pages;
75 EXPORT_SYMBOL(obd_max_dirty_pages);
76 atomic_long_t obd_dirty_pages;
77 EXPORT_SYMBOL(obd_dirty_pages);
78 unsigned int obd_timeout = OBD_TIMEOUT_DEFAULT;   /* seconds */
79 EXPORT_SYMBOL(obd_timeout);
80 unsigned int ldlm_timeout = LDLM_TIMEOUT_DEFAULT; /* seconds */
81 EXPORT_SYMBOL(ldlm_timeout);
82 unsigned int obd_timeout_set;
83 EXPORT_SYMBOL(obd_timeout_set);
84 unsigned int ldlm_timeout_set;
85 EXPORT_SYMBOL(ldlm_timeout_set);
86 /* bulk transfer timeout, give up after 100s by default */
87 unsigned int bulk_timeout = 100; /* seconds */
88 EXPORT_SYMBOL(bulk_timeout);
89 /* Adaptive timeout defs here instead of ptlrpc module for /proc/sys/ access */
90 unsigned int at_min = 0;
91 EXPORT_SYMBOL(at_min);
92 unsigned int at_max = 600;
93 EXPORT_SYMBOL(at_max);
94 unsigned int at_history = 600;
95 EXPORT_SYMBOL(at_history);
96 int at_early_margin = 5;
97 EXPORT_SYMBOL(at_early_margin);
98 int at_extra = 30;
99 EXPORT_SYMBOL(at_extra);
100
101 atomic_long_t obd_dirty_transit_pages;
102 EXPORT_SYMBOL(obd_dirty_transit_pages);
103
104 #ifdef CONFIG_PROC_FS
105 struct lprocfs_stats *obd_memory = NULL;
106 EXPORT_SYMBOL(obd_memory);
107 #endif
108
109 static int class_resolve_dev_name(__u32 len, const char *name)
110 {
111         int rc;
112         int dev;
113
114         ENTRY;
115         if (!len || !name) {
116                 CERROR("No name passed,!\n");
117                 GOTO(out, rc = -EINVAL);
118         }
119         if (name[len - 1] != 0) {
120                 CERROR("Name not nul terminated!\n");
121                 GOTO(out, rc = -EINVAL);
122         }
123
124         CDEBUG(D_IOCTL, "device name %s\n", name);
125         dev = class_name2dev(name);
126         if (dev == -1) {
127                 CDEBUG(D_IOCTL, "No device for name %s!\n", name);
128                 GOTO(out, rc = -EINVAL);
129         }
130
131         CDEBUG(D_IOCTL, "device name %s, dev %d\n", name, dev);
132         rc = dev;
133
134 out:
135         RETURN(rc);
136 }
137
138 #define OBD_MAX_IOCTL_BUFFER    8192
139
140 static int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
141 {
142         if (data->ioc_len > BIT(30)) {
143                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
144                 return 1;
145         }
146
147         if (data->ioc_inllen1 > BIT(30)) {
148                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
149                 return 1;
150         }
151
152         if (data->ioc_inllen2 > BIT(30)) {
153                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
154                 return 1;
155         }
156
157         if (data->ioc_inllen3 > BIT(30)) {
158                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
159                 return 1;
160         }
161
162         if (data->ioc_inllen4 > BIT(30)) {
163                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
164                 return 1;
165         }
166
167         if (data->ioc_inlbuf1 && data->ioc_inllen1 == 0) {
168                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
169                 return 1;
170         }
171
172         if (data->ioc_inlbuf2 && data->ioc_inllen2 == 0) {
173                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
174                 return 1;
175         }
176
177         if (data->ioc_inlbuf3 && data->ioc_inllen3 == 0) {
178                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
179                 return 1;
180         }
181
182         if (data->ioc_inlbuf4 && data->ioc_inllen4 == 0) {
183                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
184                 return 1;
185         }
186
187         if (data->ioc_pbuf1 && data->ioc_plen1 == 0) {
188                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
189                 return 1;
190         }
191
192         if (data->ioc_pbuf2 && data->ioc_plen2 == 0) {
193                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
194                 return 1;
195         }
196
197         if (!data->ioc_pbuf1 && data->ioc_plen1 != 0) {
198                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
199                 return 1;
200         }
201
202         if (!data->ioc_pbuf2 && data->ioc_plen2 != 0) {
203                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
204                 return 1;
205         }
206
207         if (obd_ioctl_packlen(data) > data->ioc_len) {
208                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
209                        obd_ioctl_packlen(data), data->ioc_len);
210                 return 1;
211         }
212
213         return 0;
214 }
215
216 /* buffer MUST be at least the size of obd_ioctl_hdr */
217 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
218 {
219         struct obd_ioctl_hdr hdr;
220         struct obd_ioctl_data *data;
221         int offset = 0;
222
223         ENTRY;
224         if (copy_from_user(&hdr, arg, sizeof(hdr)))
225                 RETURN(-EFAULT);
226
227         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
228                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
229                        OBD_IOCTL_VERSION, hdr.ioc_version);
230                 RETURN(-EINVAL);
231         }
232
233         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
234                 CERROR("User buffer len %d exceeds %d max buffer\n",
235                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
236                 RETURN(-EINVAL);
237         }
238
239         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
240                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
241                 RETURN(-EINVAL);
242         }
243
244         /* When there are lots of processes calling vmalloc on multi-core
245          * system, the high lock contention will hurt performance badly,
246          * obdfilter-survey is an example, which relies on ioctl. So we'd
247          * better avoid vmalloc on ioctl path. LU-66
248          */
249         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
250         if (!*buf) {
251                 CERROR("Cannot allocate control buffer of len %d\n",
252                        hdr.ioc_len);
253                 RETURN(-EINVAL);
254         }
255         *len = hdr.ioc_len;
256         data = (struct obd_ioctl_data *)*buf;
257
258         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
259                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
260                 RETURN(-EFAULT);
261         }
262
263         if (obd_ioctl_is_invalid(data)) {
264                 CERROR("ioctl not correctly formatted\n");
265                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
266                 RETURN(-EINVAL);
267         }
268
269         if (data->ioc_inllen1) {
270                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
271                 offset += cfs_size_round(data->ioc_inllen1);
272         }
273
274         if (data->ioc_inllen2) {
275                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
276                 offset += cfs_size_round(data->ioc_inllen2);
277         }
278
279         if (data->ioc_inllen3) {
280                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
281                 offset += cfs_size_round(data->ioc_inllen3);
282         }
283
284         if (data->ioc_inllen4)
285                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
286
287         RETURN(0);
288 }
289 EXPORT_SYMBOL(obd_ioctl_getdata);
290
291 int class_handle_ioctl(unsigned int cmd, unsigned long arg)
292 {
293         char *buf = NULL;
294         struct obd_ioctl_data *data;
295         struct obd_device *obd = NULL;
296         int err = 0, len = 0;
297
298         ENTRY;
299         CDEBUG(D_IOCTL, "cmd = %x\n", cmd);
300         if (obd_ioctl_getdata(&buf, &len, (void __user *)arg)) {
301                 CERROR("OBD ioctl: data error\n");
302                 RETURN(-EINVAL);
303         }
304         data = (struct obd_ioctl_data *)buf;
305
306         switch (cmd) {
307         case OBD_IOC_PROCESS_CFG: {
308                 struct lustre_cfg *lcfg;
309
310                 if (!data->ioc_plen1 || !data->ioc_pbuf1) {
311                         CERROR("No config buffer passed!\n");
312                         GOTO(out, err = -EINVAL);
313                 }
314                 OBD_ALLOC(lcfg, data->ioc_plen1);
315                 if (lcfg == NULL)
316                         GOTO(out, err = -ENOMEM);
317                 err = copy_from_user(lcfg, data->ioc_pbuf1,
318                                          data->ioc_plen1);
319                 if (!err)
320                         err = lustre_cfg_sanity_check(lcfg, data->ioc_plen1);
321                 if (!err)
322                         err = class_process_config(lcfg);
323
324                 OBD_FREE(lcfg, data->ioc_plen1);
325                 GOTO(out, err);
326         }
327
328 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
329         case OBD_GET_VERSION: {
330                 static bool warned;
331
332                 if (!data->ioc_inlbuf1) {
333                         CERROR("No buffer passed in ioctl\n");
334                         GOTO(out, err = -EINVAL);
335                 }
336
337                 if (strlen(LUSTRE_VERSION_STRING) + 1 > data->ioc_inllen1) {
338                         CERROR("ioctl buffer too small to hold version\n");
339                         GOTO(out, err = -EINVAL);
340                 }
341
342                 if (!warned) {
343                         warned = true;
344                         CWARN("%s: ioctl(OBD_GET_VERSION) is deprecated, "
345                               "use llapi_get_version_string() and/or relink\n",
346                               current->comm);
347                 }
348                 memcpy(data->ioc_bulk, LUSTRE_VERSION_STRING,
349                        strlen(LUSTRE_VERSION_STRING) + 1);
350
351                 if (copy_to_user((void __user *)arg, data, len))
352                         err = -EFAULT;
353                 GOTO(out, err);
354         }
355 #endif
356         case OBD_IOC_NAME2DEV: {
357                 /* Resolve a device name.  This does not change the
358                  * currently selected device.
359                  */
360                 int dev;
361
362                 dev = class_resolve_dev_name(data->ioc_inllen1,
363                                              data->ioc_inlbuf1);
364                 data->ioc_dev = dev;
365                 if (dev < 0)
366                         GOTO(out, err = -EINVAL);
367
368                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
369                         err = -EFAULT;
370                 GOTO(out, err);
371         }
372
373         case OBD_IOC_UUID2DEV: {
374                 /* Resolve a device uuid.  This does not change the
375                  * currently selected device.
376                  */
377                 int dev;
378                 struct obd_uuid uuid;
379
380                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
381                         CERROR("No UUID passed!\n");
382                         GOTO(out, err = -EINVAL);
383                 }
384                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
385                         CERROR("UUID not NUL terminated!\n");
386                         GOTO(out, err = -EINVAL);
387                 }
388
389                 CDEBUG(D_IOCTL, "device name %s\n", data->ioc_inlbuf1);
390                 obd_str2uuid(&uuid, data->ioc_inlbuf1);
391                 dev = class_uuid2dev(&uuid);
392                 data->ioc_dev = dev;
393                 if (dev == -1) {
394                         CDEBUG(D_IOCTL, "No device for UUID %s!\n",
395                                data->ioc_inlbuf1);
396                         GOTO(out, err = -EINVAL);
397                 }
398
399                 CDEBUG(D_IOCTL, "device name %s, dev %d\n", data->ioc_inlbuf1,
400                        dev);
401                 if (copy_to_user((void __user *)arg, data, sizeof(*data)))
402                         err = -EFAULT;
403                 GOTO(out, err);
404         }
405
406         case OBD_IOC_GETDEVICE: {
407                 int     index = data->ioc_count;
408                 char    *status, *str;
409
410                 if (!data->ioc_inlbuf1) {
411                         CERROR("No buffer passed in ioctl\n");
412                         GOTO(out, err = -EINVAL);
413                 }
414                 if (data->ioc_inllen1 < 128) {
415                         CERROR("ioctl buffer too small to hold version\n");
416                         GOTO(out, err = -EINVAL);
417                 }
418
419                 obd = class_num2obd(index);
420                 if (!obd)
421                         GOTO(out, err = -ENOENT);
422
423                 if (obd->obd_stopping)
424                         status = "ST";
425                 else if (obd->obd_inactive)
426                         status = "IN";
427                 else if (obd->obd_set_up)
428                         status = "UP";
429                 else if (obd->obd_attached)
430                         status = "AT";
431                 else
432                         status = "--";
433
434                 str = (char *)data->ioc_bulk;
435                 snprintf(str, len - sizeof(*data), "%3d %s %s %s %s %d",
436                          (int)index, status, obd->obd_type->typ_name,
437                          obd->obd_name, obd->obd_uuid.uuid,
438                          atomic_read(&obd->obd_refcount));
439
440                 if (copy_to_user((void __user *)arg, data, len))
441                         err = -EFAULT;
442
443                 GOTO(out, err);
444         }
445
446         }
447
448         if (data->ioc_dev == OBD_DEV_BY_DEVNAME) {
449                 if (data->ioc_inllen4 <= 0 || data->ioc_inlbuf4 == NULL)
450                         GOTO(out, err = -EINVAL);
451                 if (strnlen(data->ioc_inlbuf4, MAX_OBD_NAME) >= MAX_OBD_NAME)
452                         GOTO(out, err = -EINVAL);
453                 obd = class_name2obd(data->ioc_inlbuf4);
454         } else if (data->ioc_dev < class_devno_max()) {
455                 obd = class_num2obd(data->ioc_dev);
456         } else {
457                 CERROR("OBD ioctl: No device\n");
458                 GOTO(out, err = -EINVAL);
459         }
460
461         if (obd == NULL) {
462                 CERROR("OBD ioctl : No Device %d\n", data->ioc_dev);
463                 GOTO(out, err = -EINVAL);
464         }
465         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
466
467         if (!obd->obd_set_up || obd->obd_stopping) {
468                 CERROR("OBD ioctl: device not setup %d \n", data->ioc_dev);
469                 GOTO(out, err = -EINVAL);
470         }
471
472         switch(cmd) {
473         case OBD_IOC_NO_TRANSNO: {
474                 if (!obd->obd_attached) {
475                         CERROR("Device %d not attached\n", obd->obd_minor);
476                         GOTO(out, err = -ENODEV);
477                 }
478                 CDEBUG(D_HA, "%s: disabling committed-transno notification\n",
479                        obd->obd_name);
480                 obd->obd_no_transno = 1;
481                 GOTO(out, err = 0);
482         }
483
484         default: {
485                 err = obd_iocontrol(cmd, obd->obd_self_export, len, data, NULL);
486                 if (err)
487                         GOTO(out, err);
488
489                 if (copy_to_user((void __user *)arg, data, len))
490                         err = -EFAULT;
491                 GOTO(out, err);
492         }
493         }
494
495 out:
496         OBD_FREE_LARGE(buf, len);
497         RETURN(err);
498 } /* class_handle_ioctl */
499
500 /*  opening /dev/obd */
501 static int obd_class_open(struct inode * inode, struct file * file)
502 {
503         ENTRY;
504         try_module_get(THIS_MODULE);
505         RETURN(0);
506 }
507
508 /*  closing /dev/obd */
509 static int obd_class_release(struct inode * inode, struct file * file)
510 {
511         ENTRY;
512
513         module_put(THIS_MODULE);
514         RETURN(0);
515 }
516
517 /* to control /dev/obd */
518 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
519                             unsigned long arg)
520 {
521         int err = 0;
522
523         ENTRY;
524         /* Allow non-root access for some limited ioctls */
525         if (!cfs_capable(CFS_CAP_SYS_ADMIN))
526                 RETURN(err = -EACCES);
527
528         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
529                 RETURN(err = -ENOTTY);
530
531         err = class_handle_ioctl(cmd, (unsigned long)arg);
532
533         RETURN(err);
534 }
535
536 /* declare character device */
537 static struct file_operations obd_psdev_fops = {
538         .owner          = THIS_MODULE,
539         .unlocked_ioctl = obd_class_ioctl,      /* unlocked_ioctl */
540         .open           = obd_class_open,       /* open */
541         .release        = obd_class_release,    /* release */
542 };
543
544 /* modules setup */
545 struct miscdevice obd_psdev = {
546         .minor  = MISC_DYNAMIC_MINOR,
547         .name   = OBD_DEV_NAME,
548         .fops   = &obd_psdev_fops,
549 };
550
551 static int obd_init_checks(void)
552 {
553         __u64 u64val, div64val;
554         char buf[64];
555         int len, ret = 0;
556
557         CDEBUG(D_INFO, "OBD_OBJECT_EOF = %#llx\n", (__u64)OBD_OBJECT_EOF);
558
559         u64val = OBD_OBJECT_EOF;
560         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
561         if (u64val != OBD_OBJECT_EOF) {
562                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
563                        u64val, (int)sizeof(u64val));
564                 ret = -EINVAL;
565         }
566         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
567         if (len != 18) {
568                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
569                 ret = -EINVAL;
570         }
571
572         div64val = OBD_OBJECT_EOF;
573         CDEBUG(D_INFO, "u64val OBD_OBJECT_EOF = %#llx\n", u64val);
574         if (u64val != OBD_OBJECT_EOF) {
575                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
576                        u64val, (int)sizeof(u64val));
577                 ret = -EOVERFLOW;
578         }
579         if (u64val >> 8 != OBD_OBJECT_EOF >> 8) {
580                 CERROR("__u64 %#llx(%d) != 0xffffffffffffffff\n",
581                        u64val, (int)sizeof(u64val));
582                 return -EOVERFLOW;
583         }
584         if (do_div(div64val, 256) != (u64val & 255)) {
585                 CERROR("do_div(%#llx,256) != %llu\n", u64val, u64val & 255);
586                 return -EOVERFLOW;
587         }
588         if (u64val >> 8 != div64val) {
589                 CERROR("do_div(%#llx,256) %llu != %llu\n",
590                        u64val, div64val, u64val >> 8);
591                 return -EOVERFLOW;
592         }
593         len = snprintf(buf, sizeof(buf), "%#llx", u64val);
594         if (len != 18) {
595                 CWARN("u64 hex wrong length! strlen(%s)=%d != 18\n", buf, len);
596                 ret = -EINVAL;
597         }
598         len = snprintf(buf, sizeof(buf), "%llu", u64val);
599         if (len != 20) {
600                 CWARN("u64 wrong length! strlen(%s)=%d != 20\n", buf, len);
601                 ret = -EINVAL;
602         }
603         len = snprintf(buf, sizeof(buf), "%lld", u64val);
604         if (len != 2) {
605                 CWARN("s64 wrong length! strlen(%s)=%d != 2\n", buf, len);
606                 ret = -EINVAL;
607         }
608         if ((u64val & ~PAGE_MASK) >= PAGE_SIZE) {
609                 CWARN("mask failed: u64val %llu >= %llu\n", u64val,
610                       (__u64)PAGE_SIZE);
611                 ret = -EINVAL;
612         }
613
614         return ret;
615 }
616
617 static int __init obdclass_init(void)
618 {
619         int err;
620
621         LCONSOLE_INFO("Lustre: Build Version: "LUSTRE_VERSION_STRING"\n");
622
623         libcfs_kkuc_init();
624
625         err = obd_init_checks();
626         if (err == -EOVERFLOW)
627                 return err;
628
629 #ifdef CONFIG_PROC_FS
630         obd_memory = lprocfs_alloc_stats(OBD_STATS_NUM,
631                                          LPROCFS_STATS_FLAG_NONE |
632                                          LPROCFS_STATS_FLAG_IRQ_SAFE);
633         if (obd_memory == NULL) {
634                 CERROR("kmalloc of 'obd_memory' failed\n");
635                 return -ENOMEM;
636         }
637
638         lprocfs_counter_init(obd_memory, OBD_MEMORY_STAT,
639                              LPROCFS_CNTR_AVGMINMAX,
640                              "memused", "bytes");
641 #endif
642         err = obd_zombie_impexp_init();
643         if (err)
644                 goto cleanup_obd_memory;
645
646         err = class_handle_init();
647         if (err)
648                 goto cleanup_zombie_impexp;
649
650         err = misc_register(&obd_psdev);
651         if (err) {
652                 CERROR("cannot register OBD miscdevice: err = %d\n", err);
653                 goto cleanup_class_handle;
654         }
655
656         /* Default the dirty page cache cap to 1/2 of system memory.
657          * For clients with less memory, a larger fraction is needed
658          * for other purposes (mostly for BGL). */
659         if (cfs_totalram_pages() <= 512 << (20 - PAGE_SHIFT))
660                 obd_max_dirty_pages = cfs_totalram_pages() / 4;
661         else
662                 obd_max_dirty_pages = cfs_totalram_pages() / 2;
663
664         err = obd_init_caches();
665         if (err)
666                 goto cleanup_deregister;
667
668         err = class_procfs_init();
669         if (err)
670                 goto cleanup_caches;
671
672         err = lu_global_init();
673         if (err)
674                 goto cleanup_class_procfs;
675
676         err = cl_global_init();
677         if (err != 0)
678                 goto cleanup_lu_global;
679
680 #ifdef HAVE_SERVER_SUPPORT
681         err = dt_global_init();
682         if (err != 0)
683                 goto cleanup_cl_global;
684
685         err = lu_ucred_global_init();
686         if (err != 0)
687                 goto cleanup_dt_global;
688 #endif /* HAVE_SERVER_SUPPORT */
689
690         err = llog_info_init();
691         if (err)
692 #ifdef HAVE_SERVER_SUPPORT
693                 goto cleanup_lu_ucred_global;
694 #else /* !HAVE_SERVER_SUPPORT */
695                 goto cleanup_cl_global;
696 #endif /* HAVE_SERVER_SUPPORT */
697
698         err = lustre_register_fs();
699
700         /* simulate a late OOM situation now to require all
701          * alloc'ed/initialized resources to be freed */
702         if (OBD_FAIL_CHECK(OBD_FAIL_OBDCLASS_MODULE_LOAD)) {
703                 /* fake error but filesystem has been registered */
704                 lustre_unregister_fs();
705                 /* force error to ensure module will be unloaded/cleaned */
706                 err = -ENOMEM;
707         }
708
709         if (err)
710                 goto cleanup_llog_info;
711
712         return 0;
713
714 cleanup_llog_info:
715         llog_info_fini();
716
717 #ifdef HAVE_SERVER_SUPPORT
718 cleanup_lu_ucred_global:
719         lu_ucred_global_fini();
720
721 cleanup_dt_global:
722         dt_global_fini();
723 #endif /* HAVE_SERVER_SUPPORT */
724
725 cleanup_cl_global:
726         cl_global_fini();
727
728 cleanup_lu_global:
729         lu_global_fini();
730
731 cleanup_class_procfs:
732         class_procfs_clean();
733
734 cleanup_caches:
735         obd_cleanup_caches();
736
737 cleanup_deregister:
738         misc_deregister(&obd_psdev);
739
740 cleanup_class_handle:
741         class_handle_cleanup();
742
743 cleanup_zombie_impexp:
744         obd_zombie_impexp_stop();
745
746 cleanup_obd_memory:
747 #ifdef CONFIG_PROC_FS
748         lprocfs_free_stats(&obd_memory);
749 #endif
750
751         return err;
752 }
753
754 void obd_update_maxusage(void)
755 {
756         __u64 max;
757
758         max = obd_memory_sum();
759
760         spin_lock(&obd_updatemax_lock);
761         if (max > obd_max_alloc)
762                 obd_max_alloc = max;
763         spin_unlock(&obd_updatemax_lock);
764 }
765 EXPORT_SYMBOL(obd_update_maxusage);
766
767 #ifdef CONFIG_PROC_FS
768 __u64 obd_memory_max(void)
769 {
770         __u64 ret;
771
772         obd_update_maxusage();
773         spin_lock(&obd_updatemax_lock);
774         ret = obd_max_alloc;
775         spin_unlock(&obd_updatemax_lock);
776
777         return ret;
778 }
779 #endif /* CONFIG_PROC_FS */
780
781 static void __exit obdclass_exit(void)
782 {
783 #ifdef CONFIG_PROC_FS
784         __u64 memory_leaked;
785         __u64 memory_max;
786 #endif /* CONFIG_PROC_FS */
787         ENTRY;
788
789         lustre_unregister_fs();
790
791         misc_deregister(&obd_psdev);
792         llog_info_fini();
793 #ifdef HAVE_SERVER_SUPPORT
794         lu_ucred_global_fini();
795         dt_global_fini();
796 #endif /* HAVE_SERVER_SUPPORT */
797         cl_global_fini();
798         lu_global_fini();
799
800         obd_cleanup_caches();
801
802         class_procfs_clean();
803
804         class_handle_cleanup();
805         class_del_uuid(NULL); /* Delete all UUIDs. */
806         obd_zombie_impexp_stop();
807
808 #ifdef CONFIG_PROC_FS
809         memory_leaked = obd_memory_sum();
810         memory_max = obd_memory_max();
811
812         lprocfs_free_stats(&obd_memory);
813         CDEBUG((memory_leaked) ? D_ERROR : D_INFO,
814                "obd_memory max: %llu, leaked: %llu\n",
815                memory_max, memory_leaked);
816 #endif /* CONFIG_PROC_FS */
817
818         EXIT;
819 }
820
821 void obd_heat_clear(struct obd_heat_instance *instance, int count)
822 {
823         ENTRY;
824
825         memset(instance, 0, sizeof(*instance) * count);
826         RETURN_EXIT;
827 }
828 EXPORT_SYMBOL(obd_heat_clear);
829
830 /*
831  * The file heat is calculated for every time interval period I. The access
832  * frequency during each period is counted. The file heat is only recalculated
833  * at the end of a time period.  And a percentage of the former file heat is
834  * lost when recalculated. The recursion formula to calculate the heat of the
835  * file f is as follow:
836  *
837  * Hi+1(f) = (1-P)*Hi(f)+ P*Ci
838  *
839  * Where Hi is the heat value in the period between time points i*I and
840  * (i+1)*I; Ci is the access count in the period; the symbol P refers to the
841  * weight of Ci. The larger the value the value of P is, the more influence Ci
842  * has on the file heat.
843  */
844 void obd_heat_decay(struct obd_heat_instance *instance,  __u64 time_second,
845                     unsigned int weight, unsigned int period_second)
846 {
847         u64 second;
848
849         ENTRY;
850
851         if (instance->ohi_time_second > time_second) {
852                 obd_heat_clear(instance, 1);
853                 RETURN_EXIT;
854         }
855
856         if (instance->ohi_time_second == 0)
857                 RETURN_EXIT;
858
859         for (second = instance->ohi_time_second + period_second;
860              second < time_second;
861              second += period_second) {
862                 instance->ohi_heat = instance->ohi_heat *
863                                 (256 - weight) / 256 +
864                                 instance->ohi_count * weight / 256;
865                 instance->ohi_count = 0;
866                 instance->ohi_time_second = second;
867         }
868         RETURN_EXIT;
869 }
870 EXPORT_SYMBOL(obd_heat_decay);
871
872 __u64 obd_heat_get(struct obd_heat_instance *instance, unsigned int time_second,
873                    unsigned int weight, unsigned int period_second)
874 {
875         ENTRY;
876
877         obd_heat_decay(instance, time_second, weight, period_second);
878
879         if (instance->ohi_count == 0)
880                 RETURN(instance->ohi_heat);
881
882         RETURN(instance->ohi_heat * (256 - weight) / 256 +
883                instance->ohi_count * weight / 256);
884 }
885 EXPORT_SYMBOL(obd_heat_get);
886
887 void obd_heat_add(struct obd_heat_instance *instance,
888                   unsigned int time_second,  __u64 count,
889                   unsigned int weight, unsigned int period_second)
890 {
891         ENTRY;
892
893         obd_heat_decay(instance, time_second, weight, period_second);
894         if (instance->ohi_time_second == 0) {
895                 instance->ohi_time_second = time_second;
896                 instance->ohi_heat = 0;
897                 instance->ohi_count = count;
898         } else {
899                 instance->ohi_count += count;
900         }
901         RETURN_EXIT;
902 }
903 EXPORT_SYMBOL(obd_heat_add);
904
905 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
906 MODULE_DESCRIPTION("Lustre Class Driver");
907 MODULE_VERSION(LUSTRE_VERSION_STRING);
908 MODULE_LICENSE("GPL");
909
910 module_init(obdclass_init);
911 module_exit(obdclass_exit);