xfs_qm.c 69 KB

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