xfs_qm.c 62 KB

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