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