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