xfs_dquot.c 42 KB

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