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