Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdclass / lu_tgt_descs.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  * This file is part of Lustre, http://www.lustre.org/
24  *
25  * lustre/obdclass/lu_tgt_descs.c
26  *
27  * Lustre target descriptions
28  * These are the only exported functions, they provide some generic
29  * infrastructure for target description management used by LOD/LMV
30  *
31  */
32
33 #define DEBUG_SUBSYSTEM S_CLASS
34
35 #include <linux/module.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <libcfs/libcfs.h>
39 #include <libcfs/libcfs_hash.h> /* hash_long() */
40 #include <libcfs/linux/linux-mem.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_disk.h>
44 #include <lustre_fid.h>
45 #include <lu_object.h>
46
47 /**
48  * lu_prandom_u64_max - returns a pseudo-random u64 number in interval
49  * [0, ep_ro)
50  *
51  * \param[in] ep_ro     right open interval endpoint
52  *
53  * \retval a pseudo-random 64-bit number that is in interval [0, ep_ro).
54  */
55 u64 lu_prandom_u64_max(u64 ep_ro)
56 {
57         u64 rand = 0;
58
59         if (ep_ro) {
60 #if BITS_PER_LONG == 32
61                 /*
62                  * If ep_ro > 32-bit, first generate the high
63                  * 32 bits of the random number, then add in the low
64                  * 32 bits (truncated to the upper limit, if needed)
65                  */
66                 if (ep_ro > 0xffffffffULL)
67                         rand = (u64)prandom_u32_max((u32)(ep_ro >> 32)) << 32;
68
69                 if (rand == (ep_ro & 0xffffffff00000000ULL))
70                         rand |= prandom_u32_max((u32)ep_ro);
71                 else
72                         rand |= prandom_u32();
73 #else
74                 rand = ((u64)prandom_u32() << 32 | prandom_u32()) % ep_ro;
75 #endif
76         }
77
78         return rand;
79 }
80 EXPORT_SYMBOL(lu_prandom_u64_max);
81
82 /**
83  * Add a new target to Quality of Service (QoS) target table.
84  *
85  * Add a new MDT/OST target to the structure representing an OSS. Resort the
86  * list of known MDSs/OSSs by the number of MDTs/OSTs attached to each MDS/OSS.
87  * The MDS/OSS list is protected internally and no external locking is required.
88  *
89  * \param[in] qos               lu_qos data
90  * \param[in] tgt               target description
91  *
92  * \retval 0                    on success
93  * \retval -ENOMEM              on error
94  */
95 int lu_qos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *tgt)
96 {
97         struct lu_svr_qos *svr = NULL;
98         struct lu_svr_qos *tempsvr;
99         struct obd_export *exp = tgt->ltd_exp;
100         int found = 0;
101         __u32 id = 0;
102         int rc = 0;
103
104         ENTRY;
105
106         down_write(&qos->lq_rw_sem);
107         /*
108          * a bit hacky approach to learn NID of corresponding connection
109          * but there is no official API to access information like this
110          * with OSD API.
111          */
112         list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
113                 if (obd_uuid_equals(&svr->lsq_uuid,
114                                     &exp->exp_connection->c_remote_uuid)) {
115                         found++;
116                         break;
117                 }
118                 if (svr->lsq_id > id)
119                         id = svr->lsq_id;
120         }
121
122         if (!found) {
123                 OBD_ALLOC_PTR(svr);
124                 if (!svr)
125                         GOTO(out, rc = -ENOMEM);
126                 memcpy(&svr->lsq_uuid, &exp->exp_connection->c_remote_uuid,
127                        sizeof(svr->lsq_uuid));
128                 ++id;
129                 svr->lsq_id = id;
130         } else {
131                 /* Assume we have to move this one */
132                 list_del(&svr->lsq_svr_list);
133         }
134
135         svr->lsq_tgt_count++;
136         tgt->ltd_qos.ltq_svr = svr;
137
138         CDEBUG(D_OTHER, "add tgt %s to server %s (%d targets)\n",
139                obd_uuid2str(&tgt->ltd_uuid), obd_uuid2str(&svr->lsq_uuid),
140                svr->lsq_tgt_count);
141
142         /*
143          * Add sorted by # of tgts.  Find the first entry that we're
144          * bigger than...
145          */
146         list_for_each_entry(tempsvr, &qos->lq_svr_list, lsq_svr_list) {
147                 if (svr->lsq_tgt_count > tempsvr->lsq_tgt_count)
148                         break;
149         }
150         /*
151          * ...and add before it.  If we're the first or smallest, tempsvr
152          * points to the list head, and we add to the end.
153          */
154         list_add_tail(&svr->lsq_svr_list, &tempsvr->lsq_svr_list);
155
156         set_bit(LQ_DIRTY, &qos->lq_flags);
157 #ifdef HAVE_SERVER_SUPPORT
158         set_bit(LQ_DIRTY, &qos->lq_rr.lqr_flags);
159 #endif
160 out:
161         up_write(&qos->lq_rw_sem);
162         RETURN(rc);
163 }
164 EXPORT_SYMBOL(lu_qos_add_tgt);
165
166 /**
167  * Remove MDT/OST target from QoS table.
168  *
169  * Removes given MDT/OST target from QoS table and releases related
170  * MDS/OSS structure if no target remain on the MDS/OSS.
171  *
172  * \param[in] qos               lu_qos data
173  * \param[in] ltd               target description
174  *
175  * \retval 0                    on success
176  * \retval -ENOENT              if no server was found
177  */
178 static int lu_qos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
179 {
180         struct lu_svr_qos *svr;
181         int rc = 0;
182
183         ENTRY;
184
185         down_write(&qos->lq_rw_sem);
186         svr = ltd->ltd_qos.ltq_svr;
187         if (!svr)
188                 GOTO(out, rc = -ENOENT);
189
190         svr->lsq_tgt_count--;
191         if (svr->lsq_tgt_count == 0) {
192                 CDEBUG(D_OTHER, "removing server %s\n",
193                        obd_uuid2str(&svr->lsq_uuid));
194                 list_del(&svr->lsq_svr_list);
195                 ltd->ltd_qos.ltq_svr = NULL;
196                 OBD_FREE_PTR(svr);
197         }
198
199         set_bit(LQ_DIRTY, &qos->lq_flags);
200 #ifdef HAVE_SERVER_SUPPORT
201         set_bit(LQ_DIRTY, &qos->lq_rr.lqr_flags);
202 #endif
203 out:
204         up_write(&qos->lq_rw_sem);
205         RETURN(rc);
206 }
207
208 static inline __u64 tgt_statfs_bavail(struct lu_tgt_desc *tgt)
209 {
210         struct obd_statfs *statfs = &tgt->ltd_statfs;
211
212         return statfs->os_bavail * statfs->os_bsize;
213 }
214
215 static inline __u64 tgt_statfs_iavail(struct lu_tgt_desc *tgt)
216 {
217         return tgt->ltd_statfs.os_ffree;
218 }
219
220 /**
221  * Calculate weight for a given tgt.
222  *
223  * The final tgt weight is bavail >> 16 * iavail >> 8 minus the tgt and server
224  * penalties.  See ltd_qos_penalties_calc() for how penalties are calculated.
225  *
226  * \param[in] tgt       target descriptor
227  */
228 void lu_tgt_qos_weight_calc(struct lu_tgt_desc *tgt)
229 {
230         struct lu_tgt_qos *ltq = &tgt->ltd_qos;
231         __u64 penalty;
232
233         ltq->ltq_avail = (tgt_statfs_bavail(tgt) >> 16) *
234                          (tgt_statfs_iavail(tgt) >> 8);
235         penalty = ltq->ltq_penalty + ltq->ltq_svr->lsq_penalty;
236         if (ltq->ltq_avail < penalty)
237                 ltq->ltq_weight = 0;
238         else
239                 ltq->ltq_weight = ltq->ltq_avail - penalty;
240 }
241 EXPORT_SYMBOL(lu_tgt_qos_weight_calc);
242
243 /**
244  * Allocate and initialize target table.
245  *
246  * A helper function to initialize the target table and allocate
247  * a bitmap of the available targets.
248  *
249  * \param[in] ltd               target's table to initialize
250  * \param[in] is_mdt            target table for MDTs
251  *
252  * \retval 0                    on success
253  * \retval negative             negated errno on error
254  **/
255 int lu_tgt_descs_init(struct lu_tgt_descs *ltd, bool is_mdt)
256 {
257         mutex_init(&ltd->ltd_mutex);
258         init_rwsem(&ltd->ltd_rw_sem);
259
260         /*
261          * the tgt array and bitmap are allocated/grown dynamically as tgts are
262          * added to the LOD/LMV, see lu_tgt_descs_add()
263          */
264         ltd->ltd_tgt_bitmap = bitmap_zalloc(BITS_PER_LONG, GFP_NOFS);
265         if (!ltd->ltd_tgt_bitmap)
266                 return -ENOMEM;
267
268         ltd->ltd_tgts_size  = BITS_PER_LONG;
269         ltd->ltd_death_row = 0;
270         ltd->ltd_refcount  = 0;
271
272         /* Set up allocation policy (QoS and RR) */
273         INIT_LIST_HEAD(&ltd->ltd_qos.lq_svr_list);
274         init_rwsem(&ltd->ltd_qos.lq_rw_sem);
275         set_bit(LQ_DIRTY, &ltd->ltd_qos.lq_flags);
276         set_bit(LQ_RESET, &ltd->ltd_qos.lq_flags);
277         ltd->ltd_is_mdt = is_mdt;
278         /* MDT imbalance threshold is low to balance across MDTs
279          * relatively quickly, because each directory may result
280          * in a large number of files/subdirs created therein.
281          */
282         if (is_mdt) {
283                 ltd->ltd_lmv_desc.ld_pattern = LMV_HASH_TYPE_DEFAULT;
284                 ltd->ltd_qos.lq_prio_free = LMV_QOS_DEF_PRIO_FREE * 256 / 100;
285                 ltd->ltd_qos.lq_threshold_rr =
286                         LMV_QOS_DEF_THRESHOLD_RR_PCT *
287                         QOS_THRESHOLD_MAX / 100;
288         } else {
289                 ltd->ltd_qos.lq_prio_free = LOV_QOS_DEF_PRIO_FREE * 256 / 100;
290                 ltd->ltd_qos.lq_threshold_rr =
291                         LOV_QOS_DEF_THRESHOLD_RR_PCT *
292                         QOS_THRESHOLD_MAX / 100;
293         }
294
295         return 0;
296 }
297 EXPORT_SYMBOL(lu_tgt_descs_init);
298
299 /**
300  * Free bitmap and target table pages.
301  *
302  * \param[in] ltd       target table
303  */
304 void lu_tgt_descs_fini(struct lu_tgt_descs *ltd)
305 {
306         int i;
307
308         bitmap_free(ltd->ltd_tgt_bitmap);
309         for (i = 0; i < ARRAY_SIZE(ltd->ltd_tgt_idx); i++) {
310                 if (ltd->ltd_tgt_idx[i])
311                         OBD_FREE_PTR(ltd->ltd_tgt_idx[i]);
312         }
313         ltd->ltd_tgts_size = 0;
314 }
315 EXPORT_SYMBOL(lu_tgt_descs_fini);
316
317 /**
318  * Expand size of target table.
319  *
320  * When the target table is full, we have to extend the table. To do so,
321  * we allocate new memory with some reserve, move data from the old table
322  * to the new one and release memory consumed by the old table.
323  *
324  * \param[in] ltd               target table
325  * \param[in] newsize           new size of the table
326  *
327  * \retval                      0 on success
328  * \retval                      -ENOMEM if reallocation failed
329  */
330 static int lu_tgt_descs_resize(struct lu_tgt_descs *ltd, __u32 newsize)
331 {
332         unsigned long *new_bitmap, *old_bitmap = NULL;
333
334         /* someone else has already resize the array */
335         if (newsize <= ltd->ltd_tgts_size)
336                 return 0;
337
338         new_bitmap = bitmap_zalloc(newsize, GFP_NOFS);
339         if (!new_bitmap)
340                 return -ENOMEM;
341
342         if (ltd->ltd_tgts_size > 0) {
343                 /* the bitmap already exists, copy data from old one */
344                 bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap,
345                             ltd->ltd_tgts_size);
346                 old_bitmap = ltd->ltd_tgt_bitmap;
347         }
348
349         ltd->ltd_tgts_size  = newsize;
350         ltd->ltd_tgt_bitmap = new_bitmap;
351
352         bitmap_free(old_bitmap);
353
354         CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
355
356         return 0;
357 }
358
359 /**
360  * Add new target to target table.
361  *
362  * Extend target table if it's full, update target table and bitmap.
363  * Notice we need to take ltd_rw_sem exclusively before entry to ensure
364  * atomic switch.
365  *
366  * \param[in] ltd               target table
367  * \param[in] tgt               new target desc
368  *
369  * \retval                      0 on success
370  * \retval                      -ENOMEM if reallocation failed
371  *                              -EEXIST if target existed
372  */
373 int ltd_add_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
374 {
375         __u32 index = tgt->ltd_index;
376         int rc;
377
378         ENTRY;
379
380         if (index >= ltd->ltd_tgts_size) {
381                 __u32 newsize = 1;
382
383                 if (index > TGT_PTRS * TGT_PTRS_PER_BLOCK)
384                         RETURN(-ENFILE);
385
386                 while (newsize < index + 1)
387                         newsize = newsize << 1;
388
389                 rc = lu_tgt_descs_resize(ltd, newsize);
390                 if (rc)
391                         RETURN(rc);
392         } else if (test_bit(index, ltd->ltd_tgt_bitmap)) {
393                 RETURN(-EEXIST);
394         }
395
396         if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
397                 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
398                 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
399                         RETURN(-ENOMEM);
400         }
401
402         LTD_TGT(ltd, tgt->ltd_index) = tgt;
403         set_bit(tgt->ltd_index, ltd->ltd_tgt_bitmap);
404
405         ltd->ltd_lov_desc.ld_tgt_count++;
406         if (tgt->ltd_active)
407                 ltd->ltd_lov_desc.ld_active_tgt_count++;
408
409         RETURN(0);
410 }
411 EXPORT_SYMBOL(ltd_add_tgt);
412
413 /**
414  * Delete target from target table
415  */
416 void ltd_del_tgt(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt)
417 {
418         lu_qos_del_tgt(&ltd->ltd_qos, tgt);
419         LTD_TGT(ltd, tgt->ltd_index) = NULL;
420         clear_bit(tgt->ltd_index, ltd->ltd_tgt_bitmap);
421         ltd->ltd_lov_desc.ld_tgt_count--;
422         if (tgt->ltd_active)
423                 ltd->ltd_lov_desc.ld_active_tgt_count--;
424 }
425 EXPORT_SYMBOL(ltd_del_tgt);
426
427 /**
428  * Calculate penalties per-tgt and per-server
429  *
430  * Re-calculate penalties when the configuration changes, active targets
431  * change and after statfs refresh (all these are reflected by lq_dirty flag).
432  * On every tgt and server: decay the penalty by half for every 8x the update
433  * interval that the device has been idle. That gives lots of time for the
434  * statfs information to be updated (which the penalty is only a proxy for),
435  * and avoids penalizing server/tgt under light load.
436  * See lu_qos_tgt_weight_calc() for how penalties are factored into the weight.
437  *
438  * \param[in] ltd               lu_tgt_descs
439  *
440  * \retval 0            on success
441  * \retval -EAGAIN      the number of tgt isn't enough or all tgt spaces are
442  *                      almost the same
443  */
444 int ltd_qos_penalties_calc(struct lu_tgt_descs *ltd)
445 {
446         struct lu_qos *qos = &ltd->ltd_qos;
447         struct lov_desc *desc = &ltd->ltd_lov_desc;
448         struct lu_tgt_desc *tgt;
449         struct lu_svr_qos *svr;
450         __u64 ba_max, ba_min, ba;
451         __u64 ia_max, ia_min, ia = 1;
452         __u32 num_active;
453         int prio_wide;
454         time64_t now, age;
455         int rc;
456
457         ENTRY;
458
459         if (!test_bit(LQ_DIRTY, &qos->lq_flags))
460                 GOTO(out, rc = 0);
461
462         num_active = desc->ld_active_tgt_count - 1;
463         if (num_active < 1)
464                 GOTO(out, rc = -EAGAIN);
465
466         /* find bavail on each server */
467         list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
468                 svr->lsq_bavail = 0;
469                 /* if inode is not counted, set to 1 to ignore */
470                 svr->lsq_iavail = ltd->ltd_is_mdt ? 0 : 1;
471         }
472         qos->lq_active_svr_count = 0;
473
474         /*
475          * How badly user wants to select targets "widely" (not recently chosen
476          * and not on recent MDS's).  As opposed to "freely" (free space avail.)
477          * 0-256
478          */
479         prio_wide = 256 - qos->lq_prio_free;
480
481         ba_min = (__u64)(-1);
482         ba_max = 0;
483         ia_min = (__u64)(-1);
484         ia_max = 0;
485         now = ktime_get_real_seconds();
486
487         /* Calculate server penalty per object */
488         ltd_foreach_tgt(ltd, tgt) {
489                 if (!tgt->ltd_active)
490                         continue;
491
492                 /* when inode is counted, bavail >> 16 to avoid overflow */
493                 ba = tgt_statfs_bavail(tgt);
494                 if (ltd->ltd_is_mdt)
495                         ba >>= 16;
496                 else
497                         ba >>= 8;
498                 if (!ba)
499                         continue;
500
501                 ba_min = min(ba, ba_min);
502                 ba_max = max(ba, ba_max);
503
504                 /* Count the number of usable servers */
505                 if (tgt->ltd_qos.ltq_svr->lsq_bavail == 0)
506                         qos->lq_active_svr_count++;
507                 tgt->ltd_qos.ltq_svr->lsq_bavail += ba;
508
509                 if (ltd->ltd_is_mdt) {
510                         /* iavail >> 8 to avoid overflow */
511                         ia = tgt_statfs_iavail(tgt) >> 8;
512                         if (!ia)
513                                 continue;
514
515                         ia_min = min(ia, ia_min);
516                         ia_max = max(ia, ia_max);
517
518                         tgt->ltd_qos.ltq_svr->lsq_iavail += ia;
519                 }
520
521                 /*
522                  * per-tgt penalty is
523                  * prio * bavail * iavail / (num_tgt - 1) / 2
524                  */
525                 tgt->ltd_qos.ltq_penalty_per_obj = prio_wide * ba * ia >> 8;
526                 do_div(tgt->ltd_qos.ltq_penalty_per_obj, num_active);
527                 tgt->ltd_qos.ltq_penalty_per_obj >>= 1;
528
529                 age = (now - tgt->ltd_qos.ltq_used) >> 3;
530                 if (test_bit(LQ_RESET, &qos->lq_flags) || 
531                     age > 32 * desc->ld_qos_maxage)
532                         tgt->ltd_qos.ltq_penalty = 0;
533                 else if (age > desc->ld_qos_maxage)
534                         /* Decay tgt penalty. */
535                         tgt->ltd_qos.ltq_penalty >>= age / desc->ld_qos_maxage;
536         }
537
538         num_active = qos->lq_active_svr_count - 1;
539         if (num_active < 1) {
540                 /*
541                  * If there's only 1 server, we can't penalize it, so instead
542                  * we have to double the tgt penalty
543                  */
544                 num_active = 1;
545                 ltd_foreach_tgt(ltd, tgt) {
546                         if (!tgt->ltd_active)
547                                 continue;
548
549                         tgt->ltd_qos.ltq_penalty_per_obj <<= 1;
550                 }
551         }
552
553         /*
554          * Per-server penalty is
555          * prio * bavail * iavail / server_tgts / (num_svr - 1) / 2
556          */
557         list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
558                 ba = svr->lsq_bavail;
559                 ia = svr->lsq_iavail;
560                 svr->lsq_penalty_per_obj = prio_wide * ba  * ia >> 8;
561                 do_div(svr->lsq_penalty_per_obj,
562                        svr->lsq_tgt_count * num_active);
563                 svr->lsq_penalty_per_obj >>= 1;
564
565                 age = (now - svr->lsq_used) >> 3;
566                 if (test_bit(LQ_RESET, &qos->lq_flags) || 
567                     age > 32 * desc->ld_qos_maxage)
568                         svr->lsq_penalty = 0;
569                 else if (age > desc->ld_qos_maxage)
570                         /* Decay server penalty. */
571                         svr->lsq_penalty >>= age / desc->ld_qos_maxage;
572         }
573
574         clear_bit(LQ_DIRTY, &qos->lq_flags);
575         clear_bit(LQ_RESET, &qos->lq_flags);
576
577         /*
578          * If each tgt has almost same free space, do rr allocation for better
579          * creation performance
580          */
581         clear_bit(LQ_SAME_SPACE, &qos->lq_flags);
582         if (((ba_max * (QOS_THRESHOLD_MAX - qos->lq_threshold_rr)) /
583             QOS_THRESHOLD_MAX) < ba_min &&
584             ((ia_max * (QOS_THRESHOLD_MAX - qos->lq_threshold_rr)) /
585             QOS_THRESHOLD_MAX) < ia_min) {
586                 set_bit(LQ_SAME_SPACE, &qos->lq_flags);
587                 /* Reset weights for the next time we enter qos mode */
588                 set_bit(LQ_RESET, &qos->lq_flags);
589         }
590         rc = 0;
591
592 out:
593         if (!rc && test_bit(LQ_SAME_SPACE, &qos->lq_flags))
594                 RETURN(-EAGAIN);
595
596         RETURN(rc);
597 }
598 EXPORT_SYMBOL(ltd_qos_penalties_calc);
599
600 /**
601  * Re-calculate penalties and weights of all tgts.
602  *
603  * The function is called when some target was used for a new object. In
604  * this case we should re-calculate all the weights to keep new allocations
605  * balanced well.
606  *
607  * \param[in] ltd               lu_tgt_descs
608  * \param[in] tgt               recently used tgt
609  * \param[out] total_wt         new total weight for the pool
610  *
611  * \retval              0
612  */
613 int ltd_qos_update(struct lu_tgt_descs *ltd, struct lu_tgt_desc *tgt,
614                    __u64 *total_wt)
615 {
616         struct lu_qos *qos = &ltd->ltd_qos;
617         struct lu_tgt_qos *ltq;
618         struct lu_svr_qos *svr;
619
620         ENTRY;
621
622         ltq = &tgt->ltd_qos;
623         LASSERT(ltq);
624
625         /* Don't allocate on this device anymore, until the next alloc_qos */
626         ltq->ltq_usable = 0;
627
628         svr = ltq->ltq_svr;
629
630         /*
631          * Decay old penalty by half (we're adding max penalty, and don't
632          * want it to run away.)
633          */
634         ltq->ltq_penalty >>= 1;
635         svr->lsq_penalty >>= 1;
636
637         /* mark the server and tgt as recently used */
638         ltq->ltq_used = svr->lsq_used = ktime_get_real_seconds();
639
640         /* Set max penalties for this tgt and server */
641         ltq->ltq_penalty += ltq->ltq_penalty_per_obj *
642                             ltd->ltd_lov_desc.ld_active_tgt_count;
643         svr->lsq_penalty += svr->lsq_penalty_per_obj *
644                             qos->lq_active_svr_count;
645
646         /* Decrease all MDS penalties */
647         list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
648                 if (svr->lsq_penalty < svr->lsq_penalty_per_obj)
649                         svr->lsq_penalty = 0;
650                 else
651                         svr->lsq_penalty -= svr->lsq_penalty_per_obj;
652         }
653
654         *total_wt = 0;
655         /* Decrease all tgt penalties */
656         ltd_foreach_tgt(ltd, tgt) {
657                 if (!tgt->ltd_active)
658                         continue;
659
660                 ltq = &tgt->ltd_qos;
661                 if (ltq->ltq_penalty < ltq->ltq_penalty_per_obj)
662                         ltq->ltq_penalty = 0;
663                 else
664                         ltq->ltq_penalty -= ltq->ltq_penalty_per_obj;
665
666                 lu_tgt_qos_weight_calc(tgt);
667
668                 /* Recalc the total weight of usable osts */
669                 if (ltq->ltq_usable)
670                         *total_wt += ltq->ltq_weight;
671
672                 CDEBUG(D_OTHER, "recalc tgt %d usable=%d bavail=%llu ffree=%llu tgtppo=%llu tgtp=%llu svrppo=%llu svrp=%llu wt=%llu\n",
673                           tgt->ltd_index, ltq->ltq_usable,
674                           tgt_statfs_bavail(tgt) >> 16,
675                           tgt_statfs_iavail(tgt) >> 8,
676                           ltq->ltq_penalty_per_obj >> 10,
677                           ltq->ltq_penalty >> 10,
678                           ltq->ltq_svr->lsq_penalty_per_obj >> 10,
679                           ltq->ltq_svr->lsq_penalty >> 10,
680                           ltq->ltq_weight >> 10);
681         }
682
683         RETURN(0);
684 }
685 EXPORT_SYMBOL(ltd_qos_update);