xfs_qm.c 47 KB

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