Whamcloud - gitweb
0e04bdd9f01dba0cf2cd33a52569dcf79a2597cc
[fs/lustre-release.git] / lustre / obdclass / lu_qos.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_qos.c
26  *
27  * Lustre QoS.
28  * These are the only exported functions, they provide some generic
29  * infrastructure for object allocation QoS
30  *
31  */
32
33 #define DEBUG_SUBSYSTEM S_CLASS
34
35 #include <linux/module.h>
36 #include <linux/list.h>
37 #include <libcfs/libcfs.h>
38 #include <libcfs/libcfs_hash.h> /* hash_long() */
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  * Add a new target to Quality of Service (QoS) target table.
48  *
49  * Add a new MDT/OST target to the structure representing an OSS. Resort the
50  * list of known MDSs/OSSs by the number of MDTs/OSTs attached to each MDS/OSS.
51  * The MDS/OSS list is protected internally and no external locking is required.
52  *
53  * \param[in] qos               lu_qos data
54  * \param[in] ltd               target description
55  *
56  * \retval 0                    on success
57  * \retval -ENOMEM              on error
58  */
59 int lqos_add_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
60 {
61         struct lu_svr_qos *svr = NULL;
62         struct lu_svr_qos *tempsvr;
63         struct obd_export *exp = ltd->ltd_exp;
64         int found = 0;
65         __u32 id = 0;
66         int rc = 0;
67
68         ENTRY;
69
70         down_write(&qos->lq_rw_sem);
71         /*
72          * a bit hacky approach to learn NID of corresponding connection
73          * but there is no official API to access information like this
74          * with OSD API.
75          */
76         list_for_each_entry(svr, &qos->lq_svr_list, lsq_svr_list) {
77                 if (obd_uuid_equals(&svr->lsq_uuid,
78                                     &exp->exp_connection->c_remote_uuid)) {
79                         found++;
80                         break;
81                 }
82                 if (svr->lsq_id > id)
83                         id = svr->lsq_id;
84         }
85
86         if (!found) {
87                 OBD_ALLOC_PTR(svr);
88                 if (!svr)
89                         GOTO(out, rc = -ENOMEM);
90                 memcpy(&svr->lsq_uuid, &exp->exp_connection->c_remote_uuid,
91                        sizeof(svr->lsq_uuid));
92                 ++id;
93                 svr->lsq_id = id;
94         } else {
95                 /* Assume we have to move this one */
96                 list_del(&svr->lsq_svr_list);
97         }
98
99         svr->lsq_tgt_count++;
100         ltd->ltd_qos.ltq_svr = svr;
101
102         CDEBUG(D_OTHER, "add tgt %s to server %s (%d targets)\n",
103                obd_uuid2str(&ltd->ltd_uuid), obd_uuid2str(&svr->lsq_uuid),
104                svr->lsq_tgt_count);
105
106         /*
107          * Add sorted by # of tgts.  Find the first entry that we're
108          * bigger than...
109          */
110         list_for_each_entry(tempsvr, &qos->lq_svr_list, lsq_svr_list) {
111                 if (svr->lsq_tgt_count > tempsvr->lsq_tgt_count)
112                         break;
113         }
114         /*
115          * ...and add before it.  If we're the first or smallest, tempsvr
116          * points to the list head, and we add to the end.
117          */
118         list_add_tail(&svr->lsq_svr_list, &tempsvr->lsq_svr_list);
119
120         qos->lq_dirty = 1;
121         qos->lq_rr.lqr_dirty = 1;
122
123 out:
124         up_write(&qos->lq_rw_sem);
125         RETURN(rc);
126 }
127 EXPORT_SYMBOL(lqos_add_tgt);
128
129 /**
130  * Remove MDT/OST target from QoS table.
131  *
132  * Removes given MDT/OST target from QoS table and releases related
133  * MDS/OSS structure if no target remain on the MDS/OSS.
134  *
135  * \param[in] qos               lu_qos data
136  * \param[in] ltd               target description
137  *
138  * \retval 0                    on success
139  * \retval -ENOENT              if no server was found
140  */
141 int lqos_del_tgt(struct lu_qos *qos, struct lu_tgt_desc *ltd)
142 {
143         struct lu_svr_qos *svr;
144         int rc = 0;
145
146         ENTRY;
147
148         down_write(&qos->lq_rw_sem);
149         svr = ltd->ltd_qos.ltq_svr;
150         if (!svr)
151                 GOTO(out, rc = -ENOENT);
152
153         svr->lsq_tgt_count--;
154         if (svr->lsq_tgt_count == 0) {
155                 CDEBUG(D_OTHER, "removing server %s\n",
156                        obd_uuid2str(&svr->lsq_uuid));
157                 list_del(&svr->lsq_svr_list);
158                 ltd->ltd_qos.ltq_svr = NULL;
159                 OBD_FREE_PTR(svr);
160         }
161
162         qos->lq_dirty = 1;
163         qos->lq_rr.lqr_dirty = 1;
164 out:
165         up_write(&qos->lq_rw_sem);
166         RETURN(rc);
167 }
168 EXPORT_SYMBOL(lqos_del_tgt);