xfs_qm.c 47 KB

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