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