xfs_qm.c 58 KB

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