xfs_qm.c 50 KB

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