xfs_qm.c 60 KB

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