xfs_qm.c 66 KB

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