xfs_dquot.c 42 KB

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