xfs_qm.c 70 KB

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