4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * This file is part of Lustre, http://www.lustre.org/
25 * lustre/obdclass/lu_qos.c
28 * These are the only exported functions, they provide some generic
29 * infrastructure for object allocation QoS
33 #define DEBUG_SUBSYSTEM S_CLASS
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>
47 void lu_qos_rr_init(struct lu_qos_rr *lqr)
49 spin_lock_init(&lqr->lqr_alloc);
52 EXPORT_SYMBOL(lu_qos_rr_init);
55 * Add a new target to Quality of Service (QoS) target table.
57 * Add a new MDT/OST target to the structure representing an OSS. Resort the
58 * list of known MDSs/OSSs by the number of MDTs/OSTs attached to each MDS/OSS.
59 * The MDS/OSS list is protected internally and no external locking is required.
61 * \param[in] qos lu_qos data
62 * \param[in] ltd target description
64 * \retval 0 on success
65 * \retval -ENOMEM on error
67 int lqos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
69 struct lu_svr_qos *svr = NULL;
70 struct lu_svr_qos *tempsvr;
71 struct obd_export *exp = ltd->ltd_exp;
78 down_write(&qos->lq_rw_sem);
80 * a bit hacky approach to learn NID of corresponding connection
81 * but there is no official API to access information like this
84 list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
85 if (obd_uuid_equals(&svr->lsq_uuid,
86 &exp->exp_connection->c_remote_uuid)) {
97 GOTO(out, rc = -ENOMEM);
98 memcpy(&svr->lsq_uuid, &exp->exp_connection->c_remote_uuid,
99 sizeof(svr->lsq_uuid));
103 /* Assume we have to move this one */
104 list_del(&svr->lsq_svr_list);
107 svr->lsq_tgt_count++;
108 ltd->ltd_qos.ltq_svr = svr;
110 CDEBUG(D_OTHER, "add tgt %s to server %s (%d targets)\n",
111 obd_uuid2str(<d->ltd_uuid), obd_uuid2str(&svr->lsq_uuid),
115 * Add sorted by # of tgts. Find the first entry that we're
118 list_for_each_entry(tempsvr, &qos->lq_svr_list, lsq_svr_list) {
119 if (svr->lsq_tgt_count > tempsvr->lsq_tgt_count)
123 * ...and add before it. If we're the first or smallest, tempsvr
124 * points to the list head, and we add to the end.
126 list_add_tail(&svr->lsq_svr_list, &tempsvr->lsq_svr_list);
129 qos->lq_rr.lqr_dirty = 1;
132 up_write(&qos->lq_rw_sem);
135 EXPORT_SYMBOL(lqos_add_tgt);
138 * Remove MDT/OST target from QoS table.
140 * Removes given MDT/OST target from QoS table and releases related
141 * MDS/OSS structure if no target remain on the MDS/OSS.
143 * \param[in] qos lu_qos data
144 * \param[in] ltd target description
146 * \retval 0 on success
147 * \retval -ENOENT if no server was found
149 int lqos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
151 struct lu_svr_qos *svr;
156 down_write(&qos->lq_rw_sem);
157 svr = ltd->ltd_qos.ltq_svr;
159 GOTO(out, rc = -ENOENT);
161 svr->lsq_tgt_count--;
162 if (svr->lsq_tgt_count == 0) {
163 CDEBUG(D_OTHER, "removing server %s\n",
164 obd_uuid2str(&svr->lsq_uuid));
165 list_del(&svr->lsq_svr_list);
166 ltd->ltd_qos.ltq_svr = NULL;
171 qos->lq_rr.lqr_dirty = 1;
173 up_write(&qos->lq_rw_sem);
176 EXPORT_SYMBOL(lqos_del_tgt);
179 * lu_prandom_u64_max - returns a pseudo-random u64 number in interval
182 * \param[in] ep_ro right open interval endpoint
184 * \retval a pseudo-random 64-bit number that is in interval [0, ep_ro).
186 u64 lu_prandom_u64_max(u64 ep_ro)
191 #if BITS_PER_LONG == 32
193 * If ep_ro > 32-bit, first generate the high
194 * 32 bits of the random number, then add in the low
195 * 32 bits (truncated to the upper limit, if needed)
197 if (ep_ro > 0xffffffffULL)
198 rand = prandom_u32_max((u32)(ep_ro >> 32)) << 32;
200 if (rand == (ep_ro & 0xffffffff00000000ULL))
201 rand |= prandom_u32_max((u32)ep_ro);
203 rand |= prandom_u32();
205 rand = ((u64)prandom_u32() << 32 | prandom_u32()) % ep_ro;
211 EXPORT_SYMBOL(lu_prandom_u64_max);