Whamcloud - gitweb
57fe0c0f21b712a8bdcb7a0ef7ddeed2ad75c37b
[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, 2017, 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=%llu",
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         wake_up(&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:%llu", repbody->qb_count);
354
355                 if (lqe->lqe_is_reset) {
356                         lqe->lqe_granted = 0;
357                 } else if (req_is_rel(reqbody->qb_flags)) {
358                         if (lqe->lqe_granted < repbody->qb_count) {
359                                 LQUOTA_ERROR(lqe, "can't release more space "
360                                              "than owned %llu<%llu",
361                                              lqe->lqe_granted,
362                                              repbody->qb_count);
363                                 lqe->lqe_granted = 0;
364                         } else {
365                                 lqe->lqe_granted -= repbody->qb_count;
366                         }
367                         /* Cancel the per-ID lock initiatively when there
368                          * isn't any usage & grant, which can avoid master
369                          * sending glimpse unnecessarily to this slave on
370                          * quota revoking */
371                         if (!lqe->lqe_pending_write && !lqe->lqe_granted &&
372                             !lqe->lqe_waiting_write && !lqe->lqe_usage)
373                                 cancel = true;
374                 } else {
375                         lqe->lqe_granted += repbody->qb_count;
376                 }
377                 qti->qti_rec.lqr_slv_rec.qsr_granted = lqe->lqe_granted;
378                 lqe_write_unlock(lqe);
379
380                 /* Update the slave index file in the dedicated thread. So far,
381                  * We don't update the version of slave index copy on DQACQ.
382                  * No locking is necessary since nobody can change
383                  * lqe->lqe_granted while lqe->lqe_pending_req > 0 */
384                 if (CFS_FAIL_CHECK(OBD_FAIL_QUOTA_GRANT))
385                         qti->qti_rec.lqr_slv_rec.qsr_granted =
386                                                         0xFFFFFFFFFFDEC80CULL;
387                 qsd_upd_schedule(qqi, lqe, &lqe->lqe_id, &qti->qti_rec, 0,
388                                  false);
389                 lqe_write_lock(lqe);
390         }
391
392         /* extract information from lvb */
393         if (ret == 0 && lvb != NULL) {
394                 if (lvb->lvb_id_qunit != 0)
395                         qsd_set_qunit(lqe, lvb->lvb_id_qunit);
396                 qsd_set_edquot(lqe, !!(lvb->lvb_flags & LQUOTA_FL_EDQUOT));
397         } else if (repbody != NULL && repbody->qb_qunit != 0) {
398                 qsd_set_qunit(lqe, repbody->qb_qunit);
399         }
400
401         /* turn off pre-acquire if it failed with -EDQUOT. This is done to avoid
402          * flooding the master with acquire request. Pre-acquire will be turned
403          * on again as soon as qunit is modified */
404         if (req_is_preacq(reqbody->qb_flags) && ret == -EDQUOT)
405                 lqe->lqe_nopreacq = true;
406 out:
407         adjust = qsd_adjust_needed(lqe);
408         if (reqbody && req_is_acq(reqbody->qb_flags) && ret != -EDQUOT) {
409                 lqe->lqe_acq_rc = ret;
410                 lqe->lqe_acq_time = ktime_get_seconds();
411         }
412 out_noadjust:
413         qsd_request_exit(lqe);
414         lqe_write_unlock(lqe);
415
416         /* release reference on per-ID lock */
417         if (lustre_handle_is_used(lockh))
418                 ldlm_lock_decref(lockh, qsd_id_einfo.ei_mode);
419
420         if (cancel) {
421                 qsd_adjust_schedule(lqe, false, true);
422         } else if (adjust) {
423                 if (!ret || ret == -EDQUOT)
424                         qsd_adjust_schedule(lqe, false, false);
425                 else
426                         qsd_adjust_schedule(lqe, true, false);
427         }
428         lqe_putref(lqe);
429
430         if (lvb)
431                 OBD_FREE_PTR(lvb);
432         EXIT;
433 }
434
435 /**
436  * Try to consume local quota space.
437  *
438  * \param lqe   - is the qid entry to be processed
439  * \param space - is the amount of quota space needed to complete the operation
440  *
441  * \retval 0       - success
442  * \retval -EDQUOT - out of quota
443  * \retval -EAGAIN - need to acquire space from master
444  */
445 static int qsd_acquire_local(struct lquota_entry *lqe, __u64 space)
446 {
447         __u64   usage;
448         int     rc;
449         ENTRY;
450
451         if (!lqe->lqe_enforced)
452                 /* not enforced any more, we are good */
453                 RETURN(-ESRCH);
454
455         lqe_write_lock(lqe);
456         /* use latest usage */
457         usage = lqe->lqe_usage;
458         /* take pending write into account */
459         usage += lqe->lqe_pending_write;
460
461         if (space + usage <= lqe->lqe_granted - lqe->lqe_pending_rel) {
462                 /* Yay! we got enough space */
463                 lqe->lqe_pending_write += space;
464                 lqe->lqe_waiting_write -= space;
465                 rc = 0;
466         /* lqe_edquot flag is used to avoid flooding dqacq requests when
467          * the user is over quota, however, the lqe_edquot could be stale
468          * sometimes due to the race reply of dqacq vs. id lock glimpse
469          * (see LU-4505), so we revalidate it every 5 seconds. */
470         } else if (lqe->lqe_edquot &&
471                    (lqe->lqe_edquot_time > ktime_get_seconds() - 5)) {
472                 rc = -EDQUOT;
473         }else {
474                 rc = -EAGAIN;
475         }
476         lqe_write_unlock(lqe);
477
478         RETURN(rc);
479 }
480
481 /**
482  * Compute how much quota space should be acquire from the master based
483  * on how much is currently granted to this slave and pending/waiting
484  * operations.
485  *
486  * \param lqe - is the lquota entry for which we would like to adjust quota
487  *              space.
488  * \param qbody - is the quota body of the acquire request to fill
489  *
490  * \retval true  - space acquisition is needed and qbody is filled
491  * \retval false - no space acquisition required
492  */
493 static inline bool qsd_calc_acquire(struct lquota_entry *lqe,
494                                     struct quota_body *qbody)
495 {
496         __u64   usage, granted;
497
498         usage   = lqe->lqe_usage;
499         usage  += lqe->lqe_pending_write + lqe->lqe_waiting_write;
500         granted = lqe->lqe_granted;
501
502         qbody->qb_flags = 0;
503
504         /* if we overconsumed quota space, we report usage in request so that
505          * master can adjust it unconditionally */
506         if (lqe->lqe_usage > lqe->lqe_granted) {
507                 qbody->qb_usage = lqe->lqe_usage;
508                 qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
509                 granted = lqe->lqe_usage;
510         }
511
512         /* acquire as much as needed, but not more */
513         if (usage > granted) {
514                 qbody->qb_count  = usage - granted;
515                 qbody->qb_flags |= QUOTA_DQACQ_FL_ACQ;
516         }
517
518         return qbody->qb_flags != 0;
519 }
520
521 /**
522  * Acquire quota space from master.
523  * There are at most 1 in-flight dqacq/dqrel.
524  *
525  * \param env    - the environment passed by the caller
526  * \param lqe    - is the qid entry to be processed
527  *
528  * \retval 0            - success
529  * \retval -EDQUOT      - out of quota
530  * \retval -EINPROGRESS - inform client to retry write/create
531  * \retval -EBUSY       - already a quota request in flight
532  * \retval -ve          - other appropriate errors
533  */
534 static int qsd_acquire_remote(const struct lu_env *env,
535                               struct lquota_entry *lqe)
536 {
537         struct qsd_thread_info  *qti = qsd_info(env);
538         struct quota_body       *qbody = &qti->qti_body;
539         struct qsd_instance     *qsd;
540         struct qsd_qtype_info   *qqi;
541         int                      rc;
542         ENTRY;
543
544         memset(qbody, 0, sizeof(*qbody));
545         rc = qsd_ready(lqe, &qbody->qb_glb_lockh);
546         if (rc)
547                 RETURN(rc);
548
549         qqi = lqe2qqi(lqe);
550         qsd = qqi->qqi_qsd;
551
552         lqe_write_lock(lqe);
553
554         /* is quota really enforced for this id? */
555         if (!lqe->lqe_enforced) {
556                 lqe_write_unlock(lqe);
557                 LQUOTA_DEBUG(lqe, "quota not enforced any more");
558                 RETURN(0);
559         }
560
561         /* fill qb_count & qb_flags */
562         if (!qsd_calc_acquire(lqe, qbody)) {
563                 lqe_write_unlock(lqe);
564                 LQUOTA_DEBUG(lqe, "No acquire required");
565                 RETURN(0);
566         }
567
568         /* check whether an acquire request completed recently */
569         if (lqe->lqe_acq_rc != 0 &&
570             lqe->lqe_acq_time > ktime_get_seconds() - 1) {
571                 lqe_write_unlock(lqe);
572                 LQUOTA_DEBUG(lqe, "using cached return code %d", lqe->lqe_acq_rc);
573                 RETURN(lqe->lqe_acq_rc);
574         }
575
576         /* only 1 quota request in flight for a given ID is allowed */
577         rc = qsd_request_enter(lqe);
578         if (rc) {
579                 lqe_write_unlock(lqe);
580                 RETURN(rc);
581         }
582
583         lustre_handle_copy(&qti->qti_lockh, &lqe->lqe_lockh);
584         lqe_write_unlock(lqe);
585
586         /* hold a refcount until completion */
587         lqe_getref(lqe);
588
589         /* fill other quota body fields */
590         qbody->qb_fid = qqi->qqi_fid;
591         qbody->qb_id  = lqe->lqe_id;
592
593         /* check whether we already own a valid lock for this ID */
594         rc = qsd_id_lock_match(&qti->qti_lockh, &qbody->qb_lockh);
595         if (rc) {
596                 struct lquota_lvb *lvb;
597
598                 OBD_ALLOC_PTR(lvb);
599                 if (lvb == NULL) {
600                         rc = -ENOMEM;
601                         qsd_req_completion(env, qqi, qbody, NULL,
602                                            &qti->qti_lockh, NULL, lqe, rc);
603                         RETURN(rc);
604                 }
605                 /* no lock found, should use intent */
606                 rc = qsd_intent_lock(env, qsd->qsd_exp, qbody, true,
607                                      IT_QUOTA_DQACQ, qsd_req_completion,
608                                      qqi, lvb, (void *)lqe);
609         } else {
610                 /* lock found, should use regular dqacq */
611                 rc = qsd_send_dqacq(env, qsd->qsd_exp, qbody, true,
612                                     qsd_req_completion, qqi, &qti->qti_lockh,
613                                     lqe);
614         }
615
616         /* the completion function will be called by qsd_send_dqacq or
617          * qsd_intent_lock */
618         RETURN(rc);
619 }
620
621 /**
622  * Acquire \a space of quota space in order to complete an operation.
623  * Try to consume local quota space first and send acquire request to quota
624  * master if required.
625  *
626  * \param env   - the environment passed by the caller
627  * \param lqe   - is the qid entry to be processed
628  * \param space - is the amount of quota required for the operation
629  * \param ret   - is the return code (-EDQUOT, -EINPROGRESS, ...)
630  *
631  * \retval true  - stop waiting in wait_event_idle_timeout,
632  *                 and real return value in \a ret
633  * \retval false - continue waiting
634  */
635 static bool qsd_acquire(const struct lu_env *env, struct lquota_entry *lqe,
636                         long long space, int *ret)
637 {
638         int rc = 0, count;
639         int wait_pending = 0;
640         struct qsd_qtype_info *qqi = lqe2qqi(lqe);
641
642         ENTRY;
643
644         for (count = 0; rc == 0; count++) {
645                 LQUOTA_DEBUG(lqe, "acquiring:%lld count=%d", space, count);
646 again:
647                 if (lqe2qqi(lqe)->qqi_qsd->qsd_stopping) {
648                         rc = -EINPROGRESS;
649                         break;
650                 }
651
652                 /* refresh disk usage */
653                 rc = qsd_refresh_usage(env, lqe);
654                 if (rc)
655                         break;
656
657                 /* try to consume local quota space first */
658                 rc = qsd_acquire_local(lqe, space);
659                 if (rc != -EAGAIN)
660                         /* rc == 0, Wouhou! enough local quota space
661                          * rc < 0, something bad happened */
662                          break;
663                 /*
664                  * There might be a window that commit transaction
665                  * have updated usage but pending write doesn't change
666                  * wait for it before acquiring remotely.
667                  */
668                 if (lqe->lqe_pending_write >= space && !wait_pending) {
669                         wait_pending = 1;
670                         dt_sync(env, qqi->qqi_qsd->qsd_dev);
671                         goto again;
672                 }
673
674                 /* if we have gotten some quota and stil wait more quota,
675                  * it's better to give QMT some time to reclaim from clients */
676                 if (count > 0)
677                         schedule_timeout_interruptible(cfs_time_seconds(1));
678
679                 /* need to acquire more quota space from master */
680                 rc = qsd_acquire_remote(env, lqe);
681         }
682
683         if (rc == -EBUSY)
684                 /* already a request in flight, continue waiting */
685                 RETURN(false);
686         *ret = rc;
687         RETURN(true);
688 }
689
690 /**
691  * Quota enforcement handler. If local quota can satisfy this operation,
692  * return success, otherwise, acquire more quota from master.
693  * (for write operation, if master isn't available at this moment, return
694  * -EINPROGRESS to inform client to retry the write)
695  *
696  * \param env   - the environment passed by the caller
697  * \param qsd   - is the qsd instance associated with the device in charge
698  *                of the operation.
699  * \param qid   - is the qid information attached in the transaction handle
700  * \param space - is the space required by the operation
701  * \param flags - if the operation is write, return caller no user/group
702  *                and sync commit flags
703  *
704  * \retval 0            - success
705  * \retval -EDQUOT      - out of quota
706  * \retval -EINPROGRESS - inform client to retry write
707  * \retval -ve          - other appropriate errors
708  */
709 static int qsd_op_begin0(const struct lu_env *env, struct qsd_qtype_info *qqi,
710                          struct lquota_id_info *qid, long long space,
711                          enum osd_quota_local_flags *local_flags)
712 {
713         struct lquota_entry *lqe;
714         enum osd_quota_local_flags qtype_flag = 0;
715         int rc, ret = -EINPROGRESS;
716         ENTRY;
717
718         if (qid->lqi_qentry != NULL) {
719                 /* we already had to deal with this id for this transaction */
720                 lqe = qid->lqi_qentry;
721                 if (!lqe->lqe_enforced)
722                         RETURN(0);
723         } else {
724                 /* look up lquota entry associated with qid */
725                 lqe = lqe_locate(env, qqi->qqi_site, &qid->lqi_id);
726                 if (IS_ERR(lqe))
727                         RETURN(PTR_ERR(lqe));
728                 if (!lqe->lqe_enforced) {
729                         lqe_putref(lqe);
730                         RETURN(0);
731                 }
732                 qid->lqi_qentry = lqe;
733                 /* lqe will be released in qsd_op_end() */
734         }
735
736         if (space <= 0) {
737                 /* when space is negative or null, we don't need to consume
738                  * quota space. That said, we still want to perform space
739                  * adjustments in qsd_op_end, so we return here, but with
740                  * a reference on the lqe */
741                 if (local_flags != NULL) {
742                         rc = qsd_refresh_usage(env, lqe);
743                         GOTO(out_flags, rc);
744                 }
745                 RETURN(0);
746         }
747
748         LQUOTA_DEBUG(lqe, "op_begin space:%lld", space);
749
750         lqe_write_lock(lqe);
751         lqe->lqe_waiting_write += space;
752         lqe_write_unlock(lqe);
753
754         /* acquire quota space for the operation, cap overall wait time to
755          * prevent a service thread from being stuck for too long */
756         rc = wait_event_idle_timeout(
757                 lqe->lqe_waiters, qsd_acquire(env, lqe, space, &ret),
758                 cfs_time_seconds(qsd_wait_timeout(qqi->qqi_qsd)));
759
760         if (rc > 0 && ret == 0) {
761                 qid->lqi_space += space;
762                 rc = 0;
763         } else {
764                 if (rc > 0)
765                         rc = ret;
766                 else if (rc == 0)
767                         rc = -ETIMEDOUT;
768
769                 LQUOTA_DEBUG(lqe, "acquire quota failed:%d", rc);
770
771                 lqe_write_lock(lqe);
772                 lqe->lqe_waiting_write -= space;
773
774                 if (local_flags && lqe->lqe_pending_write != 0)
775                         /* Inform OSD layer that there are pending writes.
776                          * It might want to retry after a sync if appropriate */
777                          *local_flags |= QUOTA_FL_SYNC;
778                 lqe_write_unlock(lqe);
779
780                 /* convert recoverable error into -EINPROGRESS, client will
781                  * retry */
782                 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ENOLCK ||
783                     rc == -EAGAIN || rc == -EINTR) {
784                         rc = -EINPROGRESS;
785                 } else if (rc == -ESRCH) {
786                         rc = 0;
787                         LQUOTA_ERROR(lqe, "ID isn't enforced on master, it "
788                                      "probably due to a legeal race, if this "
789                                      "message is showing up constantly, there "
790                                      "could be some inconsistence between "
791                                      "master & slave, and quota reintegration "
792                                      "needs be re-triggered.");
793                 }
794         }
795
796         if (local_flags != NULL) {
797 out_flags:
798                 LASSERT(qid->lqi_is_blk);
799                 if (rc != 0) {
800                         *local_flags |= lquota_over_fl(qqi->qqi_qtype);
801                 } else {
802                         __u64   usage;
803
804                         lqe_read_lock(lqe);
805                         usage = lqe->lqe_pending_write;
806                         usage += lqe->lqe_waiting_write;
807                         /* There is a chance to successfully grant more quota
808                          * but get edquot flag through glimpse. */
809                         if (lqe->lqe_edquot || (lqe->lqe_qunit != 0 &&
810                            (usage % lqe->lqe_qunit >
811                             qqi->qqi_qsd->qsd_sync_threshold)))
812                                 usage += qqi->qqi_qsd->qsd_sync_threshold;
813
814                         usage += lqe->lqe_usage;
815
816                         qtype_flag = lquota_over_fl(qqi->qqi_qtype);
817                         /* if we should notify client to start sync write */
818                         if (usage >= lqe->lqe_granted - lqe->lqe_pending_rel)
819                                 *local_flags |= qtype_flag;
820                         else
821                                 *local_flags &= ~qtype_flag;
822                         lqe_read_unlock(lqe);
823                 }
824         }
825         RETURN(rc);
826 }
827
828 /**
829  * helper function comparing two lquota_id_info structures
830  */
831 static inline bool qid_equal(struct lquota_id_info *q1,
832                              struct lquota_id_info *q2)
833 {
834         if (q1->lqi_is_blk != q2->lqi_is_blk || q1->lqi_type != q2->lqi_type)
835                 return false;
836         return (q1->lqi_id.qid_uid == q2->lqi_id.qid_uid) ? true : false;
837 }
838
839 /**
840  * Enforce quota, it's called in the declaration of each operation.
841  * qsd_op_end() will then be called later once all the operations have been
842  * completed in order to release/adjust the quota space.
843  *
844  * \param env   - the environment passed by the caller
845  * \param qsd   - is the qsd instance associated with the device in charge of
846  *                the operation.
847  * \param trans - is the quota transaction information
848  * \param qi    - qid & space required by current operation
849  * \param flags - if the operation is write, return caller no user/group and
850  *                sync commit flags
851  *
852  * \retval 0            - success
853  * \retval -EDQUOT      - out of quota
854  * \retval -EINPROGRESS - inform client to retry write
855  * \retval -ve          - other appropriate errors
856  */
857 int qsd_op_begin(const struct lu_env *env, struct qsd_instance *qsd,
858                  struct lquota_trans *trans, struct lquota_id_info *qi,
859                  enum osd_quota_local_flags *local_flags)
860 {
861         int     i, rc;
862         bool    found = false;
863         ENTRY;
864
865         /* fast path, ignore quota enforcement request for root owned files */
866         if (qi->lqi_id.qid_uid == 0)
867                 return 0;
868
869         if (unlikely(qsd == NULL))
870                 RETURN(0);
871
872         if (qsd->qsd_dev->dd_rdonly)
873                 RETURN(0);
874
875         /* We don't enforce quota until the qsd_instance is started */
876         read_lock(&qsd->qsd_lock);
877         if (!qsd->qsd_started) {
878                 read_unlock(&qsd->qsd_lock);
879                 RETURN(0);
880         }
881         read_unlock(&qsd->qsd_lock);
882
883         /* ignore block quota on MDTs, ignore inode quota on OSTs */
884         if ((!qsd->qsd_is_md && !qi->lqi_is_blk) ||
885             (qsd->qsd_is_md && qi->lqi_is_blk))
886                 RETURN(0);
887
888         /* ignore quota enforcement request when:
889          *    - quota isn't enforced for this quota type
890          * or - the user/group is root
891          * or - quota accounting isn't enabled */
892         if (!qsd_type_enabled(qsd, qi->lqi_type) ||
893             (qsd->qsd_type_array[qi->lqi_type])->qqi_acct_failed)
894                 RETURN(0);
895
896         if (local_flags && qi->lqi_id.qid_projid && qsd->qsd_root_prj_enable)
897                 *local_flags |= QUOTA_FL_ROOT_PRJQUOTA;
898
899         LASSERTF(trans->lqt_id_cnt <= QUOTA_MAX_TRANSIDS, "id_cnt=%d\n",
900                  trans->lqt_id_cnt);
901         /* check whether we already allocated a slot for this id */
902         for (i = 0; i < trans->lqt_id_cnt; i++) {
903                 if (qid_equal(qi, &trans->lqt_ids[i])) {
904                         found = true;
905                         break;
906                 }
907         }
908
909         if (!found) {
910                 if (unlikely(i >= QUOTA_MAX_TRANSIDS)) {
911                         CERROR("%s: more than %d qids enforced for a "
912                                "transaction?\n", qsd->qsd_svname, i);
913                         RETURN(-EINVAL);
914                 }
915
916                 /* fill new slot */
917                 trans->lqt_ids[i].lqi_id     = qi->lqi_id;
918                 trans->lqt_ids[i].lqi_type   = qi->lqi_type;
919                 trans->lqt_ids[i].lqi_is_blk = qi->lqi_is_blk;
920                 trans->lqt_id_cnt++;
921         }
922
923         /* manage quota enforcement for this ID */
924         rc = qsd_op_begin0(env, qsd->qsd_type_array[qi->lqi_type],
925                            &trans->lqt_ids[i], qi->lqi_space, local_flags);
926         RETURN(rc);
927 }
928 EXPORT_SYMBOL(qsd_op_begin);
929
930 /**
931  * Adjust quota space (by acquiring or releasing) hold by the quota slave.
932  * This function is called after each quota request completion and during
933  * reintegration in order to report usage or re-acquire quota locks.
934  * Space adjustment is aborted if there is already a quota request in flight
935  * for this ID.
936  *
937  * \param env    - the environment passed by the caller
938  * \param lqe    - is the qid entry to be processed
939  *
940  * \retval 0 on success, appropriate errors on failure
941  */
942 int qsd_adjust(const struct lu_env *env, struct lquota_entry *lqe)
943 {
944         struct qsd_thread_info  *qti = qsd_info(env);
945         struct quota_body       *qbody = &qti->qti_body;
946         struct qsd_instance     *qsd;
947         struct qsd_qtype_info   *qqi;
948         int                      rc;
949         bool                     intent = false;
950         ENTRY;
951
952         memset(qbody, 0, sizeof(*qbody));
953         rc = qsd_ready(lqe, &qbody->qb_glb_lockh);
954         if (rc) {
955                 /* add to adjust list again to trigger adjustment later when
956                  * slave is ready */
957                 LQUOTA_DEBUG(lqe, "delaying adjustment since qsd isn't ready");
958                 qsd_adjust_schedule(lqe, true, false);
959                 RETURN(0);
960         }
961
962         qqi = lqe2qqi(lqe);
963         qsd = qqi->qqi_qsd;
964
965         if (qsd->qsd_dev->dd_rdonly)
966                 RETURN(0);
967
968         lqe_write_lock(lqe);
969
970         /* fill qb_count & qb_flags */
971         if (!qsd_calc_adjust(lqe, qbody)) {
972                 lqe_write_unlock(lqe);
973                 LQUOTA_DEBUG(lqe, "no adjustment required");
974                 RETURN(0);
975         }
976
977         /* only 1 quota request in flight for a given ID is allowed */
978         rc = qsd_request_enter(lqe);
979         if (rc) {
980                 /* already a request in flight, space adjustment will be run
981                  * again on request completion */
982                 lqe_write_unlock(lqe);
983                 RETURN(0);
984         }
985
986         if (req_is_rel(qbody->qb_flags))
987                 lqe->lqe_pending_rel = qbody->qb_count;
988         lustre_handle_copy(&qti->qti_lockh, &lqe->lqe_lockh);
989         lqe_write_unlock(lqe);
990
991         /* hold a refcount until completion */
992         lqe_getref(lqe);
993
994         /* fill other quota body fields */
995         qbody->qb_fid = qqi->qqi_fid;
996         qbody->qb_id  = lqe->lqe_id;
997
998         if (req_is_acq(qbody->qb_flags) || req_is_preacq(qbody->qb_flags)) {
999                 /* check whether we own a valid lock for this ID */
1000                 rc = qsd_id_lock_match(&qti->qti_lockh, &qbody->qb_lockh);
1001                 if (rc) {
1002                         memset(&qti->qti_lockh, 0, sizeof(qti->qti_lockh));
1003                         if (req_is_preacq(qbody->qb_flags)) {
1004                                 if (req_has_rep(qbody->qb_flags))
1005                                         /* still want to report usage */
1006                                         qbody->qb_flags = QUOTA_DQACQ_FL_REPORT;
1007                                 else
1008                                         /* no pre-acquire if no per-ID lock */
1009                                         GOTO(out, rc = -ENOLCK);
1010                         } else {
1011                                 /* no lock found, should use intent */
1012                                 intent = true;
1013                         }
1014                 } else if (req_is_acq(qbody->qb_flags) &&
1015                            qbody->qb_count == 0) {
1016                         /* found cached lock, no need to acquire */
1017                         GOTO(out, rc = 0);
1018                 }
1019         } else {
1020                 /* release and report don't need a per-ID lock */
1021                 memset(&qti->qti_lockh, 0, sizeof(qti->qti_lockh));
1022         }
1023
1024         if (!intent) {
1025                 rc = qsd_send_dqacq(env, qsd->qsd_exp, qbody, false,
1026                                     qsd_req_completion, qqi, &qti->qti_lockh,
1027                                     lqe);
1028         } else {
1029                 struct lquota_lvb *lvb;
1030
1031                 OBD_ALLOC_PTR(lvb);
1032                 if (lvb == NULL)
1033                         GOTO(out, rc = -ENOMEM);
1034
1035                 rc = qsd_intent_lock(env, qsd->qsd_exp, qbody, false,
1036                                      IT_QUOTA_DQACQ, qsd_req_completion,
1037                                      qqi, lvb, (void *)lqe);
1038         }
1039         /* the completion function will be called by qsd_send_dqacq or
1040          * qsd_intent_lock */
1041         RETURN(rc);
1042 out:
1043         qsd_req_completion(env, qqi, qbody, NULL, &qti->qti_lockh, NULL, lqe,
1044                            rc);
1045         return rc;
1046 }
1047
1048 /**
1049  * Post quota operation, pre-acquire/release quota from master.
1050  *
1051  * \param  env  - the environment passed by the caller
1052  * \param  qsd  - is the qsd instance attached to the OSD device which
1053  *                is handling the operation.
1054  * \param  qqi  - is the qsd_qtype_info structure associated with the quota ID
1055  *                subject to the operation
1056  * \param  qid  - stores information related to his ID for the operation
1057  *                which has just completed
1058  *
1059  * \retval 0    - success
1060  * \retval -ve  - failure
1061  */
1062 static void qsd_op_end0(const struct lu_env *env, struct qsd_qtype_info *qqi,
1063                         struct lquota_id_info *qid)
1064 {
1065         struct lquota_entry     *lqe;
1066         bool                     adjust;
1067         ENTRY;
1068
1069         lqe = qid->lqi_qentry;
1070         if (lqe == NULL)
1071                 RETURN_EXIT;
1072         qid->lqi_qentry = NULL;
1073
1074         /* refresh cached usage if a suitable environment is passed */
1075         if (env != NULL)
1076                 qsd_refresh_usage(env, lqe);
1077
1078         lqe_write_lock(lqe);
1079         if (qid->lqi_space > 0) {
1080                 if (lqe->lqe_pending_write < qid->lqi_space) {
1081                         LQUOTA_ERROR(lqe,
1082                                      "More pending write quota to reduce (pending %llu, space %lld)\n",
1083                                      lqe->lqe_pending_write, qid->lqi_space);
1084                         lqe->lqe_pending_write = 0;
1085                 } else {
1086                         lqe->lqe_pending_write -= qid->lqi_space;
1087                 }
1088         }
1089         if (env != NULL)
1090                 adjust = qsd_adjust_needed(lqe);
1091         else
1092                 adjust = true;
1093         lqe_write_unlock(lqe);
1094
1095         if (adjust) {
1096                 /* pre-acquire/release quota space is needed */
1097                 if (env != NULL)
1098                         qsd_adjust(env, lqe);
1099                 else
1100                         /* no suitable environment, handle adjustment in
1101                          * separate thread context */
1102                         qsd_adjust_schedule(lqe, false, false);
1103         }
1104         lqe_putref(lqe);
1105         EXIT;
1106 }
1107
1108 /**
1109  * Post quota operation. It's called after each operation transaction stopped.
1110  *
1111  * \param  env   - the environment passed by the caller
1112  * \param  qsd   - is the qsd instance associated with device which is handling
1113  *                 the operation.
1114  * \param  qids  - all qids information attached in the transaction handle
1115  * \param  count - is the number of qid entries in the qids array.
1116  *
1117  * \retval 0     - success
1118  * \retval -ve   - failure
1119  */
1120 void qsd_op_end(const struct lu_env *env, struct qsd_instance *qsd,
1121                 struct lquota_trans *trans)
1122 {
1123         int i;
1124         ENTRY;
1125
1126         if (unlikely(qsd == NULL))
1127                 RETURN_EXIT;
1128
1129         if (qsd->qsd_dev->dd_rdonly)
1130                 RETURN_EXIT;
1131
1132         /* We don't enforce quota until the qsd_instance is started */
1133         read_lock(&qsd->qsd_lock);
1134         if (!qsd->qsd_started) {
1135                 read_unlock(&qsd->qsd_lock);
1136                 RETURN_EXIT;
1137         }
1138         read_unlock(&qsd->qsd_lock);
1139
1140         LASSERT(trans != NULL);
1141
1142         for (i = 0; i < trans->lqt_id_cnt; i++) {
1143                 struct qsd_qtype_info *qqi;
1144
1145                 if (trans->lqt_ids[i].lqi_qentry == NULL)
1146                         continue;
1147
1148                 qqi = qsd->qsd_type_array[trans->lqt_ids[i].lqi_type];
1149                 qsd_op_end0(env, qqi, &trans->lqt_ids[i]);
1150         }
1151
1152         /* reset id_count to 0 so that a second accidental call to qsd_op_end()
1153          * does not result in failure */
1154         trans->lqt_id_cnt = 0;
1155         EXIT;
1156 }
1157 EXPORT_SYMBOL(qsd_op_end);
1158
1159 /* Simple wrapper on top of qsd API which implement quota transfer for osd
1160  * setattr needs. As a reminder, only the root user can change ownership of
1161  * a file, that's why EDQUOT & EINPROGRESS errors are discarded
1162  */
1163 int qsd_transfer(const struct lu_env *env, struct qsd_instance *qsd,
1164                  struct lquota_trans *trans, unsigned int qtype,
1165                  u64 orig_id, u64 new_id, u64 bspace,
1166                  struct lquota_id_info *qi)
1167 {
1168         int rc;
1169
1170         if (unlikely(!qsd))
1171                 return 0;
1172
1173         LASSERT(qtype < LL_MAXQUOTAS);
1174         if (qtype == PRJQUOTA)
1175                 if (!projid_valid(make_kprojid(&init_user_ns, new_id)))
1176                         return -EINVAL;
1177
1178         qi->lqi_type = qtype;
1179
1180         /* inode accounting */
1181         qi->lqi_is_blk = false;
1182
1183         /* one more inode for the new owner ... */
1184         qi->lqi_id.qid_uid = new_id;
1185         qi->lqi_space = 1;
1186         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
1187         if (rc == -EDQUOT || rc == -EINPROGRESS)
1188                 rc = 0;
1189         if (rc)
1190                 return rc;
1191
1192         /* and one less inode for the current id */
1193         qi->lqi_id.qid_uid = orig_id;
1194         qi->lqi_space = -1;
1195         /* can't get EDQUOT when reducing usage */
1196         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
1197         if (rc == -EINPROGRESS)
1198                 rc = 0;
1199         if (rc)
1200                 return rc;
1201
1202         /* block accounting */
1203         qi->lqi_is_blk = true;
1204
1205         /* more blocks for the new owner ... */
1206         qi->lqi_id.qid_uid = new_id;
1207         qi->lqi_space = bspace;
1208         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
1209         if (rc == -EDQUOT || rc == -EINPROGRESS)
1210                 rc = 0;
1211         if (rc)
1212                 return rc;
1213
1214         /* and finally less blocks for the current owner */
1215         qi->lqi_id.qid_uid = orig_id;
1216         qi->lqi_space = -bspace;
1217         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
1218         /* can't get EDQUOT when reducing usage */
1219         if (rc == -EINPROGRESS)
1220                 rc = 0;
1221         return rc;
1222 }
1223 EXPORT_SYMBOL(qsd_transfer);
1224
1225 /**
1226  * Trigger pre-acquire/release if necessary.
1227  * It's only used by ldiskfs osd so far. When unlink a file in ldiskfs, the
1228  * quota accounting isn't updated when the transaction stopped. Instead, it'll
1229  * be updated on the final iput, so qsd_op_adjust() will be called then (in
1230  * osd_object_delete()) to trigger quota release if necessary.
1231  *
1232  * \param env - the environment passed by the caller
1233  * \param qsd - is the qsd instance associated with the device in charge
1234  *              of the operation.
1235  * \param qid - is the lquota ID of the user/group for which to trigger
1236  *              quota space adjustment
1237  * \param qtype - is the quota type (USRQUOTA or GRPQUOTA)
1238  */
1239 void qsd_op_adjust(const struct lu_env *env, struct qsd_instance *qsd,
1240                    union lquota_id *qid, int qtype)
1241 {
1242         struct lquota_entry    *lqe;
1243         struct qsd_qtype_info  *qqi;
1244         bool                    adjust;
1245         ENTRY;
1246
1247         if (unlikely(qsd == NULL))
1248                 RETURN_EXIT;
1249
1250         /* We don't enforce quota until the qsd_instance is started */
1251         read_lock(&qsd->qsd_lock);
1252         if (!qsd->qsd_started) {
1253                 read_unlock(&qsd->qsd_lock);
1254                 RETURN_EXIT;
1255         }
1256         read_unlock(&qsd->qsd_lock);
1257
1258         qqi = qsd->qsd_type_array[qtype];
1259         LASSERT(qqi);
1260
1261         if (!qsd_type_enabled(qsd, qtype) || qqi->qqi_acct_obj == NULL ||
1262             qid->qid_uid == 0 || qsd->qsd_dev->dd_rdonly)
1263                 RETURN_EXIT;
1264
1265         read_lock(&qsd->qsd_lock);
1266         if (!qsd->qsd_started) {
1267                 read_unlock(&qsd->qsd_lock);
1268                 RETURN_EXIT;
1269         }
1270         read_unlock(&qsd->qsd_lock);
1271
1272         lqe = lqe_locate(env, qqi->qqi_site, qid);
1273         if (IS_ERR(lqe)) {
1274                 CERROR("%s: fail to locate lqe for id:%llu, type:%d\n",
1275                        qsd->qsd_svname, qid->qid_uid, qtype);
1276                 RETURN_EXIT;
1277         }
1278
1279         qsd_refresh_usage(env, lqe);
1280
1281         lqe_read_lock(lqe);
1282         adjust = qsd_adjust_needed(lqe);
1283         lqe_read_unlock(lqe);
1284
1285         if (adjust)
1286                 qsd_adjust(env, lqe);
1287
1288         lqe_putref(lqe);
1289         EXIT;
1290 }
1291 EXPORT_SYMBOL(qsd_op_adjust);
1292
1293 /**
1294  * Reserve or free quota.
1295  *
1296  * Currently, It's used to reserve quota space before changing the file's group
1297  * for normal user and free the reserved quota after the group change.
1298  *
1299  * \param env     - the environment passed by the caller
1300  * \param qsd     - is the qsd instance associated with the device in charge of
1301  *                  the operation.
1302  * \param qi      - qid & space required by current operation
1303  *
1304  * \retval 0            - success
1305  * \retval -EDQUOT      - out of quota
1306  * \retval -EINPROGRESS - inform client to retry write
1307  * \retval -ve          - other appropriate errors
1308  */
1309 int qsd_reserve_or_free_quota(const struct lu_env *env,
1310                               struct qsd_instance *qsd,
1311                               struct lquota_id_info *qi)
1312 {
1313         struct qsd_qtype_info  *qqi;
1314         bool is_free = qi->lqi_space < 0;
1315         int rc = 0;
1316
1317         ENTRY;
1318
1319         if (unlikely(qsd == NULL))
1320                 RETURN(0);
1321
1322         if (qsd->qsd_dev->dd_rdonly)
1323                 RETURN(0);
1324
1325         if (is_free)
1326                 qi->lqi_space *= -1;
1327
1328         /* We don't enforce quota until the qsd_instance is started */
1329         read_lock(&qsd->qsd_lock);
1330         if (!qsd->qsd_started) {
1331                 read_unlock(&qsd->qsd_lock);
1332                 RETURN(0);
1333         }
1334         read_unlock(&qsd->qsd_lock);
1335
1336         qqi = qsd->qsd_type_array[qi->lqi_type];
1337         LASSERT(qqi);
1338
1339         CDEBUG(D_QUOTA, "type %s, acct %s, free %d, count %llu\n",
1340                qsd_type_enabled(qsd, qi->lqi_type) ? "enabled" : "disabled",
1341                (qsd->qsd_type_array[qi->lqi_type])->qqi_acct_failed ? "failed" :
1342                "succeed", is_free, qi->lqi_space);
1343
1344         /* ignore quota enforcement request when:
1345          *    - quota isn't enforced for this quota type
1346          * or - the user/group is root
1347          * or - quota accounting isn't enabled
1348          */
1349         if (!is_free &&
1350             (!qsd_type_enabled(qsd, qi->lqi_type) || qi->lqi_id.qid_uid == 0 ||
1351               (qsd->qsd_type_array[qi->lqi_type])->qqi_acct_failed))
1352                 RETURN(0);
1353
1354         if (is_free) {
1355                 qsd_op_end0(env, qsd->qsd_type_array[qi->lqi_type], qi);
1356         } else {
1357                 long long qspace = qi->lqi_space;
1358
1359                 /* the acquired quota will add to lqi_space in qsd_op_begin0 */
1360                 qi->lqi_space = 0;
1361                 rc = qsd_op_begin0(env, qsd->qsd_type_array[qi->lqi_type], qi,
1362                                    qspace, NULL);
1363                 if (rc && qi->lqi_qentry) {
1364                         lqe_putref(qi->lqi_qentry);
1365                         qi->lqi_qentry = NULL;
1366                 }
1367         }
1368
1369         CDEBUG(D_QUOTA, "%s quota: type %i, uid %llu, gid %llu, space %llu\n",
1370                is_free ? "Free" : "Reserve", qi->lqi_type, qi->lqi_id.qid_uid,
1371                qi->lqi_id.qid_gid, qi->lqi_space);
1372
1373         RETURN(rc);
1374 }
1375 EXPORT_SYMBOL(qsd_reserve_or_free_quota);