xfs_qm.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_iwalk.h"
  17. #include "xfs_quota.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_bmap_util.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_trans_space.h"
  22. #include "xfs_qm.h"
  23. #include "xfs_trace.h"
  24. #include "xfs_icache.h"
  25. #include "xfs_error.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_ialloc.h"
  28. #include "xfs_log_priv.h"
  29. #include "xfs_health.h"
  30. /*
  31. * The global quota manager. There is only one of these for the entire
  32. * system, _not_ one per file system. XQM keeps track of the overall
  33. * quota functionality, including maintaining the freelist and hash
  34. * tables of dquots.
  35. */
  36. STATIC int xfs_qm_init_quotainos(struct xfs_mount *mp);
  37. STATIC int xfs_qm_init_quotainfo(struct xfs_mount *mp);
  38. STATIC void xfs_qm_destroy_quotainos(struct xfs_quotainfo *qi);
  39. STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
  40. /*
  41. * We use the batch lookup interface to iterate over the dquots as it
  42. * currently is the only interface into the radix tree code that allows
  43. * fuzzy lookups instead of exact matches. Holding the lock over multiple
  44. * operations is fine as all callers are used either during mount/umount
  45. * or quotaoff.
  46. */
  47. #define XFS_DQ_LOOKUP_BATCH 32
  48. STATIC int
  49. xfs_qm_dquot_walk(
  50. struct xfs_mount *mp,
  51. xfs_dqtype_t type,
  52. int (*execute)(struct xfs_dquot *dqp, void *data),
  53. void *data)
  54. {
  55. struct xfs_quotainfo *qi = mp->m_quotainfo;
  56. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  57. uint32_t next_index;
  58. int last_error = 0;
  59. int skipped;
  60. int nr_found;
  61. restart:
  62. skipped = 0;
  63. next_index = 0;
  64. nr_found = 0;
  65. while (1) {
  66. struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
  67. int error;
  68. int i;
  69. mutex_lock(&qi->qi_tree_lock);
  70. nr_found = radix_tree_gang_lookup(tree, (void **)batch,
  71. next_index, XFS_DQ_LOOKUP_BATCH);
  72. if (!nr_found) {
  73. mutex_unlock(&qi->qi_tree_lock);
  74. break;
  75. }
  76. for (i = 0; i < nr_found; i++) {
  77. struct xfs_dquot *dqp = batch[i];
  78. next_index = dqp->q_id + 1;
  79. error = execute(batch[i], data);
  80. if (error == -EAGAIN) {
  81. skipped++;
  82. continue;
  83. }
  84. if (error && last_error != -EFSCORRUPTED)
  85. last_error = error;
  86. }
  87. mutex_unlock(&qi->qi_tree_lock);
  88. /* bail out if the filesystem is corrupted. */
  89. if (last_error == -EFSCORRUPTED) {
  90. skipped = 0;
  91. break;
  92. }
  93. /* we're done if id overflows back to zero */
  94. if (!next_index)
  95. break;
  96. }
  97. if (skipped) {
  98. delay(1);
  99. goto restart;
  100. }
  101. return last_error;
  102. }
  103. /*
  104. * Purge a dquot from all tracking data structures and free it.
  105. */
  106. STATIC int
  107. xfs_qm_dqpurge(
  108. struct xfs_dquot *dqp,
  109. void *data)
  110. {
  111. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  112. int error = -EAGAIN;
  113. xfs_dqlock(dqp);
  114. if ((dqp->q_flags & XFS_DQFLAG_FREEING) || dqp->q_nrefs != 0)
  115. goto out_unlock;
  116. dqp->q_flags |= XFS_DQFLAG_FREEING;
  117. xfs_dqflock(dqp);
  118. /*
  119. * If we are turning this type of quotas off, we don't care
  120. * about the dirty metadata sitting in this dquot. OTOH, if
  121. * we're unmounting, we do care, so we flush it and wait.
  122. */
  123. if (XFS_DQ_IS_DIRTY(dqp)) {
  124. struct xfs_buf *bp = NULL;
  125. /*
  126. * We don't care about getting disk errors here. We need
  127. * to purge this dquot anyway, so we go ahead regardless.
  128. */
  129. error = xfs_dquot_use_attached_buf(dqp, &bp);
  130. if (error == -EAGAIN) {
  131. xfs_dqfunlock(dqp);
  132. dqp->q_flags &= ~XFS_DQFLAG_FREEING;
  133. goto out_unlock;
  134. }
  135. if (!bp)
  136. goto out_funlock;
  137. /*
  138. * dqflush completes dqflock on error, and the bwrite ioend
  139. * does it on success.
  140. */
  141. error = xfs_qm_dqflush(dqp, bp);
  142. if (!error) {
  143. error = xfs_bwrite(bp);
  144. xfs_buf_relse(bp);
  145. }
  146. xfs_dqflock(dqp);
  147. }
  148. xfs_dquot_detach_buf(dqp);
  149. out_funlock:
  150. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  151. ASSERT(xlog_is_shutdown(dqp->q_logitem.qli_item.li_log) ||
  152. !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
  153. xfs_dqfunlock(dqp);
  154. xfs_dqunlock(dqp);
  155. radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
  156. qi->qi_dquots--;
  157. /*
  158. * We move dquots to the freelist as soon as their reference count
  159. * hits zero, so it really should be on the freelist here.
  160. */
  161. ASSERT(!list_empty(&dqp->q_lru));
  162. list_lru_del_obj(&qi->qi_lru, &dqp->q_lru);
  163. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
  164. xfs_qm_dqdestroy(dqp);
  165. return 0;
  166. out_unlock:
  167. xfs_dqunlock(dqp);
  168. return error;
  169. }
  170. /*
  171. * Purge the dquot cache.
  172. */
  173. static void
  174. xfs_qm_dqpurge_all(
  175. struct xfs_mount *mp)
  176. {
  177. xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_dqpurge, NULL);
  178. xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_dqpurge, NULL);
  179. xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_dqpurge, NULL);
  180. }
  181. /*
  182. * Just destroy the quotainfo structure.
  183. */
  184. void
  185. xfs_qm_unmount(
  186. struct xfs_mount *mp)
  187. {
  188. if (mp->m_quotainfo) {
  189. xfs_qm_dqpurge_all(mp);
  190. xfs_qm_destroy_quotainfo(mp);
  191. }
  192. }
  193. /*
  194. * Called from the vfsops layer.
  195. */
  196. void
  197. xfs_qm_unmount_quotas(
  198. xfs_mount_t *mp)
  199. {
  200. /*
  201. * Release the dquots that root inode, et al might be holding,
  202. * before we flush quotas and blow away the quotainfo structure.
  203. */
  204. ASSERT(mp->m_rootip);
  205. xfs_qm_dqdetach(mp->m_rootip);
  206. if (mp->m_rbmip)
  207. xfs_qm_dqdetach(mp->m_rbmip);
  208. if (mp->m_rsumip)
  209. xfs_qm_dqdetach(mp->m_rsumip);
  210. /*
  211. * Release the quota inodes.
  212. */
  213. if (mp->m_quotainfo) {
  214. if (mp->m_quotainfo->qi_uquotaip) {
  215. xfs_irele(mp->m_quotainfo->qi_uquotaip);
  216. mp->m_quotainfo->qi_uquotaip = NULL;
  217. }
  218. if (mp->m_quotainfo->qi_gquotaip) {
  219. xfs_irele(mp->m_quotainfo->qi_gquotaip);
  220. mp->m_quotainfo->qi_gquotaip = NULL;
  221. }
  222. if (mp->m_quotainfo->qi_pquotaip) {
  223. xfs_irele(mp->m_quotainfo->qi_pquotaip);
  224. mp->m_quotainfo->qi_pquotaip = NULL;
  225. }
  226. }
  227. }
  228. STATIC int
  229. xfs_qm_dqattach_one(
  230. struct xfs_inode *ip,
  231. xfs_dqtype_t type,
  232. bool doalloc,
  233. struct xfs_dquot **IO_idqpp)
  234. {
  235. struct xfs_dquot *dqp;
  236. int error;
  237. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  238. error = 0;
  239. /*
  240. * See if we already have it in the inode itself. IO_idqpp is &i_udquot
  241. * or &i_gdquot. This made the code look weird, but made the logic a lot
  242. * simpler.
  243. */
  244. dqp = *IO_idqpp;
  245. if (dqp) {
  246. trace_xfs_dqattach_found(dqp);
  247. return 0;
  248. }
  249. /*
  250. * Find the dquot from somewhere. This bumps the reference count of
  251. * dquot and returns it locked. This can return ENOENT if dquot didn't
  252. * exist on disk and we didn't ask it to allocate; ESRCH if quotas got
  253. * turned off suddenly.
  254. */
  255. error = xfs_qm_dqget_inode(ip, type, doalloc, &dqp);
  256. if (error)
  257. return error;
  258. trace_xfs_dqattach_get(dqp);
  259. /*
  260. * dqget may have dropped and re-acquired the ilock, but it guarantees
  261. * that the dquot returned is the one that should go in the inode.
  262. */
  263. *IO_idqpp = dqp;
  264. xfs_dqunlock(dqp);
  265. return 0;
  266. }
  267. static bool
  268. xfs_qm_need_dqattach(
  269. struct xfs_inode *ip)
  270. {
  271. struct xfs_mount *mp = ip->i_mount;
  272. if (!XFS_IS_QUOTA_ON(mp))
  273. return false;
  274. if (!XFS_NOT_DQATTACHED(mp, ip))
  275. return false;
  276. if (xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
  277. return false;
  278. return true;
  279. }
  280. /*
  281. * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
  282. * into account.
  283. * If @doalloc is true, the dquot(s) will be allocated if needed.
  284. * Inode may get unlocked and relocked in here, and the caller must deal with
  285. * the consequences.
  286. */
  287. int
  288. xfs_qm_dqattach_locked(
  289. xfs_inode_t *ip,
  290. bool doalloc)
  291. {
  292. xfs_mount_t *mp = ip->i_mount;
  293. int error = 0;
  294. if (!xfs_qm_need_dqattach(ip))
  295. return 0;
  296. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  297. if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
  298. error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_USER,
  299. doalloc, &ip->i_udquot);
  300. if (error)
  301. goto done;
  302. ASSERT(ip->i_udquot);
  303. }
  304. if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
  305. error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_GROUP,
  306. doalloc, &ip->i_gdquot);
  307. if (error)
  308. goto done;
  309. ASSERT(ip->i_gdquot);
  310. }
  311. if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
  312. error = xfs_qm_dqattach_one(ip, XFS_DQTYPE_PROJ,
  313. doalloc, &ip->i_pdquot);
  314. if (error)
  315. goto done;
  316. ASSERT(ip->i_pdquot);
  317. }
  318. done:
  319. /*
  320. * Don't worry about the dquots that we may have attached before any
  321. * error - they'll get detached later if it has not already been done.
  322. */
  323. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  324. return error;
  325. }
  326. int
  327. xfs_qm_dqattach(
  328. struct xfs_inode *ip)
  329. {
  330. int error;
  331. if (!xfs_qm_need_dqattach(ip))
  332. return 0;
  333. xfs_ilock(ip, XFS_ILOCK_EXCL);
  334. error = xfs_qm_dqattach_locked(ip, false);
  335. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  336. return error;
  337. }
  338. /*
  339. * Release dquots (and their references) if any.
  340. * The inode should be locked EXCL except when this's called by
  341. * xfs_ireclaim.
  342. */
  343. void
  344. xfs_qm_dqdetach(
  345. xfs_inode_t *ip)
  346. {
  347. if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
  348. return;
  349. trace_xfs_dquot_dqdetach(ip);
  350. ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, ip->i_ino));
  351. if (ip->i_udquot) {
  352. xfs_qm_dqrele(ip->i_udquot);
  353. ip->i_udquot = NULL;
  354. }
  355. if (ip->i_gdquot) {
  356. xfs_qm_dqrele(ip->i_gdquot);
  357. ip->i_gdquot = NULL;
  358. }
  359. if (ip->i_pdquot) {
  360. xfs_qm_dqrele(ip->i_pdquot);
  361. ip->i_pdquot = NULL;
  362. }
  363. }
  364. struct xfs_qm_isolate {
  365. struct list_head buffers;
  366. struct list_head dispose;
  367. };
  368. static enum lru_status
  369. xfs_qm_dquot_isolate(
  370. struct list_head *item,
  371. struct list_lru_one *lru,
  372. spinlock_t *lru_lock,
  373. void *arg)
  374. __releases(lru_lock) __acquires(lru_lock)
  375. {
  376. struct xfs_dquot *dqp = container_of(item,
  377. struct xfs_dquot, q_lru);
  378. struct xfs_qm_isolate *isol = arg;
  379. if (!xfs_dqlock_nowait(dqp))
  380. goto out_miss_busy;
  381. /*
  382. * If something else is freeing this dquot and hasn't yet removed it
  383. * from the LRU, leave it for the freeing task to complete the freeing
  384. * process rather than risk it being free from under us here.
  385. */
  386. if (dqp->q_flags & XFS_DQFLAG_FREEING)
  387. goto out_miss_unlock;
  388. /*
  389. * This dquot has acquired a reference in the meantime remove it from
  390. * the freelist and try again.
  391. */
  392. if (dqp->q_nrefs) {
  393. xfs_dqunlock(dqp);
  394. XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
  395. trace_xfs_dqreclaim_want(dqp);
  396. list_lru_isolate(lru, &dqp->q_lru);
  397. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
  398. return LRU_REMOVED;
  399. }
  400. /*
  401. * If the dquot is dirty, flush it. If it's already being flushed, just
  402. * skip it so there is time for the IO to complete before we try to
  403. * reclaim it again on the next LRU pass.
  404. */
  405. if (!xfs_dqflock_nowait(dqp))
  406. goto out_miss_unlock;
  407. if (XFS_DQ_IS_DIRTY(dqp)) {
  408. struct xfs_buf *bp = NULL;
  409. int error;
  410. trace_xfs_dqreclaim_dirty(dqp);
  411. /* we have to drop the LRU lock to flush the dquot */
  412. spin_unlock(lru_lock);
  413. error = xfs_dquot_use_attached_buf(dqp, &bp);
  414. if (!bp || error == -EAGAIN) {
  415. xfs_dqfunlock(dqp);
  416. goto out_unlock_dirty;
  417. }
  418. /*
  419. * dqflush completes dqflock on error, and the delwri ioend
  420. * does it on success.
  421. */
  422. error = xfs_qm_dqflush(dqp, bp);
  423. if (error)
  424. goto out_unlock_dirty;
  425. xfs_buf_delwri_queue(bp, &isol->buffers);
  426. xfs_buf_relse(bp);
  427. goto out_unlock_dirty;
  428. }
  429. xfs_dquot_detach_buf(dqp);
  430. xfs_dqfunlock(dqp);
  431. /*
  432. * Prevent lookups now that we are past the point of no return.
  433. */
  434. dqp->q_flags |= XFS_DQFLAG_FREEING;
  435. xfs_dqunlock(dqp);
  436. ASSERT(dqp->q_nrefs == 0);
  437. list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
  438. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
  439. trace_xfs_dqreclaim_done(dqp);
  440. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
  441. return LRU_REMOVED;
  442. out_miss_unlock:
  443. xfs_dqunlock(dqp);
  444. out_miss_busy:
  445. trace_xfs_dqreclaim_busy(dqp);
  446. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
  447. return LRU_SKIP;
  448. out_unlock_dirty:
  449. trace_xfs_dqreclaim_busy(dqp);
  450. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
  451. xfs_dqunlock(dqp);
  452. spin_lock(lru_lock);
  453. return LRU_RETRY;
  454. }
  455. static unsigned long
  456. xfs_qm_shrink_scan(
  457. struct shrinker *shrink,
  458. struct shrink_control *sc)
  459. {
  460. struct xfs_quotainfo *qi = shrink->private_data;
  461. struct xfs_qm_isolate isol;
  462. unsigned long freed;
  463. int error;
  464. if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
  465. return 0;
  466. INIT_LIST_HEAD(&isol.buffers);
  467. INIT_LIST_HEAD(&isol.dispose);
  468. freed = list_lru_shrink_walk(&qi->qi_lru, sc,
  469. xfs_qm_dquot_isolate, &isol);
  470. error = xfs_buf_delwri_submit(&isol.buffers);
  471. if (error)
  472. xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
  473. while (!list_empty(&isol.dispose)) {
  474. struct xfs_dquot *dqp;
  475. dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
  476. list_del_init(&dqp->q_lru);
  477. xfs_qm_dqfree_one(dqp);
  478. }
  479. return freed;
  480. }
  481. static unsigned long
  482. xfs_qm_shrink_count(
  483. struct shrinker *shrink,
  484. struct shrink_control *sc)
  485. {
  486. struct xfs_quotainfo *qi = shrink->private_data;
  487. return list_lru_shrink_count(&qi->qi_lru, sc);
  488. }
  489. STATIC void
  490. xfs_qm_set_defquota(
  491. struct xfs_mount *mp,
  492. xfs_dqtype_t type,
  493. struct xfs_quotainfo *qinf)
  494. {
  495. struct xfs_dquot *dqp;
  496. struct xfs_def_quota *defq;
  497. int error;
  498. error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
  499. if (error)
  500. return;
  501. defq = xfs_get_defquota(qinf, xfs_dquot_type(dqp));
  502. /*
  503. * Timers and warnings have been already set, let's just set the
  504. * default limits for this quota type
  505. */
  506. defq->blk.hard = dqp->q_blk.hardlimit;
  507. defq->blk.soft = dqp->q_blk.softlimit;
  508. defq->ino.hard = dqp->q_ino.hardlimit;
  509. defq->ino.soft = dqp->q_ino.softlimit;
  510. defq->rtb.hard = dqp->q_rtb.hardlimit;
  511. defq->rtb.soft = dqp->q_rtb.softlimit;
  512. xfs_qm_dqdestroy(dqp);
  513. }
  514. /* Initialize quota time limits from the root dquot. */
  515. static void
  516. xfs_qm_init_timelimits(
  517. struct xfs_mount *mp,
  518. xfs_dqtype_t type)
  519. {
  520. struct xfs_quotainfo *qinf = mp->m_quotainfo;
  521. struct xfs_def_quota *defq;
  522. struct xfs_dquot *dqp;
  523. int error;
  524. defq = xfs_get_defquota(qinf, type);
  525. defq->blk.time = XFS_QM_BTIMELIMIT;
  526. defq->ino.time = XFS_QM_ITIMELIMIT;
  527. defq->rtb.time = XFS_QM_RTBTIMELIMIT;
  528. /*
  529. * We try to get the limits from the superuser's limits fields.
  530. * This is quite hacky, but it is standard quota practice.
  531. *
  532. * Since we may not have done a quotacheck by this point, just read
  533. * the dquot without attaching it to any hashtables or lists.
  534. */
  535. error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
  536. if (error)
  537. return;
  538. /*
  539. * The warnings and timers set the grace period given to
  540. * a user or group before he or she can not perform any
  541. * more writing. If it is zero, a default is used.
  542. */
  543. if (dqp->q_blk.timer)
  544. defq->blk.time = dqp->q_blk.timer;
  545. if (dqp->q_ino.timer)
  546. defq->ino.time = dqp->q_ino.timer;
  547. if (dqp->q_rtb.timer)
  548. defq->rtb.time = dqp->q_rtb.timer;
  549. xfs_qm_dqdestroy(dqp);
  550. }
  551. /*
  552. * This initializes all the quota information that's kept in the
  553. * mount structure
  554. */
  555. STATIC int
  556. xfs_qm_init_quotainfo(
  557. struct xfs_mount *mp)
  558. {
  559. struct xfs_quotainfo *qinf;
  560. int error;
  561. ASSERT(XFS_IS_QUOTA_ON(mp));
  562. qinf = mp->m_quotainfo = kzalloc(sizeof(struct xfs_quotainfo),
  563. GFP_KERNEL | __GFP_NOFAIL);
  564. error = list_lru_init(&qinf->qi_lru);
  565. if (error)
  566. goto out_free_qinf;
  567. /*
  568. * See if quotainodes are setup, and if not, allocate them,
  569. * and change the superblock accordingly.
  570. */
  571. error = xfs_qm_init_quotainos(mp);
  572. if (error)
  573. goto out_free_lru;
  574. INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_KERNEL);
  575. INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_KERNEL);
  576. INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_KERNEL);
  577. mutex_init(&qinf->qi_tree_lock);
  578. /* mutex used to serialize quotaoffs */
  579. mutex_init(&qinf->qi_quotaofflock);
  580. /* Precalc some constants */
  581. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  582. qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
  583. if (xfs_has_bigtime(mp)) {
  584. qinf->qi_expiry_min =
  585. xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MIN);
  586. qinf->qi_expiry_max =
  587. xfs_dq_bigtime_to_unix(XFS_DQ_BIGTIME_EXPIRY_MAX);
  588. } else {
  589. qinf->qi_expiry_min = XFS_DQ_LEGACY_EXPIRY_MIN;
  590. qinf->qi_expiry_max = XFS_DQ_LEGACY_EXPIRY_MAX;
  591. }
  592. trace_xfs_quota_expiry_range(mp, qinf->qi_expiry_min,
  593. qinf->qi_expiry_max);
  594. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  595. xfs_qm_init_timelimits(mp, XFS_DQTYPE_USER);
  596. xfs_qm_init_timelimits(mp, XFS_DQTYPE_GROUP);
  597. xfs_qm_init_timelimits(mp, XFS_DQTYPE_PROJ);
  598. if (XFS_IS_UQUOTA_ON(mp))
  599. xfs_qm_set_defquota(mp, XFS_DQTYPE_USER, qinf);
  600. if (XFS_IS_GQUOTA_ON(mp))
  601. xfs_qm_set_defquota(mp, XFS_DQTYPE_GROUP, qinf);
  602. if (XFS_IS_PQUOTA_ON(mp))
  603. xfs_qm_set_defquota(mp, XFS_DQTYPE_PROJ, qinf);
  604. qinf->qi_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "xfs-qm:%s",
  605. mp->m_super->s_id);
  606. if (!qinf->qi_shrinker) {
  607. error = -ENOMEM;
  608. goto out_free_inos;
  609. }
  610. qinf->qi_shrinker->count_objects = xfs_qm_shrink_count;
  611. qinf->qi_shrinker->scan_objects = xfs_qm_shrink_scan;
  612. qinf->qi_shrinker->private_data = qinf;
  613. shrinker_register(qinf->qi_shrinker);
  614. xfs_hooks_init(&qinf->qi_mod_ino_dqtrx_hooks);
  615. xfs_hooks_init(&qinf->qi_apply_dqtrx_hooks);
  616. return 0;
  617. out_free_inos:
  618. mutex_destroy(&qinf->qi_quotaofflock);
  619. mutex_destroy(&qinf->qi_tree_lock);
  620. xfs_qm_destroy_quotainos(qinf);
  621. out_free_lru:
  622. list_lru_destroy(&qinf->qi_lru);
  623. out_free_qinf:
  624. kfree(qinf);
  625. mp->m_quotainfo = NULL;
  626. return error;
  627. }
  628. /*
  629. * Gets called when unmounting a filesystem or when all quotas get
  630. * turned off.
  631. * This purges the quota inodes, destroys locks and frees itself.
  632. */
  633. void
  634. xfs_qm_destroy_quotainfo(
  635. struct xfs_mount *mp)
  636. {
  637. struct xfs_quotainfo *qi;
  638. qi = mp->m_quotainfo;
  639. ASSERT(qi != NULL);
  640. shrinker_free(qi->qi_shrinker);
  641. list_lru_destroy(&qi->qi_lru);
  642. xfs_qm_destroy_quotainos(qi);
  643. mutex_destroy(&qi->qi_tree_lock);
  644. mutex_destroy(&qi->qi_quotaofflock);
  645. kfree(qi);
  646. mp->m_quotainfo = NULL;
  647. }
  648. /*
  649. * Create an inode and return with a reference already taken, but unlocked
  650. * This is how we create quota inodes
  651. */
  652. STATIC int
  653. xfs_qm_qino_alloc(
  654. struct xfs_mount *mp,
  655. struct xfs_inode **ipp,
  656. unsigned int flags)
  657. {
  658. struct xfs_trans *tp;
  659. int error;
  660. bool need_alloc = true;
  661. *ipp = NULL;
  662. /*
  663. * With superblock that doesn't have separate pquotino, we
  664. * share an inode between gquota and pquota. If the on-disk
  665. * superblock has GQUOTA and the filesystem is now mounted
  666. * with PQUOTA, just use sb_gquotino for sb_pquotino and
  667. * vice-versa.
  668. */
  669. if (!xfs_has_pquotino(mp) &&
  670. (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
  671. xfs_ino_t ino = NULLFSINO;
  672. if ((flags & XFS_QMOPT_PQUOTA) &&
  673. (mp->m_sb.sb_gquotino != NULLFSINO)) {
  674. ino = mp->m_sb.sb_gquotino;
  675. if (XFS_IS_CORRUPT(mp,
  676. mp->m_sb.sb_pquotino != NULLFSINO)) {
  677. xfs_fs_mark_sick(mp, XFS_SICK_FS_PQUOTA);
  678. return -EFSCORRUPTED;
  679. }
  680. } else if ((flags & XFS_QMOPT_GQUOTA) &&
  681. (mp->m_sb.sb_pquotino != NULLFSINO)) {
  682. ino = mp->m_sb.sb_pquotino;
  683. if (XFS_IS_CORRUPT(mp,
  684. mp->m_sb.sb_gquotino != NULLFSINO)) {
  685. xfs_fs_mark_sick(mp, XFS_SICK_FS_GQUOTA);
  686. return -EFSCORRUPTED;
  687. }
  688. }
  689. if (ino != NULLFSINO) {
  690. error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
  691. if (error)
  692. return error;
  693. mp->m_sb.sb_gquotino = NULLFSINO;
  694. mp->m_sb.sb_pquotino = NULLFSINO;
  695. need_alloc = false;
  696. }
  697. }
  698. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
  699. need_alloc ? XFS_QM_QINOCREATE_SPACE_RES(mp) : 0,
  700. 0, 0, &tp);
  701. if (error)
  702. return error;
  703. if (need_alloc) {
  704. struct xfs_icreate_args args = {
  705. .mode = S_IFREG,
  706. .flags = XFS_ICREATE_UNLINKABLE,
  707. };
  708. xfs_ino_t ino;
  709. error = xfs_dialloc(&tp, &args, &ino);
  710. if (!error)
  711. error = xfs_icreate(tp, ino, &args, ipp);
  712. if (error) {
  713. xfs_trans_cancel(tp);
  714. return error;
  715. }
  716. }
  717. /*
  718. * Make the changes in the superblock, and log those too.
  719. * sbfields arg may contain fields other than *QUOTINO;
  720. * VERSIONNUM for example.
  721. */
  722. spin_lock(&mp->m_sb_lock);
  723. if (flags & XFS_QMOPT_SBVERSION) {
  724. ASSERT(!xfs_has_quota(mp));
  725. xfs_add_quota(mp);
  726. mp->m_sb.sb_uquotino = NULLFSINO;
  727. mp->m_sb.sb_gquotino = NULLFSINO;
  728. mp->m_sb.sb_pquotino = NULLFSINO;
  729. /* qflags will get updated fully _after_ quotacheck */
  730. mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
  731. }
  732. if (flags & XFS_QMOPT_UQUOTA)
  733. mp->m_sb.sb_uquotino = (*ipp)->i_ino;
  734. else if (flags & XFS_QMOPT_GQUOTA)
  735. mp->m_sb.sb_gquotino = (*ipp)->i_ino;
  736. else
  737. mp->m_sb.sb_pquotino = (*ipp)->i_ino;
  738. spin_unlock(&mp->m_sb_lock);
  739. xfs_log_sb(tp);
  740. error = xfs_trans_commit(tp);
  741. if (error) {
  742. ASSERT(xfs_is_shutdown(mp));
  743. xfs_alert(mp, "%s failed (error %d)!", __func__, error);
  744. }
  745. if (need_alloc) {
  746. xfs_iunlock(*ipp, XFS_ILOCK_EXCL);
  747. xfs_finish_inode_setup(*ipp);
  748. }
  749. return error;
  750. }
  751. STATIC void
  752. xfs_qm_reset_dqcounts(
  753. struct xfs_mount *mp,
  754. struct xfs_buf *bp,
  755. xfs_dqid_t id,
  756. xfs_dqtype_t type)
  757. {
  758. struct xfs_dqblk *dqb;
  759. int j;
  760. trace_xfs_reset_dqcounts(bp, _RET_IP_);
  761. /*
  762. * Reset all counters and timers. They'll be
  763. * started afresh by xfs_qm_quotacheck.
  764. */
  765. #ifdef DEBUG
  766. j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
  767. sizeof(struct xfs_dqblk);
  768. ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
  769. #endif
  770. dqb = bp->b_addr;
  771. for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
  772. struct xfs_disk_dquot *ddq;
  773. ddq = (struct xfs_disk_dquot *)&dqb[j];
  774. /*
  775. * Do a sanity check, and if needed, repair the dqblk. Don't
  776. * output any warnings because it's perfectly possible to
  777. * find uninitialised dquot blks. See comment in
  778. * xfs_dquot_verify.
  779. */
  780. if (xfs_dqblk_verify(mp, &dqb[j], id + j) ||
  781. (dqb[j].dd_diskdq.d_type & XFS_DQTYPE_REC_MASK) != type)
  782. xfs_dqblk_repair(mp, &dqb[j], id + j, type);
  783. /*
  784. * Reset type in case we are reusing group quota file for
  785. * project quotas or vice versa
  786. */
  787. ddq->d_type = type;
  788. ddq->d_bcount = 0;
  789. ddq->d_icount = 0;
  790. ddq->d_rtbcount = 0;
  791. /*
  792. * dquot id 0 stores the default grace period and the maximum
  793. * warning limit that were set by the administrator, so we
  794. * should not reset them.
  795. */
  796. if (ddq->d_id != 0) {
  797. ddq->d_btimer = 0;
  798. ddq->d_itimer = 0;
  799. ddq->d_rtbtimer = 0;
  800. ddq->d_bwarns = 0;
  801. ddq->d_iwarns = 0;
  802. ddq->d_rtbwarns = 0;
  803. if (xfs_has_bigtime(mp))
  804. ddq->d_type |= XFS_DQTYPE_BIGTIME;
  805. }
  806. if (xfs_has_crc(mp)) {
  807. xfs_update_cksum((char *)&dqb[j],
  808. sizeof(struct xfs_dqblk),
  809. XFS_DQUOT_CRC_OFF);
  810. }
  811. }
  812. }
  813. STATIC int
  814. xfs_qm_reset_dqcounts_all(
  815. struct xfs_mount *mp,
  816. xfs_dqid_t firstid,
  817. xfs_fsblock_t bno,
  818. xfs_filblks_t blkcnt,
  819. xfs_dqtype_t type,
  820. struct list_head *buffer_list)
  821. {
  822. struct xfs_buf *bp;
  823. int error = 0;
  824. ASSERT(blkcnt > 0);
  825. /*
  826. * Blkcnt arg can be a very big number, and might even be
  827. * larger than the log itself. So, we have to break it up into
  828. * manageable-sized transactions.
  829. * Note that we don't start a permanent transaction here; we might
  830. * not be able to get a log reservation for the whole thing up front,
  831. * and we don't really care to either, because we just discard
  832. * everything if we were to crash in the middle of this loop.
  833. */
  834. while (blkcnt--) {
  835. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  836. XFS_FSB_TO_DADDR(mp, bno),
  837. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  838. &xfs_dquot_buf_ops);
  839. /*
  840. * CRC and validation errors will return a EFSCORRUPTED here. If
  841. * this occurs, re-read without CRC validation so that we can
  842. * repair the damage via xfs_qm_reset_dqcounts(). This process
  843. * will leave a trace in the log indicating corruption has
  844. * been detected.
  845. */
  846. if (error == -EFSCORRUPTED) {
  847. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  848. XFS_FSB_TO_DADDR(mp, bno),
  849. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  850. NULL);
  851. }
  852. if (error)
  853. break;
  854. /*
  855. * A corrupt buffer might not have a verifier attached, so
  856. * make sure we have the correct one attached before writeback
  857. * occurs.
  858. */
  859. bp->b_ops = &xfs_dquot_buf_ops;
  860. xfs_qm_reset_dqcounts(mp, bp, firstid, type);
  861. xfs_buf_delwri_queue(bp, buffer_list);
  862. xfs_buf_relse(bp);
  863. /* goto the next block. */
  864. bno++;
  865. firstid += mp->m_quotainfo->qi_dqperchunk;
  866. }
  867. return error;
  868. }
  869. /*
  870. * Iterate over all allocated dquot blocks in this quota inode, zeroing all
  871. * counters for every chunk of dquots that we find.
  872. */
  873. STATIC int
  874. xfs_qm_reset_dqcounts_buf(
  875. struct xfs_mount *mp,
  876. struct xfs_inode *qip,
  877. xfs_dqtype_t type,
  878. struct list_head *buffer_list)
  879. {
  880. struct xfs_bmbt_irec *map;
  881. int i, nmaps; /* number of map entries */
  882. int error; /* return value */
  883. xfs_fileoff_t lblkno;
  884. xfs_filblks_t maxlblkcnt;
  885. xfs_dqid_t firstid;
  886. xfs_fsblock_t rablkno;
  887. xfs_filblks_t rablkcnt;
  888. error = 0;
  889. /*
  890. * This looks racy, but we can't keep an inode lock across a
  891. * trans_reserve. But, this gets called during quotacheck, and that
  892. * happens only at mount time which is single threaded.
  893. */
  894. if (qip->i_nblocks == 0)
  895. return 0;
  896. map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
  897. GFP_KERNEL | __GFP_NOFAIL);
  898. lblkno = 0;
  899. maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  900. do {
  901. uint lock_mode;
  902. nmaps = XFS_DQITER_MAP_SIZE;
  903. /*
  904. * We aren't changing the inode itself. Just changing
  905. * some of its data. No new blocks are added here, and
  906. * the inode is never added to the transaction.
  907. */
  908. lock_mode = xfs_ilock_data_map_shared(qip);
  909. error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
  910. map, &nmaps, 0);
  911. xfs_iunlock(qip, lock_mode);
  912. if (error)
  913. break;
  914. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  915. for (i = 0; i < nmaps; i++) {
  916. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  917. ASSERT(map[i].br_blockcount);
  918. lblkno += map[i].br_blockcount;
  919. if (map[i].br_startblock == HOLESTARTBLOCK)
  920. continue;
  921. firstid = (xfs_dqid_t) map[i].br_startoff *
  922. mp->m_quotainfo->qi_dqperchunk;
  923. /*
  924. * Do a read-ahead on the next extent.
  925. */
  926. if ((i+1 < nmaps) &&
  927. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  928. rablkcnt = map[i+1].br_blockcount;
  929. rablkno = map[i+1].br_startblock;
  930. while (rablkcnt--) {
  931. xfs_buf_readahead(mp->m_ddev_targp,
  932. XFS_FSB_TO_DADDR(mp, rablkno),
  933. mp->m_quotainfo->qi_dqchunklen,
  934. &xfs_dquot_buf_ops);
  935. rablkno++;
  936. }
  937. }
  938. /*
  939. * Iterate thru all the blks in the extent and
  940. * reset the counters of all the dquots inside them.
  941. */
  942. error = xfs_qm_reset_dqcounts_all(mp, firstid,
  943. map[i].br_startblock,
  944. map[i].br_blockcount,
  945. type, buffer_list);
  946. if (error)
  947. goto out;
  948. }
  949. } while (nmaps > 0);
  950. out:
  951. kfree(map);
  952. return error;
  953. }
  954. /*
  955. * Called by dqusage_adjust in doing a quotacheck.
  956. *
  957. * Given the inode, and a dquot id this updates both the incore dqout as well
  958. * as the buffer copy. This is so that once the quotacheck is done, we can
  959. * just log all the buffers, as opposed to logging numerous updates to
  960. * individual dquots.
  961. */
  962. STATIC int
  963. xfs_qm_quotacheck_dqadjust(
  964. struct xfs_inode *ip,
  965. xfs_dqtype_t type,
  966. xfs_qcnt_t nblks,
  967. xfs_qcnt_t rtblks)
  968. {
  969. struct xfs_mount *mp = ip->i_mount;
  970. struct xfs_dquot *dqp;
  971. xfs_dqid_t id;
  972. int error;
  973. id = xfs_qm_id_for_quotatype(ip, type);
  974. error = xfs_qm_dqget(mp, id, type, true, &dqp);
  975. if (error) {
  976. /*
  977. * Shouldn't be able to turn off quotas here.
  978. */
  979. ASSERT(error != -ESRCH);
  980. ASSERT(error != -ENOENT);
  981. return error;
  982. }
  983. error = xfs_dquot_attach_buf(NULL, dqp);
  984. if (error)
  985. return error;
  986. trace_xfs_dqadjust(dqp);
  987. /*
  988. * Adjust the inode count and the block count to reflect this inode's
  989. * resource usage.
  990. */
  991. dqp->q_ino.count++;
  992. dqp->q_ino.reserved++;
  993. if (nblks) {
  994. dqp->q_blk.count += nblks;
  995. dqp->q_blk.reserved += nblks;
  996. }
  997. if (rtblks) {
  998. dqp->q_rtb.count += rtblks;
  999. dqp->q_rtb.reserved += rtblks;
  1000. }
  1001. /*
  1002. * Set default limits, adjust timers (since we changed usages)
  1003. *
  1004. * There are no timers for the default values set in the root dquot.
  1005. */
  1006. if (dqp->q_id) {
  1007. xfs_qm_adjust_dqlimits(dqp);
  1008. xfs_qm_adjust_dqtimers(dqp);
  1009. }
  1010. dqp->q_flags |= XFS_DQFLAG_DIRTY;
  1011. xfs_qm_dqput(dqp);
  1012. return 0;
  1013. }
  1014. /*
  1015. * callback routine supplied to bulkstat(). Given an inumber, find its
  1016. * dquots and update them to account for resources taken by that inode.
  1017. */
  1018. /* ARGSUSED */
  1019. STATIC int
  1020. xfs_qm_dqusage_adjust(
  1021. struct xfs_mount *mp,
  1022. struct xfs_trans *tp,
  1023. xfs_ino_t ino,
  1024. void *data)
  1025. {
  1026. struct xfs_inode *ip;
  1027. xfs_qcnt_t nblks;
  1028. xfs_filblks_t rtblks = 0; /* total rt blks */
  1029. int error;
  1030. ASSERT(XFS_IS_QUOTA_ON(mp));
  1031. /*
  1032. * rootino must have its resources accounted for, not so with the quota
  1033. * inodes.
  1034. */
  1035. if (xfs_is_quota_inode(&mp->m_sb, ino))
  1036. return 0;
  1037. /*
  1038. * We don't _need_ to take the ilock EXCL here because quotacheck runs
  1039. * at mount time and therefore nobody will be racing chown/chproj.
  1040. */
  1041. error = xfs_iget(mp, tp, ino, XFS_IGET_DONTCACHE, 0, &ip);
  1042. if (error == -EINVAL || error == -ENOENT)
  1043. return 0;
  1044. if (error)
  1045. return error;
  1046. /*
  1047. * Reload the incore unlinked list to avoid failure in inodegc.
  1048. * Use an unlocked check here because unrecovered unlinked inodes
  1049. * should be somewhat rare.
  1050. */
  1051. if (xfs_inode_unlinked_incomplete(ip)) {
  1052. error = xfs_inode_reload_unlinked(ip);
  1053. if (error) {
  1054. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1055. goto error0;
  1056. }
  1057. }
  1058. ASSERT(ip->i_delayed_blks == 0);
  1059. if (XFS_IS_REALTIME_INODE(ip)) {
  1060. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  1061. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1062. if (error)
  1063. goto error0;
  1064. xfs_bmap_count_leaves(ifp, &rtblks);
  1065. }
  1066. nblks = (xfs_qcnt_t)ip->i_nblocks - rtblks;
  1067. xfs_iflags_clear(ip, XFS_IQUOTAUNCHECKED);
  1068. /*
  1069. * Add the (disk blocks and inode) resources occupied by this
  1070. * inode to its dquots. We do this adjustment in the incore dquot,
  1071. * and also copy the changes to its buffer.
  1072. * We don't care about putting these changes in a transaction
  1073. * envelope because if we crash in the middle of a 'quotacheck'
  1074. * we have to start from the beginning anyway.
  1075. * Once we're done, we'll log all the dquot bufs.
  1076. *
  1077. * The *QUOTA_ON checks below may look pretty racy, but quotachecks
  1078. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1079. */
  1080. if (XFS_IS_UQUOTA_ON(mp)) {
  1081. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_USER, nblks,
  1082. rtblks);
  1083. if (error)
  1084. goto error0;
  1085. }
  1086. if (XFS_IS_GQUOTA_ON(mp)) {
  1087. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_GROUP, nblks,
  1088. rtblks);
  1089. if (error)
  1090. goto error0;
  1091. }
  1092. if (XFS_IS_PQUOTA_ON(mp)) {
  1093. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQTYPE_PROJ, nblks,
  1094. rtblks);
  1095. if (error)
  1096. goto error0;
  1097. }
  1098. error0:
  1099. xfs_irele(ip);
  1100. return error;
  1101. }
  1102. STATIC int
  1103. xfs_qm_flush_one(
  1104. struct xfs_dquot *dqp,
  1105. void *data)
  1106. {
  1107. struct xfs_mount *mp = dqp->q_mount;
  1108. struct list_head *buffer_list = data;
  1109. struct xfs_buf *bp = NULL;
  1110. int error = 0;
  1111. xfs_dqlock(dqp);
  1112. if (dqp->q_flags & XFS_DQFLAG_FREEING)
  1113. goto out_unlock;
  1114. if (!XFS_DQ_IS_DIRTY(dqp))
  1115. goto out_unlock;
  1116. /*
  1117. * The only way the dquot is already flush locked by the time quotacheck
  1118. * gets here is if reclaim flushed it before the dqadjust walk dirtied
  1119. * it for the final time. Quotacheck collects all dquot bufs in the
  1120. * local delwri queue before dquots are dirtied, so reclaim can't have
  1121. * possibly queued it for I/O. The only way out is to push the buffer to
  1122. * cycle the flush lock.
  1123. */
  1124. if (!xfs_dqflock_nowait(dqp)) {
  1125. /* buf is pinned in-core by delwri list */
  1126. error = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
  1127. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  1128. if (error)
  1129. goto out_unlock;
  1130. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1131. error = -EAGAIN;
  1132. xfs_buf_relse(bp);
  1133. goto out_unlock;
  1134. }
  1135. xfs_buf_unlock(bp);
  1136. xfs_buf_delwri_pushbuf(bp, buffer_list);
  1137. xfs_buf_rele(bp);
  1138. error = -EAGAIN;
  1139. goto out_unlock;
  1140. }
  1141. error = xfs_dquot_use_attached_buf(dqp, &bp);
  1142. if (error)
  1143. goto out_unlock;
  1144. if (!bp) {
  1145. error = -EFSCORRUPTED;
  1146. goto out_unlock;
  1147. }
  1148. error = xfs_qm_dqflush(dqp, bp);
  1149. if (!error)
  1150. xfs_buf_delwri_queue(bp, buffer_list);
  1151. xfs_buf_relse(bp);
  1152. out_unlock:
  1153. xfs_dqunlock(dqp);
  1154. return error;
  1155. }
  1156. /*
  1157. * Walk thru all the filesystem inodes and construct a consistent view
  1158. * of the disk quota world. If the quotacheck fails, disable quotas.
  1159. */
  1160. STATIC int
  1161. xfs_qm_quotacheck(
  1162. xfs_mount_t *mp)
  1163. {
  1164. int error, error2;
  1165. uint flags;
  1166. LIST_HEAD (buffer_list);
  1167. struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
  1168. struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
  1169. struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
  1170. flags = 0;
  1171. ASSERT(uip || gip || pip);
  1172. ASSERT(XFS_IS_QUOTA_ON(mp));
  1173. xfs_notice(mp, "Quotacheck needed: Please wait.");
  1174. /*
  1175. * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
  1176. * their counters to zero. We need a clean slate.
  1177. * We don't log our changes till later.
  1178. */
  1179. if (uip) {
  1180. error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_DQTYPE_USER,
  1181. &buffer_list);
  1182. if (error)
  1183. goto error_return;
  1184. flags |= XFS_UQUOTA_CHKD;
  1185. }
  1186. if (gip) {
  1187. error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_DQTYPE_GROUP,
  1188. &buffer_list);
  1189. if (error)
  1190. goto error_return;
  1191. flags |= XFS_GQUOTA_CHKD;
  1192. }
  1193. if (pip) {
  1194. error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_DQTYPE_PROJ,
  1195. &buffer_list);
  1196. if (error)
  1197. goto error_return;
  1198. flags |= XFS_PQUOTA_CHKD;
  1199. }
  1200. xfs_set_quotacheck_running(mp);
  1201. error = xfs_iwalk_threaded(mp, 0, 0, xfs_qm_dqusage_adjust, 0, true,
  1202. NULL);
  1203. xfs_clear_quotacheck_running(mp);
  1204. /*
  1205. * On error, the inode walk may have partially populated the dquot
  1206. * caches. We must purge them before disabling quota and tearing down
  1207. * the quotainfo, or else the dquots will leak.
  1208. */
  1209. if (error)
  1210. goto error_purge;
  1211. /*
  1212. * We've made all the changes that we need to make incore. Flush them
  1213. * down to disk buffers if everything was updated successfully.
  1214. */
  1215. if (XFS_IS_UQUOTA_ON(mp)) {
  1216. error = xfs_qm_dquot_walk(mp, XFS_DQTYPE_USER, xfs_qm_flush_one,
  1217. &buffer_list);
  1218. }
  1219. if (XFS_IS_GQUOTA_ON(mp)) {
  1220. error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_GROUP, xfs_qm_flush_one,
  1221. &buffer_list);
  1222. if (!error)
  1223. error = error2;
  1224. }
  1225. if (XFS_IS_PQUOTA_ON(mp)) {
  1226. error2 = xfs_qm_dquot_walk(mp, XFS_DQTYPE_PROJ, xfs_qm_flush_one,
  1227. &buffer_list);
  1228. if (!error)
  1229. error = error2;
  1230. }
  1231. error2 = xfs_buf_delwri_submit(&buffer_list);
  1232. if (!error)
  1233. error = error2;
  1234. /*
  1235. * We can get this error if we couldn't do a dquot allocation inside
  1236. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1237. * dirty dquots that might be cached, we just want to get rid of them
  1238. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1239. * at this point (because we intentionally didn't in dqget_noattach).
  1240. */
  1241. if (error)
  1242. goto error_purge;
  1243. /*
  1244. * If one type of quotas is off, then it will lose its
  1245. * quotachecked status, since we won't be doing accounting for
  1246. * that type anymore.
  1247. */
  1248. mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
  1249. mp->m_qflags |= flags;
  1250. error_return:
  1251. xfs_buf_delwri_cancel(&buffer_list);
  1252. if (error) {
  1253. xfs_warn(mp,
  1254. "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
  1255. error);
  1256. /*
  1257. * We must turn off quotas.
  1258. */
  1259. ASSERT(mp->m_quotainfo != NULL);
  1260. xfs_qm_destroy_quotainfo(mp);
  1261. if (xfs_mount_reset_sbqflags(mp)) {
  1262. xfs_warn(mp,
  1263. "Quotacheck: Failed to reset quota flags.");
  1264. }
  1265. xfs_fs_mark_sick(mp, XFS_SICK_FS_QUOTACHECK);
  1266. } else {
  1267. xfs_notice(mp, "Quotacheck: Done.");
  1268. xfs_fs_mark_healthy(mp, XFS_SICK_FS_QUOTACHECK);
  1269. }
  1270. return error;
  1271. error_purge:
  1272. /*
  1273. * On error, we may have inodes queued for inactivation. This may try
  1274. * to attach dquots to the inode before running cleanup operations on
  1275. * the inode and this can race with the xfs_qm_destroy_quotainfo() call
  1276. * below that frees mp->m_quotainfo. To avoid this race, flush all the
  1277. * pending inodegc operations before we purge the dquots from memory,
  1278. * ensuring that background inactivation is idle whilst we turn off
  1279. * quotas.
  1280. */
  1281. xfs_inodegc_flush(mp);
  1282. xfs_qm_dqpurge_all(mp);
  1283. goto error_return;
  1284. }
  1285. /*
  1286. * This is called from xfs_mountfs to start quotas and initialize all
  1287. * necessary data structures like quotainfo. This is also responsible for
  1288. * running a quotacheck as necessary. We are guaranteed that the superblock
  1289. * is consistently read in at this point.
  1290. *
  1291. * If we fail here, the mount will continue with quota turned off. We don't
  1292. * need to inidicate success or failure at all.
  1293. */
  1294. void
  1295. xfs_qm_mount_quotas(
  1296. struct xfs_mount *mp)
  1297. {
  1298. int error = 0;
  1299. uint sbf;
  1300. /*
  1301. * If quotas on realtime volumes is not supported, we disable
  1302. * quotas immediately.
  1303. */
  1304. if (mp->m_sb.sb_rextents) {
  1305. xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
  1306. mp->m_qflags = 0;
  1307. goto write_changes;
  1308. }
  1309. ASSERT(XFS_IS_QUOTA_ON(mp));
  1310. /*
  1311. * Allocate the quotainfo structure inside the mount struct, and
  1312. * create quotainode(s), and change/rev superblock if necessary.
  1313. */
  1314. error = xfs_qm_init_quotainfo(mp);
  1315. if (error) {
  1316. /*
  1317. * We must turn off quotas.
  1318. */
  1319. ASSERT(mp->m_quotainfo == NULL);
  1320. mp->m_qflags = 0;
  1321. goto write_changes;
  1322. }
  1323. /*
  1324. * If any of the quotas are not consistent, do a quotacheck.
  1325. */
  1326. if (XFS_QM_NEED_QUOTACHECK(mp)) {
  1327. error = xfs_qm_quotacheck(mp);
  1328. if (error) {
  1329. /* Quotacheck failed and disabled quotas. */
  1330. return;
  1331. }
  1332. }
  1333. /*
  1334. * If one type of quotas is off, then it will lose its
  1335. * quotachecked status, since we won't be doing accounting for
  1336. * that type anymore.
  1337. */
  1338. if (!XFS_IS_UQUOTA_ON(mp))
  1339. mp->m_qflags &= ~XFS_UQUOTA_CHKD;
  1340. if (!XFS_IS_GQUOTA_ON(mp))
  1341. mp->m_qflags &= ~XFS_GQUOTA_CHKD;
  1342. if (!XFS_IS_PQUOTA_ON(mp))
  1343. mp->m_qflags &= ~XFS_PQUOTA_CHKD;
  1344. write_changes:
  1345. /*
  1346. * We actually don't have to acquire the m_sb_lock at all.
  1347. * This can only be called from mount, and that's single threaded. XXX
  1348. */
  1349. spin_lock(&mp->m_sb_lock);
  1350. sbf = mp->m_sb.sb_qflags;
  1351. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  1352. spin_unlock(&mp->m_sb_lock);
  1353. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  1354. if (xfs_sync_sb(mp, false)) {
  1355. /*
  1356. * We could only have been turning quotas off.
  1357. * We aren't in very good shape actually because
  1358. * the incore structures are convinced that quotas are
  1359. * off, but the on disk superblock doesn't know that !
  1360. */
  1361. ASSERT(!(XFS_IS_QUOTA_ON(mp)));
  1362. xfs_alert(mp, "%s: Superblock update failed!",
  1363. __func__);
  1364. }
  1365. }
  1366. if (error) {
  1367. xfs_warn(mp, "Failed to initialize disk quotas.");
  1368. return;
  1369. }
  1370. }
  1371. /*
  1372. * Load the inode for a given type of quota, assuming that the sb fields have
  1373. * been sorted out. This is not true when switching quota types on a V4
  1374. * filesystem, so do not use this function for that.
  1375. *
  1376. * Returns -ENOENT if the quota inode field is NULLFSINO; 0 and an inode on
  1377. * success; or a negative errno.
  1378. */
  1379. int
  1380. xfs_qm_qino_load(
  1381. struct xfs_mount *mp,
  1382. xfs_dqtype_t type,
  1383. struct xfs_inode **ipp)
  1384. {
  1385. xfs_ino_t ino = NULLFSINO;
  1386. switch (type) {
  1387. case XFS_DQTYPE_USER:
  1388. ino = mp->m_sb.sb_uquotino;
  1389. break;
  1390. case XFS_DQTYPE_GROUP:
  1391. ino = mp->m_sb.sb_gquotino;
  1392. break;
  1393. case XFS_DQTYPE_PROJ:
  1394. ino = mp->m_sb.sb_pquotino;
  1395. break;
  1396. default:
  1397. ASSERT(0);
  1398. return -EFSCORRUPTED;
  1399. }
  1400. if (ino == NULLFSINO)
  1401. return -ENOENT;
  1402. return xfs_iget(mp, NULL, ino, 0, 0, ipp);
  1403. }
  1404. /*
  1405. * This is called after the superblock has been read in and we're ready to
  1406. * iget the quota inodes.
  1407. */
  1408. STATIC int
  1409. xfs_qm_init_quotainos(
  1410. xfs_mount_t *mp)
  1411. {
  1412. struct xfs_inode *uip = NULL;
  1413. struct xfs_inode *gip = NULL;
  1414. struct xfs_inode *pip = NULL;
  1415. int error;
  1416. uint flags = 0;
  1417. ASSERT(mp->m_quotainfo);
  1418. /*
  1419. * Get the uquota and gquota inodes
  1420. */
  1421. if (xfs_has_quota(mp)) {
  1422. if (XFS_IS_UQUOTA_ON(mp) &&
  1423. mp->m_sb.sb_uquotino != NULLFSINO) {
  1424. ASSERT(mp->m_sb.sb_uquotino > 0);
  1425. error = xfs_qm_qino_load(mp, XFS_DQTYPE_USER, &uip);
  1426. if (error)
  1427. return error;
  1428. }
  1429. if (XFS_IS_GQUOTA_ON(mp) &&
  1430. mp->m_sb.sb_gquotino != NULLFSINO) {
  1431. ASSERT(mp->m_sb.sb_gquotino > 0);
  1432. error = xfs_qm_qino_load(mp, XFS_DQTYPE_GROUP, &gip);
  1433. if (error)
  1434. goto error_rele;
  1435. }
  1436. if (XFS_IS_PQUOTA_ON(mp) &&
  1437. mp->m_sb.sb_pquotino != NULLFSINO) {
  1438. ASSERT(mp->m_sb.sb_pquotino > 0);
  1439. error = xfs_qm_qino_load(mp, XFS_DQTYPE_PROJ, &pip);
  1440. if (error)
  1441. goto error_rele;
  1442. }
  1443. } else {
  1444. flags |= XFS_QMOPT_SBVERSION;
  1445. }
  1446. /*
  1447. * Create the three inodes, if they don't exist already. The changes
  1448. * made above will get added to a transaction and logged in one of
  1449. * the qino_alloc calls below. If the device is readonly,
  1450. * temporarily switch to read-write to do this.
  1451. */
  1452. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1453. error = xfs_qm_qino_alloc(mp, &uip,
  1454. flags | XFS_QMOPT_UQUOTA);
  1455. if (error)
  1456. goto error_rele;
  1457. flags &= ~XFS_QMOPT_SBVERSION;
  1458. }
  1459. if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
  1460. error = xfs_qm_qino_alloc(mp, &gip,
  1461. flags | XFS_QMOPT_GQUOTA);
  1462. if (error)
  1463. goto error_rele;
  1464. flags &= ~XFS_QMOPT_SBVERSION;
  1465. }
  1466. if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
  1467. error = xfs_qm_qino_alloc(mp, &pip,
  1468. flags | XFS_QMOPT_PQUOTA);
  1469. if (error)
  1470. goto error_rele;
  1471. }
  1472. mp->m_quotainfo->qi_uquotaip = uip;
  1473. mp->m_quotainfo->qi_gquotaip = gip;
  1474. mp->m_quotainfo->qi_pquotaip = pip;
  1475. return 0;
  1476. error_rele:
  1477. if (uip)
  1478. xfs_irele(uip);
  1479. if (gip)
  1480. xfs_irele(gip);
  1481. if (pip)
  1482. xfs_irele(pip);
  1483. return error;
  1484. }
  1485. STATIC void
  1486. xfs_qm_destroy_quotainos(
  1487. struct xfs_quotainfo *qi)
  1488. {
  1489. if (qi->qi_uquotaip) {
  1490. xfs_irele(qi->qi_uquotaip);
  1491. qi->qi_uquotaip = NULL; /* paranoia */
  1492. }
  1493. if (qi->qi_gquotaip) {
  1494. xfs_irele(qi->qi_gquotaip);
  1495. qi->qi_gquotaip = NULL;
  1496. }
  1497. if (qi->qi_pquotaip) {
  1498. xfs_irele(qi->qi_pquotaip);
  1499. qi->qi_pquotaip = NULL;
  1500. }
  1501. }
  1502. STATIC void
  1503. xfs_qm_dqfree_one(
  1504. struct xfs_dquot *dqp)
  1505. {
  1506. struct xfs_mount *mp = dqp->q_mount;
  1507. struct xfs_quotainfo *qi = mp->m_quotainfo;
  1508. mutex_lock(&qi->qi_tree_lock);
  1509. radix_tree_delete(xfs_dquot_tree(qi, xfs_dquot_type(dqp)), dqp->q_id);
  1510. qi->qi_dquots--;
  1511. mutex_unlock(&qi->qi_tree_lock);
  1512. xfs_qm_dqdestroy(dqp);
  1513. }
  1514. /* --------------- utility functions for vnodeops ---------------- */
  1515. /*
  1516. * Given an inode, a uid, gid and prid make sure that we have
  1517. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  1518. * quotas by creating this file.
  1519. * This also attaches dquot(s) to the given inode after locking it,
  1520. * and returns the dquots corresponding to the uid and/or gid.
  1521. *
  1522. * in : inode (unlocked)
  1523. * out : udquot, gdquot with references taken and unlocked
  1524. */
  1525. int
  1526. xfs_qm_vop_dqalloc(
  1527. struct xfs_inode *ip,
  1528. kuid_t uid,
  1529. kgid_t gid,
  1530. prid_t prid,
  1531. uint flags,
  1532. struct xfs_dquot **O_udqpp,
  1533. struct xfs_dquot **O_gdqpp,
  1534. struct xfs_dquot **O_pdqpp)
  1535. {
  1536. struct xfs_mount *mp = ip->i_mount;
  1537. struct inode *inode = VFS_I(ip);
  1538. struct user_namespace *user_ns = inode->i_sb->s_user_ns;
  1539. struct xfs_dquot *uq = NULL;
  1540. struct xfs_dquot *gq = NULL;
  1541. struct xfs_dquot *pq = NULL;
  1542. int error;
  1543. uint lockflags;
  1544. if (!XFS_IS_QUOTA_ON(mp))
  1545. return 0;
  1546. lockflags = XFS_ILOCK_EXCL;
  1547. xfs_ilock(ip, lockflags);
  1548. if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
  1549. gid = inode->i_gid;
  1550. /*
  1551. * Attach the dquot(s) to this inode, doing a dquot allocation
  1552. * if necessary. The dquot(s) will not be locked.
  1553. */
  1554. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1555. error = xfs_qm_dqattach_locked(ip, true);
  1556. if (error) {
  1557. xfs_iunlock(ip, lockflags);
  1558. return error;
  1559. }
  1560. }
  1561. if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
  1562. ASSERT(O_udqpp);
  1563. if (!uid_eq(inode->i_uid, uid)) {
  1564. /*
  1565. * What we need is the dquot that has this uid, and
  1566. * if we send the inode to dqget, the uid of the inode
  1567. * takes priority over what's sent in the uid argument.
  1568. * We must unlock inode here before calling dqget if
  1569. * we're not sending the inode, because otherwise
  1570. * we'll deadlock by doing trans_reserve while
  1571. * holding ilock.
  1572. */
  1573. xfs_iunlock(ip, lockflags);
  1574. error = xfs_qm_dqget(mp, from_kuid(user_ns, uid),
  1575. XFS_DQTYPE_USER, true, &uq);
  1576. if (error) {
  1577. ASSERT(error != -ENOENT);
  1578. return error;
  1579. }
  1580. /*
  1581. * Get the ilock in the right order.
  1582. */
  1583. xfs_dqunlock(uq);
  1584. lockflags = XFS_ILOCK_SHARED;
  1585. xfs_ilock(ip, lockflags);
  1586. } else {
  1587. /*
  1588. * Take an extra reference, because we'll return
  1589. * this to caller
  1590. */
  1591. ASSERT(ip->i_udquot);
  1592. uq = xfs_qm_dqhold(ip->i_udquot);
  1593. }
  1594. }
  1595. if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
  1596. ASSERT(O_gdqpp);
  1597. if (!gid_eq(inode->i_gid, gid)) {
  1598. xfs_iunlock(ip, lockflags);
  1599. error = xfs_qm_dqget(mp, from_kgid(user_ns, gid),
  1600. XFS_DQTYPE_GROUP, true, &gq);
  1601. if (error) {
  1602. ASSERT(error != -ENOENT);
  1603. goto error_rele;
  1604. }
  1605. xfs_dqunlock(gq);
  1606. lockflags = XFS_ILOCK_SHARED;
  1607. xfs_ilock(ip, lockflags);
  1608. } else {
  1609. ASSERT(ip->i_gdquot);
  1610. gq = xfs_qm_dqhold(ip->i_gdquot);
  1611. }
  1612. }
  1613. if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
  1614. ASSERT(O_pdqpp);
  1615. if (ip->i_projid != prid) {
  1616. xfs_iunlock(ip, lockflags);
  1617. error = xfs_qm_dqget(mp, prid,
  1618. XFS_DQTYPE_PROJ, true, &pq);
  1619. if (error) {
  1620. ASSERT(error != -ENOENT);
  1621. goto error_rele;
  1622. }
  1623. xfs_dqunlock(pq);
  1624. lockflags = XFS_ILOCK_SHARED;
  1625. xfs_ilock(ip, lockflags);
  1626. } else {
  1627. ASSERT(ip->i_pdquot);
  1628. pq = xfs_qm_dqhold(ip->i_pdquot);
  1629. }
  1630. }
  1631. trace_xfs_dquot_dqalloc(ip);
  1632. xfs_iunlock(ip, lockflags);
  1633. if (O_udqpp)
  1634. *O_udqpp = uq;
  1635. else
  1636. xfs_qm_dqrele(uq);
  1637. if (O_gdqpp)
  1638. *O_gdqpp = gq;
  1639. else
  1640. xfs_qm_dqrele(gq);
  1641. if (O_pdqpp)
  1642. *O_pdqpp = pq;
  1643. else
  1644. xfs_qm_dqrele(pq);
  1645. return 0;
  1646. error_rele:
  1647. xfs_qm_dqrele(gq);
  1648. xfs_qm_dqrele(uq);
  1649. return error;
  1650. }
  1651. /*
  1652. * Actually transfer ownership, and do dquot modifications.
  1653. * These were already reserved.
  1654. */
  1655. struct xfs_dquot *
  1656. xfs_qm_vop_chown(
  1657. struct xfs_trans *tp,
  1658. struct xfs_inode *ip,
  1659. struct xfs_dquot **IO_olddq,
  1660. struct xfs_dquot *newdq)
  1661. {
  1662. struct xfs_dquot *prevdq;
  1663. uint bfield = XFS_IS_REALTIME_INODE(ip) ?
  1664. XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
  1665. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  1666. ASSERT(XFS_IS_QUOTA_ON(ip->i_mount));
  1667. /* old dquot */
  1668. prevdq = *IO_olddq;
  1669. ASSERT(prevdq);
  1670. ASSERT(prevdq != newdq);
  1671. xfs_trans_mod_ino_dquot(tp, ip, prevdq, bfield, -(ip->i_nblocks));
  1672. xfs_trans_mod_ino_dquot(tp, ip, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
  1673. /* the sparkling new dquot */
  1674. xfs_trans_mod_ino_dquot(tp, ip, newdq, bfield, ip->i_nblocks);
  1675. xfs_trans_mod_ino_dquot(tp, ip, newdq, XFS_TRANS_DQ_ICOUNT, 1);
  1676. /*
  1677. * Back when we made quota reservations for the chown, we reserved the
  1678. * ondisk blocks + delalloc blocks with the new dquot. Now that we've
  1679. * switched the dquots, decrease the new dquot's block reservation
  1680. * (having already bumped up the real counter) so that we don't have
  1681. * any reservation to give back when we commit.
  1682. */
  1683. xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_RES_BLKS,
  1684. -ip->i_delayed_blks);
  1685. /*
  1686. * Give the incore reservation for delalloc blocks back to the old
  1687. * dquot. We don't normally handle delalloc quota reservations
  1688. * transactionally, so just lock the dquot and subtract from the
  1689. * reservation. Dirty the transaction because it's too late to turn
  1690. * back now.
  1691. */
  1692. tp->t_flags |= XFS_TRANS_DIRTY;
  1693. xfs_dqlock(prevdq);
  1694. ASSERT(prevdq->q_blk.reserved >= ip->i_delayed_blks);
  1695. prevdq->q_blk.reserved -= ip->i_delayed_blks;
  1696. xfs_dqunlock(prevdq);
  1697. /*
  1698. * Take an extra reference, because the inode is going to keep
  1699. * this dquot pointer even after the trans_commit.
  1700. */
  1701. *IO_olddq = xfs_qm_dqhold(newdq);
  1702. return prevdq;
  1703. }
  1704. int
  1705. xfs_qm_vop_rename_dqattach(
  1706. struct xfs_inode **i_tab)
  1707. {
  1708. struct xfs_mount *mp = i_tab[0]->i_mount;
  1709. int i;
  1710. if (!XFS_IS_QUOTA_ON(mp))
  1711. return 0;
  1712. for (i = 0; (i < 4 && i_tab[i]); i++) {
  1713. struct xfs_inode *ip = i_tab[i];
  1714. int error;
  1715. /*
  1716. * Watch out for duplicate entries in the table.
  1717. */
  1718. if (i == 0 || ip != i_tab[i-1]) {
  1719. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1720. error = xfs_qm_dqattach(ip);
  1721. if (error)
  1722. return error;
  1723. }
  1724. }
  1725. }
  1726. return 0;
  1727. }
  1728. void
  1729. xfs_qm_vop_create_dqattach(
  1730. struct xfs_trans *tp,
  1731. struct xfs_inode *ip,
  1732. struct xfs_dquot *udqp,
  1733. struct xfs_dquot *gdqp,
  1734. struct xfs_dquot *pdqp)
  1735. {
  1736. struct xfs_mount *mp = tp->t_mountp;
  1737. if (!XFS_IS_QUOTA_ON(mp))
  1738. return;
  1739. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  1740. if (udqp && XFS_IS_UQUOTA_ON(mp)) {
  1741. ASSERT(ip->i_udquot == NULL);
  1742. ASSERT(i_uid_read(VFS_I(ip)) == udqp->q_id);
  1743. ip->i_udquot = xfs_qm_dqhold(udqp);
  1744. }
  1745. if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
  1746. ASSERT(ip->i_gdquot == NULL);
  1747. ASSERT(i_gid_read(VFS_I(ip)) == gdqp->q_id);
  1748. ip->i_gdquot = xfs_qm_dqhold(gdqp);
  1749. }
  1750. if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
  1751. ASSERT(ip->i_pdquot == NULL);
  1752. ASSERT(ip->i_projid == pdqp->q_id);
  1753. ip->i_pdquot = xfs_qm_dqhold(pdqp);
  1754. }
  1755. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, 1);
  1756. }
  1757. /* Decide if this inode's dquot is near an enforcement boundary. */
  1758. bool
  1759. xfs_inode_near_dquot_enforcement(
  1760. struct xfs_inode *ip,
  1761. xfs_dqtype_t type)
  1762. {
  1763. struct xfs_dquot *dqp;
  1764. int64_t freesp;
  1765. /* We only care for quotas that are enabled and enforced. */
  1766. dqp = xfs_inode_dquot(ip, type);
  1767. if (!dqp || !xfs_dquot_is_enforced(dqp))
  1768. return false;
  1769. if (xfs_dquot_res_over_limits(&dqp->q_ino) ||
  1770. xfs_dquot_res_over_limits(&dqp->q_rtb))
  1771. return true;
  1772. /* For space on the data device, check the various thresholds. */
  1773. if (!dqp->q_prealloc_hi_wmark)
  1774. return false;
  1775. if (dqp->q_blk.reserved < dqp->q_prealloc_lo_wmark)
  1776. return false;
  1777. if (dqp->q_blk.reserved >= dqp->q_prealloc_hi_wmark)
  1778. return true;
  1779. freesp = dqp->q_prealloc_hi_wmark - dqp->q_blk.reserved;
  1780. if (freesp < dqp->q_low_space[XFS_QLOWSP_5_PCNT])
  1781. return true;
  1782. return false;
  1783. }