xfs_dquot.c 42 KB

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