xfs_dquot.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_fs.h"
  34. #include "xfs_inum.h"
  35. #include "xfs_log.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_ag.h"
  39. #include "xfs_dir.h"
  40. #include "xfs_dir2.h"
  41. #include "xfs_alloc.h"
  42. #include "xfs_dmapi.h"
  43. #include "xfs_quota.h"
  44. #include "xfs_mount.h"
  45. #include "xfs_alloc_btree.h"
  46. #include "xfs_bmap_btree.h"
  47. #include "xfs_ialloc_btree.h"
  48. #include "xfs_btree.h"
  49. #include "xfs_ialloc.h"
  50. #include "xfs_attr_sf.h"
  51. #include "xfs_dir_sf.h"
  52. #include "xfs_dir2_sf.h"
  53. #include "xfs_dinode.h"
  54. #include "xfs_inode.h"
  55. #include "xfs_bmap.h"
  56. #include "xfs_bit.h"
  57. #include "xfs_rtalloc.h"
  58. #include "xfs_error.h"
  59. #include "xfs_itable.h"
  60. #include "xfs_rw.h"
  61. #include "xfs_acl.h"
  62. #include "xfs_cap.h"
  63. #include "xfs_mac.h"
  64. #include "xfs_attr.h"
  65. #include "xfs_buf_item.h"
  66. #include "xfs_trans_space.h"
  67. #include "xfs_trans_priv.h"
  68. #include "xfs_qm.h"
  69. /*
  70. LOCK ORDER
  71. inode lock (ilock)
  72. dquot hash-chain lock (hashlock)
  73. xqm dquot freelist lock (freelistlock
  74. mount's dquot list lock (mplistlock)
  75. user dquot lock - lock ordering among dquots is based on the uid or gid
  76. group dquot lock - similar to udquots. Between the two dquots, the udquot
  77. has to be locked first.
  78. pin lock - the dquot lock must be held to take this lock.
  79. flush lock - ditto.
  80. */
  81. STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
  82. #ifdef DEBUG
  83. xfs_buftarg_t *xfs_dqerror_target;
  84. int xfs_do_dqerror;
  85. int xfs_dqreq_num;
  86. int xfs_dqerror_mod = 33;
  87. #endif
  88. /*
  89. * Allocate and initialize a dquot. We don't always allocate fresh memory;
  90. * we try to reclaim a free dquot if the number of incore dquots are above
  91. * a threshold.
  92. * The only field inside the core that gets initialized at this point
  93. * is the d_id field. The idea is to fill in the entire q_core
  94. * when we read in the on disk dquot.
  95. */
  96. STATIC xfs_dquot_t *
  97. xfs_qm_dqinit(
  98. xfs_mount_t *mp,
  99. xfs_dqid_t id,
  100. uint type)
  101. {
  102. xfs_dquot_t *dqp;
  103. boolean_t brandnewdquot;
  104. brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
  105. dqp->dq_flags = type;
  106. INT_SET(dqp->q_core.d_id, ARCH_CONVERT, id);
  107. dqp->q_mount = mp;
  108. /*
  109. * No need to re-initialize these if this is a reclaimed dquot.
  110. */
  111. if (brandnewdquot) {
  112. dqp->dq_flnext = dqp->dq_flprev = dqp;
  113. mutex_init(&dqp->q_qlock, MUTEX_DEFAULT, "xdq");
  114. initnsema(&dqp->q_flock, 1, "fdq");
  115. sv_init(&dqp->q_pinwait, SV_DEFAULT, "pdq");
  116. #ifdef XFS_DQUOT_TRACE
  117. dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_SLEEP);
  118. xfs_dqtrace_entry(dqp, "DQINIT");
  119. #endif
  120. } else {
  121. /*
  122. * Only the q_core portion was zeroed in dqreclaim_one().
  123. * So, we need to reset others.
  124. */
  125. dqp->q_nrefs = 0;
  126. dqp->q_blkno = 0;
  127. dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
  128. dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
  129. dqp->q_bufoffset = 0;
  130. dqp->q_fileoffset = 0;
  131. dqp->q_transp = NULL;
  132. dqp->q_gdquot = NULL;
  133. dqp->q_res_bcount = 0;
  134. dqp->q_res_icount = 0;
  135. dqp->q_res_rtbcount = 0;
  136. dqp->q_pincount = 0;
  137. dqp->q_hash = NULL;
  138. ASSERT(dqp->dq_flnext == dqp->dq_flprev);
  139. #ifdef XFS_DQUOT_TRACE
  140. ASSERT(dqp->q_trace);
  141. xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
  142. #endif
  143. }
  144. /*
  145. * log item gets initialized later
  146. */
  147. return (dqp);
  148. }
  149. /*
  150. * This is called to free all the memory associated with a dquot
  151. */
  152. void
  153. xfs_qm_dqdestroy(
  154. xfs_dquot_t *dqp)
  155. {
  156. ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
  157. mutex_destroy(&dqp->q_qlock);
  158. freesema(&dqp->q_flock);
  159. sv_destroy(&dqp->q_pinwait);
  160. #ifdef XFS_DQUOT_TRACE
  161. if (dqp->q_trace)
  162. ktrace_free(dqp->q_trace);
  163. dqp->q_trace = NULL;
  164. #endif
  165. kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
  166. atomic_dec(&xfs_Gqm->qm_totaldquots);
  167. }
  168. /*
  169. * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
  170. */
  171. STATIC void
  172. xfs_qm_dqinit_core(
  173. xfs_dqid_t id,
  174. uint type,
  175. xfs_dqblk_t *d)
  176. {
  177. /*
  178. * Caller has zero'd the entire dquot 'chunk' already.
  179. */
  180. INT_SET(d->dd_diskdq.d_magic, ARCH_CONVERT, XFS_DQUOT_MAGIC);
  181. INT_SET(d->dd_diskdq.d_version, ARCH_CONVERT, XFS_DQUOT_VERSION);
  182. INT_SET(d->dd_diskdq.d_id, ARCH_CONVERT, id);
  183. INT_SET(d->dd_diskdq.d_flags, ARCH_CONVERT, type);
  184. }
  185. #ifdef XFS_DQUOT_TRACE
  186. /*
  187. * Dquot tracing for debugging.
  188. */
  189. /* ARGSUSED */
  190. void
  191. __xfs_dqtrace_entry(
  192. xfs_dquot_t *dqp,
  193. char *func,
  194. void *retaddr,
  195. xfs_inode_t *ip)
  196. {
  197. xfs_dquot_t *udqp = NULL;
  198. xfs_ino_t ino = 0;
  199. ASSERT(dqp->q_trace);
  200. if (ip) {
  201. ino = ip->i_ino;
  202. udqp = ip->i_udquot;
  203. }
  204. ktrace_enter(dqp->q_trace,
  205. (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
  206. (void *)func,
  207. (void *)(__psint_t)dqp->q_nrefs,
  208. (void *)(__psint_t)dqp->dq_flags,
  209. (void *)(__psint_t)dqp->q_res_bcount,
  210. (void *)(__psint_t)INT_GET(dqp->q_core.d_bcount,
  211. ARCH_CONVERT),
  212. (void *)(__psint_t)INT_GET(dqp->q_core.d_icount,
  213. ARCH_CONVERT),
  214. (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_hardlimit,
  215. ARCH_CONVERT),
  216. (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_softlimit,
  217. ARCH_CONVERT),
  218. (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_hardlimit,
  219. ARCH_CONVERT),
  220. (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_softlimit,
  221. ARCH_CONVERT),
  222. (void *)(__psint_t)INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
  223. (void *)(__psint_t)current_pid(),
  224. (void *)(__psint_t)ino,
  225. (void *)(__psint_t)retaddr,
  226. (void *)(__psint_t)udqp);
  227. return;
  228. }
  229. #endif
  230. /*
  231. * If default limits are in force, push them into the dquot now.
  232. * We overwrite the dquot limits only if they are zero and this
  233. * is not the root dquot.
  234. */
  235. void
  236. xfs_qm_adjust_dqlimits(
  237. xfs_mount_t *mp,
  238. xfs_disk_dquot_t *d)
  239. {
  240. xfs_quotainfo_t *q = mp->m_quotainfo;
  241. ASSERT(d->d_id);
  242. if (q->qi_bsoftlimit && !d->d_blk_softlimit)
  243. INT_SET(d->d_blk_softlimit, ARCH_CONVERT, q->qi_bsoftlimit);
  244. if (q->qi_bhardlimit && !d->d_blk_hardlimit)
  245. INT_SET(d->d_blk_hardlimit, ARCH_CONVERT, q->qi_bhardlimit);
  246. if (q->qi_isoftlimit && !d->d_ino_softlimit)
  247. INT_SET(d->d_ino_softlimit, ARCH_CONVERT, q->qi_isoftlimit);
  248. if (q->qi_ihardlimit && !d->d_ino_hardlimit)
  249. INT_SET(d->d_ino_hardlimit, ARCH_CONVERT, q->qi_ihardlimit);
  250. if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
  251. INT_SET(d->d_rtb_softlimit, ARCH_CONVERT, q->qi_rtbsoftlimit);
  252. if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
  253. INT_SET(d->d_rtb_hardlimit, ARCH_CONVERT, q->qi_rtbhardlimit);
  254. }
  255. /*
  256. * Check the limits and timers of a dquot and start or reset timers
  257. * if necessary.
  258. * This gets called even when quota enforcement is OFF, which makes our
  259. * life a little less complicated. (We just don't reject any quota
  260. * reservations in that case, when enforcement is off).
  261. * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
  262. * enforcement's off.
  263. * In contrast, warnings are a little different in that they don't
  264. * 'automatically' get started when limits get exceeded.
  265. */
  266. void
  267. xfs_qm_adjust_dqtimers(
  268. xfs_mount_t *mp,
  269. xfs_disk_dquot_t *d)
  270. {
  271. ASSERT(d->d_id);
  272. #ifdef QUOTADEBUG
  273. if (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT))
  274. ASSERT(INT_GET(d->d_blk_softlimit, ARCH_CONVERT) <=
  275. INT_GET(d->d_blk_hardlimit, ARCH_CONVERT));
  276. if (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT))
  277. ASSERT(INT_GET(d->d_ino_softlimit, ARCH_CONVERT) <=
  278. INT_GET(d->d_ino_hardlimit, ARCH_CONVERT));
  279. if (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT))
  280. ASSERT(INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) <=
  281. INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT));
  282. #endif
  283. if (!d->d_btimer) {
  284. if ((INT_GET(d->d_blk_softlimit, ARCH_CONVERT) &&
  285. (INT_GET(d->d_bcount, ARCH_CONVERT) >=
  286. INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) ||
  287. (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT) &&
  288. (INT_GET(d->d_bcount, ARCH_CONVERT) >=
  289. INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
  290. INT_SET(d->d_btimer, ARCH_CONVERT,
  291. get_seconds() + XFS_QI_BTIMELIMIT(mp));
  292. }
  293. } else {
  294. if ((!d->d_blk_softlimit ||
  295. (INT_GET(d->d_bcount, ARCH_CONVERT) <
  296. INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) &&
  297. (!d->d_blk_hardlimit ||
  298. (INT_GET(d->d_bcount, ARCH_CONVERT) <
  299. INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
  300. d->d_btimer = 0;
  301. }
  302. }
  303. if (!d->d_itimer) {
  304. if ((INT_GET(d->d_ino_softlimit, ARCH_CONVERT) &&
  305. (INT_GET(d->d_icount, ARCH_CONVERT) >=
  306. INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) ||
  307. (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT) &&
  308. (INT_GET(d->d_icount, ARCH_CONVERT) >=
  309. INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
  310. INT_SET(d->d_itimer, ARCH_CONVERT,
  311. get_seconds() + XFS_QI_ITIMELIMIT(mp));
  312. }
  313. } else {
  314. if ((!d->d_ino_softlimit ||
  315. (INT_GET(d->d_icount, ARCH_CONVERT) <
  316. INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) &&
  317. (!d->d_ino_hardlimit ||
  318. (INT_GET(d->d_icount, ARCH_CONVERT) <
  319. INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
  320. d->d_itimer = 0;
  321. }
  322. }
  323. if (!d->d_rtbtimer) {
  324. if ((INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) &&
  325. (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
  326. INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) ||
  327. (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT) &&
  328. (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
  329. INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
  330. INT_SET(d->d_rtbtimer, ARCH_CONVERT,
  331. get_seconds() + XFS_QI_RTBTIMELIMIT(mp));
  332. }
  333. } else {
  334. if ((!d->d_rtb_softlimit ||
  335. (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
  336. INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) &&
  337. (!d->d_rtb_hardlimit ||
  338. (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
  339. INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
  340. d->d_rtbtimer = 0;
  341. }
  342. }
  343. }
  344. /*
  345. * initialize a buffer full of dquots and log the whole thing
  346. */
  347. STATIC void
  348. xfs_qm_init_dquot_blk(
  349. xfs_trans_t *tp,
  350. xfs_mount_t *mp,
  351. xfs_dqid_t id,
  352. uint type,
  353. xfs_buf_t *bp)
  354. {
  355. xfs_dqblk_t *d;
  356. int curid, i;
  357. ASSERT(tp);
  358. ASSERT(XFS_BUF_ISBUSY(bp));
  359. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  360. d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
  361. /*
  362. * ID of the first dquot in the block - id's are zero based.
  363. */
  364. curid = id - (id % XFS_QM_DQPERBLK(mp));
  365. ASSERT(curid >= 0);
  366. memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
  367. for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
  368. xfs_qm_dqinit_core(curid, type, d);
  369. xfs_trans_dquot_buf(tp, bp,
  370. (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
  371. ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
  372. XFS_BLI_GDQUOT_BUF)));
  373. xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
  374. }
  375. /*
  376. * Allocate a block and fill it with dquots.
  377. * This is called when the bmapi finds a hole.
  378. */
  379. STATIC int
  380. xfs_qm_dqalloc(
  381. xfs_trans_t *tp,
  382. xfs_mount_t *mp,
  383. xfs_dquot_t *dqp,
  384. xfs_inode_t *quotip,
  385. xfs_fileoff_t offset_fsb,
  386. xfs_buf_t **O_bpp)
  387. {
  388. xfs_fsblock_t firstblock;
  389. xfs_bmap_free_t flist;
  390. xfs_bmbt_irec_t map;
  391. int nmaps, error, committed;
  392. xfs_buf_t *bp;
  393. ASSERT(tp != NULL);
  394. xfs_dqtrace_entry(dqp, "DQALLOC");
  395. /*
  396. * Initialize the bmap freelist prior to calling bmapi code.
  397. */
  398. XFS_BMAP_INIT(&flist, &firstblock);
  399. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  400. /*
  401. * Return if this type of quotas is turned off while we didn't
  402. * have an inode lock
  403. */
  404. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  405. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  406. return (ESRCH);
  407. }
  408. /*
  409. * xfs_trans_commit normally decrements the vnode ref count
  410. * when it unlocks the inode. Since we want to keep the quota
  411. * inode around, we bump the vnode ref count now.
  412. */
  413. VN_HOLD(XFS_ITOV(quotip));
  414. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  415. nmaps = 1;
  416. if ((error = xfs_bmapi(tp, quotip,
  417. offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
  418. XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
  419. &firstblock,
  420. XFS_QM_DQALLOC_SPACE_RES(mp),
  421. &map, &nmaps, &flist))) {
  422. goto error0;
  423. }
  424. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  425. ASSERT(nmaps == 1);
  426. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  427. (map.br_startblock != HOLESTARTBLOCK));
  428. /*
  429. * Keep track of the blkno to save a lookup later
  430. */
  431. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  432. /* now we can just get the buffer (there's nothing to read yet) */
  433. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  434. dqp->q_blkno,
  435. XFS_QI_DQCHUNKLEN(mp),
  436. 0);
  437. if (!bp || (error = XFS_BUF_GETERROR(bp)))
  438. goto error1;
  439. /*
  440. * Make a chunk of dquots out of this buffer and log
  441. * the entire thing.
  442. */
  443. xfs_qm_init_dquot_blk(tp, mp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
  444. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  445. if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed))) {
  446. goto error1;
  447. }
  448. *O_bpp = bp;
  449. return 0;
  450. error1:
  451. xfs_bmap_cancel(&flist);
  452. error0:
  453. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  454. return (error);
  455. }
  456. /*
  457. * Maps a dquot to the buffer containing its on-disk version.
  458. * This returns a ptr to the buffer containing the on-disk dquot
  459. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  460. */
  461. STATIC int
  462. xfs_qm_dqtobp(
  463. xfs_trans_t *tp,
  464. xfs_dquot_t *dqp,
  465. xfs_disk_dquot_t **O_ddpp,
  466. xfs_buf_t **O_bpp,
  467. uint flags)
  468. {
  469. xfs_bmbt_irec_t map;
  470. int nmaps, error;
  471. xfs_buf_t *bp;
  472. xfs_inode_t *quotip;
  473. xfs_mount_t *mp;
  474. xfs_disk_dquot_t *ddq;
  475. xfs_dqid_t id;
  476. boolean_t newdquot;
  477. mp = dqp->q_mount;
  478. id = INT_GET(dqp->q_core.d_id, ARCH_CONVERT);
  479. nmaps = 1;
  480. newdquot = B_FALSE;
  481. /*
  482. * If we don't know where the dquot lives, find out.
  483. */
  484. if (dqp->q_blkno == (xfs_daddr_t) 0) {
  485. /* We use the id as an index */
  486. dqp->q_fileoffset = (xfs_fileoff_t) ((uint)id /
  487. XFS_QM_DQPERBLK(mp));
  488. nmaps = 1;
  489. quotip = XFS_DQ_TO_QIP(dqp);
  490. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  491. /*
  492. * Return if this type of quotas is turned off while we didn't
  493. * have an inode lock
  494. */
  495. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  496. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  497. return (ESRCH);
  498. }
  499. /*
  500. * Find the block map; no allocations yet
  501. */
  502. error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
  503. XFS_DQUOT_CLUSTER_SIZE_FSB,
  504. XFS_BMAPI_METADATA,
  505. NULL, 0, &map, &nmaps, NULL);
  506. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  507. if (error)
  508. return (error);
  509. ASSERT(nmaps == 1);
  510. ASSERT(map.br_blockcount == 1);
  511. /*
  512. * offset of dquot in the (fixed sized) dquot chunk.
  513. */
  514. dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
  515. sizeof(xfs_dqblk_t);
  516. if (map.br_startblock == HOLESTARTBLOCK) {
  517. /*
  518. * We don't allocate unless we're asked to
  519. */
  520. if (!(flags & XFS_QMOPT_DQALLOC))
  521. return (ENOENT);
  522. ASSERT(tp);
  523. if ((error = xfs_qm_dqalloc(tp, mp, dqp, quotip,
  524. dqp->q_fileoffset, &bp)))
  525. return (error);
  526. newdquot = B_TRUE;
  527. } else {
  528. /*
  529. * store the blkno etc so that we don't have to do the
  530. * mapping all the time
  531. */
  532. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  533. }
  534. }
  535. ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
  536. ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
  537. /*
  538. * Read in the buffer, unless we've just done the allocation
  539. * (in which case we already have the buf).
  540. */
  541. if (! newdquot) {
  542. xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
  543. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  544. dqp->q_blkno,
  545. XFS_QI_DQCHUNKLEN(mp),
  546. 0, &bp))) {
  547. return (error);
  548. }
  549. if (error || !bp)
  550. return XFS_ERROR(error);
  551. }
  552. ASSERT(XFS_BUF_ISBUSY(bp));
  553. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  554. /*
  555. * calculate the location of the dquot inside the buffer.
  556. */
  557. ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
  558. /*
  559. * A simple sanity check in case we got a corrupted dquot...
  560. */
  561. if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
  562. flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
  563. "dqtobp")) {
  564. if (!(flags & XFS_QMOPT_DQREPAIR)) {
  565. xfs_trans_brelse(tp, bp);
  566. return XFS_ERROR(EIO);
  567. }
  568. XFS_BUF_BUSY(bp); /* We dirtied this */
  569. }
  570. *O_bpp = bp;
  571. *O_ddpp = ddq;
  572. return (0);
  573. }
  574. /*
  575. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  576. * and release the buffer immediately.
  577. *
  578. */
  579. /* ARGSUSED */
  580. STATIC int
  581. xfs_qm_dqread(
  582. xfs_trans_t *tp,
  583. xfs_dqid_t id,
  584. xfs_dquot_t *dqp, /* dquot to get filled in */
  585. uint flags)
  586. {
  587. xfs_disk_dquot_t *ddqp;
  588. xfs_buf_t *bp;
  589. int error;
  590. /*
  591. * get a pointer to the on-disk dquot and the buffer containing it
  592. * dqp already knows its own type (GROUP/USER).
  593. */
  594. xfs_dqtrace_entry(dqp, "DQREAD");
  595. if ((error = xfs_qm_dqtobp(tp, dqp, &ddqp, &bp, flags))) {
  596. return (error);
  597. }
  598. /* copy everything from disk dquot to the incore dquot */
  599. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  600. ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
  601. xfs_qm_dquot_logitem_init(dqp);
  602. /*
  603. * Reservation counters are defined as reservation plus current usage
  604. * to avoid having to add everytime.
  605. */
  606. dqp->q_res_bcount = INT_GET(ddqp->d_bcount, ARCH_CONVERT);
  607. dqp->q_res_icount = INT_GET(ddqp->d_icount, ARCH_CONVERT);
  608. dqp->q_res_rtbcount = INT_GET(ddqp->d_rtbcount, ARCH_CONVERT);
  609. /* Mark the buf so that this will stay incore a little longer */
  610. XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
  611. /*
  612. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  613. * So we need to release with xfs_trans_brelse().
  614. * The strategy here is identical to that of inodes; we lock
  615. * the dquot in xfs_qm_dqget() before making it accessible to
  616. * others. This is because dquots, like inodes, need a good level of
  617. * concurrency, and we don't want to take locks on the entire buffers
  618. * for dquot accesses.
  619. * Note also that the dquot buffer may even be dirty at this point, if
  620. * this particular dquot was repaired. We still aren't afraid to
  621. * brelse it because we have the changes incore.
  622. */
  623. ASSERT(XFS_BUF_ISBUSY(bp));
  624. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  625. xfs_trans_brelse(tp, bp);
  626. return (error);
  627. }
  628. /*
  629. * allocate an incore dquot from the kernel heap,
  630. * and fill its core with quota information kept on disk.
  631. * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
  632. * if it wasn't already allocated.
  633. */
  634. STATIC int
  635. xfs_qm_idtodq(
  636. xfs_mount_t *mp,
  637. xfs_dqid_t id, /* gid or uid, depending on type */
  638. uint type, /* UDQUOT or GDQUOT */
  639. uint flags, /* DQALLOC, DQREPAIR */
  640. xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
  641. {
  642. xfs_dquot_t *dqp;
  643. int error;
  644. xfs_trans_t *tp;
  645. int cancelflags=0;
  646. dqp = xfs_qm_dqinit(mp, id, type);
  647. tp = NULL;
  648. if (flags & XFS_QMOPT_DQALLOC) {
  649. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  650. if ((error = xfs_trans_reserve(tp,
  651. XFS_QM_DQALLOC_SPACE_RES(mp),
  652. XFS_WRITE_LOG_RES(mp) +
  653. BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
  654. 128,
  655. 0,
  656. XFS_TRANS_PERM_LOG_RES,
  657. XFS_WRITE_LOG_COUNT))) {
  658. cancelflags = 0;
  659. goto error0;
  660. }
  661. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  662. }
  663. /*
  664. * Read it from disk; xfs_dqread() takes care of
  665. * all the necessary initialization of dquot's fields (locks, etc)
  666. */
  667. if ((error = xfs_qm_dqread(tp, id, dqp, flags))) {
  668. /*
  669. * This can happen if quotas got turned off (ESRCH),
  670. * or if the dquot didn't exist on disk and we ask to
  671. * allocate (ENOENT).
  672. */
  673. xfs_dqtrace_entry(dqp, "DQREAD FAIL");
  674. cancelflags |= XFS_TRANS_ABORT;
  675. goto error0;
  676. }
  677. if (tp) {
  678. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
  679. NULL)))
  680. goto error1;
  681. }
  682. *O_dqpp = dqp;
  683. return (0);
  684. error0:
  685. ASSERT(error);
  686. if (tp)
  687. xfs_trans_cancel(tp, cancelflags);
  688. error1:
  689. xfs_qm_dqdestroy(dqp);
  690. *O_dqpp = NULL;
  691. return (error);
  692. }
  693. /*
  694. * Lookup a dquot in the incore dquot hashtable. We keep two separate
  695. * hashtables for user and group dquots; and, these are global tables
  696. * inside the XQM, not per-filesystem tables.
  697. * The hash chain must be locked by caller, and it is left locked
  698. * on return. Returning dquot is locked.
  699. */
  700. STATIC int
  701. xfs_qm_dqlookup(
  702. xfs_mount_t *mp,
  703. xfs_dqid_t id,
  704. xfs_dqhash_t *qh,
  705. xfs_dquot_t **O_dqpp)
  706. {
  707. xfs_dquot_t *dqp;
  708. uint flist_locked;
  709. xfs_dquot_t *d;
  710. ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
  711. flist_locked = B_FALSE;
  712. /*
  713. * Traverse the hashchain looking for a match
  714. */
  715. for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
  716. /*
  717. * We already have the hashlock. We don't need the
  718. * dqlock to look at the id field of the dquot, since the
  719. * id can't be modified without the hashlock anyway.
  720. */
  721. if (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id && dqp->q_mount == mp) {
  722. xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
  723. /*
  724. * All in core dquots must be on the dqlist of mp
  725. */
  726. ASSERT(dqp->MPL_PREVP != NULL);
  727. xfs_dqlock(dqp);
  728. if (dqp->q_nrefs == 0) {
  729. ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
  730. if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
  731. xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
  732. /*
  733. * We may have raced with dqreclaim_one()
  734. * (and lost). So, flag that we don't
  735. * want the dquot to be reclaimed.
  736. */
  737. dqp->dq_flags |= XFS_DQ_WANT;
  738. xfs_dqunlock(dqp);
  739. xfs_qm_freelist_lock(xfs_Gqm);
  740. xfs_dqlock(dqp);
  741. dqp->dq_flags &= ~(XFS_DQ_WANT);
  742. }
  743. flist_locked = B_TRUE;
  744. }
  745. /*
  746. * id couldn't have changed; we had the hashlock all
  747. * along
  748. */
  749. ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
  750. if (flist_locked) {
  751. if (dqp->q_nrefs != 0) {
  752. xfs_qm_freelist_unlock(xfs_Gqm);
  753. flist_locked = B_FALSE;
  754. } else {
  755. /*
  756. * take it off the freelist
  757. */
  758. xfs_dqtrace_entry(dqp,
  759. "DQLOOKUP: TAKEOFF FL");
  760. XQM_FREELIST_REMOVE(dqp);
  761. /* xfs_qm_freelist_print(&(xfs_Gqm->
  762. qm_dqfreelist),
  763. "after removal"); */
  764. }
  765. }
  766. /*
  767. * grab a reference
  768. */
  769. XFS_DQHOLD(dqp);
  770. if (flist_locked)
  771. xfs_qm_freelist_unlock(xfs_Gqm);
  772. /*
  773. * move the dquot to the front of the hashchain
  774. */
  775. ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
  776. if (dqp->HL_PREVP != &qh->qh_next) {
  777. xfs_dqtrace_entry(dqp,
  778. "DQLOOKUP: HASH MOVETOFRONT");
  779. if ((d = dqp->HL_NEXT))
  780. d->HL_PREVP = dqp->HL_PREVP;
  781. *(dqp->HL_PREVP) = d;
  782. d = qh->qh_next;
  783. d->HL_PREVP = &dqp->HL_NEXT;
  784. dqp->HL_NEXT = d;
  785. dqp->HL_PREVP = &qh->qh_next;
  786. qh->qh_next = dqp;
  787. }
  788. xfs_dqtrace_entry(dqp, "LOOKUP END");
  789. *O_dqpp = dqp;
  790. ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
  791. return (0);
  792. }
  793. }
  794. *O_dqpp = NULL;
  795. ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
  796. return (1);
  797. }
  798. /*
  799. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  800. * a locked dquot, doing an allocation (if requested) as needed.
  801. * When both an inode and an id are given, the inode's id takes precedence.
  802. * That is, if the id changes while we don't hold the ilock inside this
  803. * function, the new dquot is returned, not necessarily the one requested
  804. * in the id argument.
  805. */
  806. int
  807. xfs_qm_dqget(
  808. xfs_mount_t *mp,
  809. xfs_inode_t *ip, /* locked inode (optional) */
  810. xfs_dqid_t id, /* uid/projid/gid depending on type */
  811. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  812. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  813. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  814. {
  815. xfs_dquot_t *dqp;
  816. xfs_dqhash_t *h;
  817. uint version;
  818. int error;
  819. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  820. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  821. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  822. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  823. printk("XQM: ESRCH1\n");
  824. return (ESRCH);
  825. }
  826. h = XFS_DQ_HASH(mp, id, type);
  827. #ifdef DEBUG
  828. if (xfs_do_dqerror) {
  829. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  830. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  831. cmn_err(CE_DEBUG, "Returning error in dqget");
  832. return (EIO);
  833. }
  834. }
  835. #endif
  836. again:
  837. #ifdef DEBUG
  838. ASSERT(type == XFS_DQ_USER ||
  839. type == XFS_DQ_PROJ ||
  840. type == XFS_DQ_GROUP);
  841. if (ip) {
  842. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  843. if (type == XFS_DQ_USER)
  844. ASSERT(ip->i_udquot == NULL);
  845. else
  846. ASSERT(ip->i_gdquot == NULL);
  847. }
  848. #endif
  849. XFS_DQ_HASH_LOCK(h);
  850. /*
  851. * Look in the cache (hashtable).
  852. * The chain is kept locked during lookup.
  853. */
  854. if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
  855. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  856. /*
  857. * The dquot was found, moved to the front of the chain,
  858. * taken off the freelist if it was on it, and locked
  859. * at this point. Just unlock the hashchain and return.
  860. */
  861. ASSERT(*O_dqpp);
  862. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  863. XFS_DQ_HASH_UNLOCK(h);
  864. xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
  865. return (0); /* success */
  866. }
  867. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  868. /*
  869. * Dquot cache miss. We don't want to keep the inode lock across
  870. * a (potential) disk read. Also we don't want to deal with the lock
  871. * ordering between quotainode and this inode. OTOH, dropping the inode
  872. * lock here means dealing with a chown that can happen before
  873. * we re-acquire the lock.
  874. */
  875. if (ip)
  876. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  877. /*
  878. * Save the hashchain version stamp, and unlock the chain, so that
  879. * we don't keep the lock across a disk read
  880. */
  881. version = h->qh_version;
  882. XFS_DQ_HASH_UNLOCK(h);
  883. /*
  884. * Allocate the dquot on the kernel heap, and read the ondisk
  885. * portion off the disk. Also, do all the necessary initialization
  886. * This can return ENOENT if dquot didn't exist on disk and we didn't
  887. * ask it to allocate; ESRCH if quotas got turned off suddenly.
  888. */
  889. if ((error = xfs_qm_idtodq(mp, id, type,
  890. flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
  891. XFS_QMOPT_DOWARN),
  892. &dqp))) {
  893. if (ip)
  894. xfs_ilock(ip, XFS_ILOCK_EXCL);
  895. if (error == ESRCH) printk("XQM: ESRCH2\n");
  896. return (error);
  897. }
  898. /*
  899. * See if this is mount code calling to look at the overall quota limits
  900. * which are stored in the id == 0 user or group's dquot.
  901. * Since we may not have done a quotacheck by this point, just return
  902. * the dquot without attaching it to any hashtables, lists, etc, or even
  903. * taking a reference.
  904. * The caller must dqdestroy this once done.
  905. */
  906. if (flags & XFS_QMOPT_DQSUSER) {
  907. ASSERT(id == 0);
  908. ASSERT(! ip);
  909. goto dqret;
  910. }
  911. /*
  912. * Dquot lock comes after hashlock in the lock ordering
  913. */
  914. if (ip) {
  915. xfs_ilock(ip, XFS_ILOCK_EXCL);
  916. if (! XFS_IS_DQTYPE_ON(mp, type)) {
  917. /* inode stays locked on return */
  918. xfs_qm_dqdestroy(dqp);
  919. printk("XQM: ESRCH3\n");
  920. return XFS_ERROR(ESRCH);
  921. }
  922. /*
  923. * A dquot could be attached to this inode by now, since
  924. * we had dropped the ilock.
  925. */
  926. if (type == XFS_DQ_USER) {
  927. if (ip->i_udquot) {
  928. xfs_qm_dqdestroy(dqp);
  929. dqp = ip->i_udquot;
  930. xfs_dqlock(dqp);
  931. goto dqret;
  932. }
  933. } else {
  934. if (ip->i_gdquot) {
  935. xfs_qm_dqdestroy(dqp);
  936. dqp = ip->i_gdquot;
  937. xfs_dqlock(dqp);
  938. goto dqret;
  939. }
  940. }
  941. }
  942. /*
  943. * Hashlock comes after ilock in lock order
  944. */
  945. XFS_DQ_HASH_LOCK(h);
  946. if (version != h->qh_version) {
  947. xfs_dquot_t *tmpdqp;
  948. /*
  949. * Now, see if somebody else put the dquot in the
  950. * hashtable before us. This can happen because we didn't
  951. * keep the hashchain lock. We don't have to worry about
  952. * lock order between the two dquots here since dqp isn't
  953. * on any findable lists yet.
  954. */
  955. if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
  956. /*
  957. * Duplicate found. Just throw away the new dquot
  958. * and start over.
  959. */
  960. xfs_qm_dqput(tmpdqp);
  961. XFS_DQ_HASH_UNLOCK(h);
  962. xfs_qm_dqdestroy(dqp);
  963. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  964. goto again;
  965. }
  966. }
  967. /*
  968. * Put the dquot at the beginning of the hash-chain and mp's list
  969. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  970. */
  971. ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
  972. dqp->q_hash = h;
  973. XQM_HASHLIST_INSERT(h, dqp);
  974. /*
  975. * Attach this dquot to this filesystem's list of all dquots,
  976. * kept inside the mount structure in m_quotainfo field
  977. */
  978. xfs_qm_mplist_lock(mp);
  979. /*
  980. * We return a locked dquot to the caller, with a reference taken
  981. */
  982. xfs_dqlock(dqp);
  983. dqp->q_nrefs = 1;
  984. XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
  985. xfs_qm_mplist_unlock(mp);
  986. XFS_DQ_HASH_UNLOCK(h);
  987. dqret:
  988. ASSERT((ip == NULL) || XFS_ISLOCKED_INODE_EXCL(ip));
  989. xfs_dqtrace_entry(dqp, "DQGET DONE");
  990. *O_dqpp = dqp;
  991. return (0);
  992. }
  993. /*
  994. * Release a reference to the dquot (decrement ref-count)
  995. * and unlock it. If there is a group quota attached to this
  996. * dquot, carefully release that too without tripping over
  997. * deadlocks'n'stuff.
  998. */
  999. void
  1000. xfs_qm_dqput(
  1001. xfs_dquot_t *dqp)
  1002. {
  1003. xfs_dquot_t *gdqp;
  1004. ASSERT(dqp->q_nrefs > 0);
  1005. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1006. xfs_dqtrace_entry(dqp, "DQPUT");
  1007. if (dqp->q_nrefs != 1) {
  1008. dqp->q_nrefs--;
  1009. xfs_dqunlock(dqp);
  1010. return;
  1011. }
  1012. /*
  1013. * drop the dqlock and acquire the freelist and dqlock
  1014. * in the right order; but try to get it out-of-order first
  1015. */
  1016. if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
  1017. xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
  1018. xfs_dqunlock(dqp);
  1019. xfs_qm_freelist_lock(xfs_Gqm);
  1020. xfs_dqlock(dqp);
  1021. }
  1022. while (1) {
  1023. gdqp = NULL;
  1024. /* We can't depend on nrefs being == 1 here */
  1025. if (--dqp->q_nrefs == 0) {
  1026. xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
  1027. /*
  1028. * insert at end of the freelist.
  1029. */
  1030. XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
  1031. /*
  1032. * If we just added a udquot to the freelist, then
  1033. * we want to release the gdquot reference that
  1034. * it (probably) has. Otherwise it'll keep the
  1035. * gdquot from getting reclaimed.
  1036. */
  1037. if ((gdqp = dqp->q_gdquot)) {
  1038. /*
  1039. * Avoid a recursive dqput call
  1040. */
  1041. xfs_dqlock(gdqp);
  1042. dqp->q_gdquot = NULL;
  1043. }
  1044. /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
  1045. "@@@@@++ Free list (after append) @@@@@+");
  1046. */
  1047. }
  1048. xfs_dqunlock(dqp);
  1049. /*
  1050. * If we had a group quota inside the user quota as a hint,
  1051. * release it now.
  1052. */
  1053. if (! gdqp)
  1054. break;
  1055. dqp = gdqp;
  1056. }
  1057. xfs_qm_freelist_unlock(xfs_Gqm);
  1058. }
  1059. /*
  1060. * Release a dquot. Flush it if dirty, then dqput() it.
  1061. * dquot must not be locked.
  1062. */
  1063. void
  1064. xfs_qm_dqrele(
  1065. xfs_dquot_t *dqp)
  1066. {
  1067. ASSERT(dqp);
  1068. xfs_dqtrace_entry(dqp, "DQRELE");
  1069. xfs_dqlock(dqp);
  1070. /*
  1071. * We don't care to flush it if the dquot is dirty here.
  1072. * That will create stutters that we want to avoid.
  1073. * Instead we do a delayed write when we try to reclaim
  1074. * a dirty dquot. Also xfs_sync will take part of the burden...
  1075. */
  1076. xfs_qm_dqput(dqp);
  1077. }
  1078. /*
  1079. * Write a modified dquot to disk.
  1080. * The dquot must be locked and the flush lock too taken by caller.
  1081. * The flush lock will not be unlocked until the dquot reaches the disk,
  1082. * but the dquot is free to be unlocked and modified by the caller
  1083. * in the interim. Dquot is still locked on return. This behavior is
  1084. * identical to that of inodes.
  1085. */
  1086. int
  1087. xfs_qm_dqflush(
  1088. xfs_dquot_t *dqp,
  1089. uint flags)
  1090. {
  1091. xfs_mount_t *mp;
  1092. xfs_buf_t *bp;
  1093. xfs_disk_dquot_t *ddqp;
  1094. int error;
  1095. SPLDECL(s);
  1096. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1097. ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
  1098. xfs_dqtrace_entry(dqp, "DQFLUSH");
  1099. /*
  1100. * If not dirty, nada.
  1101. */
  1102. if (!XFS_DQ_IS_DIRTY(dqp)) {
  1103. xfs_dqfunlock(dqp);
  1104. return (0);
  1105. }
  1106. /*
  1107. * Cant flush a pinned dquot. Wait for it.
  1108. */
  1109. xfs_qm_dqunpin_wait(dqp);
  1110. /*
  1111. * This may have been unpinned because the filesystem is shutting
  1112. * down forcibly. If that's the case we must not write this dquot
  1113. * to disk, because the log record didn't make it to disk!
  1114. */
  1115. if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
  1116. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1117. xfs_dqfunlock(dqp);
  1118. return XFS_ERROR(EIO);
  1119. }
  1120. /*
  1121. * Get the buffer containing the on-disk dquot
  1122. * We don't need a transaction envelope because we know that the
  1123. * the ondisk-dquot has already been allocated for.
  1124. */
  1125. if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
  1126. xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
  1127. ASSERT(error != ENOENT);
  1128. /*
  1129. * Quotas could have gotten turned off (ESRCH)
  1130. */
  1131. xfs_dqfunlock(dqp);
  1132. return (error);
  1133. }
  1134. if (xfs_qm_dqcheck(&dqp->q_core, INT_GET(ddqp->d_id, ARCH_CONVERT),
  1135. 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
  1136. xfs_force_shutdown(dqp->q_mount, XFS_CORRUPT_INCORE);
  1137. return XFS_ERROR(EIO);
  1138. }
  1139. /* This is the only portion of data that needs to persist */
  1140. memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
  1141. /*
  1142. * Clear the dirty field and remember the flush lsn for later use.
  1143. */
  1144. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1145. mp = dqp->q_mount;
  1146. /* lsn is 64 bits */
  1147. AIL_LOCK(mp, s);
  1148. dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
  1149. AIL_UNLOCK(mp, s);
  1150. /*
  1151. * Attach an iodone routine so that we can remove this dquot from the
  1152. * AIL and release the flush lock once the dquot is synced to disk.
  1153. */
  1154. xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
  1155. xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
  1156. /*
  1157. * If the buffer is pinned then push on the log so we won't
  1158. * get stuck waiting in the write for too long.
  1159. */
  1160. if (XFS_BUF_ISPINNED(bp)) {
  1161. xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
  1162. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  1163. }
  1164. if (flags & XFS_QMOPT_DELWRI) {
  1165. xfs_bdwrite(mp, bp);
  1166. } else if (flags & XFS_QMOPT_ASYNC) {
  1167. xfs_bawrite(mp, bp);
  1168. } else {
  1169. error = xfs_bwrite(mp, bp);
  1170. }
  1171. xfs_dqtrace_entry(dqp, "DQFLUSH END");
  1172. /*
  1173. * dqp is still locked, but caller is free to unlock it now.
  1174. */
  1175. return (error);
  1176. }
  1177. /*
  1178. * This is the dquot flushing I/O completion routine. It is called
  1179. * from interrupt level when the buffer containing the dquot is
  1180. * flushed to disk. It is responsible for removing the dquot logitem
  1181. * from the AIL if it has not been re-logged, and unlocking the dquot's
  1182. * flush lock. This behavior is very similar to that of inodes..
  1183. */
  1184. /*ARGSUSED*/
  1185. STATIC void
  1186. xfs_qm_dqflush_done(
  1187. xfs_buf_t *bp,
  1188. xfs_dq_logitem_t *qip)
  1189. {
  1190. xfs_dquot_t *dqp;
  1191. SPLDECL(s);
  1192. dqp = qip->qli_dquot;
  1193. /*
  1194. * We only want to pull the item from the AIL if its
  1195. * location in the log has not changed since we started the flush.
  1196. * Thus, we only bother if the dquot's lsn has
  1197. * not changed. First we check the lsn outside the lock
  1198. * since it's cheaper, and then we recheck while
  1199. * holding the lock before removing the dquot from the AIL.
  1200. */
  1201. if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
  1202. qip->qli_item.li_lsn == qip->qli_flush_lsn) {
  1203. AIL_LOCK(dqp->q_mount, s);
  1204. /*
  1205. * xfs_trans_delete_ail() drops the AIL lock.
  1206. */
  1207. if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
  1208. xfs_trans_delete_ail(dqp->q_mount,
  1209. (xfs_log_item_t*)qip, s);
  1210. else
  1211. AIL_UNLOCK(dqp->q_mount, s);
  1212. }
  1213. /*
  1214. * Release the dq's flush lock since we're done with it.
  1215. */
  1216. xfs_dqfunlock(dqp);
  1217. }
  1218. int
  1219. xfs_qm_dqflock_nowait(
  1220. xfs_dquot_t *dqp)
  1221. {
  1222. int locked;
  1223. locked = cpsema(&((dqp)->q_flock));
  1224. /* XXX ifdef these out */
  1225. if (locked)
  1226. (dqp)->dq_flags |= XFS_DQ_FLOCKED;
  1227. return (locked);
  1228. }
  1229. int
  1230. xfs_qm_dqlock_nowait(
  1231. xfs_dquot_t *dqp)
  1232. {
  1233. return (mutex_trylock(&((dqp)->q_qlock)));
  1234. }
  1235. void
  1236. xfs_dqlock(
  1237. xfs_dquot_t *dqp)
  1238. {
  1239. mutex_lock(&(dqp->q_qlock), PINOD);
  1240. }
  1241. void
  1242. xfs_dqunlock(
  1243. xfs_dquot_t *dqp)
  1244. {
  1245. mutex_unlock(&(dqp->q_qlock));
  1246. if (dqp->q_logitem.qli_dquot == dqp) {
  1247. /* Once was dqp->q_mount, but might just have been cleared */
  1248. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
  1249. (xfs_log_item_t*)&(dqp->q_logitem));
  1250. }
  1251. }
  1252. void
  1253. xfs_dqunlock_nonotify(
  1254. xfs_dquot_t *dqp)
  1255. {
  1256. mutex_unlock(&(dqp->q_qlock));
  1257. }
  1258. void
  1259. xfs_dqlock2(
  1260. xfs_dquot_t *d1,
  1261. xfs_dquot_t *d2)
  1262. {
  1263. if (d1 && d2) {
  1264. ASSERT(d1 != d2);
  1265. if (INT_GET(d1->q_core.d_id, ARCH_CONVERT) >
  1266. INT_GET(d2->q_core.d_id, ARCH_CONVERT)) {
  1267. xfs_dqlock(d2);
  1268. xfs_dqlock(d1);
  1269. } else {
  1270. xfs_dqlock(d1);
  1271. xfs_dqlock(d2);
  1272. }
  1273. } else {
  1274. if (d1) {
  1275. xfs_dqlock(d1);
  1276. } else if (d2) {
  1277. xfs_dqlock(d2);
  1278. }
  1279. }
  1280. }
  1281. /*
  1282. * Take a dquot out of the mount's dqlist as well as the hashlist.
  1283. * This is called via unmount as well as quotaoff, and the purge
  1284. * will always succeed unless there are soft (temp) references
  1285. * outstanding.
  1286. *
  1287. * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
  1288. * that we're returning! XXXsup - not cool.
  1289. */
  1290. /* ARGSUSED */
  1291. int
  1292. xfs_qm_dqpurge(
  1293. xfs_dquot_t *dqp,
  1294. uint flags)
  1295. {
  1296. xfs_dqhash_t *thishash;
  1297. xfs_mount_t *mp;
  1298. mp = dqp->q_mount;
  1299. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  1300. ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
  1301. xfs_dqlock(dqp);
  1302. /*
  1303. * We really can't afford to purge a dquot that is
  1304. * referenced, because these are hard refs.
  1305. * It shouldn't happen in general because we went thru _all_ inodes in
  1306. * dqrele_all_inodes before calling this and didn't let the mountlock go.
  1307. * However it is possible that we have dquots with temporary
  1308. * references that are not attached to an inode. e.g. see xfs_setattr().
  1309. */
  1310. if (dqp->q_nrefs != 0) {
  1311. xfs_dqunlock(dqp);
  1312. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  1313. return (1);
  1314. }
  1315. ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
  1316. /*
  1317. * If we're turning off quotas, we have to make sure that, for
  1318. * example, we don't delete quota disk blocks while dquots are
  1319. * in the process of getting written to those disk blocks.
  1320. * This dquot might well be on AIL, and we can't leave it there
  1321. * if we're turning off quotas. Basically, we need this flush
  1322. * lock, and are willing to block on it.
  1323. */
  1324. if (! xfs_qm_dqflock_nowait(dqp)) {
  1325. /*
  1326. * Block on the flush lock after nudging dquot buffer,
  1327. * if it is incore.
  1328. */
  1329. xfs_qm_dqflock_pushbuf_wait(dqp);
  1330. }
  1331. /*
  1332. * XXXIf we're turning this type of quotas off, we don't care
  1333. * about the dirty metadata sitting in this dquot. OTOH, if
  1334. * we're unmounting, we do care, so we flush it and wait.
  1335. */
  1336. if (XFS_DQ_IS_DIRTY(dqp)) {
  1337. xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
  1338. /* dqflush unlocks dqflock */
  1339. /*
  1340. * Given that dqpurge is a very rare occurrence, it is OK
  1341. * that we're holding the hashlist and mplist locks
  1342. * across the disk write. But, ... XXXsup
  1343. *
  1344. * We don't care about getting disk errors here. We need
  1345. * to purge this dquot anyway, so we go ahead regardless.
  1346. */
  1347. (void) xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
  1348. xfs_dqflock(dqp);
  1349. }
  1350. ASSERT(dqp->q_pincount == 0);
  1351. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1352. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1353. thishash = dqp->q_hash;
  1354. XQM_HASHLIST_REMOVE(thishash, dqp);
  1355. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
  1356. /*
  1357. * XXX Move this to the front of the freelist, if we can get the
  1358. * freelist lock.
  1359. */
  1360. ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
  1361. dqp->q_mount = NULL;
  1362. dqp->q_hash = NULL;
  1363. dqp->dq_flags = XFS_DQ_INACTIVE;
  1364. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  1365. xfs_dqfunlock(dqp);
  1366. xfs_dqunlock(dqp);
  1367. XFS_DQ_HASH_UNLOCK(thishash);
  1368. return (0);
  1369. }
  1370. #ifdef QUOTADEBUG
  1371. void
  1372. xfs_qm_dqprint(xfs_dquot_t *dqp)
  1373. {
  1374. cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
  1375. cmn_err(CE_DEBUG, "---- dquotID = %d",
  1376. (int)INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
  1377. cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
  1378. cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
  1379. cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
  1380. cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
  1381. cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
  1382. INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT),
  1383. (int) INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT));
  1384. cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
  1385. INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT),
  1386. (int)INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT));
  1387. cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
  1388. INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT),
  1389. (int)INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT));
  1390. cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
  1391. INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT),
  1392. (int)INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT));
  1393. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  1394. INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
  1395. (int)INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
  1396. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  1397. INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
  1398. (int)INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
  1399. cmn_err(CE_DEBUG, "---- btimer = %d",
  1400. (int)INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT));
  1401. cmn_err(CE_DEBUG, "---- itimer = %d",
  1402. (int)INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT));
  1403. cmn_err(CE_DEBUG, "---------------------------");
  1404. }
  1405. #endif
  1406. /*
  1407. * Give the buffer a little push if it is incore and
  1408. * wait on the flush lock.
  1409. */
  1410. void
  1411. xfs_qm_dqflock_pushbuf_wait(
  1412. xfs_dquot_t *dqp)
  1413. {
  1414. xfs_buf_t *bp;
  1415. /*
  1416. * Check to see if the dquot has been flushed delayed
  1417. * write. If so, grab its buffer and send it
  1418. * out immediately. We'll be able to acquire
  1419. * the flush lock when the I/O completes.
  1420. */
  1421. bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
  1422. XFS_QI_DQCHUNKLEN(dqp->q_mount),
  1423. XFS_INCORE_TRYLOCK);
  1424. if (bp != NULL) {
  1425. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1426. if (XFS_BUF_ISPINNED(bp)) {
  1427. xfs_log_force(dqp->q_mount,
  1428. (xfs_lsn_t)0,
  1429. XFS_LOG_FORCE);
  1430. }
  1431. xfs_bawrite(dqp->q_mount, bp);
  1432. } else {
  1433. xfs_buf_relse(bp);
  1434. }
  1435. }
  1436. xfs_dqflock(dqp);
  1437. }