xfs_qm.c 66 KB

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