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