xfs_qm.c 67 KB

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