xfs_qm.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_fs.h"
  34. #include "xfs_inum.h"
  35. #include "xfs_log.h"
  36. #include "xfs_clnt.h"
  37. #include "xfs_trans.h"
  38. #include "xfs_sb.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_dir.h"
  41. #include "xfs_dir2.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_dmapi.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_mount.h"
  46. #include "xfs_alloc_btree.h"
  47. #include "xfs_bmap_btree.h"
  48. #include "xfs_ialloc_btree.h"
  49. #include "xfs_btree.h"
  50. #include "xfs_ialloc.h"
  51. #include "xfs_attr_sf.h"
  52. #include "xfs_dir_sf.h"
  53. #include "xfs_dir2_sf.h"
  54. #include "xfs_dinode.h"
  55. #include "xfs_inode.h"
  56. #include "xfs_bmap.h"
  57. #include "xfs_bit.h"
  58. #include "xfs_rtalloc.h"
  59. #include "xfs_error.h"
  60. #include "xfs_itable.h"
  61. #include "xfs_rw.h"
  62. #include "xfs_acl.h"
  63. #include "xfs_cap.h"
  64. #include "xfs_mac.h"
  65. #include "xfs_attr.h"
  66. #include "xfs_buf_item.h"
  67. #include "xfs_trans_space.h"
  68. #include "xfs_utils.h"
  69. #include "xfs_qm.h"
  70. /*
  71. * The global quota manager. There is only one of these for the entire
  72. * system, _not_ one per file system. XQM keeps track of the overall
  73. * quota functionality, including maintaining the freelist and hash
  74. * tables of dquots.
  75. */
  76. mutex_t xfs_Gqm_lock;
  77. struct xfs_qm *xfs_Gqm;
  78. kmem_zone_t *qm_dqzone;
  79. kmem_zone_t *qm_dqtrxzone;
  80. kmem_shaker_t xfs_qm_shaker;
  81. STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
  82. STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
  83. STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
  84. STATIC int xfs_qm_shake(int, unsigned int);
  85. #ifdef DEBUG
  86. extern mutex_t qcheck_lock;
  87. #endif
  88. #ifdef QUOTADEBUG
  89. #define XQM_LIST_PRINT(l, NXT, title) \
  90. { \
  91. xfs_dquot_t *dqp; int i = 0; \
  92. cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
  93. for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
  94. cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \
  95. "bcnt = %d, icnt = %d, refs = %d", \
  96. ++i, (int) INT_GET(dqp->q_core.d_id, ARCH_CONVERT), \
  97. DQFLAGTO_TYPESTR(dqp), \
  98. (int) INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT), \
  99. (int) INT_GET(dqp->q_core.d_icount, ARCH_CONVERT), \
  100. (int) dqp->q_nrefs); } \
  101. }
  102. #else
  103. #define XQM_LIST_PRINT(l, NXT, title) do { } while (0)
  104. #endif
  105. /*
  106. * Initialize the XQM structure.
  107. * Note that there is not one quota manager per file system.
  108. */
  109. STATIC struct xfs_qm *
  110. xfs_Gqm_init(void)
  111. {
  112. xfs_qm_t *xqm;
  113. int hsize, i;
  114. xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
  115. ASSERT(xqm);
  116. /*
  117. * Initialize the dquot hash tables.
  118. */
  119. hsize = (DQUOT_HASH_HEURISTIC < XFS_QM_NCSIZE_THRESHOLD) ?
  120. XFS_QM_HASHSIZE_LOW : XFS_QM_HASHSIZE_HIGH;
  121. xqm->qm_dqhashmask = hsize - 1;
  122. xqm->qm_usr_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
  123. sizeof(xfs_dqhash_t),
  124. KM_SLEEP);
  125. xqm->qm_grp_dqhtable = (xfs_dqhash_t *)kmem_zalloc(hsize *
  126. sizeof(xfs_dqhash_t),
  127. KM_SLEEP);
  128. ASSERT(xqm->qm_usr_dqhtable != NULL);
  129. ASSERT(xqm->qm_grp_dqhtable != NULL);
  130. for (i = 0; i < hsize; i++) {
  131. xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
  132. xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
  133. }
  134. /*
  135. * Freelist of all dquots of all file systems
  136. */
  137. xfs_qm_freelist_init(&(xqm->qm_dqfreelist));
  138. /*
  139. * dquot zone. we register our own low-memory callback.
  140. */
  141. if (!qm_dqzone) {
  142. xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
  143. "xfs_dquots");
  144. qm_dqzone = xqm->qm_dqzone;
  145. } else
  146. xqm->qm_dqzone = qm_dqzone;
  147. xfs_qm_shaker = kmem_shake_register(xfs_qm_shake);
  148. /*
  149. * The t_dqinfo portion of transactions.
  150. */
  151. if (!qm_dqtrxzone) {
  152. xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
  153. "xfs_dqtrx");
  154. qm_dqtrxzone = xqm->qm_dqtrxzone;
  155. } else
  156. xqm->qm_dqtrxzone = qm_dqtrxzone;
  157. atomic_set(&xqm->qm_totaldquots, 0);
  158. xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
  159. xqm->qm_nrefs = 0;
  160. #ifdef DEBUG
  161. mutex_init(&qcheck_lock, MUTEX_DEFAULT, "qchk");
  162. #endif
  163. return xqm;
  164. }
  165. /*
  166. * Destroy the global quota manager when its reference count goes to zero.
  167. */
  168. void
  169. xfs_qm_destroy(
  170. struct xfs_qm *xqm)
  171. {
  172. int hsize, i;
  173. ASSERT(xqm != NULL);
  174. ASSERT(xqm->qm_nrefs == 0);
  175. kmem_shake_deregister(xfs_qm_shaker);
  176. hsize = xqm->qm_dqhashmask + 1;
  177. for (i = 0; i < hsize; i++) {
  178. xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
  179. xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
  180. }
  181. kmem_free(xqm->qm_usr_dqhtable, hsize * sizeof(xfs_dqhash_t));
  182. kmem_free(xqm->qm_grp_dqhtable, hsize * sizeof(xfs_dqhash_t));
  183. xqm->qm_usr_dqhtable = NULL;
  184. xqm->qm_grp_dqhtable = NULL;
  185. xqm->qm_dqhashmask = 0;
  186. xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist));
  187. #ifdef DEBUG
  188. mutex_destroy(&qcheck_lock);
  189. #endif
  190. kmem_free(xqm, sizeof(xfs_qm_t));
  191. }
  192. /*
  193. * Called at mount time to let XQM know that another file system is
  194. * starting quotas. This isn't crucial information as the individual mount
  195. * structures are pretty independent, but it helps the XQM keep a
  196. * global view of what's going on.
  197. */
  198. /* ARGSUSED */
  199. STATIC int
  200. xfs_qm_hold_quotafs_ref(
  201. struct xfs_mount *mp)
  202. {
  203. /*
  204. * Need to lock the xfs_Gqm structure for things like this. For example,
  205. * the structure could disappear between the entry to this routine and
  206. * a HOLD operation if not locked.
  207. */
  208. XFS_QM_LOCK(xfs_Gqm);
  209. if (xfs_Gqm == NULL)
  210. xfs_Gqm = xfs_Gqm_init();
  211. /*
  212. * We can keep a list of all filesystems with quotas mounted for
  213. * debugging and statistical purposes, but ...
  214. * Just take a reference and get out.
  215. */
  216. XFS_QM_HOLD(xfs_Gqm);
  217. XFS_QM_UNLOCK(xfs_Gqm);
  218. return 0;
  219. }
  220. /*
  221. * Release the reference that a filesystem took at mount time,
  222. * so that we know when we need to destroy the entire quota manager.
  223. */
  224. /* ARGSUSED */
  225. STATIC void
  226. xfs_qm_rele_quotafs_ref(
  227. struct xfs_mount *mp)
  228. {
  229. xfs_dquot_t *dqp, *nextdqp;
  230. ASSERT(xfs_Gqm);
  231. ASSERT(xfs_Gqm->qm_nrefs > 0);
  232. /*
  233. * Go thru the freelist and destroy all inactive dquots.
  234. */
  235. xfs_qm_freelist_lock(xfs_Gqm);
  236. for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
  237. dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) {
  238. xfs_dqlock(dqp);
  239. nextdqp = dqp->dq_flnext;
  240. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  241. ASSERT(dqp->q_mount == NULL);
  242. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  243. ASSERT(dqp->HL_PREVP == NULL);
  244. ASSERT(dqp->MPL_PREVP == NULL);
  245. XQM_FREELIST_REMOVE(dqp);
  246. xfs_dqunlock(dqp);
  247. xfs_qm_dqdestroy(dqp);
  248. } else {
  249. xfs_dqunlock(dqp);
  250. }
  251. dqp = nextdqp;
  252. }
  253. xfs_qm_freelist_unlock(xfs_Gqm);
  254. /*
  255. * Destroy the entire XQM. If somebody mounts with quotaon, this'll
  256. * be restarted.
  257. */
  258. XFS_QM_LOCK(xfs_Gqm);
  259. XFS_QM_RELE(xfs_Gqm);
  260. if (xfs_Gqm->qm_nrefs == 0) {
  261. xfs_qm_destroy(xfs_Gqm);
  262. xfs_Gqm = NULL;
  263. }
  264. XFS_QM_UNLOCK(xfs_Gqm);
  265. }
  266. /*
  267. * This is called at mount time from xfs_mountfs to initialize the quotainfo
  268. * structure and start the global quotamanager (xfs_Gqm) if it hasn't done
  269. * so already. Note that the superblock has not been read in yet.
  270. */
  271. void
  272. xfs_qm_mount_quotainit(
  273. xfs_mount_t *mp,
  274. uint flags)
  275. {
  276. /*
  277. * User or group quotas has to be on.
  278. */
  279. ASSERT(flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA));
  280. /*
  281. * Initialize the flags in the mount structure. From this point
  282. * onwards we look at m_qflags to figure out if quotas's ON/OFF, etc.
  283. * Note that we enforce nothing if accounting is off.
  284. * ie. XFSMNT_*QUOTA must be ON for XFSMNT_*QUOTAENF.
  285. * It isn't necessary to take the quotaoff lock to do this; this is
  286. * called from mount.
  287. */
  288. if (flags & XFSMNT_UQUOTA) {
  289. mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
  290. if (flags & XFSMNT_UQUOTAENF)
  291. mp->m_qflags |= XFS_UQUOTA_ENFD;
  292. }
  293. if (flags & XFSMNT_GQUOTA) {
  294. mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
  295. if (flags & XFSMNT_GQUOTAENF)
  296. mp->m_qflags |= XFS_GQUOTA_ENFD;
  297. }
  298. }
  299. /*
  300. * Just destroy the quotainfo structure.
  301. */
  302. void
  303. xfs_qm_unmount_quotadestroy(
  304. xfs_mount_t *mp)
  305. {
  306. if (mp->m_quotainfo)
  307. xfs_qm_destroy_quotainfo(mp);
  308. }
  309. /*
  310. * This is called from xfs_mountfs to start quotas and initialize all
  311. * necessary data structures like quotainfo. This is also responsible for
  312. * running a quotacheck as necessary. We are guaranteed that the superblock
  313. * is consistently read in at this point.
  314. */
  315. int
  316. xfs_qm_mount_quotas(
  317. xfs_mount_t *mp,
  318. int mfsi_flags)
  319. {
  320. unsigned long s;
  321. int error = 0;
  322. uint sbf;
  323. /*
  324. * If a file system had quotas running earlier, but decided to
  325. * mount without -o quota/uquota/gquota options, revoke the
  326. * quotachecked license, and bail out.
  327. */
  328. if (! XFS_IS_QUOTA_ON(mp) &&
  329. (mp->m_sb.sb_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT))) {
  330. mp->m_qflags = 0;
  331. goto write_changes;
  332. }
  333. /*
  334. * If quotas on realtime volumes is not supported, we disable
  335. * quotas immediately.
  336. */
  337. if (mp->m_sb.sb_rextents) {
  338. cmn_err(CE_NOTE,
  339. "Cannot turn on quotas for realtime filesystem %s",
  340. mp->m_fsname);
  341. mp->m_qflags = 0;
  342. goto write_changes;
  343. }
  344. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  345. cmn_err(CE_NOTE, "Attempting to turn on disk quotas.");
  346. #endif
  347. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  348. /*
  349. * Allocate the quotainfo structure inside the mount struct, and
  350. * create quotainode(s), and change/rev superblock if necessary.
  351. */
  352. if ((error = xfs_qm_init_quotainfo(mp))) {
  353. /*
  354. * We must turn off quotas.
  355. */
  356. ASSERT(mp->m_quotainfo == NULL);
  357. mp->m_qflags = 0;
  358. goto write_changes;
  359. }
  360. /*
  361. * If any of the quotas are not consistent, do a quotacheck.
  362. */
  363. if (XFS_QM_NEED_QUOTACHECK(mp) &&
  364. !(mfsi_flags & XFS_MFSI_NO_QUOTACHECK)) {
  365. #ifdef DEBUG
  366. cmn_err(CE_NOTE, "Doing a quotacheck. Please wait.");
  367. #endif
  368. if ((error = xfs_qm_quotacheck(mp))) {
  369. /* Quotacheck has failed and quotas have
  370. * been disabled.
  371. */
  372. return XFS_ERROR(error);
  373. }
  374. #ifdef DEBUG
  375. cmn_err(CE_NOTE, "Done quotacheck.");
  376. #endif
  377. }
  378. write_changes:
  379. /*
  380. * We actually don't have to acquire the SB_LOCK at all.
  381. * This can only be called from mount, and that's single threaded. XXX
  382. */
  383. s = XFS_SB_LOCK(mp);
  384. sbf = mp->m_sb.sb_qflags;
  385. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  386. XFS_SB_UNLOCK(mp, s);
  387. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  388. if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
  389. /*
  390. * We could only have been turning quotas off.
  391. * We aren't in very good shape actually because
  392. * the incore structures are convinced that quotas are
  393. * off, but the on disk superblock doesn't know that !
  394. */
  395. ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
  396. xfs_fs_cmn_err(CE_ALERT, mp,
  397. "XFS mount_quotas: Superblock update failed!");
  398. }
  399. }
  400. if (error) {
  401. xfs_fs_cmn_err(CE_WARN, mp,
  402. "Failed to initialize disk quotas.");
  403. }
  404. return XFS_ERROR(error);
  405. }
  406. /*
  407. * Called from the vfsops layer.
  408. */
  409. int
  410. xfs_qm_unmount_quotas(
  411. xfs_mount_t *mp)
  412. {
  413. xfs_inode_t *uqp, *gqp;
  414. int error = 0;
  415. /*
  416. * Release the dquots that root inode, et al might be holding,
  417. * before we flush quotas and blow away the quotainfo structure.
  418. */
  419. ASSERT(mp->m_rootip);
  420. xfs_qm_dqdetach(mp->m_rootip);
  421. if (mp->m_rbmip)
  422. xfs_qm_dqdetach(mp->m_rbmip);
  423. if (mp->m_rsumip)
  424. xfs_qm_dqdetach(mp->m_rsumip);
  425. /*
  426. * Flush out the quota inodes.
  427. */
  428. uqp = gqp = NULL;
  429. if (mp->m_quotainfo) {
  430. if ((uqp = mp->m_quotainfo->qi_uquotaip) != NULL) {
  431. xfs_ilock(uqp, XFS_ILOCK_EXCL);
  432. xfs_iflock(uqp);
  433. error = xfs_iflush(uqp, XFS_IFLUSH_SYNC);
  434. xfs_iunlock(uqp, XFS_ILOCK_EXCL);
  435. if (unlikely(error == EFSCORRUPTED)) {
  436. XFS_ERROR_REPORT("xfs_qm_unmount_quotas(1)",
  437. XFS_ERRLEVEL_LOW, mp);
  438. goto out;
  439. }
  440. }
  441. if ((gqp = mp->m_quotainfo->qi_gquotaip) != NULL) {
  442. xfs_ilock(gqp, XFS_ILOCK_EXCL);
  443. xfs_iflock(gqp);
  444. error = xfs_iflush(gqp, XFS_IFLUSH_SYNC);
  445. xfs_iunlock(gqp, XFS_ILOCK_EXCL);
  446. if (unlikely(error == EFSCORRUPTED)) {
  447. XFS_ERROR_REPORT("xfs_qm_unmount_quotas(2)",
  448. XFS_ERRLEVEL_LOW, mp);
  449. goto out;
  450. }
  451. }
  452. }
  453. if (uqp) {
  454. XFS_PURGE_INODE(uqp);
  455. mp->m_quotainfo->qi_uquotaip = NULL;
  456. }
  457. if (gqp) {
  458. XFS_PURGE_INODE(gqp);
  459. mp->m_quotainfo->qi_gquotaip = NULL;
  460. }
  461. out:
  462. return XFS_ERROR(error);
  463. }
  464. /*
  465. * Flush all dquots of the given file system to disk. The dquots are
  466. * _not_ purged from memory here, just their data written to disk.
  467. */
  468. int
  469. xfs_qm_dqflush_all(
  470. xfs_mount_t *mp,
  471. int flags)
  472. {
  473. int recl;
  474. xfs_dquot_t *dqp;
  475. int niters;
  476. int error;
  477. if (mp->m_quotainfo == NULL)
  478. return (0);
  479. niters = 0;
  480. again:
  481. xfs_qm_mplist_lock(mp);
  482. FOREACH_DQUOT_IN_MP(dqp, mp) {
  483. xfs_dqlock(dqp);
  484. if (! XFS_DQ_IS_DIRTY(dqp)) {
  485. xfs_dqunlock(dqp);
  486. continue;
  487. }
  488. xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
  489. /* XXX a sentinel would be better */
  490. recl = XFS_QI_MPLRECLAIMS(mp);
  491. if (! xfs_qm_dqflock_nowait(dqp)) {
  492. /*
  493. * If we can't grab the flush lock then check
  494. * to see if the dquot has been flushed delayed
  495. * write. If so, grab its buffer and send it
  496. * out immediately. We'll be able to acquire
  497. * the flush lock when the I/O completes.
  498. */
  499. xfs_qm_dqflock_pushbuf_wait(dqp);
  500. }
  501. /*
  502. * Let go of the mplist lock. We don't want to hold it
  503. * across a disk write.
  504. */
  505. xfs_qm_mplist_unlock(mp);
  506. error = xfs_qm_dqflush(dqp, flags);
  507. xfs_dqunlock(dqp);
  508. if (error)
  509. return (error);
  510. xfs_qm_mplist_lock(mp);
  511. if (recl != XFS_QI_MPLRECLAIMS(mp)) {
  512. xfs_qm_mplist_unlock(mp);
  513. /* XXX restart limit */
  514. goto again;
  515. }
  516. }
  517. xfs_qm_mplist_unlock(mp);
  518. /* return ! busy */
  519. return (0);
  520. }
  521. /*
  522. * Release the group dquot pointers the user dquots may be
  523. * carrying around as a hint. mplist is locked on entry and exit.
  524. */
  525. STATIC void
  526. xfs_qm_detach_gdquots(
  527. xfs_mount_t *mp)
  528. {
  529. xfs_dquot_t *dqp, *gdqp;
  530. int nrecl;
  531. again:
  532. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  533. dqp = XFS_QI_MPLNEXT(mp);
  534. while (dqp) {
  535. xfs_dqlock(dqp);
  536. if ((gdqp = dqp->q_gdquot)) {
  537. xfs_dqlock(gdqp);
  538. dqp->q_gdquot = NULL;
  539. }
  540. xfs_dqunlock(dqp);
  541. if (gdqp) {
  542. /*
  543. * Can't hold the mplist lock across a dqput.
  544. * XXXmust convert to marker based iterations here.
  545. */
  546. nrecl = XFS_QI_MPLRECLAIMS(mp);
  547. xfs_qm_mplist_unlock(mp);
  548. xfs_qm_dqput(gdqp);
  549. xfs_qm_mplist_lock(mp);
  550. if (nrecl != XFS_QI_MPLRECLAIMS(mp))
  551. goto again;
  552. }
  553. dqp = dqp->MPL_NEXT;
  554. }
  555. }
  556. /*
  557. * Go through all the incore dquots of this file system and take them
  558. * off the mplist and hashlist, if the dquot type matches the dqtype
  559. * parameter. This is used when turning off quota accounting for
  560. * users and/or groups, as well as when the filesystem is unmounting.
  561. */
  562. STATIC int
  563. xfs_qm_dqpurge_int(
  564. xfs_mount_t *mp,
  565. uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/GQUOTA */
  566. {
  567. xfs_dquot_t *dqp;
  568. uint dqtype;
  569. int nrecl;
  570. xfs_dquot_t *nextdqp;
  571. int nmisses;
  572. if (mp->m_quotainfo == NULL)
  573. return (0);
  574. dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
  575. dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
  576. xfs_qm_mplist_lock(mp);
  577. /*
  578. * In the first pass through all incore dquots of this filesystem,
  579. * we release the group dquot pointers the user dquots may be
  580. * carrying around as a hint. We need to do this irrespective of
  581. * what's being turned off.
  582. */
  583. xfs_qm_detach_gdquots(mp);
  584. again:
  585. nmisses = 0;
  586. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  587. /*
  588. * Try to get rid of all of the unwanted dquots. The idea is to
  589. * get them off mplist and hashlist, but leave them on freelist.
  590. */
  591. dqp = XFS_QI_MPLNEXT(mp);
  592. while (dqp) {
  593. /*
  594. * It's OK to look at the type without taking dqlock here.
  595. * We're holding the mplist lock here, and that's needed for
  596. * a dqreclaim.
  597. */
  598. if ((dqp->dq_flags & dqtype) == 0) {
  599. dqp = dqp->MPL_NEXT;
  600. continue;
  601. }
  602. if (! xfs_qm_dqhashlock_nowait(dqp)) {
  603. nrecl = XFS_QI_MPLRECLAIMS(mp);
  604. xfs_qm_mplist_unlock(mp);
  605. XFS_DQ_HASH_LOCK(dqp->q_hash);
  606. xfs_qm_mplist_lock(mp);
  607. /*
  608. * XXXTheoretically, we can get into a very long
  609. * ping pong game here.
  610. * No one can be adding dquots to the mplist at
  611. * this point, but somebody might be taking things off.
  612. */
  613. if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
  614. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  615. goto again;
  616. }
  617. }
  618. /*
  619. * Take the dquot off the mplist and hashlist. It may remain on
  620. * freelist in INACTIVE state.
  621. */
  622. nextdqp = dqp->MPL_NEXT;
  623. nmisses += xfs_qm_dqpurge(dqp, flags);
  624. dqp = nextdqp;
  625. }
  626. xfs_qm_mplist_unlock(mp);
  627. return nmisses;
  628. }
  629. int
  630. xfs_qm_dqpurge_all(
  631. xfs_mount_t *mp,
  632. uint flags)
  633. {
  634. int ndquots;
  635. /*
  636. * Purge the dquot cache.
  637. * None of the dquots should really be busy at this point.
  638. */
  639. if (mp->m_quotainfo) {
  640. while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
  641. delay(ndquots * 10);
  642. }
  643. }
  644. return 0;
  645. }
  646. STATIC int
  647. xfs_qm_dqattach_one(
  648. xfs_inode_t *ip,
  649. xfs_dqid_t id,
  650. uint type,
  651. uint doalloc,
  652. uint dolock,
  653. xfs_dquot_t *udqhint, /* hint */
  654. xfs_dquot_t **IO_idqpp)
  655. {
  656. xfs_dquot_t *dqp;
  657. int error;
  658. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  659. error = 0;
  660. /*
  661. * See if we already have it in the inode itself. IO_idqpp is
  662. * &i_udquot or &i_gdquot. This made the code look weird, but
  663. * made the logic a lot simpler.
  664. */
  665. if ((dqp = *IO_idqpp)) {
  666. if (dolock)
  667. xfs_dqlock(dqp);
  668. xfs_dqtrace_entry(dqp, "DQATTACH: found in ip");
  669. goto done;
  670. }
  671. /*
  672. * udqhint is the i_udquot field in inode, and is non-NULL only
  673. * when the type arg is XFS_DQ_GROUP. Its purpose is to save a
  674. * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
  675. * the user dquot.
  676. */
  677. ASSERT(!udqhint || type == XFS_DQ_GROUP);
  678. if (udqhint && !dolock)
  679. xfs_dqlock(udqhint);
  680. /*
  681. * No need to take dqlock to look at the id.
  682. * The ID can't change until it gets reclaimed, and it won't
  683. * be reclaimed as long as we have a ref from inode and we hold
  684. * the ilock.
  685. */
  686. if (udqhint &&
  687. (dqp = udqhint->q_gdquot) &&
  688. (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id)) {
  689. ASSERT(XFS_DQ_IS_LOCKED(udqhint));
  690. xfs_dqlock(dqp);
  691. XFS_DQHOLD(dqp);
  692. ASSERT(*IO_idqpp == NULL);
  693. *IO_idqpp = dqp;
  694. if (!dolock) {
  695. xfs_dqunlock(dqp);
  696. xfs_dqunlock(udqhint);
  697. }
  698. goto done;
  699. }
  700. /*
  701. * We can't hold a dquot lock when we call the dqget code.
  702. * We'll deadlock in no time, because of (not conforming to)
  703. * lock ordering - the inodelock comes before any dquot lock,
  704. * and we may drop and reacquire the ilock in xfs_qm_dqget().
  705. */
  706. if (udqhint)
  707. xfs_dqunlock(udqhint);
  708. /*
  709. * Find the dquot from somewhere. This bumps the
  710. * reference count of dquot and returns it locked.
  711. * This can return ENOENT if dquot didn't exist on
  712. * disk and we didn't ask it to allocate;
  713. * ESRCH if quotas got turned off suddenly.
  714. */
  715. if ((error = xfs_qm_dqget(ip->i_mount, ip, id, type,
  716. doalloc|XFS_QMOPT_DOWARN, &dqp))) {
  717. if (udqhint && dolock)
  718. xfs_dqlock(udqhint);
  719. goto done;
  720. }
  721. xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget");
  722. /*
  723. * dqget may have dropped and re-acquired the ilock, but it guarantees
  724. * that the dquot returned is the one that should go in the inode.
  725. */
  726. *IO_idqpp = dqp;
  727. ASSERT(dqp);
  728. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  729. if (! dolock) {
  730. xfs_dqunlock(dqp);
  731. goto done;
  732. }
  733. if (! udqhint)
  734. goto done;
  735. ASSERT(udqhint);
  736. ASSERT(dolock);
  737. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  738. if (! xfs_qm_dqlock_nowait(udqhint)) {
  739. xfs_dqunlock(dqp);
  740. xfs_dqlock(udqhint);
  741. xfs_dqlock(dqp);
  742. }
  743. done:
  744. #ifdef QUOTADEBUG
  745. if (udqhint) {
  746. if (dolock)
  747. ASSERT(XFS_DQ_IS_LOCKED(udqhint));
  748. }
  749. if (! error) {
  750. if (dolock)
  751. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  752. }
  753. #endif
  754. return (error);
  755. }
  756. /*
  757. * Given a udquot and gdquot, attach a ptr to the group dquot in the
  758. * udquot as a hint for future lookups. The idea sounds simple, but the
  759. * execution isn't, because the udquot might have a group dquot attached
  760. * already and getting rid of that gets us into lock ordering contraints.
  761. * The process is complicated more by the fact that the dquots may or may not
  762. * be locked on entry.
  763. */
  764. STATIC void
  765. xfs_qm_dqattach_grouphint(
  766. xfs_dquot_t *udq,
  767. xfs_dquot_t *gdq,
  768. uint locked)
  769. {
  770. xfs_dquot_t *tmp;
  771. #ifdef QUOTADEBUG
  772. if (locked) {
  773. ASSERT(XFS_DQ_IS_LOCKED(udq));
  774. ASSERT(XFS_DQ_IS_LOCKED(gdq));
  775. }
  776. #endif
  777. if (! locked)
  778. xfs_dqlock(udq);
  779. if ((tmp = udq->q_gdquot)) {
  780. if (tmp == gdq) {
  781. if (! locked)
  782. xfs_dqunlock(udq);
  783. return;
  784. }
  785. udq->q_gdquot = NULL;
  786. /*
  787. * We can't keep any dqlocks when calling dqrele,
  788. * because the freelist lock comes before dqlocks.
  789. */
  790. xfs_dqunlock(udq);
  791. if (locked)
  792. xfs_dqunlock(gdq);
  793. /*
  794. * we took a hard reference once upon a time in dqget,
  795. * so give it back when the udquot no longer points at it
  796. * dqput() does the unlocking of the dquot.
  797. */
  798. xfs_qm_dqrele(tmp);
  799. xfs_dqlock(udq);
  800. xfs_dqlock(gdq);
  801. } else {
  802. ASSERT(XFS_DQ_IS_LOCKED(udq));
  803. if (! locked) {
  804. xfs_dqlock(gdq);
  805. }
  806. }
  807. ASSERT(XFS_DQ_IS_LOCKED(udq));
  808. ASSERT(XFS_DQ_IS_LOCKED(gdq));
  809. /*
  810. * Somebody could have attached a gdquot here,
  811. * when we dropped the uqlock. If so, just do nothing.
  812. */
  813. if (udq->q_gdquot == NULL) {
  814. XFS_DQHOLD(gdq);
  815. udq->q_gdquot = gdq;
  816. }
  817. if (! locked) {
  818. xfs_dqunlock(gdq);
  819. xfs_dqunlock(udq);
  820. }
  821. }
  822. /*
  823. * Given a locked inode, attach dquot(s) to it, taking UQUOTAON / GQUOTAON
  824. * in to account.
  825. * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
  826. * If XFS_QMOPT_DQLOCK, the dquot(s) will be returned locked. This option pretty
  827. * much made this code a complete mess, but it has been pretty useful.
  828. * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL.
  829. * Inode may get unlocked and relocked in here, and the caller must deal with
  830. * the consequences.
  831. */
  832. int
  833. xfs_qm_dqattach(
  834. xfs_inode_t *ip,
  835. uint flags)
  836. {
  837. xfs_mount_t *mp = ip->i_mount;
  838. uint nquotas = 0;
  839. int error = 0;
  840. if ((! XFS_IS_QUOTA_ON(mp)) ||
  841. (! XFS_NOT_DQATTACHED(mp, ip)) ||
  842. (ip->i_ino == mp->m_sb.sb_uquotino) ||
  843. (ip->i_ino == mp->m_sb.sb_gquotino))
  844. return (0);
  845. ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
  846. XFS_ISLOCKED_INODE_EXCL(ip));
  847. if (! (flags & XFS_QMOPT_ILOCKED))
  848. xfs_ilock(ip, XFS_ILOCK_EXCL);
  849. if (XFS_IS_UQUOTA_ON(mp)) {
  850. error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
  851. flags & XFS_QMOPT_DQALLOC,
  852. flags & XFS_QMOPT_DQLOCK,
  853. NULL, &ip->i_udquot);
  854. if (error)
  855. goto done;
  856. nquotas++;
  857. }
  858. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  859. if (XFS_IS_GQUOTA_ON(mp)) {
  860. error = xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
  861. flags & XFS_QMOPT_DQALLOC,
  862. flags & XFS_QMOPT_DQLOCK,
  863. ip->i_udquot, &ip->i_gdquot);
  864. /*
  865. * Don't worry about the udquot that we may have
  866. * attached above. It'll get detached, if not already.
  867. */
  868. if (error)
  869. goto done;
  870. nquotas++;
  871. }
  872. /*
  873. * Attach this group quota to the user quota as a hint.
  874. * This WON'T, in general, result in a thrash.
  875. */
  876. if (nquotas == 2) {
  877. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  878. ASSERT(ip->i_udquot);
  879. ASSERT(ip->i_gdquot);
  880. /*
  881. * We may or may not have the i_udquot locked at this point,
  882. * but this check is OK since we don't depend on the i_gdquot to
  883. * be accurate 100% all the time. It is just a hint, and this
  884. * will succeed in general.
  885. */
  886. if (ip->i_udquot->q_gdquot == ip->i_gdquot)
  887. goto done;
  888. /*
  889. * Attach i_gdquot to the gdquot hint inside the i_udquot.
  890. */
  891. xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot,
  892. flags & XFS_QMOPT_DQLOCK);
  893. }
  894. done:
  895. #ifdef QUOTADEBUG
  896. if (! error) {
  897. if (ip->i_udquot) {
  898. if (flags & XFS_QMOPT_DQLOCK)
  899. ASSERT(XFS_DQ_IS_LOCKED(ip->i_udquot));
  900. }
  901. if (ip->i_gdquot) {
  902. if (flags & XFS_QMOPT_DQLOCK)
  903. ASSERT(XFS_DQ_IS_LOCKED(ip->i_gdquot));
  904. }
  905. if (XFS_IS_UQUOTA_ON(mp))
  906. ASSERT(ip->i_udquot);
  907. if (XFS_IS_GQUOTA_ON(mp))
  908. ASSERT(ip->i_gdquot);
  909. }
  910. #endif
  911. if (! (flags & XFS_QMOPT_ILOCKED))
  912. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  913. #ifdef QUOTADEBUG
  914. else
  915. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  916. #endif
  917. return (error);
  918. }
  919. /*
  920. * Release dquots (and their references) if any.
  921. * The inode should be locked EXCL except when this's called by
  922. * xfs_ireclaim.
  923. */
  924. void
  925. xfs_qm_dqdetach(
  926. xfs_inode_t *ip)
  927. {
  928. if (!(ip->i_udquot || ip->i_gdquot))
  929. return;
  930. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
  931. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
  932. if (ip->i_udquot)
  933. xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
  934. if (ip->i_udquot) {
  935. xfs_qm_dqrele(ip->i_udquot);
  936. ip->i_udquot = NULL;
  937. }
  938. if (ip->i_gdquot) {
  939. xfs_qm_dqrele(ip->i_gdquot);
  940. ip->i_gdquot = NULL;
  941. }
  942. }
  943. /*
  944. * This is called by VFS_SYNC and flags arg determines the caller,
  945. * and its motives, as done in xfs_sync.
  946. *
  947. * vfs_sync: SYNC_FSDATA|SYNC_ATTR|SYNC_BDFLUSH 0x31
  948. * syscall sync: SYNC_FSDATA|SYNC_ATTR|SYNC_DELWRI 0x25
  949. * umountroot : SYNC_WAIT | SYNC_CLOSE | SYNC_ATTR | SYNC_FSDATA
  950. */
  951. int
  952. xfs_qm_sync(
  953. xfs_mount_t *mp,
  954. short flags)
  955. {
  956. int recl, restarts;
  957. xfs_dquot_t *dqp;
  958. uint flush_flags;
  959. boolean_t nowait;
  960. int error;
  961. restarts = 0;
  962. /*
  963. * We won't block unless we are asked to.
  964. */
  965. nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0);
  966. again:
  967. xfs_qm_mplist_lock(mp);
  968. /*
  969. * dqpurge_all() also takes the mplist lock and iterate thru all dquots
  970. * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
  971. * when we have the mplist lock, we know that dquots will be consistent
  972. * as long as we have it locked.
  973. */
  974. if (! XFS_IS_QUOTA_ON(mp)) {
  975. xfs_qm_mplist_unlock(mp);
  976. return (0);
  977. }
  978. FOREACH_DQUOT_IN_MP(dqp, mp) {
  979. /*
  980. * If this is vfs_sync calling, then skip the dquots that
  981. * don't 'seem' to be dirty. ie. don't acquire dqlock.
  982. * This is very similar to what xfs_sync does with inodes.
  983. */
  984. if (flags & SYNC_BDFLUSH) {
  985. if (! XFS_DQ_IS_DIRTY(dqp))
  986. continue;
  987. }
  988. if (nowait) {
  989. /*
  990. * Try to acquire the dquot lock. We are NOT out of
  991. * lock order, but we just don't want to wait for this
  992. * lock, unless somebody wanted us to.
  993. */
  994. if (! xfs_qm_dqlock_nowait(dqp))
  995. continue;
  996. } else {
  997. xfs_dqlock(dqp);
  998. }
  999. /*
  1000. * Now, find out for sure if this dquot is dirty or not.
  1001. */
  1002. if (! XFS_DQ_IS_DIRTY(dqp)) {
  1003. xfs_dqunlock(dqp);
  1004. continue;
  1005. }
  1006. /* XXX a sentinel would be better */
  1007. recl = XFS_QI_MPLRECLAIMS(mp);
  1008. if (! xfs_qm_dqflock_nowait(dqp)) {
  1009. if (nowait) {
  1010. xfs_dqunlock(dqp);
  1011. continue;
  1012. }
  1013. /*
  1014. * If we can't grab the flush lock then if the caller
  1015. * really wanted us to give this our best shot,
  1016. * see if we can give a push to the buffer before we wait
  1017. * on the flush lock. At this point, we know that
  1018. * eventhough the dquot is being flushed,
  1019. * it has (new) dirty data.
  1020. */
  1021. xfs_qm_dqflock_pushbuf_wait(dqp);
  1022. }
  1023. /*
  1024. * Let go of the mplist lock. We don't want to hold it
  1025. * across a disk write
  1026. */
  1027. flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC;
  1028. xfs_qm_mplist_unlock(mp);
  1029. xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH");
  1030. error = xfs_qm_dqflush(dqp, flush_flags);
  1031. xfs_dqunlock(dqp);
  1032. if (error && XFS_FORCED_SHUTDOWN(mp))
  1033. return(0); /* Need to prevent umount failure */
  1034. else if (error)
  1035. return (error);
  1036. xfs_qm_mplist_lock(mp);
  1037. if (recl != XFS_QI_MPLRECLAIMS(mp)) {
  1038. if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
  1039. break;
  1040. xfs_qm_mplist_unlock(mp);
  1041. goto again;
  1042. }
  1043. }
  1044. xfs_qm_mplist_unlock(mp);
  1045. return (0);
  1046. }
  1047. /*
  1048. * This initializes all the quota information that's kept in the
  1049. * mount structure
  1050. */
  1051. int
  1052. xfs_qm_init_quotainfo(
  1053. xfs_mount_t *mp)
  1054. {
  1055. xfs_quotainfo_t *qinf;
  1056. int error;
  1057. xfs_dquot_t *dqp;
  1058. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1059. /*
  1060. * Tell XQM that we exist as soon as possible.
  1061. */
  1062. if ((error = xfs_qm_hold_quotafs_ref(mp))) {
  1063. return (error);
  1064. }
  1065. qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
  1066. /*
  1067. * See if quotainodes are setup, and if not, allocate them,
  1068. * and change the superblock accordingly.
  1069. */
  1070. if ((error = xfs_qm_init_quotainos(mp))) {
  1071. kmem_free(qinf, sizeof(xfs_quotainfo_t));
  1072. mp->m_quotainfo = NULL;
  1073. return (error);
  1074. }
  1075. spinlock_init(&qinf->qi_pinlock, "xfs_qinf_pin");
  1076. xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
  1077. qinf->qi_dqreclaims = 0;
  1078. /* mutex used to serialize quotaoffs */
  1079. mutex_init(&qinf->qi_quotaofflock, MUTEX_DEFAULT, "qoff");
  1080. /* Precalc some constants */
  1081. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  1082. ASSERT(qinf->qi_dqchunklen);
  1083. qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
  1084. do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
  1085. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  1086. /*
  1087. * We try to get the limits from the superuser's limits fields.
  1088. * This is quite hacky, but it is standard quota practice.
  1089. * We look at the USR dquot with id == 0 first, but if user quotas
  1090. * are not enabled we goto the GRP dquot with id == 0.
  1091. * We don't really care to keep separate default limits for user
  1092. * and group quotas, at least not at this point.
  1093. */
  1094. error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
  1095. (XFS_IS_UQUOTA_RUNNING(mp)) ?
  1096. XFS_DQ_USER : XFS_DQ_GROUP,
  1097. XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
  1098. &dqp);
  1099. if (! error) {
  1100. xfs_disk_dquot_t *ddqp = &dqp->q_core;
  1101. /*
  1102. * The warnings and timers set the grace period given to
  1103. * a user or group before he or she can not perform any
  1104. * more writing. If it is zero, a default is used.
  1105. */
  1106. qinf->qi_btimelimit =
  1107. INT_GET(ddqp->d_btimer, ARCH_CONVERT) ?
  1108. INT_GET(ddqp->d_btimer, ARCH_CONVERT) :
  1109. XFS_QM_BTIMELIMIT;
  1110. qinf->qi_itimelimit =
  1111. INT_GET(ddqp->d_itimer, ARCH_CONVERT) ?
  1112. INT_GET(ddqp->d_itimer, ARCH_CONVERT) :
  1113. XFS_QM_ITIMELIMIT;
  1114. qinf->qi_rtbtimelimit =
  1115. INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) ?
  1116. INT_GET(ddqp->d_rtbtimer, ARCH_CONVERT) :
  1117. XFS_QM_RTBTIMELIMIT;
  1118. qinf->qi_bwarnlimit =
  1119. INT_GET(ddqp->d_bwarns, ARCH_CONVERT) ?
  1120. INT_GET(ddqp->d_bwarns, ARCH_CONVERT) :
  1121. XFS_QM_BWARNLIMIT;
  1122. qinf->qi_iwarnlimit =
  1123. INT_GET(ddqp->d_iwarns, ARCH_CONVERT) ?
  1124. INT_GET(ddqp->d_iwarns, ARCH_CONVERT) :
  1125. XFS_QM_IWARNLIMIT;
  1126. qinf->qi_bhardlimit =
  1127. INT_GET(ddqp->d_blk_hardlimit, ARCH_CONVERT);
  1128. qinf->qi_bsoftlimit =
  1129. INT_GET(ddqp->d_blk_softlimit, ARCH_CONVERT);
  1130. qinf->qi_ihardlimit =
  1131. INT_GET(ddqp->d_ino_hardlimit, ARCH_CONVERT);
  1132. qinf->qi_isoftlimit =
  1133. INT_GET(ddqp->d_ino_softlimit, ARCH_CONVERT);
  1134. qinf->qi_rtbhardlimit =
  1135. INT_GET(ddqp->d_rtb_hardlimit, ARCH_CONVERT);
  1136. qinf->qi_rtbsoftlimit =
  1137. INT_GET(ddqp->d_rtb_softlimit, ARCH_CONVERT);
  1138. /*
  1139. * We sent the XFS_QMOPT_DQSUSER flag to dqget because
  1140. * we don't want this dquot cached. We haven't done a
  1141. * quotacheck yet, and quotacheck doesn't like incore dquots.
  1142. */
  1143. xfs_qm_dqdestroy(dqp);
  1144. } else {
  1145. qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
  1146. qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
  1147. qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
  1148. qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
  1149. qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
  1150. }
  1151. return (0);
  1152. }
  1153. /*
  1154. * Gets called when unmounting a filesystem or when all quotas get
  1155. * turned off.
  1156. * This purges the quota inodes, destroys locks and frees itself.
  1157. */
  1158. void
  1159. xfs_qm_destroy_quotainfo(
  1160. xfs_mount_t *mp)
  1161. {
  1162. xfs_quotainfo_t *qi;
  1163. qi = mp->m_quotainfo;
  1164. ASSERT(qi != NULL);
  1165. ASSERT(xfs_Gqm != NULL);
  1166. /*
  1167. * Release the reference that XQM kept, so that we know
  1168. * when the XQM structure should be freed. We cannot assume
  1169. * that xfs_Gqm is non-null after this point.
  1170. */
  1171. xfs_qm_rele_quotafs_ref(mp);
  1172. spinlock_destroy(&qi->qi_pinlock);
  1173. xfs_qm_list_destroy(&qi->qi_dqlist);
  1174. if (qi->qi_uquotaip) {
  1175. XFS_PURGE_INODE(qi->qi_uquotaip);
  1176. qi->qi_uquotaip = NULL; /* paranoia */
  1177. }
  1178. if (qi->qi_gquotaip) {
  1179. XFS_PURGE_INODE(qi->qi_gquotaip);
  1180. qi->qi_gquotaip = NULL;
  1181. }
  1182. mutex_destroy(&qi->qi_quotaofflock);
  1183. kmem_free(qi, sizeof(xfs_quotainfo_t));
  1184. mp->m_quotainfo = NULL;
  1185. }
  1186. /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
  1187. /* ARGSUSED */
  1188. STATIC void
  1189. xfs_qm_list_init(
  1190. xfs_dqlist_t *list,
  1191. char *str,
  1192. int n)
  1193. {
  1194. mutex_init(&list->qh_lock, MUTEX_DEFAULT, str);
  1195. list->qh_next = NULL;
  1196. list->qh_version = 0;
  1197. list->qh_nelems = 0;
  1198. }
  1199. STATIC void
  1200. xfs_qm_list_destroy(
  1201. xfs_dqlist_t *list)
  1202. {
  1203. mutex_destroy(&(list->qh_lock));
  1204. }
  1205. /*
  1206. * Stripped down version of dqattach. This doesn't attach, or even look at the
  1207. * dquots attached to the inode. The rationale is that there won't be any
  1208. * attached at the time this is called from quotacheck.
  1209. */
  1210. STATIC int
  1211. xfs_qm_dqget_noattach(
  1212. xfs_inode_t *ip,
  1213. xfs_dquot_t **O_udqpp,
  1214. xfs_dquot_t **O_gdqpp)
  1215. {
  1216. int error;
  1217. xfs_mount_t *mp;
  1218. xfs_dquot_t *udqp, *gdqp;
  1219. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  1220. mp = ip->i_mount;
  1221. udqp = NULL;
  1222. gdqp = NULL;
  1223. if (XFS_IS_UQUOTA_ON(mp)) {
  1224. ASSERT(ip->i_udquot == NULL);
  1225. /*
  1226. * We want the dquot allocated if it doesn't exist.
  1227. */
  1228. if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
  1229. XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
  1230. &udqp))) {
  1231. /*
  1232. * Shouldn't be able to turn off quotas here.
  1233. */
  1234. ASSERT(error != ESRCH);
  1235. ASSERT(error != ENOENT);
  1236. return (error);
  1237. }
  1238. ASSERT(udqp);
  1239. }
  1240. if (XFS_IS_GQUOTA_ON(mp)) {
  1241. ASSERT(ip->i_gdquot == NULL);
  1242. if (udqp)
  1243. xfs_dqunlock(udqp);
  1244. if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_gid, XFS_DQ_GROUP,
  1245. XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
  1246. &gdqp))) {
  1247. if (udqp)
  1248. xfs_qm_dqrele(udqp);
  1249. ASSERT(error != ESRCH);
  1250. ASSERT(error != ENOENT);
  1251. return (error);
  1252. }
  1253. ASSERT(gdqp);
  1254. /* Reacquire the locks in the right order */
  1255. if (udqp) {
  1256. if (! xfs_qm_dqlock_nowait(udqp)) {
  1257. xfs_dqunlock(gdqp);
  1258. xfs_dqlock(udqp);
  1259. xfs_dqlock(gdqp);
  1260. }
  1261. }
  1262. }
  1263. *O_udqpp = udqp;
  1264. *O_gdqpp = gdqp;
  1265. #ifdef QUOTADEBUG
  1266. if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
  1267. if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
  1268. #endif
  1269. return (0);
  1270. }
  1271. /*
  1272. * Create an inode and return with a reference already taken, but unlocked
  1273. * This is how we create quota inodes
  1274. */
  1275. STATIC int
  1276. xfs_qm_qino_alloc(
  1277. xfs_mount_t *mp,
  1278. xfs_inode_t **ip,
  1279. __int64_t sbfields,
  1280. uint flags)
  1281. {
  1282. xfs_trans_t *tp;
  1283. int error;
  1284. unsigned long s;
  1285. cred_t zerocr;
  1286. int committed;
  1287. tp = xfs_trans_alloc(mp,XFS_TRANS_QM_QINOCREATE);
  1288. if ((error = xfs_trans_reserve(tp,
  1289. XFS_QM_QINOCREATE_SPACE_RES(mp),
  1290. XFS_CREATE_LOG_RES(mp), 0,
  1291. XFS_TRANS_PERM_LOG_RES,
  1292. XFS_CREATE_LOG_COUNT))) {
  1293. xfs_trans_cancel(tp, 0);
  1294. return (error);
  1295. }
  1296. memset(&zerocr, 0, sizeof(zerocr));
  1297. if ((error = xfs_dir_ialloc(&tp, mp->m_rootip, S_IFREG, 1, 0,
  1298. &zerocr, 0, 1, ip, &committed))) {
  1299. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  1300. XFS_TRANS_ABORT);
  1301. return (error);
  1302. }
  1303. /*
  1304. * Keep an extra reference to this quota inode. This inode is
  1305. * locked exclusively and joined to the transaction already.
  1306. */
  1307. ASSERT(XFS_ISLOCKED_INODE_EXCL(*ip));
  1308. VN_HOLD(XFS_ITOV((*ip)));
  1309. /*
  1310. * Make the changes in the superblock, and log those too.
  1311. * sbfields arg may contain fields other than *QUOTINO;
  1312. * VERSIONNUM for example.
  1313. */
  1314. s = XFS_SB_LOCK(mp);
  1315. if (flags & XFS_QMOPT_SBVERSION) {
  1316. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  1317. unsigned oldv = mp->m_sb.sb_versionnum;
  1318. #endif
  1319. ASSERT(!XFS_SB_VERSION_HASQUOTA(&mp->m_sb));
  1320. ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1321. XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
  1322. (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1323. XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
  1324. XFS_SB_VERSION_ADDQUOTA(&mp->m_sb);
  1325. mp->m_sb.sb_uquotino = NULLFSINO;
  1326. mp->m_sb.sb_gquotino = NULLFSINO;
  1327. /* qflags will get updated _after_ quotacheck */
  1328. mp->m_sb.sb_qflags = 0;
  1329. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  1330. cmn_err(CE_NOTE,
  1331. "Old superblock version %x, converting to %x.",
  1332. oldv, mp->m_sb.sb_versionnum);
  1333. #endif
  1334. }
  1335. if (flags & XFS_QMOPT_UQUOTA)
  1336. mp->m_sb.sb_uquotino = (*ip)->i_ino;
  1337. else
  1338. mp->m_sb.sb_gquotino = (*ip)->i_ino;
  1339. XFS_SB_UNLOCK(mp, s);
  1340. xfs_mod_sb(tp, sbfields);
  1341. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
  1342. NULL))) {
  1343. xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
  1344. return (error);
  1345. }
  1346. return (0);
  1347. }
  1348. STATIC int
  1349. xfs_qm_reset_dqcounts(
  1350. xfs_mount_t *mp,
  1351. xfs_buf_t *bp,
  1352. xfs_dqid_t id,
  1353. uint type)
  1354. {
  1355. xfs_disk_dquot_t *ddq;
  1356. int j;
  1357. xfs_buftrace("RESET DQUOTS", bp);
  1358. /*
  1359. * Reset all counters and timers. They'll be
  1360. * started afresh by xfs_qm_quotacheck.
  1361. */
  1362. #ifdef DEBUG
  1363. j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  1364. do_div(j, sizeof(xfs_dqblk_t));
  1365. ASSERT(XFS_QM_DQPERBLK(mp) == j);
  1366. #endif
  1367. ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
  1368. for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) {
  1369. /*
  1370. * Do a sanity check, and if needed, repair the dqblk. Don't
  1371. * output any warnings because it's perfectly possible to
  1372. * find unitialized dquot blks. See comment in xfs_qm_dqcheck.
  1373. */
  1374. (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
  1375. "xfs_quotacheck");
  1376. INT_SET(ddq->d_bcount, ARCH_CONVERT, 0ULL);
  1377. INT_SET(ddq->d_icount, ARCH_CONVERT, 0ULL);
  1378. INT_SET(ddq->d_rtbcount, ARCH_CONVERT, 0ULL);
  1379. INT_SET(ddq->d_btimer, ARCH_CONVERT, (time_t)0);
  1380. INT_SET(ddq->d_itimer, ARCH_CONVERT, (time_t)0);
  1381. INT_SET(ddq->d_bwarns, ARCH_CONVERT, 0UL);
  1382. INT_SET(ddq->d_iwarns, ARCH_CONVERT, 0UL);
  1383. ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
  1384. }
  1385. return (0);
  1386. }
  1387. STATIC int
  1388. xfs_qm_dqiter_bufs(
  1389. xfs_mount_t *mp,
  1390. xfs_dqid_t firstid,
  1391. xfs_fsblock_t bno,
  1392. xfs_filblks_t blkcnt,
  1393. uint flags)
  1394. {
  1395. xfs_buf_t *bp;
  1396. int error;
  1397. int notcommitted;
  1398. int incr;
  1399. ASSERT(blkcnt > 0);
  1400. notcommitted = 0;
  1401. incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
  1402. XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
  1403. error = 0;
  1404. /*
  1405. * Blkcnt arg can be a very big number, and might even be
  1406. * larger than the log itself. So, we have to break it up into
  1407. * manageable-sized transactions.
  1408. * Note that we don't start a permanent transaction here; we might
  1409. * not be able to get a log reservation for the whole thing up front,
  1410. * and we don't really care to either, because we just discard
  1411. * everything if we were to crash in the middle of this loop.
  1412. */
  1413. while (blkcnt--) {
  1414. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  1415. XFS_FSB_TO_DADDR(mp, bno),
  1416. (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp);
  1417. if (error)
  1418. break;
  1419. (void) xfs_qm_reset_dqcounts(mp, bp, firstid,
  1420. flags & XFS_QMOPT_UQUOTA ?
  1421. XFS_DQ_USER : XFS_DQ_GROUP);
  1422. xfs_bdwrite(mp, bp);
  1423. /*
  1424. * goto the next block.
  1425. */
  1426. bno++;
  1427. firstid += XFS_QM_DQPERBLK(mp);
  1428. }
  1429. return (error);
  1430. }
  1431. /*
  1432. * Iterate over all allocated USR/GRP dquots in the system, calling a
  1433. * caller supplied function for every chunk of dquots that we find.
  1434. */
  1435. STATIC int
  1436. xfs_qm_dqiterate(
  1437. xfs_mount_t *mp,
  1438. xfs_inode_t *qip,
  1439. uint flags)
  1440. {
  1441. xfs_bmbt_irec_t *map;
  1442. int i, nmaps; /* number of map entries */
  1443. int error; /* return value */
  1444. xfs_fileoff_t lblkno;
  1445. xfs_filblks_t maxlblkcnt;
  1446. xfs_dqid_t firstid;
  1447. xfs_fsblock_t rablkno;
  1448. xfs_filblks_t rablkcnt;
  1449. error = 0;
  1450. /*
  1451. * This looks racey, but we can't keep an inode lock across a
  1452. * trans_reserve. But, this gets called during quotacheck, and that
  1453. * happens only at mount time which is single threaded.
  1454. */
  1455. if (qip->i_d.di_nblocks == 0)
  1456. return (0);
  1457. map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
  1458. lblkno = 0;
  1459. maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1460. do {
  1461. nmaps = XFS_DQITER_MAP_SIZE;
  1462. /*
  1463. * We aren't changing the inode itself. Just changing
  1464. * some of its data. No new blocks are added here, and
  1465. * the inode is never added to the transaction.
  1466. */
  1467. xfs_ilock(qip, XFS_ILOCK_SHARED);
  1468. error = xfs_bmapi(NULL, qip, lblkno,
  1469. maxlblkcnt - lblkno,
  1470. XFS_BMAPI_METADATA,
  1471. NULL,
  1472. 0, map, &nmaps, NULL);
  1473. xfs_iunlock(qip, XFS_ILOCK_SHARED);
  1474. if (error)
  1475. break;
  1476. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  1477. for (i = 0; i < nmaps; i++) {
  1478. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  1479. ASSERT(map[i].br_blockcount);
  1480. lblkno += map[i].br_blockcount;
  1481. if (map[i].br_startblock == HOLESTARTBLOCK)
  1482. continue;
  1483. firstid = (xfs_dqid_t) map[i].br_startoff *
  1484. XFS_QM_DQPERBLK(mp);
  1485. /*
  1486. * Do a read-ahead on the next extent.
  1487. */
  1488. if ((i+1 < nmaps) &&
  1489. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  1490. rablkcnt = map[i+1].br_blockcount;
  1491. rablkno = map[i+1].br_startblock;
  1492. while (rablkcnt--) {
  1493. xfs_baread(mp->m_ddev_targp,
  1494. XFS_FSB_TO_DADDR(mp, rablkno),
  1495. (int)XFS_QI_DQCHUNKLEN(mp));
  1496. rablkno++;
  1497. }
  1498. }
  1499. /*
  1500. * Iterate thru all the blks in the extent and
  1501. * reset the counters of all the dquots inside them.
  1502. */
  1503. if ((error = xfs_qm_dqiter_bufs(mp,
  1504. firstid,
  1505. map[i].br_startblock,
  1506. map[i].br_blockcount,
  1507. flags))) {
  1508. break;
  1509. }
  1510. }
  1511. if (error)
  1512. break;
  1513. } while (nmaps > 0);
  1514. kmem_free(map, XFS_DQITER_MAP_SIZE * sizeof(*map));
  1515. return (error);
  1516. }
  1517. /*
  1518. * Called by dqusage_adjust in doing a quotacheck.
  1519. * Given the inode, and a dquot (either USR or GRP, doesn't matter),
  1520. * this updates its incore copy as well as the buffer copy. This is
  1521. * so that once the quotacheck is done, we can just log all the buffers,
  1522. * as opposed to logging numerous updates to individual dquots.
  1523. */
  1524. STATIC void
  1525. xfs_qm_quotacheck_dqadjust(
  1526. xfs_dquot_t *dqp,
  1527. xfs_qcnt_t nblks,
  1528. xfs_qcnt_t rtblks)
  1529. {
  1530. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1531. xfs_dqtrace_entry(dqp, "QCHECK DQADJUST");
  1532. /*
  1533. * Adjust the inode count and the block count to reflect this inode's
  1534. * resource usage.
  1535. */
  1536. INT_MOD(dqp->q_core.d_icount, ARCH_CONVERT, +1);
  1537. dqp->q_res_icount++;
  1538. if (nblks) {
  1539. INT_MOD(dqp->q_core.d_bcount, ARCH_CONVERT, nblks);
  1540. dqp->q_res_bcount += nblks;
  1541. }
  1542. if (rtblks) {
  1543. INT_MOD(dqp->q_core.d_rtbcount, ARCH_CONVERT, rtblks);
  1544. dqp->q_res_rtbcount += rtblks;
  1545. }
  1546. /*
  1547. * Set default limits, adjust timers (since we changed usages)
  1548. */
  1549. if (! XFS_IS_SUSER_DQUOT(dqp)) {
  1550. xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
  1551. xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
  1552. }
  1553. dqp->dq_flags |= XFS_DQ_DIRTY;
  1554. }
  1555. STATIC int
  1556. xfs_qm_get_rtblks(
  1557. xfs_inode_t *ip,
  1558. xfs_qcnt_t *O_rtblks)
  1559. {
  1560. xfs_filblks_t rtblks; /* total rt blks */
  1561. xfs_ifork_t *ifp; /* inode fork pointer */
  1562. xfs_extnum_t nextents; /* number of extent entries */
  1563. xfs_bmbt_rec_t *base; /* base of extent array */
  1564. xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
  1565. int error;
  1566. ASSERT(XFS_IS_REALTIME_INODE(ip));
  1567. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1568. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1569. if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
  1570. return (error);
  1571. }
  1572. rtblks = 0;
  1573. nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
  1574. base = &ifp->if_u1.if_extents[0];
  1575. for (ep = base; ep < &base[nextents]; ep++)
  1576. rtblks += xfs_bmbt_get_blockcount(ep);
  1577. *O_rtblks = (xfs_qcnt_t)rtblks;
  1578. return (0);
  1579. }
  1580. /*
  1581. * callback routine supplied to bulkstat(). Given an inumber, find its
  1582. * dquots and update them to account for resources taken by that inode.
  1583. */
  1584. /* ARGSUSED */
  1585. STATIC int
  1586. xfs_qm_dqusage_adjust(
  1587. xfs_mount_t *mp, /* mount point for filesystem */
  1588. xfs_ino_t ino, /* inode number to get data for */
  1589. void __user *buffer, /* not used */
  1590. int ubsize, /* not used */
  1591. void *private_data, /* not used */
  1592. xfs_daddr_t bno, /* starting block of inode cluster */
  1593. int *ubused, /* not used */
  1594. void *dip, /* on-disk inode pointer (not used) */
  1595. int *res) /* result code value */
  1596. {
  1597. xfs_inode_t *ip;
  1598. xfs_dquot_t *udqp, *gdqp;
  1599. xfs_qcnt_t nblks, rtblks;
  1600. int error;
  1601. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1602. /*
  1603. * rootino must have its resources accounted for, not so with the quota
  1604. * inodes.
  1605. */
  1606. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1607. *res = BULKSTAT_RV_NOTHING;
  1608. return XFS_ERROR(EINVAL);
  1609. }
  1610. /*
  1611. * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
  1612. * interface expects the inode to be exclusively locked because that's
  1613. * the case in all other instances. It's OK that we do this because
  1614. * quotacheck is done only at mount time.
  1615. */
  1616. if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
  1617. *res = BULKSTAT_RV_NOTHING;
  1618. return (error);
  1619. }
  1620. if (ip->i_d.di_mode == 0) {
  1621. xfs_iput_new(ip, XFS_ILOCK_EXCL);
  1622. *res = BULKSTAT_RV_NOTHING;
  1623. return XFS_ERROR(ENOENT);
  1624. }
  1625. /*
  1626. * Obtain the locked dquots. In case of an error (eg. allocation
  1627. * fails for ENOSPC), we return the negative of the error number
  1628. * to bulkstat, so that it can get propagated to quotacheck() and
  1629. * making us disable quotas for the file system.
  1630. */
  1631. if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
  1632. xfs_iput(ip, XFS_ILOCK_EXCL);
  1633. *res = BULKSTAT_RV_GIVEUP;
  1634. return (error);
  1635. }
  1636. rtblks = 0;
  1637. if (! XFS_IS_REALTIME_INODE(ip)) {
  1638. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
  1639. } else {
  1640. /*
  1641. * Walk thru the extent list and count the realtime blocks.
  1642. */
  1643. if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
  1644. xfs_iput(ip, XFS_ILOCK_EXCL);
  1645. if (udqp)
  1646. xfs_qm_dqput(udqp);
  1647. if (gdqp)
  1648. xfs_qm_dqput(gdqp);
  1649. *res = BULKSTAT_RV_GIVEUP;
  1650. return (error);
  1651. }
  1652. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
  1653. }
  1654. ASSERT(ip->i_delayed_blks == 0);
  1655. /*
  1656. * We can't release the inode while holding its dquot locks.
  1657. * The inode can go into inactive and might try to acquire the dquotlocks.
  1658. * So, just unlock here and do a vn_rele at the end.
  1659. */
  1660. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1661. /*
  1662. * Add the (disk blocks and inode) resources occupied by this
  1663. * inode to its dquots. We do this adjustment in the incore dquot,
  1664. * and also copy the changes to its buffer.
  1665. * We don't care about putting these changes in a transaction
  1666. * envelope because if we crash in the middle of a 'quotacheck'
  1667. * we have to start from the beginning anyway.
  1668. * Once we're done, we'll log all the dquot bufs.
  1669. *
  1670. * The *QUOTA_ON checks below may look pretty racey, but quotachecks
  1671. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1672. */
  1673. if (XFS_IS_UQUOTA_ON(mp)) {
  1674. ASSERT(udqp);
  1675. xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
  1676. xfs_qm_dqput(udqp);
  1677. }
  1678. if (XFS_IS_GQUOTA_ON(mp)) {
  1679. ASSERT(gdqp);
  1680. xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
  1681. xfs_qm_dqput(gdqp);
  1682. }
  1683. /*
  1684. * Now release the inode. This will send it to 'inactive', and
  1685. * possibly even free blocks.
  1686. */
  1687. VN_RELE(XFS_ITOV(ip));
  1688. /*
  1689. * Goto next inode.
  1690. */
  1691. *res = BULKSTAT_RV_DIDONE;
  1692. return (0);
  1693. }
  1694. /*
  1695. * Walk thru all the filesystem inodes and construct a consistent view
  1696. * of the disk quota world. If the quotacheck fails, disable quotas.
  1697. */
  1698. int
  1699. xfs_qm_quotacheck(
  1700. xfs_mount_t *mp)
  1701. {
  1702. int done, count, error;
  1703. xfs_ino_t lastino;
  1704. size_t structsz;
  1705. xfs_inode_t *uip, *gip;
  1706. uint flags;
  1707. count = INT_MAX;
  1708. structsz = 1;
  1709. lastino = 0;
  1710. flags = 0;
  1711. ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
  1712. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1713. /*
  1714. * There should be no cached dquots. The (simplistic) quotacheck
  1715. * algorithm doesn't like that.
  1716. */
  1717. ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
  1718. cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
  1719. /*
  1720. * First we go thru all the dquots on disk, USR and GRP, and reset
  1721. * their counters to zero. We need a clean slate.
  1722. * We don't log our changes till later.
  1723. */
  1724. if ((uip = XFS_QI_UQIP(mp))) {
  1725. if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
  1726. goto error_return;
  1727. flags |= XFS_UQUOTA_CHKD;
  1728. }
  1729. if ((gip = XFS_QI_GQIP(mp))) {
  1730. if ((error = xfs_qm_dqiterate(mp, gip, XFS_QMOPT_GQUOTA)))
  1731. goto error_return;
  1732. flags |= XFS_GQUOTA_CHKD;
  1733. }
  1734. do {
  1735. /*
  1736. * Iterate thru all the inodes in the file system,
  1737. * adjusting the corresponding dquot counters in core.
  1738. */
  1739. if ((error = xfs_bulkstat(mp, &lastino, &count,
  1740. xfs_qm_dqusage_adjust, NULL,
  1741. structsz, NULL,
  1742. BULKSTAT_FG_IGET|BULKSTAT_FG_VFSLOCKED,
  1743. &done)))
  1744. break;
  1745. } while (! done);
  1746. /*
  1747. * We can get this error if we couldn't do a dquot allocation inside
  1748. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1749. * dirty dquots that might be cached, we just want to get rid of them
  1750. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1751. * at this point (because we intentionally didn't in dqget_noattach).
  1752. */
  1753. if (error) {
  1754. xfs_qm_dqpurge_all(mp,
  1755. XFS_QMOPT_UQUOTA|XFS_QMOPT_GQUOTA|
  1756. XFS_QMOPT_QUOTAOFF);
  1757. goto error_return;
  1758. }
  1759. /*
  1760. * We've made all the changes that we need to make incore.
  1761. * Now flush_them down to disk buffers.
  1762. */
  1763. xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
  1764. /*
  1765. * We didn't log anything, because if we crashed, we'll have to
  1766. * start the quotacheck from scratch anyway. However, we must make
  1767. * sure that our dquot changes are secure before we put the
  1768. * quotacheck'd stamp on the superblock. So, here we do a synchronous
  1769. * flush.
  1770. */
  1771. XFS_bflush(mp->m_ddev_targp);
  1772. /*
  1773. * If one type of quotas is off, then it will lose its
  1774. * quotachecked status, since we won't be doing accounting for
  1775. * that type anymore.
  1776. */
  1777. mp->m_qflags &= ~(XFS_GQUOTA_CHKD | XFS_UQUOTA_CHKD);
  1778. mp->m_qflags |= flags;
  1779. XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
  1780. error_return:
  1781. if (error) {
  1782. cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
  1783. "Disabling quotas.",
  1784. mp->m_fsname, error);
  1785. /*
  1786. * We must turn off quotas.
  1787. */
  1788. ASSERT(mp->m_quotainfo != NULL);
  1789. ASSERT(xfs_Gqm != NULL);
  1790. xfs_qm_destroy_quotainfo(mp);
  1791. xfs_mount_reset_sbqflags(mp);
  1792. } else {
  1793. cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
  1794. }
  1795. return (error);
  1796. }
  1797. /*
  1798. * This is called after the superblock has been read in and we're ready to
  1799. * iget the quota inodes.
  1800. */
  1801. STATIC int
  1802. xfs_qm_init_quotainos(
  1803. xfs_mount_t *mp)
  1804. {
  1805. xfs_inode_t *uip, *gip;
  1806. int error;
  1807. __int64_t sbflags;
  1808. uint flags;
  1809. ASSERT(mp->m_quotainfo);
  1810. uip = gip = NULL;
  1811. sbflags = 0;
  1812. flags = 0;
  1813. /*
  1814. * Get the uquota and gquota inodes
  1815. */
  1816. if (XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
  1817. if (XFS_IS_UQUOTA_ON(mp) &&
  1818. mp->m_sb.sb_uquotino != NULLFSINO) {
  1819. ASSERT(mp->m_sb.sb_uquotino > 0);
  1820. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  1821. 0, 0, &uip, 0)))
  1822. return XFS_ERROR(error);
  1823. }
  1824. if (XFS_IS_GQUOTA_ON(mp) &&
  1825. mp->m_sb.sb_gquotino != NULLFSINO) {
  1826. ASSERT(mp->m_sb.sb_gquotino > 0);
  1827. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  1828. 0, 0, &gip, 0))) {
  1829. if (uip)
  1830. VN_RELE(XFS_ITOV(uip));
  1831. return XFS_ERROR(error);
  1832. }
  1833. }
  1834. } else {
  1835. flags |= XFS_QMOPT_SBVERSION;
  1836. sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1837. XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
  1838. }
  1839. /*
  1840. * Create the two inodes, if they don't exist already. The changes
  1841. * made above will get added to a transaction and logged in one of
  1842. * the qino_alloc calls below. If the device is readonly,
  1843. * temporarily switch to read-write to do this.
  1844. */
  1845. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1846. if ((error = xfs_qm_qino_alloc(mp, &uip,
  1847. sbflags | XFS_SB_UQUOTINO,
  1848. flags | XFS_QMOPT_UQUOTA)))
  1849. return XFS_ERROR(error);
  1850. flags &= ~XFS_QMOPT_SBVERSION;
  1851. }
  1852. if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
  1853. if ((error = xfs_qm_qino_alloc(mp, &gip,
  1854. sbflags | XFS_SB_GQUOTINO,
  1855. flags | XFS_QMOPT_GQUOTA))) {
  1856. if (uip)
  1857. VN_RELE(XFS_ITOV(uip));
  1858. return XFS_ERROR(error);
  1859. }
  1860. }
  1861. XFS_QI_UQIP(mp) = uip;
  1862. XFS_QI_GQIP(mp) = gip;
  1863. return (0);
  1864. }
  1865. /*
  1866. * Traverse the freelist of dquots and attempt to reclaim a maximum of
  1867. * 'howmany' dquots. This operation races with dqlookup(), and attempts to
  1868. * favor the lookup function ...
  1869. * XXXsup merge this with qm_reclaim_one().
  1870. */
  1871. STATIC int
  1872. xfs_qm_shake_freelist(
  1873. int howmany)
  1874. {
  1875. int nreclaimed;
  1876. xfs_dqhash_t *hash;
  1877. xfs_dquot_t *dqp, *nextdqp;
  1878. int restarts;
  1879. int nflushes;
  1880. if (howmany <= 0)
  1881. return (0);
  1882. nreclaimed = 0;
  1883. restarts = 0;
  1884. nflushes = 0;
  1885. #ifdef QUOTADEBUG
  1886. cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
  1887. #endif
  1888. /* lock order is : hashchainlock, freelistlock, mplistlock */
  1889. tryagain:
  1890. xfs_qm_freelist_lock(xfs_Gqm);
  1891. for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
  1892. ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
  1893. nreclaimed < howmany); ) {
  1894. xfs_dqlock(dqp);
  1895. /*
  1896. * We are racing with dqlookup here. Naturally we don't
  1897. * want to reclaim a dquot that lookup wants.
  1898. */
  1899. if (dqp->dq_flags & XFS_DQ_WANT) {
  1900. xfs_dqunlock(dqp);
  1901. xfs_qm_freelist_unlock(xfs_Gqm);
  1902. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1903. return (nreclaimed);
  1904. XQM_STATS_INC(xqmstats.xs_qm_dqwants);
  1905. goto tryagain;
  1906. }
  1907. /*
  1908. * If the dquot is inactive, we are assured that it is
  1909. * not on the mplist or the hashlist, and that makes our
  1910. * life easier.
  1911. */
  1912. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  1913. ASSERT(dqp->q_mount == NULL);
  1914. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  1915. ASSERT(dqp->HL_PREVP == NULL);
  1916. ASSERT(dqp->MPL_PREVP == NULL);
  1917. XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
  1918. nextdqp = dqp->dq_flnext;
  1919. goto off_freelist;
  1920. }
  1921. ASSERT(dqp->MPL_PREVP);
  1922. /*
  1923. * Try to grab the flush lock. If this dquot is in the process of
  1924. * getting flushed to disk, we don't want to reclaim it.
  1925. */
  1926. if (! xfs_qm_dqflock_nowait(dqp)) {
  1927. xfs_dqunlock(dqp);
  1928. dqp = dqp->dq_flnext;
  1929. continue;
  1930. }
  1931. /*
  1932. * We have the flush lock so we know that this is not in the
  1933. * process of being flushed. So, if this is dirty, flush it
  1934. * DELWRI so that we don't get a freelist infested with
  1935. * dirty dquots.
  1936. */
  1937. if (XFS_DQ_IS_DIRTY(dqp)) {
  1938. xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
  1939. /*
  1940. * We flush it delayed write, so don't bother
  1941. * releasing the mplock.
  1942. */
  1943. (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
  1944. xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
  1945. dqp = dqp->dq_flnext;
  1946. continue;
  1947. }
  1948. /*
  1949. * We're trying to get the hashlock out of order. This races
  1950. * with dqlookup; so, we giveup and goto the next dquot if
  1951. * we couldn't get the hashlock. This way, we won't starve
  1952. * a dqlookup process that holds the hashlock that is
  1953. * waiting for the freelist lock.
  1954. */
  1955. if (! xfs_qm_dqhashlock_nowait(dqp)) {
  1956. xfs_dqfunlock(dqp);
  1957. xfs_dqunlock(dqp);
  1958. dqp = dqp->dq_flnext;
  1959. continue;
  1960. }
  1961. /*
  1962. * This races with dquot allocation code as well as dqflush_all
  1963. * and reclaim code. So, if we failed to grab the mplist lock,
  1964. * giveup everything and start over.
  1965. */
  1966. hash = dqp->q_hash;
  1967. ASSERT(hash);
  1968. if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
  1969. /* XXX put a sentinel so that we can come back here */
  1970. xfs_dqfunlock(dqp);
  1971. xfs_dqunlock(dqp);
  1972. XFS_DQ_HASH_UNLOCK(hash);
  1973. xfs_qm_freelist_unlock(xfs_Gqm);
  1974. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1975. return (nreclaimed);
  1976. goto tryagain;
  1977. }
  1978. xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
  1979. #ifdef QUOTADEBUG
  1980. cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
  1981. dqp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
  1982. #endif
  1983. ASSERT(dqp->q_nrefs == 0);
  1984. nextdqp = dqp->dq_flnext;
  1985. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
  1986. XQM_HASHLIST_REMOVE(hash, dqp);
  1987. xfs_dqfunlock(dqp);
  1988. xfs_qm_mplist_unlock(dqp->q_mount);
  1989. XFS_DQ_HASH_UNLOCK(hash);
  1990. off_freelist:
  1991. XQM_FREELIST_REMOVE(dqp);
  1992. xfs_dqunlock(dqp);
  1993. nreclaimed++;
  1994. XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
  1995. xfs_qm_dqdestroy(dqp);
  1996. dqp = nextdqp;
  1997. }
  1998. xfs_qm_freelist_unlock(xfs_Gqm);
  1999. return (nreclaimed);
  2000. }
  2001. /*
  2002. * The kmem_shake interface is invoked when memory is running low.
  2003. */
  2004. /* ARGSUSED */
  2005. STATIC int
  2006. xfs_qm_shake(int nr_to_scan, unsigned int gfp_mask)
  2007. {
  2008. int ndqused, nfree, n;
  2009. if (!kmem_shake_allow(gfp_mask))
  2010. return (0);
  2011. if (!xfs_Gqm)
  2012. return (0);
  2013. nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
  2014. /* incore dquots in all f/s's */
  2015. ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
  2016. ASSERT(ndqused >= 0);
  2017. if (nfree <= ndqused && nfree < ndquot)
  2018. return (0);
  2019. ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
  2020. n = nfree - ndqused - ndquot; /* # over target */
  2021. return xfs_qm_shake_freelist(MAX(nfree, n));
  2022. }
  2023. /*
  2024. * Just pop the least recently used dquot off the freelist and
  2025. * recycle it. The returned dquot is locked.
  2026. */
  2027. STATIC xfs_dquot_t *
  2028. xfs_qm_dqreclaim_one(void)
  2029. {
  2030. xfs_dquot_t *dqpout;
  2031. xfs_dquot_t *dqp;
  2032. int restarts;
  2033. int nflushes;
  2034. restarts = 0;
  2035. dqpout = NULL;
  2036. nflushes = 0;
  2037. /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
  2038. startagain:
  2039. xfs_qm_freelist_lock(xfs_Gqm);
  2040. FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
  2041. xfs_dqlock(dqp);
  2042. /*
  2043. * We are racing with dqlookup here. Naturally we don't
  2044. * want to reclaim a dquot that lookup wants. We release the
  2045. * freelist lock and start over, so that lookup will grab
  2046. * both the dquot and the freelistlock.
  2047. */
  2048. if (dqp->dq_flags & XFS_DQ_WANT) {
  2049. ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
  2050. xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT");
  2051. xfs_dqunlock(dqp);
  2052. xfs_qm_freelist_unlock(xfs_Gqm);
  2053. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  2054. return (NULL);
  2055. XQM_STATS_INC(xqmstats.xs_qm_dqwants);
  2056. goto startagain;
  2057. }
  2058. /*
  2059. * If the dquot is inactive, we are assured that it is
  2060. * not on the mplist or the hashlist, and that makes our
  2061. * life easier.
  2062. */
  2063. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  2064. ASSERT(dqp->q_mount == NULL);
  2065. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  2066. ASSERT(dqp->HL_PREVP == NULL);
  2067. ASSERT(dqp->MPL_PREVP == NULL);
  2068. XQM_FREELIST_REMOVE(dqp);
  2069. xfs_dqunlock(dqp);
  2070. dqpout = dqp;
  2071. XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
  2072. break;
  2073. }
  2074. ASSERT(dqp->q_hash);
  2075. ASSERT(dqp->MPL_PREVP);
  2076. /*
  2077. * Try to grab the flush lock. If this dquot is in the process of
  2078. * getting flushed to disk, we don't want to reclaim it.
  2079. */
  2080. if (! xfs_qm_dqflock_nowait(dqp)) {
  2081. xfs_dqunlock(dqp);
  2082. continue;
  2083. }
  2084. /*
  2085. * We have the flush lock so we know that this is not in the
  2086. * process of being flushed. So, if this is dirty, flush it
  2087. * DELWRI so that we don't get a freelist infested with
  2088. * dirty dquots.
  2089. */
  2090. if (XFS_DQ_IS_DIRTY(dqp)) {
  2091. xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
  2092. /*
  2093. * We flush it delayed write, so don't bother
  2094. * releasing the freelist lock.
  2095. */
  2096. (void) xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
  2097. xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
  2098. continue;
  2099. }
  2100. if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
  2101. xfs_dqfunlock(dqp);
  2102. xfs_dqunlock(dqp);
  2103. continue;
  2104. }
  2105. if (! xfs_qm_dqhashlock_nowait(dqp))
  2106. goto mplistunlock;
  2107. ASSERT(dqp->q_nrefs == 0);
  2108. xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING");
  2109. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
  2110. XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
  2111. XQM_FREELIST_REMOVE(dqp);
  2112. dqpout = dqp;
  2113. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  2114. mplistunlock:
  2115. xfs_qm_mplist_unlock(dqp->q_mount);
  2116. xfs_dqfunlock(dqp);
  2117. xfs_dqunlock(dqp);
  2118. if (dqpout)
  2119. break;
  2120. }
  2121. xfs_qm_freelist_unlock(xfs_Gqm);
  2122. return (dqpout);
  2123. }
  2124. /*------------------------------------------------------------------*/
  2125. /*
  2126. * Return a new incore dquot. Depending on the number of
  2127. * dquots in the system, we either allocate a new one on the kernel heap,
  2128. * or reclaim a free one.
  2129. * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
  2130. * to reclaim an existing one from the freelist.
  2131. */
  2132. boolean_t
  2133. xfs_qm_dqalloc_incore(
  2134. xfs_dquot_t **O_dqpp)
  2135. {
  2136. xfs_dquot_t *dqp;
  2137. /*
  2138. * Check against high water mark to see if we want to pop
  2139. * a nincompoop dquot off the freelist.
  2140. */
  2141. if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
  2142. /*
  2143. * Try to recycle a dquot from the freelist.
  2144. */
  2145. if ((dqp = xfs_qm_dqreclaim_one())) {
  2146. XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
  2147. /*
  2148. * Just zero the core here. The rest will get
  2149. * reinitialized by caller. XXX we shouldn't even
  2150. * do this zero ...
  2151. */
  2152. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  2153. *O_dqpp = dqp;
  2154. return (B_FALSE);
  2155. }
  2156. XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
  2157. }
  2158. /*
  2159. * Allocate a brand new dquot on the kernel heap and return it
  2160. * to the caller to initialize.
  2161. */
  2162. ASSERT(xfs_Gqm->qm_dqzone != NULL);
  2163. *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
  2164. atomic_inc(&xfs_Gqm->qm_totaldquots);
  2165. return (B_TRUE);
  2166. }
  2167. /*
  2168. * Start a transaction and write the incore superblock changes to
  2169. * disk. flags parameter indicates which fields have changed.
  2170. */
  2171. int
  2172. xfs_qm_write_sb_changes(
  2173. xfs_mount_t *mp,
  2174. __int64_t flags)
  2175. {
  2176. xfs_trans_t *tp;
  2177. int error;
  2178. #ifdef QUOTADEBUG
  2179. cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
  2180. #endif
  2181. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  2182. if ((error = xfs_trans_reserve(tp, 0,
  2183. mp->m_sb.sb_sectsize + 128, 0,
  2184. 0,
  2185. XFS_DEFAULT_LOG_COUNT))) {
  2186. xfs_trans_cancel(tp, 0);
  2187. return (error);
  2188. }
  2189. xfs_mod_sb(tp, flags);
  2190. (void) xfs_trans_commit(tp, 0, NULL);
  2191. return (0);
  2192. }
  2193. /* --------------- utility functions for vnodeops ---------------- */
  2194. /*
  2195. * Given an inode, a uid and gid (from cred_t) make sure that we have
  2196. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  2197. * quotas by creating this file.
  2198. * This also attaches dquot(s) to the given inode after locking it,
  2199. * and returns the dquots corresponding to the uid and/or gid.
  2200. *
  2201. * in : inode (unlocked)
  2202. * out : udquot, gdquot with references taken and unlocked
  2203. */
  2204. int
  2205. xfs_qm_vop_dqalloc(
  2206. xfs_mount_t *mp,
  2207. xfs_inode_t *ip,
  2208. uid_t uid,
  2209. gid_t gid,
  2210. uint flags,
  2211. xfs_dquot_t **O_udqpp,
  2212. xfs_dquot_t **O_gdqpp)
  2213. {
  2214. int error;
  2215. xfs_dquot_t *uq, *gq;
  2216. uint lockflags;
  2217. if (!XFS_IS_QUOTA_ON(mp))
  2218. return 0;
  2219. lockflags = XFS_ILOCK_EXCL;
  2220. xfs_ilock(ip, lockflags);
  2221. if ((flags & XFS_QMOPT_INHERIT) &&
  2222. XFS_INHERIT_GID(ip, XFS_MTOVFS(mp)))
  2223. gid = ip->i_d.di_gid;
  2224. /*
  2225. * Attach the dquot(s) to this inode, doing a dquot allocation
  2226. * if necessary. The dquot(s) will not be locked.
  2227. */
  2228. if (XFS_NOT_DQATTACHED(mp, ip)) {
  2229. if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
  2230. XFS_QMOPT_ILOCKED))) {
  2231. xfs_iunlock(ip, lockflags);
  2232. return (error);
  2233. }
  2234. }
  2235. uq = gq = NULL;
  2236. if ((flags & XFS_QMOPT_UQUOTA) &&
  2237. XFS_IS_UQUOTA_ON(mp)) {
  2238. if (ip->i_d.di_uid != uid) {
  2239. /*
  2240. * What we need is the dquot that has this uid, and
  2241. * if we send the inode to dqget, the uid of the inode
  2242. * takes priority over what's sent in the uid argument.
  2243. * We must unlock inode here before calling dqget if
  2244. * we're not sending the inode, because otherwise
  2245. * we'll deadlock by doing trans_reserve while
  2246. * holding ilock.
  2247. */
  2248. xfs_iunlock(ip, lockflags);
  2249. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
  2250. XFS_DQ_USER,
  2251. XFS_QMOPT_DQALLOC |
  2252. XFS_QMOPT_DOWARN,
  2253. &uq))) {
  2254. ASSERT(error != ENOENT);
  2255. return (error);
  2256. }
  2257. /*
  2258. * Get the ilock in the right order.
  2259. */
  2260. xfs_dqunlock(uq);
  2261. lockflags = XFS_ILOCK_SHARED;
  2262. xfs_ilock(ip, lockflags);
  2263. } else {
  2264. /*
  2265. * Take an extra reference, because we'll return
  2266. * this to caller
  2267. */
  2268. ASSERT(ip->i_udquot);
  2269. uq = ip->i_udquot;
  2270. xfs_dqlock(uq);
  2271. XFS_DQHOLD(uq);
  2272. xfs_dqunlock(uq);
  2273. }
  2274. }
  2275. if ((flags & XFS_QMOPT_GQUOTA) &&
  2276. XFS_IS_GQUOTA_ON(mp)) {
  2277. if (ip->i_d.di_gid != gid) {
  2278. xfs_iunlock(ip, lockflags);
  2279. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
  2280. XFS_DQ_GROUP,
  2281. XFS_QMOPT_DQALLOC |
  2282. XFS_QMOPT_DOWARN,
  2283. &gq))) {
  2284. if (uq)
  2285. xfs_qm_dqrele(uq);
  2286. ASSERT(error != ENOENT);
  2287. return (error);
  2288. }
  2289. xfs_dqunlock(gq);
  2290. lockflags = XFS_ILOCK_SHARED;
  2291. xfs_ilock(ip, lockflags);
  2292. } else {
  2293. ASSERT(ip->i_gdquot);
  2294. gq = ip->i_gdquot;
  2295. xfs_dqlock(gq);
  2296. XFS_DQHOLD(gq);
  2297. xfs_dqunlock(gq);
  2298. }
  2299. }
  2300. if (uq)
  2301. xfs_dqtrace_entry_ino(uq, "DQALLOC", ip);
  2302. xfs_iunlock(ip, lockflags);
  2303. if (O_udqpp)
  2304. *O_udqpp = uq;
  2305. else if (uq)
  2306. xfs_qm_dqrele(uq);
  2307. if (O_gdqpp)
  2308. *O_gdqpp = gq;
  2309. else if (gq)
  2310. xfs_qm_dqrele(gq);
  2311. return (0);
  2312. }
  2313. /*
  2314. * Actually transfer ownership, and do dquot modifications.
  2315. * These were already reserved.
  2316. */
  2317. xfs_dquot_t *
  2318. xfs_qm_vop_chown(
  2319. xfs_trans_t *tp,
  2320. xfs_inode_t *ip,
  2321. xfs_dquot_t **IO_olddq,
  2322. xfs_dquot_t *newdq)
  2323. {
  2324. xfs_dquot_t *prevdq;
  2325. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  2326. ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
  2327. /* old dquot */
  2328. prevdq = *IO_olddq;
  2329. ASSERT(prevdq);
  2330. ASSERT(prevdq != newdq);
  2331. xfs_trans_mod_dquot(tp, prevdq,
  2332. XFS_TRANS_DQ_BCOUNT,
  2333. -(ip->i_d.di_nblocks));
  2334. xfs_trans_mod_dquot(tp, prevdq,
  2335. XFS_TRANS_DQ_ICOUNT,
  2336. -1);
  2337. /* the sparkling new dquot */
  2338. xfs_trans_mod_dquot(tp, newdq,
  2339. XFS_TRANS_DQ_BCOUNT,
  2340. ip->i_d.di_nblocks);
  2341. xfs_trans_mod_dquot(tp, newdq,
  2342. XFS_TRANS_DQ_ICOUNT,
  2343. 1);
  2344. /*
  2345. * Take an extra reference, because the inode
  2346. * is going to keep this dquot pointer even
  2347. * after the trans_commit.
  2348. */
  2349. xfs_dqlock(newdq);
  2350. XFS_DQHOLD(newdq);
  2351. xfs_dqunlock(newdq);
  2352. *IO_olddq = newdq;
  2353. return (prevdq);
  2354. }
  2355. /*
  2356. * Quota reservations for setattr(AT_UID|AT_GID).
  2357. */
  2358. int
  2359. xfs_qm_vop_chown_reserve(
  2360. xfs_trans_t *tp,
  2361. xfs_inode_t *ip,
  2362. xfs_dquot_t *udqp,
  2363. xfs_dquot_t *gdqp,
  2364. uint flags)
  2365. {
  2366. int error;
  2367. xfs_mount_t *mp;
  2368. uint delblks;
  2369. xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
  2370. ASSERT(XFS_ISLOCKED_INODE(ip));
  2371. mp = ip->i_mount;
  2372. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  2373. delblks = ip->i_delayed_blks;
  2374. delblksudq = delblksgdq = unresudq = unresgdq = NULL;
  2375. if (XFS_IS_UQUOTA_ON(mp) && udqp &&
  2376. ip->i_d.di_uid != (uid_t)INT_GET(udqp->q_core.d_id, ARCH_CONVERT)) {
  2377. delblksudq = udqp;
  2378. /*
  2379. * If there are delayed allocation blocks, then we have to
  2380. * unreserve those from the old dquot, and add them to the
  2381. * new dquot.
  2382. */
  2383. if (delblks) {
  2384. ASSERT(ip->i_udquot);
  2385. unresudq = ip->i_udquot;
  2386. }
  2387. }
  2388. if (XFS_IS_GQUOTA_ON(ip->i_mount) && gdqp &&
  2389. ip->i_d.di_gid != INT_GET(gdqp->q_core.d_id, ARCH_CONVERT)) {
  2390. delblksgdq = gdqp;
  2391. if (delblks) {
  2392. ASSERT(ip->i_gdquot);
  2393. unresgdq = ip->i_gdquot;
  2394. }
  2395. }
  2396. if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
  2397. delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
  2398. flags | XFS_QMOPT_RES_REGBLKS)))
  2399. return (error);
  2400. /*
  2401. * Do the delayed blks reservations/unreservations now. Since, these
  2402. * are done without the help of a transaction, if a reservation fails
  2403. * its previous reservations won't be automatically undone by trans
  2404. * code. So, we have to do it manually here.
  2405. */
  2406. if (delblks) {
  2407. /*
  2408. * Do the reservations first. Unreservation can't fail.
  2409. */
  2410. ASSERT(delblksudq || delblksgdq);
  2411. ASSERT(unresudq || unresgdq);
  2412. if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2413. delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
  2414. flags | XFS_QMOPT_RES_REGBLKS)))
  2415. return (error);
  2416. xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2417. unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
  2418. XFS_QMOPT_RES_REGBLKS);
  2419. }
  2420. return (0);
  2421. }
  2422. int
  2423. xfs_qm_vop_rename_dqattach(
  2424. xfs_inode_t **i_tab)
  2425. {
  2426. xfs_inode_t *ip;
  2427. int i;
  2428. int error;
  2429. ip = i_tab[0];
  2430. if (! XFS_IS_QUOTA_ON(ip->i_mount))
  2431. return (0);
  2432. if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
  2433. error = xfs_qm_dqattach(ip, 0);
  2434. if (error)
  2435. return (error);
  2436. }
  2437. for (i = 1; (i < 4 && i_tab[i]); i++) {
  2438. /*
  2439. * Watch out for duplicate entries in the table.
  2440. */
  2441. if ((ip = i_tab[i]) != i_tab[i-1]) {
  2442. if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
  2443. error = xfs_qm_dqattach(ip, 0);
  2444. if (error)
  2445. return (error);
  2446. }
  2447. }
  2448. }
  2449. return (0);
  2450. }
  2451. void
  2452. xfs_qm_vop_dqattach_and_dqmod_newinode(
  2453. xfs_trans_t *tp,
  2454. xfs_inode_t *ip,
  2455. xfs_dquot_t *udqp,
  2456. xfs_dquot_t *gdqp)
  2457. {
  2458. if (!XFS_IS_QUOTA_ON(tp->t_mountp))
  2459. return;
  2460. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  2461. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  2462. if (udqp) {
  2463. xfs_dqlock(udqp);
  2464. XFS_DQHOLD(udqp);
  2465. xfs_dqunlock(udqp);
  2466. ASSERT(ip->i_udquot == NULL);
  2467. ip->i_udquot = udqp;
  2468. ASSERT(ip->i_d.di_uid == INT_GET(udqp->q_core.d_id, ARCH_CONVERT));
  2469. xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
  2470. }
  2471. if (gdqp) {
  2472. xfs_dqlock(gdqp);
  2473. XFS_DQHOLD(gdqp);
  2474. xfs_dqunlock(gdqp);
  2475. ASSERT(ip->i_gdquot == NULL);
  2476. ip->i_gdquot = gdqp;
  2477. ASSERT(ip->i_d.di_gid == INT_GET(gdqp->q_core.d_id, ARCH_CONVERT));
  2478. xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
  2479. }
  2480. }
  2481. /* ------------- list stuff -----------------*/
  2482. void
  2483. xfs_qm_freelist_init(xfs_frlist_t *ql)
  2484. {
  2485. ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
  2486. mutex_init(&ql->qh_lock, MUTEX_DEFAULT, "dqf");
  2487. ql->qh_version = 0;
  2488. ql->qh_nelems = 0;
  2489. }
  2490. void
  2491. xfs_qm_freelist_destroy(xfs_frlist_t *ql)
  2492. {
  2493. xfs_dquot_t *dqp, *nextdqp;
  2494. mutex_lock(&ql->qh_lock, PINOD);
  2495. for (dqp = ql->qh_next;
  2496. dqp != (xfs_dquot_t *)ql; ) {
  2497. xfs_dqlock(dqp);
  2498. nextdqp = dqp->dq_flnext;
  2499. #ifdef QUOTADEBUG
  2500. cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
  2501. #endif
  2502. XQM_FREELIST_REMOVE(dqp);
  2503. xfs_dqunlock(dqp);
  2504. xfs_qm_dqdestroy(dqp);
  2505. dqp = nextdqp;
  2506. }
  2507. /*
  2508. * Don't bother about unlocking.
  2509. */
  2510. mutex_destroy(&ql->qh_lock);
  2511. ASSERT(ql->qh_nelems == 0);
  2512. }
  2513. void
  2514. xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
  2515. {
  2516. dq->dq_flnext = ql->qh_next;
  2517. dq->dq_flprev = (xfs_dquot_t *)ql;
  2518. ql->qh_next = dq;
  2519. dq->dq_flnext->dq_flprev = dq;
  2520. xfs_Gqm->qm_dqfreelist.qh_nelems++;
  2521. xfs_Gqm->qm_dqfreelist.qh_version++;
  2522. }
  2523. void
  2524. xfs_qm_freelist_unlink(xfs_dquot_t *dq)
  2525. {
  2526. xfs_dquot_t *next = dq->dq_flnext;
  2527. xfs_dquot_t *prev = dq->dq_flprev;
  2528. next->dq_flprev = prev;
  2529. prev->dq_flnext = next;
  2530. dq->dq_flnext = dq->dq_flprev = dq;
  2531. xfs_Gqm->qm_dqfreelist.qh_nelems--;
  2532. xfs_Gqm->qm_dqfreelist.qh_version++;
  2533. }
  2534. void
  2535. xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
  2536. {
  2537. xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
  2538. }
  2539. int
  2540. xfs_qm_dqhashlock_nowait(
  2541. xfs_dquot_t *dqp)
  2542. {
  2543. int locked;
  2544. locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
  2545. return (locked);
  2546. }
  2547. int
  2548. xfs_qm_freelist_lock_nowait(
  2549. xfs_qm_t *xqm)
  2550. {
  2551. int locked;
  2552. locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
  2553. return (locked);
  2554. }
  2555. int
  2556. xfs_qm_mplist_nowait(
  2557. xfs_mount_t *mp)
  2558. {
  2559. int locked;
  2560. ASSERT(mp->m_quotainfo);
  2561. locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
  2562. return (locked);
  2563. }