xfs_qm.c 52 KB

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