Whamcloud - gitweb
LU-657 test: limit the write size in run_dd
[fs/lustre-release.git] / lustre / quota / qsd_lib.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 /*
32  * Quota Slave Driver (QSD) management.
33  *
34  * The quota slave feature is implemented under the form of a library called
35  * QSD. Each OSD device should create a QSD instance via qsd_init() which will
36  * be used to manage quota enforcement for this device. This implies:
37  * - completing the reintegration procedure with the quota master (aka QMT, see
38  *   qmt_dev.c) to retrieve the latest quota settings and space distribution.
39  * - managing quota locks in order to be notified of configuration changes.
40  * - acquiring space from the QMT when quota space for a given user/group is
41  *   close to exhaustion.
42  * - allocating quota space to service threads for local request processing.
43  *
44  * Once the QSD instance created, the OSD device should invoke qsd_start()
45  * when recovery is completed. This notifies the QSD that we are about to
46  * process new requests on which quota should be strictly enforced.
47  * Then, qsd_op_begin/end can be used to reserve/release/pre-acquire quota space
48  * for/after each operation until shutdown where the QSD instance should be
49  * freed via qsd_fini().
50  */
51
52 #ifndef EXPORT_SYMTAB
53 # define EXPORT_SYMTAB
54 #endif
55
56 #define DEBUG_SUBSYSTEM S_LQUOTA
57
58 #include <obd_class.h>
59 #include "qsd_internal.h"
60
61 cfs_mem_cache_t *upd_kmem;
62
63 struct lu_kmem_descr qsd_caches[] = {
64         {
65                 .ckd_cache = &upd_kmem,
66                 .ckd_name  = "upd_kmem",
67                 .ckd_size  = sizeof(struct qsd_upd_rec)
68         },
69         {
70                 .ckd_cache = NULL
71         }
72 };
73
74 /* define qsd thread key */
75 LU_KEY_INIT_FINI(qsd, struct qsd_thread_info);
76 LU_CONTEXT_KEY_DEFINE(qsd, LCT_MD_THREAD | LCT_DT_THREAD | LCT_LOCAL);
77 LU_KEY_INIT_GENERIC(qsd);
78
79 /* some procfs helpers */
80 static int lprocfs_qsd_rd_state(char *page, char **start, off_t off,
81                                 int count, int *eof, void *data)
82 {
83         struct qsd_instance     *qsd = (struct qsd_instance *)data;
84         char                     enabled[5];
85         int                      rc;
86
87         LASSERT(qsd != NULL);
88
89         memset(enabled, 0, sizeof(enabled));
90         if (qsd_type_enabled(qsd, USRQUOTA))
91                 strcat(enabled, "u");
92         if (qsd_type_enabled(qsd, GRPQUOTA))
93                 strcat(enabled, "g");
94         if (strlen(enabled) == 0)
95                 strcat(enabled, "none");
96
97         rc = snprintf(page, count,
98                       "target name:    %s\n"
99                       "pool ID:        %d\n"
100                       "type:           %s\n"
101                       "quota enabled:  %s\n"
102                       "conn to master: %s\n",
103                       qsd->qsd_svname, qsd->qsd_pool_id,
104                       qsd->qsd_is_md ? "md" : "dt", enabled,
105                       qsd->qsd_exp_valid ? "setup" : "not setup yet");
106
107         if (qsd->qsd_prepared) {
108                 memset(enabled, 0, sizeof(enabled));
109                 if (qsd->qsd_type_array[USRQUOTA]->qqi_acct_obj != NULL)
110                         strcat(enabled, "u");
111                 if (qsd->qsd_type_array[GRPQUOTA]->qqi_acct_obj != NULL)
112                         strcat(enabled, "g");
113                 if (strlen(enabled) == 0)
114                         strcat(enabled, "none");
115                 rc +=  snprintf(page + rc, count - rc,
116                                 "space acct:     %s\n"
117                                 "user uptodate:  glb[%d],slv[%d],reint[%d]\n"
118                                 "group uptodate: glb[%d],slv[%d],reint[%d]\n",
119                                 enabled,
120                                 qsd->qsd_type_array[USRQUOTA]->qqi_glb_uptodate,
121                                 qsd->qsd_type_array[USRQUOTA]->qqi_slv_uptodate,
122                                 qsd->qsd_type_array[USRQUOTA]->qqi_reint,
123                                 qsd->qsd_type_array[GRPQUOTA]->qqi_glb_uptodate,
124                                 qsd->qsd_type_array[GRPQUOTA]->qqi_slv_uptodate,
125                                 qsd->qsd_type_array[GRPQUOTA]->qqi_reint);
126         }
127         return rc;
128 }
129
130 static int lprocfs_qsd_rd_enabled(char *page, char **start, off_t off,
131                                   int count, int *eof, void *data)
132 {
133         struct qsd_instance     *qsd = (struct qsd_instance *)data;
134         char                     enabled[5];
135
136         LASSERT(qsd != NULL);
137
138         memset(enabled, 0, sizeof(enabled));
139         if (qsd_type_enabled(qsd, USRQUOTA))
140                 strcat(enabled, "u");
141         if (qsd_type_enabled(qsd, GRPQUOTA))
142                 strcat(enabled, "g");
143         if (strlen(enabled) == 0)
144                 strcat(enabled, "none");
145
146         return snprintf(page, count, "%s\n", enabled);
147 }
148
149 /* force reintegration procedure to be executed.
150  * Used for test/debugging purpose */
151 static int lprocfs_qsd_wr_force_reint(struct file *file, const char *buffer,
152                                       unsigned long count, void *data)
153 {
154         struct qsd_instance     *qsd = (struct qsd_instance *)data;
155         int                      rc = 0, qtype;
156
157         LASSERT(qsd != NULL);
158
159         write_lock(&qsd->qsd_lock);
160         if (qsd->qsd_stopping) {
161                 /* don't mess up with shutdown procedure, it is already
162                  * complicated enough */
163                 rc = -ESHUTDOWN;
164         } else if (!qsd->qsd_prepared) {
165                 rc = -EAGAIN;
166         } else {
167                 /* mark all indexes as stale */
168                 for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
169                         qsd->qsd_type_array[qtype]->qqi_glb_uptodate = false;
170                         qsd->qsd_type_array[qtype]->qqi_slv_uptodate = false;
171                 }
172         }
173         write_unlock(&qsd->qsd_lock);
174
175         if (rc)
176                 return rc;
177
178         /* kick off reintegration */
179         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
180                 rc = qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
181                 if (rc)
182                         break;
183         }
184         return rc == 0 ? count : rc;
185 }
186
187 static int lprocfs_qsd_rd_timeout(char *page, char **start, off_t off,
188                                   int count, int *eof, void *data)
189 {
190         struct qsd_instance     *qsd = (struct qsd_instance *)data;
191         LASSERT(qsd != NULL);
192
193         return snprintf(page, count, "%d\n", qsd_wait_timeout(qsd));
194 }
195
196 static int lprocfs_qsd_wr_timeout(struct file *file, const char *buffer,
197                                   unsigned long count, void *data)
198 {
199         struct qsd_instance     *qsd = (struct qsd_instance *)data;
200         int                      timeout, rc;
201         LASSERT(qsd != NULL);
202
203         rc = lprocfs_write_helper(buffer, count, &timeout);
204         if (rc)
205                 return rc;
206         if (timeout < 0)
207                 return -EINVAL;
208
209         qsd->qsd_timeout = timeout;
210         return count;
211 }
212
213 static struct lprocfs_vars lprocfs_quota_qsd_vars[] = {
214         { "info", lprocfs_qsd_rd_state, 0, 0},
215         { "enabled", lprocfs_qsd_rd_enabled, 0, 0},
216         { "force_reint", 0, lprocfs_qsd_wr_force_reint, 0},
217         { "timeout", lprocfs_qsd_rd_timeout, lprocfs_qsd_wr_timeout, 0},
218         { NULL }
219 };
220
221 /*
222  * Callback function invoked by the OSP layer when the connection to the master
223  * has been set up.
224  *
225  * \param data - is a pointer to the qsd_instance
226  *
227  * \retval - 0 on success, appropriate error on failure
228  */
229 static int qsd_conn_callback(void *data)
230 {
231         struct qsd_instance *qsd = (struct qsd_instance *)data;
232         int                  type;
233         ENTRY;
234
235         /* qsd_exp should now be valid */
236         LASSERT(qsd->qsd_exp);
237
238         /* grab reference on namespace */
239         ldlm_namespace_get(class_exp2obd(qsd->qsd_exp)->obd_namespace);
240         qsd->qsd_ns = class_exp2obd(qsd->qsd_exp)->obd_namespace;
241
242         write_lock(&qsd->qsd_lock);
243         /* notify that qsd_exp is now valid */
244         qsd->qsd_exp_valid = true;
245         write_unlock(&qsd->qsd_lock);
246
247         /* Now that the connection to master is setup, we can initiate the
248          * reintegration procedure for quota types which are enabled.
249          * It is worth noting that, if the qsd_instance hasn't been started
250          * already, then we can only complete the first two steps of the
251          * reintegration procedure (i.e. global lock enqueue and slave
252          * index transfer) since the space usage reconciliation (i.e.
253          * step 3) will have to wait for qsd_start() to be called */
254         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
255                 struct qsd_qtype_info *qqi = qsd->qsd_type_array[type];
256                 cfs_waitq_signal(&qqi->qqi_reint_thread.t_ctl_waitq);
257         }
258
259         RETURN(0);
260 }
261
262 /*
263  * Release qsd_qtype_info structure which contains data associated with a
264  * given quota type. This releases the accounting objects.
265  * It's called on OSD cleanup when the qsd instance is released.
266  *
267  * \param env - is the environment passed by the caller
268  * \param qsd - is the qsd instance managing the qsd_qtype_info structure
269  *              to be released
270  * \param qtype - is the quota type to be shutdown
271  */
272 static void qsd_qtype_fini(const struct lu_env *env, struct qsd_instance *qsd,
273                            int qtype)
274 {
275         struct qsd_qtype_info   *qqi;
276         ENTRY;
277
278         if (qsd->qsd_type_array[qtype] == NULL)
279                 RETURN_EXIT;
280         qqi = qsd->qsd_type_array[qtype];
281         qsd->qsd_type_array[qtype] = NULL;
282
283         /* all deferred work lists should be empty */
284         LASSERT(cfs_list_empty(&qqi->qqi_deferred_glb));
285         LASSERT(cfs_list_empty(&qqi->qqi_deferred_slv));
286
287         /* shutdown lquota site */
288         if (qqi->qqi_site != NULL && !IS_ERR(qqi->qqi_site)) {
289                 lquota_site_free(env, qqi->qqi_site);
290                 qqi->qqi_site = NULL;
291         }
292
293         /* by now, all qqi users should have gone away */
294         LASSERT(cfs_atomic_read(&qqi->qqi_ref) == 1);
295         lu_ref_fini(&qqi->qqi_reference);
296
297         /* release accounting object */
298         if (qqi->qqi_acct_obj != NULL && !IS_ERR(qqi->qqi_acct_obj)) {
299                 lu_object_put(env, &qqi->qqi_acct_obj->do_lu);
300                 qqi->qqi_acct_obj = NULL;
301         }
302
303         /* release slv index */
304         if (qqi->qqi_slv_obj != NULL && !IS_ERR(qqi->qqi_slv_obj)) {
305                 lu_object_put(env, &qqi->qqi_slv_obj->do_lu);
306                 qqi->qqi_slv_obj = NULL;
307                 qqi->qqi_slv_ver = 0;
308         }
309
310         /* release global index */
311         if (qqi->qqi_glb_obj != NULL && !IS_ERR(qqi->qqi_glb_obj)) {
312                 lu_object_put(env, &qqi->qqi_glb_obj->do_lu);
313                 qqi->qqi_glb_obj = NULL;
314                 qqi->qqi_glb_ver = 0;
315         }
316
317         OBD_FREE_PTR(qqi);
318         EXIT;
319 }
320
321 /*
322  * Allocate and initialize a qsd_qtype_info structure for quota type \qtype.
323  * This opens the accounting object and initializes the proc file.
324  * It's called on OSD start when the qsd_prepare() is invoked on the qsd
325  * instance.
326  *
327  * \param env  - the environment passed by the caller
328  * \param qsd  - is the qsd instance which will be in charge of the new
329  *               qsd_qtype_info instance.
330  * \param qtype - is quota type to set up
331  *
332  * \retval - 0 on success and qsd->qsd_type_array[qtype] is allocated,
333  *           appropriate error on failure
334  */
335 static int qsd_qtype_init(const struct lu_env *env, struct qsd_instance *qsd,
336                           int qtype)
337 {
338         struct qsd_qtype_info   *qqi;
339         int                      rc;
340         struct obd_uuid          uuid;
341         ENTRY;
342
343         LASSERT(qsd->qsd_type_array[qtype] == NULL);
344
345         /* allocate structure for this quota type */
346         OBD_ALLOC_PTR(qqi);
347         if (qqi == NULL)
348                 RETURN(-ENOMEM);
349         qsd->qsd_type_array[qtype] = qqi;
350         cfs_atomic_set(&qqi->qqi_ref, 1); /* referenced from qsd */
351
352         /* set backpointer and other parameters */
353         qqi->qqi_qsd   = qsd;
354         qqi->qqi_qtype = qtype;
355         lu_ref_init(&qqi->qqi_reference);
356         lquota_generate_fid(&qqi->qqi_fid, qsd->qsd_pool_id, QSD_RES_TYPE(qsd),
357                             qtype);
358         qqi->qqi_glb_uptodate = false;
359         qqi->qqi_slv_uptodate = false;
360         qqi->qqi_reint        = false;
361         cfs_waitq_init(&qqi->qqi_reint_thread.t_ctl_waitq);
362         thread_set_flags(&qqi->qqi_reint_thread, SVC_STOPPED);
363         CFS_INIT_LIST_HEAD(&qqi->qqi_deferred_glb);
364         CFS_INIT_LIST_HEAD(&qqi->qqi_deferred_slv);
365         memset(&qqi->qqi_lockh, 0, sizeof(qqi->qqi_lockh));
366
367         /* open accounting object */
368         LASSERT(qqi->qqi_acct_obj == NULL);
369         qqi->qqi_acct_obj = acct_obj_lookup(env, qsd->qsd_dev, qtype);
370         if (IS_ERR(qqi->qqi_acct_obj)) {
371                 LCONSOLE_WARN("%s: No %s space accounting support. Please "
372                               "consider running tunefs.lustre --quota on an "
373                               "unmounted filesystem to enable quota accounting."
374                               "\n", qsd->qsd_svname,
375                               QTYPE_NAME(qtype));
376                 qqi->qqi_acct_obj = NULL;
377                 qsd->qsd_acct_failed = true;
378         }
379
380         /* open global index copy */
381         LASSERT(qqi->qqi_glb_obj == NULL);
382         qqi->qqi_glb_obj = lquota_disk_glb_find_create(env, qsd->qsd_dev,
383                                                        qsd->qsd_root,
384                                                        &qqi->qqi_fid, true);
385         if (IS_ERR(qqi->qqi_glb_obj)) {
386                 CERROR("%s: can't open global index copy "DFID" %ld\n",
387                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
388                        PTR_ERR(qqi->qqi_glb_obj));
389                 GOTO(out, rc = PTR_ERR(qqi->qqi_glb_obj));
390         }
391         qqi->qqi_glb_ver = dt_version_get(env, qqi->qqi_glb_obj);
392
393         /* open slave index copy */
394         LASSERT(qqi->qqi_slv_obj == NULL);
395         obd_str2uuid(&uuid, qsd->qsd_svname);
396         qqi->qqi_slv_obj = lquota_disk_slv_find_create(env, qsd->qsd_dev,
397                                                        qsd->qsd_root,
398                                                        &qqi->qqi_fid, &uuid,
399                                                        true);
400         if (IS_ERR(qqi->qqi_slv_obj)) {
401                 CERROR("%s: can't open slave index copy "DFID" %ld\n",
402                        qsd->qsd_svname, PFID(&qqi->qqi_fid),
403                        PTR_ERR(qqi->qqi_slv_obj));
404                 GOTO(out, rc = PTR_ERR(qqi->qqi_slv_obj));
405         }
406         qqi->qqi_slv_ver = dt_version_get(env, qqi->qqi_slv_obj);
407
408         /* allocate site */
409         qqi->qqi_site = lquota_site_alloc(env, qqi, false, qtype, &qsd_lqe_ops);
410         if (IS_ERR(qqi->qqi_site)) {
411                 CERROR("%s: can't allocate site "DFID" %ld\n", qsd->qsd_svname,
412                        PFID(&qqi->qqi_fid), PTR_ERR(qqi->qqi_site));
413                 GOTO(out, rc = PTR_ERR(qqi->qqi_site));
414         }
415
416         /* register proc entry for accounting & global index copy objects */
417         rc = lprocfs_seq_create(qsd->qsd_proc,
418                                 qtype == USRQUOTA ? "acct_user" : "acct_group",
419                                 0444, &lprocfs_quota_seq_fops,
420                                 qqi->qqi_acct_obj);
421         if (rc) {
422                 CERROR("%s: can't add procfs entry for accounting file %d\n",
423                        qsd->qsd_svname, rc);
424                 GOTO(out, rc);
425         }
426
427         rc = lprocfs_seq_create(qsd->qsd_proc,
428                                 qtype == USRQUOTA ? "limit_user" : "limit_group",
429                                 0444, &lprocfs_quota_seq_fops,
430                                 qqi->qqi_glb_obj);
431         if (rc) {
432                 CERROR("%s: can't add procfs entry for global index copy %d\n",
433                        qsd->qsd_svname, rc);
434                 GOTO(out, rc);
435         }
436         EXIT;
437 out:
438         if (rc)
439                 qsd_qtype_fini(env, qsd, qtype);
440         return rc;
441 }
442
443 /*
444  * Release a qsd_instance. Companion of qsd_init(). This releases all data
445  * structures associated with the quota slave (on-disk objects, lquota entry
446  * tables, ...).
447  * This function should be called when the OSD is shutting down.
448  *
449  * \param env - is the environment passed by the caller
450  * \param qsd - is the qsd instance to shutdown
451  */
452 void qsd_fini(const struct lu_env *env, struct qsd_instance *qsd)
453 {
454         int     qtype;
455         ENTRY;
456
457         if (unlikely(qsd == NULL))
458                 RETURN_EXIT;
459
460         CDEBUG(D_QUOTA, "%s: initiating QSD shutdown\n", qsd->qsd_svname);
461         write_lock(&qsd->qsd_lock);
462         qsd->qsd_stopping = true;
463         write_unlock(&qsd->qsd_lock);
464
465         /* remove from the list of fsinfo */
466         if (!cfs_list_empty(&qsd->qsd_link)) {
467                 LASSERT(qsd->qsd_fsinfo != NULL);
468                 down(&qsd->qsd_fsinfo->qfs_sem);
469                 cfs_list_del_init(&qsd->qsd_link);
470                 up(&qsd->qsd_fsinfo->qfs_sem);
471         }
472
473         /* remove qsd proc entry */
474         if (qsd->qsd_proc != NULL) {
475                 lprocfs_remove(&qsd->qsd_proc);
476                 qsd->qsd_proc = NULL;
477         }
478
479         /* stop the writeback thread */
480         qsd_stop_upd_thread(qsd);
481
482         /* shutdown the reintegration threads */
483         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
484                 if (qsd->qsd_type_array[qtype] == NULL)
485                         continue;
486                 qsd_stop_reint_thread(qsd->qsd_type_array[qtype]);
487         }
488
489         /* release reference on namespace */
490         if (qsd->qsd_ns != NULL) {
491                 ldlm_namespace_put(qsd->qsd_ns);
492                 qsd->qsd_ns = NULL;
493         }
494
495         /* free per-quota type data */
496         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++)
497                 qsd_qtype_fini(env, qsd, qtype);
498
499         /* deregister connection to the quota master */
500         qsd->qsd_exp_valid = false;
501         lustre_deregister_osp_item(&qsd->qsd_exp);
502
503         /* release per-filesystem information */
504         if (qsd->qsd_fsinfo != NULL)
505                 qsd_put_fsinfo(qsd->qsd_fsinfo);
506
507         /* release quota root directory */
508         if (qsd->qsd_root != NULL) {
509                 lu_object_put(env, &qsd->qsd_root->do_lu);
510                 qsd->qsd_root = NULL;
511         }
512
513         /* release reference on dt_device */
514         if (qsd->qsd_dev != NULL) {
515                 lu_ref_del(&qsd->qsd_dev->dd_lu_dev.ld_reference, "qsd", qsd);
516                 lu_device_put(&qsd->qsd_dev->dd_lu_dev);
517                 qsd->qsd_dev = NULL;
518         }
519
520         CDEBUG(D_QUOTA, "%s: QSD shutdown completed\n", qsd->qsd_svname);
521         OBD_FREE_PTR(qsd);
522         EXIT;
523 }
524 EXPORT_SYMBOL(qsd_fini);
525
526 /*
527  * Create a new qsd_instance to be associated with backend osd device
528  * identified by \dev.
529  *
530  * \param env    - the environment passed by the caller
531  * \param svname - is the service name of the OSD device creating this instance
532  * \param dev    - is the dt_device where to store quota index files
533  * \param osd_proc - is the procfs parent directory where to create procfs file
534  *                   related to this new qsd instance
535  *
536  * \retval - pointer to new qsd_instance associated with dev \dev on success,
537  *           appropriate error on failure
538  */
539 struct qsd_instance *qsd_init(const struct lu_env *env, char *svname,
540                               struct dt_device *dev,
541                               cfs_proc_dir_entry_t *osd_proc)
542 {
543         struct qsd_thread_info  *qti = qsd_info(env);
544         struct qsd_instance     *qsd;
545         int                      rc, type, idx;
546         ENTRY;
547
548         /* only configure qsd for MDT & OST */
549         type = server_name2index(svname, &idx, NULL);
550         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST)
551                 RETURN(NULL);
552
553         /* allocate qsd instance */
554         OBD_ALLOC_PTR(qsd);
555         if (qsd == NULL)
556                 RETURN(ERR_PTR(-ENOMEM));
557
558         /* generic initializations */
559         rwlock_init(&qsd->qsd_lock);
560         CFS_INIT_LIST_HEAD(&qsd->qsd_link);
561         thread_set_flags(&qsd->qsd_upd_thread, SVC_STOPPED);
562         cfs_waitq_init(&qsd->qsd_upd_thread.t_ctl_waitq);
563         CFS_INIT_LIST_HEAD(&qsd->qsd_upd_list);
564         spin_lock_init(&qsd->qsd_adjust_lock);
565         CFS_INIT_LIST_HEAD(&qsd->qsd_adjust_list);
566         qsd->qsd_prepared = false;
567         qsd->qsd_started = false;
568
569         /* copy service name */
570         strncpy(qsd->qsd_svname, svname, MAX_OBD_NAME);
571
572         /* grab reference on osd device */
573         lu_device_get(&dev->dd_lu_dev);
574         lu_ref_add(&dev->dd_lu_dev.ld_reference, "qsd", qsd);
575         qsd->qsd_dev = dev;
576
577         /* we only support pool ID 0 (default data or metadata pool) for the
578          * time being. A different pool ID could be assigned to this target via
579          * the configuration log in the future */
580         qsd->qsd_pool_id  = 0;
581
582         /* get fsname from svname */
583         rc = server_name2fsname(svname, qti->qti_buf, NULL);
584         if (rc) {
585                 CERROR("%s: fail to extract filesystem name\n", svname);
586                 GOTO(out, rc);
587         }
588
589         /* look up quota setting for the filesystem the target belongs to */
590         qsd->qsd_fsinfo = qsd_get_fsinfo(qti->qti_buf, 1);
591         if (qsd->qsd_fsinfo == NULL) {
592                 CERROR("%s: failed to locate filesystem information\n", svname);
593                 GOTO(out, rc = -EINVAL);
594         }
595
596         /* add in the list of lquota_fsinfo */
597         down(&qsd->qsd_fsinfo->qfs_sem);
598         list_add_tail(&qsd->qsd_link, &qsd->qsd_fsinfo->qfs_qsd_list);
599         up(&qsd->qsd_fsinfo->qfs_sem);
600
601         /* register procfs directory */
602         qsd->qsd_proc = lprocfs_register(QSD_DIR, osd_proc,
603                                          lprocfs_quota_qsd_vars, qsd);
604         if (IS_ERR(qsd->qsd_proc)) {
605                 rc = PTR_ERR(qsd->qsd_proc);
606                 qsd->qsd_proc = NULL;
607                 CERROR("%s: fail to create quota slave proc entry (%d)\n",
608                        svname, rc);
609                 GOTO(out, rc);
610         }
611         EXIT;
612 out:
613         if (rc) {
614                 qsd_fini(env, qsd);
615                 return ERR_PTR(rc);
616         }
617         RETURN(qsd);
618 }
619 EXPORT_SYMBOL(qsd_init);
620
621 /*
622  * Initialize on-disk structures in order to manage quota enforcement for
623  * the target associated with the qsd instance \qsd and starts the reintegration
624  * procedure for each quota type as soon as possible.
625  * The last step of the reintegration will be completed once qsd_start() is
626  * called, at which points the space reconciliation with the master will be
627  * executed.
628  * This function must be called when the server stack is fully configured,
629  * typically when ->ldo_prepare is called across the stack.
630  *
631  * \param env - the environment passed by the caller
632  * \param qsd - is qsd_instance to prepare
633  *
634  * \retval - 0 on success, appropriate error on failure
635  */
636 int qsd_prepare(const struct lu_env *env, struct qsd_instance *qsd)
637 {
638         struct qsd_thread_info  *qti = qsd_info(env);
639         int                      qtype, rc = 0;
640         ENTRY;
641
642         if (unlikely(qsd == NULL))
643                 RETURN(0);
644
645         read_lock(&qsd->qsd_lock);
646         if (qsd->qsd_prepared) {
647                 CERROR("%s: qsd instance already prepared\n", qsd->qsd_svname);
648                 rc = -EALREADY;
649         }
650         read_unlock(&qsd->qsd_lock);
651         if (rc)
652                 RETURN(rc);
653
654         /* Record whether this qsd instance is managing quota enforcement for a
655          * MDT (i.e. inode quota) or OST (block quota) */
656         if (lu_device_is_md(qsd->qsd_dev->dd_lu_dev.ld_site->ls_top_dev)) {
657                 qsd->qsd_is_md = true;
658                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_MD);
659         } else {
660                 qsd->qsd_sync_threshold = LQUOTA_LEAST_QUNIT(LQUOTA_RES_DT);
661         }
662
663         /* look-up on-disk directory for the quota slave */
664         qsd->qsd_root = lquota_disk_dir_find_create(env, qsd->qsd_dev, NULL,
665                                                     QSD_DIR);
666         if (IS_ERR(qsd->qsd_root)) {
667                 rc = PTR_ERR(qsd->qsd_root);
668                 qsd->qsd_root = NULL;
669                 CERROR("%s: failed to create quota slave root dir (%d)\n",
670                        qsd->qsd_svname, rc);
671                 RETURN(rc);
672         }
673
674         /* initialize per-quota type data */
675         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
676                 rc = qsd_qtype_init(env, qsd, qtype);
677                 if (rc)
678                         RETURN(rc);
679         }
680
681         /* pools successfully setup, mark the qsd as prepared */
682         write_lock(&qsd->qsd_lock);
683         qsd->qsd_prepared = true;
684         write_unlock(&qsd->qsd_lock);
685
686         /* start reintegration thread for each type, if required */
687         for (qtype = USRQUOTA; qtype < MAXQUOTAS; qtype++) {
688                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[qtype];
689
690                 if (qsd_type_enabled(qsd, qtype) && qsd->qsd_acct_failed) {
691                         LCONSOLE_ERROR("%s: can't enable quota enforcement "
692                                        "since space accounting isn't functional"
693                                        ". Please run tunefs.lustre --quota on "
694                                        "an unmounted filesystem if not done "
695                                        "already\n", qsd->qsd_svname);
696                         break;
697                 }
698
699                 rc = qsd_start_reint_thread(qqi);
700                 if (rc) {
701                         CERROR("%s: failed to start reint thread for type %s "
702                                "(%d)\n", qsd->qsd_svname, QTYPE_NAME(qtype),
703                                rc);
704                         RETURN(rc);
705                 }
706         }
707
708         /* start writeback thread */
709         rc = qsd_start_upd_thread(qsd);
710         if (rc) {
711                 CERROR("%s: failed to start writeback thread (%d)\n",
712                        qsd->qsd_svname, rc);
713                 RETURN(rc);
714         }
715
716         /* generate osp name */
717         rc = tgt_name2ospname((char *)qsd->qsd_svname, qti->qti_buf);
718         if (rc) {
719                 CERROR("%s: failed to generate ospname (%d)\n",
720                        qsd->qsd_svname, rc);
721                 RETURN(rc);
722         }
723
724         /* the connection callback will start the reintegration
725          * procedure if quota is enabled */
726         rc = lustre_register_osp_item(qti->qti_buf, &qsd->qsd_exp,
727                                       qsd_conn_callback, (void *)qsd);
728         if (rc) {
729                 CERROR("%s: fail to get connection to master (%d)\n",
730                        qsd->qsd_svname, rc);
731                 RETURN(rc);
732         }
733
734         RETURN(0);
735 }
736 EXPORT_SYMBOL(qsd_prepare);
737
738 /*
739  * Start a qsd instance. This will complete the last step of the reintegration
740  * procedure as soon as possible (provided that the master is reachable).
741  * This should be called when recovery has been completed and quota should now
742  * be enforced on every operations.
743  *
744  * \param env - the environment passed by the caller
745  * \param qsd - is the qsd instance associated with the osd device to start
746  */
747 int qsd_start(const struct lu_env *env, struct qsd_instance *qsd)
748 {
749         int     type, rc = 0;
750         ENTRY;
751
752         if (unlikely(qsd == NULL))
753                 RETURN(0);
754
755         write_lock(&qsd->qsd_lock);
756         if (!qsd->qsd_prepared) {
757                 CERROR("%s: can't start qsd instance since it wasn't properly "
758                        "initialized\n", qsd->qsd_svname);
759                 rc = -EFAULT;
760         } else if (qsd->qsd_started) {
761                 CERROR("%s: qsd instance already started\n", qsd->qsd_svname);
762                 rc = -EALREADY;
763         } else {
764                 /* notify that the qsd_instance is now started */
765                 qsd->qsd_started = true;
766         }
767         write_unlock(&qsd->qsd_lock);
768
769         if (rc)
770                 RETURN(rc);
771
772         /* Trigger the 3rd step of reintegration: If usage > granted, acquire
773          * up to usage; If usage < granted, release down to usage.  */
774         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
775                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[type];
776                 cfs_waitq_signal(&qqi->qqi_reint_thread.t_ctl_waitq);
777         }
778
779         RETURN(rc);
780 }
781 EXPORT_SYMBOL(qsd_start);
782
783 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg));
784
785 /*
786  * Global initialization performed at module load time
787  */
788 int qsd_glb_init(void)
789 {
790         int     rc;
791
792         rc = lu_kmem_init(qsd_caches);
793         if (rc)
794                 return rc;
795
796         qsd_key_init_generic(&qsd_thread_key, NULL);
797         lu_context_key_register(&qsd_thread_key);
798         lustre_register_quota_process_config(qsd_process_config);
799
800         return 0;
801 }
802
803 /*
804  * Companion of qsd_glb_init() called at module unload time
805  */
806 void qsd_glb_fini(void)
807 {
808         lustre_register_quota_process_config(NULL);
809         lu_kmem_fini(qsd_caches);
810         lu_context_key_degister(&qsd_thread_key);
811 }