xfs_qm.c 71 KB

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