xfs_qm.c 70 KB

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