xfs_qm.c 62 KB

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