xfs_qm.c 54 KB

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