xfs_qm.c 68 KB

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