xfs_qm.c 69 KB

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