Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros v2
[fs/lustre-release.git] / lustre / quota / qsd_handler.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 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include "qsd_internal.h"
34
35 /**
36  * helper function bumping lqe_pending_req if there is no quota request in
37  * flight for the lquota entry \a lqe. Otherwise, EBUSY is returned.
38  */
39 static inline int qsd_request_enter(struct lquota_entry *lqe)
40 {
41         /* is there already a quota request in flight? */
42         if (lqe->lqe_pending_req != 0) {
43                 LQUOTA_DEBUG(lqe, "already a request in flight");
44                 return -EBUSY;
45         }
46
47         if (lqe->lqe_pending_rel != 0) {
48                 LQUOTA_ERROR(lqe, "no request in flight with pending_rel="LPU64,
49                              lqe->lqe_pending_rel);
50                 LBUG();
51         }
52
53         lqe->lqe_pending_req++;
54         return 0;
55 }
56
57 /**
58  * Companion of qsd_request_enter() dropping lqe_pending_req to 0.
59  */
60 static inline void qsd_request_exit(struct lquota_entry *lqe)
61 {
62         if (lqe->lqe_pending_req != 1) {
63                 LQUOTA_ERROR(lqe, "lqe_pending_req != 1!!!");
64                 LBUG();
65         }
66         lqe->lqe_pending_req--;
67         lqe->lqe_pending_rel = 0;
68         cfs_waitq_broadcast(&lqe->lqe_waiters);
69 }
70
71 /**
72  * Check whether a qsd instance is all set to send quota request to master.
73  * This includes checking whether:
74  * - the connection to master is set up and usable,
75  * - the qsd isn't stopping
76  * - reintegration has been successfully completed and all indexes are
77  *   up-to-date
78  *
79  * \param lqe - is the lquota entry for which we would like to send an quota
80  *              request
81  * \param lockh - is the remote handle of the global lock returned on success
82  *
83  * \retval 0 on success, appropriate error on failure
84  */
85 static int qsd_ready(struct lquota_entry *lqe, struct lustre_handle *lockh)
86 {
87         struct qsd_qtype_info   *qqi = lqe2qqi(lqe);
88         struct qsd_instance     *qsd = qqi->qqi_qsd;
89         struct obd_import       *imp = NULL;
90         struct ldlm_lock        *lock;
91         ENTRY;
92
93         read_lock(&qsd->qsd_lock);
94         /* is the qsd about to shut down? */
95         if (qsd->qsd_stopping) {
96                 read_unlock(&qsd->qsd_lock);
97                 LQUOTA_DEBUG(lqe, "dropping quota req since qsd is stopping");
98                 /* Target is about to shut down, client will retry */
99                 RETURN(-EINPROGRESS);
100         }
101
102         /* is the connection to the quota master ready? */
103         if (qsd->qsd_exp_valid)
104                 imp = class_exp2cliimp(qsd->qsd_exp);
105         if (imp == NULL || imp->imp_invalid) {
106                 read_unlock(&qsd->qsd_lock);
107                 LQUOTA_DEBUG(lqe, "connection to master not ready");
108                 RETURN(-ENOTCONN);
109         }
110
111         /* In most case, reintegration must have been triggered (when enable
112          * quota or on OST start), however, in rare race condition (enabling
113          * quota when starting OSTs), we might miss triggering reintegration
114          * for some qqi.
115          *
116          * If the previous reintegration failed for some reason, we'll
117          * re-trigger it here as well. */
118         if (!qqi->qqi_glb_uptodate || !qqi->qqi_slv_uptodate) {
119                 read_unlock(&qsd->qsd_lock);
120                 LQUOTA_DEBUG(lqe, "not up-to-date, dropping request and "
121                              "kicking off reintegration");
122                 qsd_start_reint_thread(qqi);
123                 RETURN(-EINPROGRESS);
124         }
125
126         /* Fill the remote global lock handle, master will check this handle
127          * to see if the slave is sending request with stale lock */
128         lustre_handle_copy(lockh, &qqi->qqi_lockh);
129         read_unlock(&qsd->qsd_lock);
130
131         if (!lustre_handle_is_used(lockh))
132                 RETURN(-ENOLCK);
133
134         lock = ldlm_handle2lock(lockh);
135         if (lock == NULL)
136                 RETURN(-ENOLCK);
137
138         /* return remote lock handle to be packed in quota request */
139         lustre_handle_copy(lockh, &lock->l_remote_handle);
140         LDLM_LOCK_PUT(lock);
141
142         RETURN(0);
143 }
144
145 /**
146  * Check whether any quota space adjustment (pre-acquire/release/report) is
147  * needed for a given quota ID. If a non-null \a qbody is passed, then the
148  * \a qbody structure (qb_count/flags/usage) is filled with appropriate data
149  * to be packed in the quota request.
150  *
151  * \param lqe   - is the lquota entry for which we would like to adjust quota
152  *                space.
153  * \param qbody - is the quota body to fill, if not NULL.
154  *
155  * \retval true  - space adjustment is required and \a qbody is filled, if not
156  *                 NULL
157  * \retval false - no space adjustment required
158  */
159 static bool qsd_calc_adjust(struct lquota_entry *lqe, struct quota_body *qbody)
160 {
161         __u64   usage, granted;
162         ENTRY;
163
164         usage   = lqe->lqe_usage;
165         usage  += lqe->lqe_pending_write + lqe->lqe_waiting_write;
166         granted = lqe->lqe_granted;
167
168         if (qbody != NULL)
169                 qbody->qb_flags = 0;
170
171         if (!lqe->lqe_enforced) {
172                 /* quota not enforced any more for this ID */
173                 if (granted != 0) {
174                         /* release all quota space unconditionally */
175                         LQUOTA_DEBUG(lqe, "not enforced, releasing all space");
176                         if (qbody != NULL) {
177                                 qbody->qb_count = granted;
178                                 qbody->qb_flags = QUOTA_DQACQ_FL_REL;
179                         }
180                         RETURN(true);
181                 }
182                 RETURN(false);
183         }
184
185         if (!lustre_handle_is_used(&lqe->lqe_lockh)) {
186                 /* No valid per-ID lock
187                  * When reporting quota (during reintegration or on setquota
188                  * glimpse), we should release granted space if usage is 0.
189                  * Otherwise, if the usage is less than granted, we need to
190                  * acquire the per-ID lock to make sure the unused grant can be
191                  * reclaimed by per-ID lock glimpse. */
192                 if (usage == 0) {
193                         /* no on-disk usage and no outstanding activity, release
194                          * space */
195                         if (granted != 0) {
196                                 LQUOTA_DEBUG(lqe, "no usage, releasing all "
197                                              "space");
198                                 if (qbody != NULL) {
199                                         qbody->qb_count = granted;
200                                         qbody->qb_flags = QUOTA_DQACQ_FL_REL;
201                                 }
202                                 RETURN(true);
203                         }
204                         LQUOTA_DEBUG(lqe, "no usage + no granted, nothing to "
205                                      "do");
206                         RETURN(false);
207                 }
208
209                 if (lqe->lqe_usage < lqe->lqe_granted) {
210                         /* holding quota space w/o any lock, enqueue per-ID lock
211                          * again */
212                         LQUOTA_DEBUG(lqe, "(re)acquiring per-ID lock");
213                         if (qbody != NULL) {
214                                 qbody->qb_count = 0;
215                                 qbody->qb_flags = QUOTA_DQACQ_FL_ACQ;
216                         }
217                         RETURN(true);
218                 }
219
220                 if (lqe->lqe_usage > lqe->lqe_granted) {
221                         /* quota overrun, report usage */
222                         LQUOTA_DEBUG(lqe, "overrun, reporting usage");
223                         if (qbody != NULL) {
224                                 qbody->qb_usage = lqe->lqe_usage;
225                                 qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
226                         }
227                         RETURN(true);
228                 }
229                 LQUOTA_DEBUG(lqe, "granted matches usage, nothing to do");
230                 RETURN(false);
231         }
232
233         /* valid per-ID lock
234          * Apply good old quota qunit adjustment logic which has been around
235          * since lustre 1.4:
236          * 1. release spare quota space? */
237         if (granted > usage + lqe->lqe_qunit) {
238                 /* pre-release quota space */
239                 if (qbody == NULL)
240                         RETURN(true);
241                 qbody->qb_count = granted - usage;
242                 /* if usage == 0, release all granted space */
243                 if (usage) {
244                         /* try to keep one qunit of quota space */
245                         qbody->qb_count -= lqe->lqe_qunit;
246                         /* but don't release less than qtune to avoid releasing
247                          * space too often */
248                         if (qbody->qb_count < lqe->lqe_qtune)
249                                 qbody->qb_count = lqe->lqe_qtune;
250                 }
251                 qbody->qb_flags = QUOTA_DQACQ_FL_REL;
252                 RETURN(true);
253         }
254
255         /* 2. Any quota overrun? */
256         if (lqe->lqe_usage > lqe->lqe_granted) {
257                 /* we overconsumed quota space, we report usage in request so
258                  * that master can adjust it unconditionally */
259                 if (qbody == NULL)
260                         RETURN(true);
261                 qbody->qb_usage = lqe->lqe_usage;
262                 granted         = lqe->lqe_usage;
263                 qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
264         }
265
266         /* 3. Time to pre-acquire? */
267         if (!lqe->lqe_edquot && !lqe->lqe_nopreacq && usage > 0 &&
268             lqe->lqe_qunit != 0 && granted < usage + lqe->lqe_qtune) {
269                 /* To pre-acquire quota space, we report how much spare quota
270                  * space the slave currently owns, then the master will grant us
271                  * back how much we can pretend given the current state of
272                  * affairs */
273                 if (qbody == NULL)
274                         RETURN(true);
275                 if (granted <= usage)
276                         qbody->qb_count = 0;
277                 else
278                         qbody->qb_count = granted - usage;
279                 qbody->qb_flags |= QUOTA_DQACQ_FL_PREACQ;
280                 RETURN(true);
281         }
282
283         if (qbody != NULL)
284                 RETURN(qbody->qb_flags != 0);
285         else
286                 RETURN(false);
287 }
288
289 /**
290  * Helper function returning true when quota space need to be adjusted (some
291  * unused space should be free or pre-acquire) and false otherwise.
292  */
293 static inline bool qsd_adjust_needed(struct lquota_entry *lqe)
294 {
295         return qsd_calc_adjust(lqe, NULL);
296 }
297
298 /**
299  * Callback function called when an acquire/release request sent to the master
300  * is completed
301  */
302 static void qsd_req_completion(const struct lu_env *env,
303                                struct qsd_qtype_info *qqi,
304                                struct quota_body *reqbody,
305                                struct quota_body *repbody,
306                                struct lustre_handle *lockh,
307                                struct lquota_lvb *lvb,
308                                void *arg, int ret)
309 {
310         struct lquota_entry     *lqe = (struct lquota_entry *)arg;
311         struct qsd_thread_info  *qti;
312         int                      rc;
313         bool                     adjust = false, cancel = false;
314         ENTRY;
315
316         LASSERT(qqi != NULL && lqe != NULL);
317
318         /* environment passed by ptlrpcd is mostly used by CLIO and hasn't the
319          * DT tags set. */
320         rc = lu_env_refill_by_tags((struct lu_env *)env, LCT_DT_THREAD, 0);
321         if (rc) {
322                 LQUOTA_ERROR(lqe, "failed to refill environmnent %d", rc);
323                 lqe_write_lock(lqe);
324                 /* can't afford to adjust quota space with no suitable lu_env */
325                 GOTO(out_noadjust, rc);
326         }
327         qti = qsd_info(env);
328
329         lqe_write_lock(lqe);
330         LQUOTA_DEBUG(lqe, "DQACQ returned %d, flags:0x%x", ret,
331                      reqbody->qb_flags);
332
333         /* despite -EDQUOT & -EINPROGRESS errors, the master might still
334          * grant us back quota space to adjust quota overrun */
335         if (ret != 0 && ret != -EDQUOT && ret != -EINPROGRESS) {
336                 if (ret != -ETIMEDOUT && ret != -ENOTCONN &&
337                    ret != -ESHUTDOWN && ret != -EAGAIN)
338                         /* print errors only if return code is unexpected */
339                         LQUOTA_ERROR(lqe, "DQACQ failed with %d, flags:0x%x",
340                                      ret, reqbody->qb_flags);
341                 GOTO(out, ret);
342         }
343
344         /* Set the lqe_lockh */
345         if (lustre_handle_is_used(lockh) &&
346             !lustre_handle_equal(lockh, &lqe->lqe_lockh))
347                 lustre_handle_copy(&lqe->lqe_lockh, lockh);
348
349         /* If the replied qb_count is zero, it means master didn't process
350          * the DQACQ since the limit for this ID has been removed, so we
351          * should not update quota entry & slave index copy neither. */
352         if (repbody != NULL && repbody->qb_count != 0) {
353                 LQUOTA_DEBUG(lqe, "DQACQ qb_count:"LPU64, repbody->qb_count);
354
355                 if (req_is_rel(reqbody->qb_flags)) {
356                         if (lqe->lqe_granted < repbody->qb_count) {
357                                 LQUOTA_ERROR(lqe, "can't release more space "
358                                              "than owned "LPU64"<"LPU64,
359                                              lqe->lqe_granted,
360                                              repbody->qb_count);
361                                 lqe->lqe_granted = 0;
362                         } else {
363                                 lqe->lqe_granted -= repbody->qb_count;
364                         }
365                         /* Cancel the per-ID lock initiatively when there
366                          * isn't any usage & grant, which can avoid master
367                          * sending glimpse unnecessarily to this slave on
368                          * quota revoking */
369                         if (!lqe->lqe_pending_write && !lqe->lqe_granted &&
370                             !lqe->lqe_waiting_write && !lqe->lqe_usage)
371                                 cancel = true;
372                 } else {
373                         lqe->lqe_granted += repbody->qb_count;
374                 }
375                 qti->qti_rec.lqr_slv_rec.qsr_granted = lqe->lqe_granted;
376                 lqe_write_unlock(lqe);
377
378                 /* Update the slave index file in the dedicated thread. So far,
379                  * We don't update the version of slave index copy on DQACQ.
380                  * No locking is necessary since nobody can change
381                  * lqe->lqe_granted while lqe->lqe_pending_req > 0 */
382                 qsd_upd_schedule(qqi, lqe, &lqe->lqe_id, &qti->qti_rec, 0,
383                                  false);
384                 lqe_write_lock(lqe);
385         }
386
387         /* extract information from lvb */
388         if (ret == 0 && lvb != 0) {
389                 if (lvb->lvb_id_qunit != 0)
390                         qsd_set_qunit(lqe, lvb->lvb_id_qunit);
391                 if (lvb->lvb_flags & LQUOTA_FL_EDQUOT)
392                         lqe->lqe_edquot = true;
393                 else
394                         lqe->lqe_edquot = false;
395         } else if (repbody != NULL && repbody->qb_qunit != 0) {
396                 qsd_set_qunit(lqe, repbody->qb_qunit);
397         }
398
399         /* turn off pre-acquire if it failed with -EDQUOT. This is done to avoid
400          * flooding the master with acquire request. Pre-acquire will be turned
401          * on again as soon as qunit is modified */
402         if (req_is_preacq(reqbody->qb_flags) && ret == -EDQUOT)
403                 lqe->lqe_nopreacq = true;
404 out:
405         adjust = qsd_adjust_needed(lqe);
406         if (reqbody && req_is_acq(reqbody->qb_flags) && ret != -EDQUOT) {
407                 lqe->lqe_acq_rc = ret;
408                 lqe->lqe_acq_time = cfs_time_current_64();
409         }
410 out_noadjust:
411         qsd_request_exit(lqe);
412         lqe_write_unlock(lqe);
413
414         /* release reference on per-ID lock */
415         if (lustre_handle_is_used(lockh))
416                 ldlm_lock_decref(lockh, qsd_id_einfo.ei_mode);
417
418         if (cancel) {
419                 qsd_adjust_schedule(lqe, false, true);
420         } else if (adjust) {
421                 if (!ret || ret == -EDQUOT)
422                         qsd_adjust_schedule(lqe, false, false);
423                 else
424                         qsd_adjust_schedule(lqe, true, false);
425         }
426         lqe_putref(lqe);
427
428         if (lvb)
429                 OBD_FREE_PTR(lvb);
430         EXIT;
431 }
432
433 /**
434  * Try to consume local quota space.
435  *
436  * \param lqe   - is the qid entry to be processed
437  * \param space - is the amount of quota space needed to complete the operation
438  *
439  * \retval 0       - success
440  * \retval -EDQUOT - out of quota
441  * \retval -EAGAIN - need to acquire space from master
442  */
443 static int qsd_acquire_local(struct lquota_entry *lqe, __u64 space)
444 {
445         __u64   usage;
446         int     rc;
447         ENTRY;
448
449         if (!lqe->lqe_enforced)
450                 /* not enforced any more, we are good */
451                 RETURN(-ESRCH);
452
453         lqe_write_lock(lqe);
454         /* use latest usage */
455         usage = lqe->lqe_usage;
456         /* take pending write into account */
457         usage += lqe->lqe_pending_write;
458
459         if (space + usage <= lqe->lqe_granted - lqe->lqe_pending_rel) {
460                 /* Yay! we got enough space */
461                 lqe->lqe_pending_write += space;
462                 lqe->lqe_waiting_write -= space;
463                 rc = 0;
464         } else if (lqe->lqe_edquot) {
465                 rc = -EDQUOT;
466         } else {
467                 rc = -EAGAIN;
468         }
469         lqe_write_unlock(lqe);
470
471         RETURN(rc);
472 }
473
474 /**
475  * Compute how much quota space should be acquire from the master based
476  * on how much is currently granted to this slave and pending/waiting
477  * operations.
478  *
479  * \param lqe - is the lquota entry for which we would like to adjust quota
480  *              space.
481  * \param qbody - is the quota body of the acquire request to fill
482  *
483  * \retval true  - space acquisition is needed and qbody is filled
484  * \retval false - no space acquisition required
485  */
486 static inline bool qsd_calc_acquire(struct lquota_entry *lqe,
487                                     struct quota_body *qbody)
488 {
489         __u64   usage, granted;
490
491         usage   = lqe->lqe_usage;
492         usage  += lqe->lqe_pending_write + lqe->lqe_waiting_write;
493         granted = lqe->lqe_granted;
494
495         qbody->qb_flags = 0;
496
497         /* if we overconsumed quota space, we report usage in request so that
498          * master can adjust it unconditionally */
499         if (lqe->lqe_usage > lqe->lqe_granted) {
500                 qbody->qb_usage = lqe->lqe_usage;
501                 qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
502                 granted = lqe->lqe_usage;
503         }
504
505         /* acquire as much as needed, but not more */
506         if (usage > granted) {
507                 qbody->qb_count  = usage - granted;
508                 qbody->qb_flags |= QUOTA_DQACQ_FL_ACQ;
509         }
510
511         return qbody->qb_flags != 0;
512 }
513
514 /**
515  * Acquire quota space from master.
516  * There are at most 1 in-flight dqacq/dqrel.
517  *
518  * \param env    - the environment passed by the caller
519  * \param lqe    - is the qid entry to be processed
520  *
521  * \retval 0            - success
522  * \retval -EDQUOT      - out of quota
523  * \retval -EINPROGRESS - inform client to retry write/create
524  * \retval -EBUSY       - already a quota request in flight
525  * \retval -ve          - other appropriate errors
526  */
527 static int qsd_acquire_remote(const struct lu_env *env,
528                               struct lquota_entry *lqe)
529 {
530         struct qsd_thread_info  *qti = qsd_info(env);
531         struct quota_body       *qbody = &qti->qti_body;
532         struct qsd_instance     *qsd;
533         struct qsd_qtype_info   *qqi;
534         int                      rc;
535         ENTRY;
536
537         memset(qbody, 0, sizeof(*qbody));
538         rc = qsd_ready(lqe, &qbody->qb_glb_lockh);
539         if (rc)
540                 RETURN(rc);
541
542         qqi = lqe2qqi(lqe);
543         qsd = qqi->qqi_qsd;
544
545         lqe_write_lock(lqe);
546
547         /* is quota really enforced for this id? */
548         if (!lqe->lqe_enforced) {
549                 lqe_write_unlock(lqe);
550                 LQUOTA_DEBUG(lqe, "quota not enforced any more");
551                 RETURN(0);
552         }
553
554         /* fill qb_count & qb_flags */
555         if (!qsd_calc_acquire(lqe, qbody)) {
556                 lqe_write_unlock(lqe);
557                 LQUOTA_DEBUG(lqe, "No acquire required");
558                 RETURN(0);
559         }
560
561         /* check whether an acquire request completed recently */
562         if (lqe->lqe_acq_rc != 0 &&
563             cfs_time_before_64(cfs_time_shift_64(-1), lqe->lqe_acq_time)) {
564                 lqe_write_unlock(lqe);
565                 LQUOTA_DEBUG(lqe, "using cached return code %d", lqe->lqe_acq_rc);
566                 RETURN(lqe->lqe_acq_rc);
567         }
568
569         /* only 1 quota request in flight for a given ID is allowed */
570         rc = qsd_request_enter(lqe);
571         if (rc) {
572                 lqe_write_unlock(lqe);
573                 RETURN(rc);
574         }
575
576         lustre_handle_copy(&qti->qti_lockh, &lqe->lqe_lockh);
577         lqe_write_unlock(lqe);
578
579         /* hold a refcount until completion */
580         lqe_getref(lqe);
581
582         /* fill other quota body fields */
583         qbody->qb_fid = qqi->qqi_fid;
584         qbody->qb_id  = lqe->lqe_id;
585
586         /* check whether we already own a valid lock for this ID */
587         rc = qsd_id_lock_match(&qti->qti_lockh, &qbody->qb_lockh);
588         if (rc) {
589                 struct lquota_lvb *lvb;
590
591                 OBD_ALLOC_PTR(lvb);
592                 if (lvb == NULL) {
593                         rc = -ENOMEM;
594                         qsd_req_completion(env, qqi, qbody, NULL,
595                                            &qti->qti_lockh, NULL, lqe, rc);
596                         RETURN(rc);
597                 }
598                 /* no lock found, should use intent */
599                 rc = qsd_intent_lock(env, qsd->qsd_exp, qbody, true,
600                                      IT_QUOTA_DQACQ, qsd_req_completion,
601                                      qqi, lvb, (void *)lqe);
602         } else {
603                 /* lock found, should use regular dqacq */
604                 rc = qsd_send_dqacq(env, qsd->qsd_exp, qbody, true,
605                                     qsd_req_completion, qqi, &qti->qti_lockh,
606                                     lqe);
607         }
608
609         /* the completion function will be called by qsd_send_dqacq or
610          * qsd_intent_lock */
611         RETURN(rc);
612 }
613
614 /**
615  * Acquire \a space of quota space in order to complete an operation.
616  * Try to consume local quota space first and send acquire request to quota
617  * master if required.
618  *
619  * \param env   - the environment passed by the caller
620  * \param lqe   - is the qid entry to be processed
621  * \param space - is the amount of quota required for the operation
622  * \param ret   - is the return code (-EDQUOT, -EINPROGRESS, ...)
623  *
624  * \retval true  - exit from l_wait_event and real return value in \a ret
625  * \retval false - continue waiting
626  */
627 static bool qsd_acquire(const struct lu_env *env, struct lquota_entry *lqe,
628                         long long space, int *ret)
629 {
630         int rc = 0, count;
631         ENTRY;
632
633         for (count = 0; rc == 0; count++) {
634                 LQUOTA_DEBUG(lqe, "acquiring:"LPD64 " count=%d", space, count);
635
636                 if (lqe2qqi(lqe)->qqi_qsd->qsd_stopping) {
637                         rc = -EINPROGRESS;
638                         break;
639                 }
640
641                 /* refresh disk usage */
642                 rc = qsd_refresh_usage(env, lqe);
643                 if (rc)
644                         break;
645
646                 /* try to consume local quota space first */
647                 rc = qsd_acquire_local(lqe, space);
648                 if (rc != -EAGAIN)
649                         /* rc == 0, Wouhou! enough local quota space
650                          * rc < 0, something bad happened */
651                          break;
652
653                 /* need to acquire more quota space from master */
654                 rc = qsd_acquire_remote(env, lqe);
655         }
656
657         if (rc == -EBUSY)
658                 /* already a request in flight, continue waiting */
659                 RETURN(false);
660         *ret = rc;
661         RETURN(true); /* exit from l_wait_event */
662 }
663
664 /**
665  * Quota enforcement handler. If local quota can satisfy this operation,
666  * return success, otherwise, acquire more quota from master.
667  * (for write operation, if master isn't available at this moment, return
668  * -EINPROGRESS to inform client to retry the write)
669  *
670  * \param env   - the environment passed by the caller
671  * \param qsd   - is the qsd instance associated with the device in charge
672  *                of the operation.
673  * \param qid   - is the qid information attached in the transaction handle
674  * \param space - is the space required by the operation
675  * \param flags - if the operation is write, return caller no user/group
676  *                and sync commit flags
677  *
678  * \retval 0            - success
679  * \retval -EDQUOT      - out of quota
680  * \retval -EINPROGRESS - inform client to retry write
681  * \retval -ve          - other appropriate errors
682  */
683 static int qsd_op_begin0(const struct lu_env *env, struct qsd_qtype_info *qqi,
684                          struct lquota_id_info *qid, long long space,
685                          int *flags)
686 {
687         struct lquota_entry     *lqe;
688         int                      rc, ret = -EINPROGRESS;
689         struct l_wait_info       lwi;
690         ENTRY;
691
692         if (qid->lqi_qentry != NULL) {
693                 /* we already had to deal with this id for this transaction */
694                 lqe = qid->lqi_qentry;
695                 if (!lqe->lqe_enforced)
696                         RETURN(0);
697         } else {
698                 /* look up lquota entry associated with qid */
699                 lqe = lqe_locate(env, qqi->qqi_site, &qid->lqi_id);
700                 if (IS_ERR(lqe))
701                         RETURN(PTR_ERR(lqe));
702                 if (!lqe->lqe_enforced) {
703                         lqe_putref(lqe);
704                         RETURN(0);
705                 }
706                 qid->lqi_qentry = lqe;
707                 /* lqe will be released in qsd_op_end() */
708         }
709
710         if (space <= 0) {
711                 /* when space is negative or null, we don't need to consume
712                  * quota space. That said, we still want to perform space
713                  * adjustments in qsd_op_end, so we return here, but with
714                  * a reference on the lqe */
715                 if (flags != NULL) {
716                         rc = qsd_refresh_usage(env, lqe);
717                         GOTO(out_flags, rc);
718                 }
719                 RETURN(0);
720         }
721
722         LQUOTA_DEBUG(lqe, "op_begin space:"LPD64, space);
723
724         lqe_write_lock(lqe);
725         lqe->lqe_waiting_write += space;
726         lqe_write_unlock(lqe);
727
728         /* acquire quota space for the operation, cap overall wait time to
729          * prevent a service thread from being stuck for too long */
730         lwi = LWI_TIMEOUT(cfs_time_seconds(qsd_wait_timeout(qqi->qqi_qsd)),
731                           NULL, NULL);
732         rc = l_wait_event(lqe->lqe_waiters, qsd_acquire(env, lqe, space, &ret),
733                           &lwi);
734
735         if (rc == 0 && ret == 0) {
736                 qid->lqi_space += space;
737         } else {
738                 if (rc == 0)
739                         rc = ret;
740
741                 LQUOTA_DEBUG(lqe, "acquire quota failed:%d", rc);
742
743                 lqe_write_lock(lqe);
744                 lqe->lqe_waiting_write -= space;
745
746                 if (flags && lqe->lqe_pending_write != 0)
747                         /* Inform OSD layer that there are pending writes.
748                          * It might want to retry after a sync if appropriate */
749                          *flags |= QUOTA_FL_SYNC;
750                 lqe_write_unlock(lqe);
751
752                 /* convert recoverable error into -EINPROGRESS, client will
753                  * retry */
754                 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ENOLCK ||
755                     rc == -EAGAIN || rc == -EINTR) {
756                         rc = -EINPROGRESS;
757                 } else if (rc == -ESRCH) {
758                         rc = 0;
759                         LQUOTA_ERROR(lqe, "ID isn't enforced on master, it "
760                                      "probably due to a legeal race, if this "
761                                      "message is showing up constantly, there "
762                                      "could be some inconsistence between "
763                                      "master & slave, and quota reintegration "
764                                      "needs be re-triggered.");
765                 }
766         }
767
768         if (flags != NULL) {
769 out_flags:
770                 LASSERT(qid->lqi_is_blk);
771                 if (rc != 0) {
772                         *flags |= LQUOTA_OVER_FL(qqi->qqi_qtype);
773                 } else {
774                         __u64   usage;
775
776                         lqe_read_lock(lqe);
777                         usage  = lqe->lqe_usage;
778                         usage += lqe->lqe_pending_write;
779                         usage += lqe->lqe_waiting_write;
780                         usage += qqi->qqi_qsd->qsd_sync_threshold;
781
782                         /* if we should notify client to start sync write */
783                         if (usage >= lqe->lqe_granted - lqe->lqe_pending_rel)
784                                 *flags |= LQUOTA_OVER_FL(qqi->qqi_qtype);
785                         else
786                                 *flags &= ~LQUOTA_OVER_FL(qqi->qqi_qtype);
787                         lqe_read_unlock(lqe);
788                 }
789         }
790         RETURN(rc);
791 }
792
793 /**
794  * helper function comparing two lquota_id_info structures
795  */
796 static inline bool qid_equal(struct lquota_id_info *q1,
797                              struct lquota_id_info *q2)
798 {
799         if (q1->lqi_type != q2->lqi_type)
800                 return false;
801         return (q1->lqi_id.qid_uid == q2->lqi_id.qid_uid) ? true : false;
802 }
803
804 /**
805  * Enforce quota, it's called in the declaration of each operation.
806  * qsd_op_end() will then be called later once all the operations have been
807  * completed in order to release/adjust the quota space.
808  *
809  * \param env   - the environment passed by the caller
810  * \param qsd   - is the qsd instance associated with the device in charge of
811  *                the operation.
812  * \param trans - is the quota transaction information
813  * \param qi    - qid & space required by current operation
814  * \param flags - if the operation is write, return caller no user/group and
815  *                sync commit flags
816  *
817  * \retval 0            - success
818  * \retval -EDQUOT      - out of quota
819  * \retval -EINPROGRESS - inform client to retry write
820  * \retval -ve          - other appropriate errors
821  */
822 int qsd_op_begin(const struct lu_env *env, struct qsd_instance *qsd,
823                  struct lquota_trans *trans, struct lquota_id_info *qi,
824                  int *flags)
825 {
826         int     i, rc;
827         bool    found = false;
828         ENTRY;
829
830         if (unlikely(qsd == NULL))
831                 RETURN(0);
832
833         /* We don't enforce quota until the qsd_instance is started */
834         read_lock(&qsd->qsd_lock);
835         if (!qsd->qsd_started) {
836                 read_unlock(&qsd->qsd_lock);
837                 RETURN(0);
838         }
839         read_unlock(&qsd->qsd_lock);
840
841         /* ignore block quota on MDTs, ignore inode quota on OSTs */
842         if ((!qsd->qsd_is_md && !qi->lqi_is_blk) ||
843             (qsd->qsd_is_md && qi->lqi_is_blk))
844                 RETURN(0);
845
846         /* ignore quota enforcement request when:
847          *    - quota isn't enforced for this quota type
848          * or - the user/group is root
849          * or - quota accounting isn't enabled */
850         if (!qsd_type_enabled(qsd, qi->lqi_type) || qi->lqi_id.qid_uid == 0 ||
851             qsd->qsd_acct_failed)
852                 RETURN(0);
853
854         LASSERTF(trans->lqt_id_cnt <= QUOTA_MAX_TRANSIDS, "id_cnt=%d",
855                  trans->lqt_id_cnt);
856         /* check whether we already allocated a slot for this id */
857         for (i = 0; i < trans->lqt_id_cnt; i++) {
858                 if (qid_equal(qi, &trans->lqt_ids[i])) {
859                         found = true;
860                         /* make sure we are not mixing inodes & blocks */
861                         LASSERT(trans->lqt_ids[i].lqi_is_blk == qi->lqi_is_blk);
862                         break;
863                 }
864         }
865
866         if (!found) {
867                 if (unlikely(i >= QUOTA_MAX_TRANSIDS)) {
868                         CERROR("%s: more than %d qids enforced for a "
869                                "transaction?\n", qsd->qsd_svname, i);
870                         RETURN(-EINVAL);
871                 }
872
873                 /* fill new slot */
874                 trans->lqt_ids[i].lqi_id     = qi->lqi_id;
875                 trans->lqt_ids[i].lqi_type   = qi->lqi_type;
876                 trans->lqt_ids[i].lqi_is_blk = qi->lqi_is_blk;
877                 trans->lqt_id_cnt++;
878         }
879
880         /* manage quota enforcement for this ID */
881         rc = qsd_op_begin0(env, qsd->qsd_type_array[qi->lqi_type],
882                            &trans->lqt_ids[i], qi->lqi_space, flags);
883         RETURN(rc);
884 }
885 EXPORT_SYMBOL(qsd_op_begin);
886
887 /**
888  * Adjust quota space (by acquiring or releasing) hold by the quota slave.
889  * This function is called after each quota request completion and during
890  * reintegration in order to report usage or re-acquire quota locks.
891  * Space adjustment is aborted if there is already a quota request in flight
892  * for this ID.
893  *
894  * \param env    - the environment passed by the caller
895  * \param lqe    - is the qid entry to be processed
896  *
897  * \retval 0 on success, appropriate errors on failure
898  */
899 int qsd_adjust(const struct lu_env *env, struct lquota_entry *lqe)
900 {
901         struct qsd_thread_info  *qti = qsd_info(env);
902         struct quota_body       *qbody = &qti->qti_body;
903         struct qsd_instance     *qsd;
904         struct qsd_qtype_info   *qqi;
905         int                      rc;
906         bool                     intent = false;
907         ENTRY;
908
909         memset(qbody, 0, sizeof(*qbody));
910         rc = qsd_ready(lqe, &qbody->qb_glb_lockh);
911         if (rc) {
912                 /* add to adjust list again to trigger adjustment later when
913                  * slave is ready */
914                 LQUOTA_DEBUG(lqe, "delaying adjustment since qsd isn't ready");
915                 qsd_adjust_schedule(lqe, true, false);
916                 RETURN(0);
917         }
918
919         qqi = lqe2qqi(lqe);
920         qsd = qqi->qqi_qsd;
921
922         lqe_write_lock(lqe);
923
924         /* fill qb_count & qb_flags */
925         if (!qsd_calc_adjust(lqe, qbody)) {
926                 lqe_write_unlock(lqe);
927                 LQUOTA_DEBUG(lqe, "no adjustment required");
928                 RETURN(0);
929         }
930
931         /* only 1 quota request in flight for a given ID is allowed */
932         rc = qsd_request_enter(lqe);
933         if (rc) {
934                 /* already a request in flight, space adjustment will be run
935                  * again on request completion */
936                 lqe_write_unlock(lqe);
937                 RETURN(0);
938         }
939
940         if (req_is_rel(qbody->qb_flags))
941                 lqe->lqe_pending_rel = qbody->qb_count;
942         lustre_handle_copy(&qti->qti_lockh, &lqe->lqe_lockh);
943         lqe_write_unlock(lqe);
944
945         /* hold a refcount until completion */
946         lqe_getref(lqe);
947
948         /* fill other quota body fields */
949         qbody->qb_fid = qqi->qqi_fid;
950         qbody->qb_id  = lqe->lqe_id;
951
952         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
953                 /* check whether we own a valid lock for this ID */
954                 rc = qsd_id_lock_match(&qti->qti_lockh, &qbody->qb_lockh);
955                 if (rc) {
956                         memset(&qti->qti_lockh, 0, sizeof(qti->qti_lockh));
957                         if (req_is_preacq(qbody->qb_flags)) {
958                                 if (req_has_rep(qbody->qb_flags))
959                                         /* still want to report usage */
960                                         qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
961                                 else
962                                         /* no pre-acquire if no per-ID lock */
963                                         GOTO(out, rc = -ENOLCK);
964                         } else {
965                                 /* no lock found, should use intent */
966                                 intent = true;
967                         }
968                 } else if (req_is_acq(qbody->qb_flags) &&
969                            qbody->qb_count == 0) {
970                         /* found cached lock, no need to acquire */
971                         GOTO(out, rc = 0);
972                 }
973         } else {
974                 /* release and report don't need a per-ID lock */
975                 memset(&qti->qti_lockh, 0, sizeof(qti->qti_lockh));
976         }
977
978         if (!intent) {
979                 rc = qsd_send_dqacq(env, qsd->qsd_exp, qbody, false,
980                                     qsd_req_completion, qqi, &qti->qti_lockh,
981                                     lqe);
982         } else {
983                 struct lquota_lvb *lvb;
984
985                 OBD_ALLOC_PTR(lvb);
986                 if (lvb == NULL)
987                         GOTO(out, rc = -ENOMEM);
988
989                 rc = qsd_intent_lock(env, qsd->qsd_exp, qbody, false,
990                                      IT_QUOTA_DQACQ, qsd_req_completion,
991                                      qqi, lvb, (void *)lqe);
992         }
993         /* the completion function will be called by qsd_send_dqacq or
994          * qsd_intent_lock */
995         RETURN(rc);
996 out:
997         qsd_req_completion(env, qqi, qbody, NULL, &qti->qti_lockh, NULL, lqe,
998                            rc);
999         return rc;
1000 }
1001
1002 /**
1003  * Post quota operation, pre-acquire/release quota from master.
1004  *
1005  * \param  env  - the environment passed by the caller
1006  * \param  qsd  - is the qsd instance attached to the OSD device which
1007  *                is handling the operation.
1008  * \param  qqi  - is the qsd_qtype_info structure associated with the quota ID
1009  *                subject to the operation
1010  * \param  qid  - stores information related to his ID for the operation
1011  *                which has just completed
1012  *
1013  * \retval 0    - success
1014  * \retval -ve  - failure
1015  */
1016 static void qsd_op_end0(const struct lu_env *env, struct qsd_qtype_info *qqi,
1017                         struct lquota_id_info *qid)
1018 {
1019         struct lquota_entry     *lqe;
1020         bool                     adjust;
1021         ENTRY;
1022
1023         lqe = qid->lqi_qentry;
1024         if (lqe == NULL)
1025                 RETURN_EXIT;
1026         qid->lqi_qentry = NULL;
1027
1028         /* refresh cached usage if a suitable environment is passed */
1029         if (env != NULL)
1030                 qsd_refresh_usage(env, lqe);
1031
1032         lqe_write_lock(lqe);
1033         if (qid->lqi_space > 0)
1034                 lqe->lqe_pending_write -= qid->lqi_space;
1035         if (env != NULL)
1036                 adjust = qsd_adjust_needed(lqe);
1037         else
1038                 adjust = true;
1039         lqe_write_unlock(lqe);
1040
1041         if (adjust) {
1042                 /* pre-acquire/release quota space is needed */
1043                 if (env != NULL)
1044                         qsd_adjust(env, lqe);
1045                 else
1046                         /* no suitable environment, handle adjustment in
1047                          * separate thread context */
1048                         qsd_adjust_schedule(lqe, false, false);
1049         }
1050         lqe_putref(lqe);
1051         EXIT;
1052 }
1053
1054 /**
1055  * Post quota operation. It's called after each operation transaction stopped.
1056  *
1057  * \param  env   - the environment passed by the caller
1058  * \param  qsd   - is the qsd instance associated with device which is handling
1059  *                 the operation.
1060  * \param  qids  - all qids information attached in the transaction handle
1061  * \param  count - is the number of qid entries in the qids array.
1062  *
1063  * \retval 0     - success
1064  * \retval -ve   - failure
1065  */
1066 void qsd_op_end(const struct lu_env *env, struct qsd_instance *qsd,
1067                 struct lquota_trans *trans)
1068 {
1069         int i;
1070         ENTRY;
1071
1072         if (unlikely(qsd == NULL))
1073                 RETURN_EXIT;
1074
1075         /* We don't enforce quota until the qsd_instance is started */
1076         read_lock(&qsd->qsd_lock);
1077         if (!qsd->qsd_started) {
1078                 read_unlock(&qsd->qsd_lock);
1079                 RETURN_EXIT;
1080         }
1081         read_unlock(&qsd->qsd_lock);
1082
1083         LASSERT(trans != NULL);
1084
1085         for (i = 0; i < trans->lqt_id_cnt; i++) {
1086                 struct qsd_qtype_info *qqi;
1087
1088                 if (trans->lqt_ids[i].lqi_qentry == NULL)
1089                         continue;
1090
1091                 qqi = qsd->qsd_type_array[trans->lqt_ids[i].lqi_type];
1092                 qsd_op_end0(env, qqi, &trans->lqt_ids[i]);
1093         }
1094
1095         /* reset id_count to 0 so that a second accidental call to qsd_op_end()
1096          * does not result in failure */
1097         trans->lqt_id_cnt = 0;
1098         EXIT;
1099 }
1100 EXPORT_SYMBOL(qsd_op_end);
1101
1102 /**
1103  * Trigger pre-acquire/release if necessary.
1104  * It's only used by ldiskfs osd so far. When unlink a file in ldiskfs, the
1105  * quota accounting isn't updated when the transaction stopped. Instead, it'll
1106  * be updated on the final iput, so qsd_op_adjust() will be called then (in
1107  * osd_object_delete()) to trigger quota release if necessary.
1108  *
1109  * \param env - the environment passed by the caller
1110  * \param qsd - is the qsd instance associated with the device in charge
1111  *              of the operation.
1112  * \param qid - is the lquota ID of the user/group for which to trigger
1113  *              quota space adjustment
1114  * \param qtype - is the quota type (USRQUOTA or GRPQUOTA)
1115  */
1116 void qsd_op_adjust(const struct lu_env *env, struct qsd_instance *qsd,
1117                    union lquota_id *qid, int qtype)
1118 {
1119         struct lquota_entry    *lqe;
1120         struct qsd_qtype_info  *qqi;
1121         bool                    adjust;
1122         ENTRY;
1123
1124         if (unlikely(qsd == NULL))
1125                 RETURN_EXIT;
1126
1127         /* We don't enforce quota until the qsd_instance is started */
1128         read_lock(&qsd->qsd_lock);
1129         if (!qsd->qsd_started) {
1130                 read_unlock(&qsd->qsd_lock);
1131                 RETURN_EXIT;
1132         }
1133         read_unlock(&qsd->qsd_lock);
1134
1135         qqi = qsd->qsd_type_array[qtype];
1136         LASSERT(qqi);
1137
1138         if (!qsd_type_enabled(qsd, qtype) || qqi->qqi_acct_obj == NULL ||
1139             qid->qid_uid == 0)
1140                 RETURN_EXIT;
1141
1142         read_lock(&qsd->qsd_lock);
1143         if (!qsd->qsd_started) {
1144                 read_unlock(&qsd->qsd_lock);
1145                 RETURN_EXIT;
1146         }
1147         read_unlock(&qsd->qsd_lock);
1148
1149         lqe = lqe_locate(env, qqi->qqi_site, qid);
1150         if (IS_ERR(lqe)) {
1151                 CERROR("%s: fail to locate lqe for id:"LPU64", type:%d\n",
1152                        qsd->qsd_svname, qid->qid_uid, qtype);
1153                 RETURN_EXIT;
1154         }
1155
1156         qsd_refresh_usage(env, lqe);
1157
1158         lqe_read_lock(lqe);
1159         adjust = qsd_adjust_needed(lqe);
1160         lqe_read_unlock(lqe);
1161
1162         if (adjust)
1163                 qsd_adjust(env, lqe);
1164
1165         lqe_putref(lqe);
1166         EXIT;
1167 }
1168 EXPORT_SYMBOL(qsd_op_adjust);