xfs_qm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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(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(xs_qm_dqwants);
  396. trace_xfs_dqreclaim_want(dqp);
  397. list_lru_isolate(lru, &dqp->q_lru);
  398. XFS_STATS_DEC(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(xs_qm_dquot_unused);
  435. trace_xfs_dqreclaim_done(dqp);
  436. XFS_STATS_INC(xs_qm_dqreclaims);
  437. return LRU_REMOVED;
  438. out_miss_busy:
  439. trace_xfs_dqreclaim_busy(dqp);
  440. XFS_STATS_INC(xs_qm_dqreclaim_misses);
  441. return LRU_SKIP;
  442. out_unlock_dirty:
  443. trace_xfs_dqreclaim_busy(dqp);
  444. XFS_STATS_INC(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_WAIT)) != (__GFP_FS|__GFP_WAIT))
  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. /*
  486. * This initializes all the quota information that's kept in the
  487. * mount structure
  488. */
  489. STATIC int
  490. xfs_qm_init_quotainfo(
  491. xfs_mount_t *mp)
  492. {
  493. xfs_quotainfo_t *qinf;
  494. int error;
  495. xfs_dquot_t *dqp;
  496. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  497. qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
  498. error = list_lru_init(&qinf->qi_lru);
  499. if (error)
  500. goto out_free_qinf;
  501. /*
  502. * See if quotainodes are setup, and if not, allocate them,
  503. * and change the superblock accordingly.
  504. */
  505. error = xfs_qm_init_quotainos(mp);
  506. if (error)
  507. goto out_free_lru;
  508. INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
  509. INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
  510. INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_NOFS);
  511. mutex_init(&qinf->qi_tree_lock);
  512. /* mutex used to serialize quotaoffs */
  513. mutex_init(&qinf->qi_quotaofflock);
  514. /* Precalc some constants */
  515. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  516. qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
  517. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  518. /*
  519. * We try to get the limits from the superuser's limits fields.
  520. * This is quite hacky, but it is standard quota practice.
  521. *
  522. * We look at the USR dquot with id == 0 first, but if user quotas
  523. * are not enabled we goto the GRP dquot with id == 0.
  524. * We don't really care to keep separate default limits for user
  525. * and group quotas, at least not at this point.
  526. *
  527. * Since we may not have done a quotacheck by this point, just read
  528. * the dquot without attaching it to any hashtables or lists.
  529. */
  530. error = xfs_qm_dqread(mp, 0,
  531. XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
  532. (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
  533. XFS_DQ_PROJ),
  534. XFS_QMOPT_DOWARN, &dqp);
  535. if (!error) {
  536. xfs_disk_dquot_t *ddqp = &dqp->q_core;
  537. /*
  538. * The warnings and timers set the grace period given to
  539. * a user or group before he or she can not perform any
  540. * more writing. If it is zero, a default is used.
  541. */
  542. qinf->qi_btimelimit = ddqp->d_btimer ?
  543. be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
  544. qinf->qi_itimelimit = ddqp->d_itimer ?
  545. be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
  546. qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
  547. be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
  548. qinf->qi_bwarnlimit = ddqp->d_bwarns ?
  549. be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
  550. qinf->qi_iwarnlimit = ddqp->d_iwarns ?
  551. be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
  552. qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
  553. be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
  554. qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
  555. qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
  556. qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
  557. qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
  558. qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
  559. qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
  560. xfs_qm_dqdestroy(dqp);
  561. } else {
  562. qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
  563. qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
  564. qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
  565. qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
  566. qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
  567. qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
  568. }
  569. qinf->qi_shrinker.count_objects = xfs_qm_shrink_count;
  570. qinf->qi_shrinker.scan_objects = xfs_qm_shrink_scan;
  571. qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
  572. qinf->qi_shrinker.flags = SHRINKER_NUMA_AWARE;
  573. register_shrinker(&qinf->qi_shrinker);
  574. return 0;
  575. out_free_lru:
  576. list_lru_destroy(&qinf->qi_lru);
  577. out_free_qinf:
  578. kmem_free(qinf);
  579. mp->m_quotainfo = NULL;
  580. return error;
  581. }
  582. /*
  583. * Gets called when unmounting a filesystem or when all quotas get
  584. * turned off.
  585. * This purges the quota inodes, destroys locks and frees itself.
  586. */
  587. void
  588. xfs_qm_destroy_quotainfo(
  589. xfs_mount_t *mp)
  590. {
  591. xfs_quotainfo_t *qi;
  592. qi = mp->m_quotainfo;
  593. ASSERT(qi != NULL);
  594. unregister_shrinker(&qi->qi_shrinker);
  595. list_lru_destroy(&qi->qi_lru);
  596. if (qi->qi_uquotaip) {
  597. IRELE(qi->qi_uquotaip);
  598. qi->qi_uquotaip = NULL; /* paranoia */
  599. }
  600. if (qi->qi_gquotaip) {
  601. IRELE(qi->qi_gquotaip);
  602. qi->qi_gquotaip = NULL;
  603. }
  604. if (qi->qi_pquotaip) {
  605. IRELE(qi->qi_pquotaip);
  606. qi->qi_pquotaip = NULL;
  607. }
  608. mutex_destroy(&qi->qi_quotaofflock);
  609. kmem_free(qi);
  610. mp->m_quotainfo = NULL;
  611. }
  612. /*
  613. * Create an inode and return with a reference already taken, but unlocked
  614. * This is how we create quota inodes
  615. */
  616. STATIC int
  617. xfs_qm_qino_alloc(
  618. xfs_mount_t *mp,
  619. xfs_inode_t **ip,
  620. uint flags)
  621. {
  622. xfs_trans_t *tp;
  623. int error;
  624. int committed;
  625. bool need_alloc = true;
  626. *ip = NULL;
  627. /*
  628. * With superblock that doesn't have separate pquotino, we
  629. * share an inode between gquota and pquota. If the on-disk
  630. * superblock has GQUOTA and the filesystem is now mounted
  631. * with PQUOTA, just use sb_gquotino for sb_pquotino and
  632. * vice-versa.
  633. */
  634. if (!xfs_sb_version_has_pquotino(&mp->m_sb) &&
  635. (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
  636. xfs_ino_t ino = NULLFSINO;
  637. if ((flags & XFS_QMOPT_PQUOTA) &&
  638. (mp->m_sb.sb_gquotino != NULLFSINO)) {
  639. ino = mp->m_sb.sb_gquotino;
  640. ASSERT(mp->m_sb.sb_pquotino == NULLFSINO);
  641. } else if ((flags & XFS_QMOPT_GQUOTA) &&
  642. (mp->m_sb.sb_pquotino != NULLFSINO)) {
  643. ino = mp->m_sb.sb_pquotino;
  644. ASSERT(mp->m_sb.sb_gquotino == NULLFSINO);
  645. }
  646. if (ino != NULLFSINO) {
  647. error = xfs_iget(mp, NULL, ino, 0, 0, ip);
  648. if (error)
  649. return error;
  650. mp->m_sb.sb_gquotino = NULLFSINO;
  651. mp->m_sb.sb_pquotino = NULLFSINO;
  652. need_alloc = false;
  653. }
  654. }
  655. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
  656. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_create,
  657. XFS_QM_QINOCREATE_SPACE_RES(mp), 0);
  658. if (error) {
  659. xfs_trans_cancel(tp);
  660. return error;
  661. }
  662. if (need_alloc) {
  663. error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, 1, ip,
  664. &committed);
  665. if (error) {
  666. xfs_trans_cancel(tp);
  667. return error;
  668. }
  669. }
  670. /*
  671. * Make the changes in the superblock, and log those too.
  672. * sbfields arg may contain fields other than *QUOTINO;
  673. * VERSIONNUM for example.
  674. */
  675. spin_lock(&mp->m_sb_lock);
  676. if (flags & XFS_QMOPT_SBVERSION) {
  677. ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
  678. xfs_sb_version_addquota(&mp->m_sb);
  679. mp->m_sb.sb_uquotino = NULLFSINO;
  680. mp->m_sb.sb_gquotino = NULLFSINO;
  681. mp->m_sb.sb_pquotino = NULLFSINO;
  682. /* qflags will get updated fully _after_ quotacheck */
  683. mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
  684. }
  685. if (flags & XFS_QMOPT_UQUOTA)
  686. mp->m_sb.sb_uquotino = (*ip)->i_ino;
  687. else if (flags & XFS_QMOPT_GQUOTA)
  688. mp->m_sb.sb_gquotino = (*ip)->i_ino;
  689. else
  690. mp->m_sb.sb_pquotino = (*ip)->i_ino;
  691. spin_unlock(&mp->m_sb_lock);
  692. xfs_log_sb(tp);
  693. error = xfs_trans_commit(tp);
  694. if (error) {
  695. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  696. xfs_alert(mp, "%s failed (error %d)!", __func__, error);
  697. }
  698. if (need_alloc)
  699. xfs_finish_inode_setup(*ip);
  700. return error;
  701. }
  702. STATIC void
  703. xfs_qm_reset_dqcounts(
  704. xfs_mount_t *mp,
  705. xfs_buf_t *bp,
  706. xfs_dqid_t id,
  707. uint type)
  708. {
  709. struct xfs_dqblk *dqb;
  710. int j;
  711. trace_xfs_reset_dqcounts(bp, _RET_IP_);
  712. /*
  713. * Reset all counters and timers. They'll be
  714. * started afresh by xfs_qm_quotacheck.
  715. */
  716. #ifdef DEBUG
  717. j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  718. do_div(j, sizeof(xfs_dqblk_t));
  719. ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
  720. #endif
  721. dqb = bp->b_addr;
  722. for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
  723. struct xfs_disk_dquot *ddq;
  724. ddq = (struct xfs_disk_dquot *)&dqb[j];
  725. /*
  726. * Do a sanity check, and if needed, repair the dqblk. Don't
  727. * output any warnings because it's perfectly possible to
  728. * find uninitialised dquot blks. See comment in xfs_dqcheck.
  729. */
  730. xfs_dqcheck(mp, ddq, id+j, type, XFS_QMOPT_DQREPAIR,
  731. "xfs_quotacheck");
  732. /*
  733. * Reset type in case we are reusing group quota file for
  734. * project quotas or vice versa
  735. */
  736. ddq->d_flags = type;
  737. ddq->d_bcount = 0;
  738. ddq->d_icount = 0;
  739. ddq->d_rtbcount = 0;
  740. ddq->d_btimer = 0;
  741. ddq->d_itimer = 0;
  742. ddq->d_rtbtimer = 0;
  743. ddq->d_bwarns = 0;
  744. ddq->d_iwarns = 0;
  745. ddq->d_rtbwarns = 0;
  746. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  747. xfs_update_cksum((char *)&dqb[j],
  748. sizeof(struct xfs_dqblk),
  749. XFS_DQUOT_CRC_OFF);
  750. }
  751. }
  752. }
  753. STATIC int
  754. xfs_qm_dqiter_bufs(
  755. struct xfs_mount *mp,
  756. xfs_dqid_t firstid,
  757. xfs_fsblock_t bno,
  758. xfs_filblks_t blkcnt,
  759. uint flags,
  760. struct list_head *buffer_list)
  761. {
  762. struct xfs_buf *bp;
  763. int error;
  764. int type;
  765. ASSERT(blkcnt > 0);
  766. type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
  767. (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
  768. error = 0;
  769. /*
  770. * Blkcnt arg can be a very big number, and might even be
  771. * larger than the log itself. So, we have to break it up into
  772. * manageable-sized transactions.
  773. * Note that we don't start a permanent transaction here; we might
  774. * not be able to get a log reservation for the whole thing up front,
  775. * and we don't really care to either, because we just discard
  776. * everything if we were to crash in the middle of this loop.
  777. */
  778. while (blkcnt--) {
  779. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  780. XFS_FSB_TO_DADDR(mp, bno),
  781. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  782. &xfs_dquot_buf_ops);
  783. /*
  784. * CRC and validation errors will return a EFSCORRUPTED here. If
  785. * this occurs, re-read without CRC validation so that we can
  786. * repair the damage via xfs_qm_reset_dqcounts(). This process
  787. * will leave a trace in the log indicating corruption has
  788. * been detected.
  789. */
  790. if (error == -EFSCORRUPTED) {
  791. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  792. XFS_FSB_TO_DADDR(mp, bno),
  793. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  794. NULL);
  795. }
  796. if (error)
  797. break;
  798. /*
  799. * A corrupt buffer might not have a verifier attached, so
  800. * make sure we have the correct one attached before writeback
  801. * occurs.
  802. */
  803. bp->b_ops = &xfs_dquot_buf_ops;
  804. xfs_qm_reset_dqcounts(mp, bp, firstid, type);
  805. xfs_buf_delwri_queue(bp, buffer_list);
  806. xfs_buf_relse(bp);
  807. /* goto the next block. */
  808. bno++;
  809. firstid += mp->m_quotainfo->qi_dqperchunk;
  810. }
  811. return error;
  812. }
  813. /*
  814. * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
  815. * caller supplied function for every chunk of dquots that we find.
  816. */
  817. STATIC int
  818. xfs_qm_dqiterate(
  819. struct xfs_mount *mp,
  820. struct xfs_inode *qip,
  821. uint flags,
  822. struct list_head *buffer_list)
  823. {
  824. struct xfs_bmbt_irec *map;
  825. int i, nmaps; /* number of map entries */
  826. int error; /* return value */
  827. xfs_fileoff_t lblkno;
  828. xfs_filblks_t maxlblkcnt;
  829. xfs_dqid_t firstid;
  830. xfs_fsblock_t rablkno;
  831. xfs_filblks_t rablkcnt;
  832. error = 0;
  833. /*
  834. * This looks racy, but we can't keep an inode lock across a
  835. * trans_reserve. But, this gets called during quotacheck, and that
  836. * happens only at mount time which is single threaded.
  837. */
  838. if (qip->i_d.di_nblocks == 0)
  839. return 0;
  840. map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
  841. lblkno = 0;
  842. maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  843. do {
  844. uint lock_mode;
  845. nmaps = XFS_DQITER_MAP_SIZE;
  846. /*
  847. * We aren't changing the inode itself. Just changing
  848. * some of its data. No new blocks are added here, and
  849. * the inode is never added to the transaction.
  850. */
  851. lock_mode = xfs_ilock_data_map_shared(qip);
  852. error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
  853. map, &nmaps, 0);
  854. xfs_iunlock(qip, lock_mode);
  855. if (error)
  856. break;
  857. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  858. for (i = 0; i < nmaps; i++) {
  859. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  860. ASSERT(map[i].br_blockcount);
  861. lblkno += map[i].br_blockcount;
  862. if (map[i].br_startblock == HOLESTARTBLOCK)
  863. continue;
  864. firstid = (xfs_dqid_t) map[i].br_startoff *
  865. mp->m_quotainfo->qi_dqperchunk;
  866. /*
  867. * Do a read-ahead on the next extent.
  868. */
  869. if ((i+1 < nmaps) &&
  870. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  871. rablkcnt = map[i+1].br_blockcount;
  872. rablkno = map[i+1].br_startblock;
  873. while (rablkcnt--) {
  874. xfs_buf_readahead(mp->m_ddev_targp,
  875. XFS_FSB_TO_DADDR(mp, rablkno),
  876. mp->m_quotainfo->qi_dqchunklen,
  877. &xfs_dquot_buf_ops);
  878. rablkno++;
  879. }
  880. }
  881. /*
  882. * Iterate thru all the blks in the extent and
  883. * reset the counters of all the dquots inside them.
  884. */
  885. error = xfs_qm_dqiter_bufs(mp, firstid,
  886. map[i].br_startblock,
  887. map[i].br_blockcount,
  888. flags, buffer_list);
  889. if (error)
  890. goto out;
  891. }
  892. } while (nmaps > 0);
  893. out:
  894. kmem_free(map);
  895. return error;
  896. }
  897. /*
  898. * Called by dqusage_adjust in doing a quotacheck.
  899. *
  900. * Given the inode, and a dquot id this updates both the incore dqout as well
  901. * as the buffer copy. This is so that once the quotacheck is done, we can
  902. * just log all the buffers, as opposed to logging numerous updates to
  903. * individual dquots.
  904. */
  905. STATIC int
  906. xfs_qm_quotacheck_dqadjust(
  907. struct xfs_inode *ip,
  908. xfs_dqid_t id,
  909. uint type,
  910. xfs_qcnt_t nblks,
  911. xfs_qcnt_t rtblks)
  912. {
  913. struct xfs_mount *mp = ip->i_mount;
  914. struct xfs_dquot *dqp;
  915. int error;
  916. error = xfs_qm_dqget(mp, ip, id, type,
  917. XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, &dqp);
  918. if (error) {
  919. /*
  920. * Shouldn't be able to turn off quotas here.
  921. */
  922. ASSERT(error != -ESRCH);
  923. ASSERT(error != -ENOENT);
  924. return error;
  925. }
  926. trace_xfs_dqadjust(dqp);
  927. /*
  928. * Adjust the inode count and the block count to reflect this inode's
  929. * resource usage.
  930. */
  931. be64_add_cpu(&dqp->q_core.d_icount, 1);
  932. dqp->q_res_icount++;
  933. if (nblks) {
  934. be64_add_cpu(&dqp->q_core.d_bcount, nblks);
  935. dqp->q_res_bcount += nblks;
  936. }
  937. if (rtblks) {
  938. be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
  939. dqp->q_res_rtbcount += rtblks;
  940. }
  941. /*
  942. * Set default limits, adjust timers (since we changed usages)
  943. *
  944. * There are no timers for the default values set in the root dquot.
  945. */
  946. if (dqp->q_core.d_id) {
  947. xfs_qm_adjust_dqlimits(mp, dqp);
  948. xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
  949. }
  950. dqp->dq_flags |= XFS_DQ_DIRTY;
  951. xfs_qm_dqput(dqp);
  952. return 0;
  953. }
  954. STATIC int
  955. xfs_qm_get_rtblks(
  956. xfs_inode_t *ip,
  957. xfs_qcnt_t *O_rtblks)
  958. {
  959. xfs_filblks_t rtblks; /* total rt blks */
  960. xfs_extnum_t idx; /* extent record index */
  961. xfs_ifork_t *ifp; /* inode fork pointer */
  962. xfs_extnum_t nextents; /* number of extent entries */
  963. int error;
  964. ASSERT(XFS_IS_REALTIME_INODE(ip));
  965. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  966. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  967. if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
  968. return error;
  969. }
  970. rtblks = 0;
  971. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  972. for (idx = 0; idx < nextents; idx++)
  973. rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
  974. *O_rtblks = (xfs_qcnt_t)rtblks;
  975. return 0;
  976. }
  977. /*
  978. * callback routine supplied to bulkstat(). Given an inumber, find its
  979. * dquots and update them to account for resources taken by that inode.
  980. */
  981. /* ARGSUSED */
  982. STATIC int
  983. xfs_qm_dqusage_adjust(
  984. xfs_mount_t *mp, /* mount point for filesystem */
  985. xfs_ino_t ino, /* inode number to get data for */
  986. void __user *buffer, /* not used */
  987. int ubsize, /* not used */
  988. int *ubused, /* not used */
  989. int *res) /* result code value */
  990. {
  991. xfs_inode_t *ip;
  992. xfs_qcnt_t nblks, rtblks = 0;
  993. int error;
  994. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  995. /*
  996. * rootino must have its resources accounted for, not so with the quota
  997. * inodes.
  998. */
  999. if (xfs_is_quota_inode(&mp->m_sb, ino)) {
  1000. *res = BULKSTAT_RV_NOTHING;
  1001. return -EINVAL;
  1002. }
  1003. /*
  1004. * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
  1005. * interface expects the inode to be exclusively locked because that's
  1006. * the case in all other instances. It's OK that we do this because
  1007. * quotacheck is done only at mount time.
  1008. */
  1009. error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip);
  1010. if (error) {
  1011. *res = BULKSTAT_RV_NOTHING;
  1012. return error;
  1013. }
  1014. ASSERT(ip->i_delayed_blks == 0);
  1015. if (XFS_IS_REALTIME_INODE(ip)) {
  1016. /*
  1017. * Walk thru the extent list and count the realtime blocks.
  1018. */
  1019. error = xfs_qm_get_rtblks(ip, &rtblks);
  1020. if (error)
  1021. goto error0;
  1022. }
  1023. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
  1024. /*
  1025. * Add the (disk blocks and inode) resources occupied by this
  1026. * inode to its dquots. We do this adjustment in the incore dquot,
  1027. * and also copy the changes to its buffer.
  1028. * We don't care about putting these changes in a transaction
  1029. * envelope because if we crash in the middle of a 'quotacheck'
  1030. * we have to start from the beginning anyway.
  1031. * Once we're done, we'll log all the dquot bufs.
  1032. *
  1033. * The *QUOTA_ON checks below may look pretty racy, but quotachecks
  1034. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1035. */
  1036. if (XFS_IS_UQUOTA_ON(mp)) {
  1037. error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_uid,
  1038. XFS_DQ_USER, nblks, rtblks);
  1039. if (error)
  1040. goto error0;
  1041. }
  1042. if (XFS_IS_GQUOTA_ON(mp)) {
  1043. error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_gid,
  1044. XFS_DQ_GROUP, nblks, rtblks);
  1045. if (error)
  1046. goto error0;
  1047. }
  1048. if (XFS_IS_PQUOTA_ON(mp)) {
  1049. error = xfs_qm_quotacheck_dqadjust(ip, xfs_get_projid(ip),
  1050. XFS_DQ_PROJ, nblks, rtblks);
  1051. if (error)
  1052. goto error0;
  1053. }
  1054. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1055. IRELE(ip);
  1056. *res = BULKSTAT_RV_DIDONE;
  1057. return 0;
  1058. error0:
  1059. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1060. IRELE(ip);
  1061. *res = BULKSTAT_RV_GIVEUP;
  1062. return error;
  1063. }
  1064. STATIC int
  1065. xfs_qm_flush_one(
  1066. struct xfs_dquot *dqp,
  1067. void *data)
  1068. {
  1069. struct list_head *buffer_list = data;
  1070. struct xfs_buf *bp = NULL;
  1071. int error = 0;
  1072. xfs_dqlock(dqp);
  1073. if (dqp->dq_flags & XFS_DQ_FREEING)
  1074. goto out_unlock;
  1075. if (!XFS_DQ_IS_DIRTY(dqp))
  1076. goto out_unlock;
  1077. xfs_dqflock(dqp);
  1078. error = xfs_qm_dqflush(dqp, &bp);
  1079. if (error)
  1080. goto out_unlock;
  1081. xfs_buf_delwri_queue(bp, buffer_list);
  1082. xfs_buf_relse(bp);
  1083. out_unlock:
  1084. xfs_dqunlock(dqp);
  1085. return error;
  1086. }
  1087. /*
  1088. * Walk thru all the filesystem inodes and construct a consistent view
  1089. * of the disk quota world. If the quotacheck fails, disable quotas.
  1090. */
  1091. STATIC int
  1092. xfs_qm_quotacheck(
  1093. xfs_mount_t *mp)
  1094. {
  1095. int done, count, error, error2;
  1096. xfs_ino_t lastino;
  1097. size_t structsz;
  1098. uint flags;
  1099. LIST_HEAD (buffer_list);
  1100. struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
  1101. struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
  1102. struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
  1103. count = INT_MAX;
  1104. structsz = 1;
  1105. lastino = 0;
  1106. flags = 0;
  1107. ASSERT(uip || gip || pip);
  1108. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1109. xfs_notice(mp, "Quotacheck needed: Please wait.");
  1110. /*
  1111. * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
  1112. * their counters to zero. We need a clean slate.
  1113. * We don't log our changes till later.
  1114. */
  1115. if (uip) {
  1116. error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA,
  1117. &buffer_list);
  1118. if (error)
  1119. goto error_return;
  1120. flags |= XFS_UQUOTA_CHKD;
  1121. }
  1122. if (gip) {
  1123. error = xfs_qm_dqiterate(mp, gip, XFS_QMOPT_GQUOTA,
  1124. &buffer_list);
  1125. if (error)
  1126. goto error_return;
  1127. flags |= XFS_GQUOTA_CHKD;
  1128. }
  1129. if (pip) {
  1130. error = xfs_qm_dqiterate(mp, pip, XFS_QMOPT_PQUOTA,
  1131. &buffer_list);
  1132. if (error)
  1133. goto error_return;
  1134. flags |= XFS_PQUOTA_CHKD;
  1135. }
  1136. do {
  1137. /*
  1138. * Iterate thru all the inodes in the file system,
  1139. * adjusting the corresponding dquot counters in core.
  1140. */
  1141. error = xfs_bulkstat(mp, &lastino, &count,
  1142. xfs_qm_dqusage_adjust,
  1143. structsz, NULL, &done);
  1144. if (error)
  1145. break;
  1146. } while (!done);
  1147. /*
  1148. * We've made all the changes that we need to make incore. Flush them
  1149. * down to disk buffers if everything was updated successfully.
  1150. */
  1151. if (XFS_IS_UQUOTA_ON(mp)) {
  1152. error = xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_flush_one,
  1153. &buffer_list);
  1154. }
  1155. if (XFS_IS_GQUOTA_ON(mp)) {
  1156. error2 = xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_flush_one,
  1157. &buffer_list);
  1158. if (!error)
  1159. error = error2;
  1160. }
  1161. if (XFS_IS_PQUOTA_ON(mp)) {
  1162. error2 = xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_flush_one,
  1163. &buffer_list);
  1164. if (!error)
  1165. error = error2;
  1166. }
  1167. error2 = xfs_buf_delwri_submit(&buffer_list);
  1168. if (!error)
  1169. error = error2;
  1170. /*
  1171. * We can get this error if we couldn't do a dquot allocation inside
  1172. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1173. * dirty dquots that might be cached, we just want to get rid of them
  1174. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1175. * at this point (because we intentionally didn't in dqget_noattach).
  1176. */
  1177. if (error) {
  1178. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
  1179. goto error_return;
  1180. }
  1181. /*
  1182. * If one type of quotas is off, then it will lose its
  1183. * quotachecked status, since we won't be doing accounting for
  1184. * that type anymore.
  1185. */
  1186. mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
  1187. mp->m_qflags |= flags;
  1188. error_return:
  1189. while (!list_empty(&buffer_list)) {
  1190. struct xfs_buf *bp =
  1191. list_first_entry(&buffer_list, struct xfs_buf, b_list);
  1192. list_del_init(&bp->b_list);
  1193. xfs_buf_relse(bp);
  1194. }
  1195. if (error) {
  1196. xfs_warn(mp,
  1197. "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
  1198. error);
  1199. /*
  1200. * We must turn off quotas.
  1201. */
  1202. ASSERT(mp->m_quotainfo != NULL);
  1203. xfs_qm_destroy_quotainfo(mp);
  1204. if (xfs_mount_reset_sbqflags(mp)) {
  1205. xfs_warn(mp,
  1206. "Quotacheck: Failed to reset quota flags.");
  1207. }
  1208. } else
  1209. xfs_notice(mp, "Quotacheck: Done.");
  1210. return error;
  1211. }
  1212. /*
  1213. * This is called from xfs_mountfs to start quotas and initialize all
  1214. * necessary data structures like quotainfo. This is also responsible for
  1215. * running a quotacheck as necessary. We are guaranteed that the superblock
  1216. * is consistently read in at this point.
  1217. *
  1218. * If we fail here, the mount will continue with quota turned off. We don't
  1219. * need to inidicate success or failure at all.
  1220. */
  1221. void
  1222. xfs_qm_mount_quotas(
  1223. struct xfs_mount *mp)
  1224. {
  1225. int error = 0;
  1226. uint sbf;
  1227. /*
  1228. * If quotas on realtime volumes is not supported, we disable
  1229. * quotas immediately.
  1230. */
  1231. if (mp->m_sb.sb_rextents) {
  1232. xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
  1233. mp->m_qflags = 0;
  1234. goto write_changes;
  1235. }
  1236. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1237. /*
  1238. * Allocate the quotainfo structure inside the mount struct, and
  1239. * create quotainode(s), and change/rev superblock if necessary.
  1240. */
  1241. error = xfs_qm_init_quotainfo(mp);
  1242. if (error) {
  1243. /*
  1244. * We must turn off quotas.
  1245. */
  1246. ASSERT(mp->m_quotainfo == NULL);
  1247. mp->m_qflags = 0;
  1248. goto write_changes;
  1249. }
  1250. /*
  1251. * If any of the quotas are not consistent, do a quotacheck.
  1252. */
  1253. if (XFS_QM_NEED_QUOTACHECK(mp)) {
  1254. error = xfs_qm_quotacheck(mp);
  1255. if (error) {
  1256. /* Quotacheck failed and disabled quotas. */
  1257. return;
  1258. }
  1259. }
  1260. /*
  1261. * If one type of quotas is off, then it will lose its
  1262. * quotachecked status, since we won't be doing accounting for
  1263. * that type anymore.
  1264. */
  1265. if (!XFS_IS_UQUOTA_ON(mp))
  1266. mp->m_qflags &= ~XFS_UQUOTA_CHKD;
  1267. if (!XFS_IS_GQUOTA_ON(mp))
  1268. mp->m_qflags &= ~XFS_GQUOTA_CHKD;
  1269. if (!XFS_IS_PQUOTA_ON(mp))
  1270. mp->m_qflags &= ~XFS_PQUOTA_CHKD;
  1271. write_changes:
  1272. /*
  1273. * We actually don't have to acquire the m_sb_lock at all.
  1274. * This can only be called from mount, and that's single threaded. XXX
  1275. */
  1276. spin_lock(&mp->m_sb_lock);
  1277. sbf = mp->m_sb.sb_qflags;
  1278. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  1279. spin_unlock(&mp->m_sb_lock);
  1280. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  1281. if (xfs_sync_sb(mp, false)) {
  1282. /*
  1283. * We could only have been turning quotas off.
  1284. * We aren't in very good shape actually because
  1285. * the incore structures are convinced that quotas are
  1286. * off, but the on disk superblock doesn't know that !
  1287. */
  1288. ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
  1289. xfs_alert(mp, "%s: Superblock update failed!",
  1290. __func__);
  1291. }
  1292. }
  1293. if (error) {
  1294. xfs_warn(mp, "Failed to initialize disk quotas.");
  1295. return;
  1296. }
  1297. }
  1298. /*
  1299. * This is called after the superblock has been read in and we're ready to
  1300. * iget the quota inodes.
  1301. */
  1302. STATIC int
  1303. xfs_qm_init_quotainos(
  1304. xfs_mount_t *mp)
  1305. {
  1306. struct xfs_inode *uip = NULL;
  1307. struct xfs_inode *gip = NULL;
  1308. struct xfs_inode *pip = NULL;
  1309. int error;
  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. }
  1343. /*
  1344. * Create the three inodes, if they don't exist already. The changes
  1345. * made above will get added to a transaction and logged in one of
  1346. * the qino_alloc calls below. If the device is readonly,
  1347. * temporarily switch to read-write to do this.
  1348. */
  1349. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1350. error = xfs_qm_qino_alloc(mp, &uip,
  1351. flags | XFS_QMOPT_UQUOTA);
  1352. if (error)
  1353. goto error_rele;
  1354. flags &= ~XFS_QMOPT_SBVERSION;
  1355. }
  1356. if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
  1357. error = xfs_qm_qino_alloc(mp, &gip,
  1358. flags | XFS_QMOPT_GQUOTA);
  1359. if (error)
  1360. goto error_rele;
  1361. flags &= ~XFS_QMOPT_SBVERSION;
  1362. }
  1363. if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
  1364. error = xfs_qm_qino_alloc(mp, &pip,
  1365. flags | XFS_QMOPT_PQUOTA);
  1366. if (error)
  1367. goto error_rele;
  1368. }
  1369. mp->m_quotainfo->qi_uquotaip = uip;
  1370. mp->m_quotainfo->qi_gquotaip = gip;
  1371. mp->m_quotainfo->qi_pquotaip = pip;
  1372. return 0;
  1373. error_rele:
  1374. if (uip)
  1375. IRELE(uip);
  1376. if (gip)
  1377. IRELE(gip);
  1378. if (pip)
  1379. IRELE(pip);
  1380. return error;
  1381. }
  1382. STATIC void
  1383. xfs_qm_dqfree_one(
  1384. struct xfs_dquot *dqp)
  1385. {
  1386. struct xfs_mount *mp = dqp->q_mount;
  1387. struct xfs_quotainfo *qi = mp->m_quotainfo;
  1388. mutex_lock(&qi->qi_tree_lock);
  1389. radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
  1390. be32_to_cpu(dqp->q_core.d_id));
  1391. qi->qi_dquots--;
  1392. mutex_unlock(&qi->qi_tree_lock);
  1393. xfs_qm_dqdestroy(dqp);
  1394. }
  1395. /* --------------- utility functions for vnodeops ---------------- */
  1396. /*
  1397. * Given an inode, a uid, gid and prid make sure that we have
  1398. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  1399. * quotas by creating this file.
  1400. * This also attaches dquot(s) to the given inode after locking it,
  1401. * and returns the dquots corresponding to the uid and/or gid.
  1402. *
  1403. * in : inode (unlocked)
  1404. * out : udquot, gdquot with references taken and unlocked
  1405. */
  1406. int
  1407. xfs_qm_vop_dqalloc(
  1408. struct xfs_inode *ip,
  1409. xfs_dqid_t uid,
  1410. xfs_dqid_t gid,
  1411. prid_t prid,
  1412. uint flags,
  1413. struct xfs_dquot **O_udqpp,
  1414. struct xfs_dquot **O_gdqpp,
  1415. struct xfs_dquot **O_pdqpp)
  1416. {
  1417. struct xfs_mount *mp = ip->i_mount;
  1418. struct xfs_dquot *uq = NULL;
  1419. struct xfs_dquot *gq = NULL;
  1420. struct xfs_dquot *pq = NULL;
  1421. int error;
  1422. uint lockflags;
  1423. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1424. return 0;
  1425. lockflags = XFS_ILOCK_EXCL;
  1426. xfs_ilock(ip, lockflags);
  1427. if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
  1428. gid = ip->i_d.di_gid;
  1429. /*
  1430. * Attach the dquot(s) to this inode, doing a dquot allocation
  1431. * if necessary. The dquot(s) will not be locked.
  1432. */
  1433. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1434. error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
  1435. if (error) {
  1436. xfs_iunlock(ip, lockflags);
  1437. return error;
  1438. }
  1439. }
  1440. if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
  1441. if (ip->i_d.di_uid != uid) {
  1442. /*
  1443. * What we need is the dquot that has this uid, and
  1444. * if we send the inode to dqget, the uid of the inode
  1445. * takes priority over what's sent in the uid argument.
  1446. * We must unlock inode here before calling dqget if
  1447. * we're not sending the inode, because otherwise
  1448. * we'll deadlock by doing trans_reserve while
  1449. * holding ilock.
  1450. */
  1451. xfs_iunlock(ip, lockflags);
  1452. error = xfs_qm_dqget(mp, NULL, uid,
  1453. XFS_DQ_USER,
  1454. XFS_QMOPT_DQALLOC |
  1455. XFS_QMOPT_DOWARN,
  1456. &uq);
  1457. if (error) {
  1458. ASSERT(error != -ENOENT);
  1459. return error;
  1460. }
  1461. /*
  1462. * Get the ilock in the right order.
  1463. */
  1464. xfs_dqunlock(uq);
  1465. lockflags = XFS_ILOCK_SHARED;
  1466. xfs_ilock(ip, lockflags);
  1467. } else {
  1468. /*
  1469. * Take an extra reference, because we'll return
  1470. * this to caller
  1471. */
  1472. ASSERT(ip->i_udquot);
  1473. uq = xfs_qm_dqhold(ip->i_udquot);
  1474. }
  1475. }
  1476. if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
  1477. if (ip->i_d.di_gid != gid) {
  1478. xfs_iunlock(ip, lockflags);
  1479. error = xfs_qm_dqget(mp, NULL, gid,
  1480. XFS_DQ_GROUP,
  1481. XFS_QMOPT_DQALLOC |
  1482. XFS_QMOPT_DOWARN,
  1483. &gq);
  1484. if (error) {
  1485. ASSERT(error != -ENOENT);
  1486. goto error_rele;
  1487. }
  1488. xfs_dqunlock(gq);
  1489. lockflags = XFS_ILOCK_SHARED;
  1490. xfs_ilock(ip, lockflags);
  1491. } else {
  1492. ASSERT(ip->i_gdquot);
  1493. gq = xfs_qm_dqhold(ip->i_gdquot);
  1494. }
  1495. }
  1496. if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
  1497. if (xfs_get_projid(ip) != prid) {
  1498. xfs_iunlock(ip, lockflags);
  1499. error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
  1500. XFS_DQ_PROJ,
  1501. XFS_QMOPT_DQALLOC |
  1502. XFS_QMOPT_DOWARN,
  1503. &pq);
  1504. if (error) {
  1505. ASSERT(error != -ENOENT);
  1506. goto error_rele;
  1507. }
  1508. xfs_dqunlock(pq);
  1509. lockflags = XFS_ILOCK_SHARED;
  1510. xfs_ilock(ip, lockflags);
  1511. } else {
  1512. ASSERT(ip->i_pdquot);
  1513. pq = xfs_qm_dqhold(ip->i_pdquot);
  1514. }
  1515. }
  1516. if (uq)
  1517. trace_xfs_dquot_dqalloc(ip);
  1518. xfs_iunlock(ip, lockflags);
  1519. if (O_udqpp)
  1520. *O_udqpp = uq;
  1521. else
  1522. xfs_qm_dqrele(uq);
  1523. if (O_gdqpp)
  1524. *O_gdqpp = gq;
  1525. else
  1526. xfs_qm_dqrele(gq);
  1527. if (O_pdqpp)
  1528. *O_pdqpp = pq;
  1529. else
  1530. xfs_qm_dqrele(pq);
  1531. return 0;
  1532. error_rele:
  1533. xfs_qm_dqrele(gq);
  1534. xfs_qm_dqrele(uq);
  1535. return error;
  1536. }
  1537. /*
  1538. * Actually transfer ownership, and do dquot modifications.
  1539. * These were already reserved.
  1540. */
  1541. xfs_dquot_t *
  1542. xfs_qm_vop_chown(
  1543. xfs_trans_t *tp,
  1544. xfs_inode_t *ip,
  1545. xfs_dquot_t **IO_olddq,
  1546. xfs_dquot_t *newdq)
  1547. {
  1548. xfs_dquot_t *prevdq;
  1549. uint bfield = XFS_IS_REALTIME_INODE(ip) ?
  1550. XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
  1551. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1552. ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
  1553. /* old dquot */
  1554. prevdq = *IO_olddq;
  1555. ASSERT(prevdq);
  1556. ASSERT(prevdq != newdq);
  1557. xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
  1558. xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
  1559. /* the sparkling new dquot */
  1560. xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
  1561. xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
  1562. /*
  1563. * Take an extra reference, because the inode is going to keep
  1564. * this dquot pointer even after the trans_commit.
  1565. */
  1566. *IO_olddq = xfs_qm_dqhold(newdq);
  1567. return prevdq;
  1568. }
  1569. /*
  1570. * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
  1571. */
  1572. int
  1573. xfs_qm_vop_chown_reserve(
  1574. struct xfs_trans *tp,
  1575. struct xfs_inode *ip,
  1576. struct xfs_dquot *udqp,
  1577. struct xfs_dquot *gdqp,
  1578. struct xfs_dquot *pdqp,
  1579. uint flags)
  1580. {
  1581. struct xfs_mount *mp = ip->i_mount;
  1582. uint delblks, blkflags, prjflags = 0;
  1583. struct xfs_dquot *udq_unres = NULL;
  1584. struct xfs_dquot *gdq_unres = NULL;
  1585. struct xfs_dquot *pdq_unres = NULL;
  1586. struct xfs_dquot *udq_delblks = NULL;
  1587. struct xfs_dquot *gdq_delblks = NULL;
  1588. struct xfs_dquot *pdq_delblks = NULL;
  1589. int error;
  1590. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1591. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1592. delblks = ip->i_delayed_blks;
  1593. blkflags = XFS_IS_REALTIME_INODE(ip) ?
  1594. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
  1595. if (XFS_IS_UQUOTA_ON(mp) && udqp &&
  1596. ip->i_d.di_uid != be32_to_cpu(udqp->q_core.d_id)) {
  1597. udq_delblks = udqp;
  1598. /*
  1599. * If there are delayed allocation blocks, then we have to
  1600. * unreserve those from the old dquot, and add them to the
  1601. * new dquot.
  1602. */
  1603. if (delblks) {
  1604. ASSERT(ip->i_udquot);
  1605. udq_unres = ip->i_udquot;
  1606. }
  1607. }
  1608. if (XFS_IS_GQUOTA_ON(ip->i_mount) && gdqp &&
  1609. ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id)) {
  1610. gdq_delblks = gdqp;
  1611. if (delblks) {
  1612. ASSERT(ip->i_gdquot);
  1613. gdq_unres = ip->i_gdquot;
  1614. }
  1615. }
  1616. if (XFS_IS_PQUOTA_ON(ip->i_mount) && pdqp &&
  1617. xfs_get_projid(ip) != be32_to_cpu(pdqp->q_core.d_id)) {
  1618. prjflags = XFS_QMOPT_ENOSPC;
  1619. pdq_delblks = pdqp;
  1620. if (delblks) {
  1621. ASSERT(ip->i_pdquot);
  1622. pdq_unres = ip->i_pdquot;
  1623. }
  1624. }
  1625. error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
  1626. udq_delblks, gdq_delblks, pdq_delblks,
  1627. ip->i_d.di_nblocks, 1,
  1628. flags | blkflags | prjflags);
  1629. if (error)
  1630. return error;
  1631. /*
  1632. * Do the delayed blks reservations/unreservations now. Since, these
  1633. * are done without the help of a transaction, if a reservation fails
  1634. * its previous reservations won't be automatically undone by trans
  1635. * code. So, we have to do it manually here.
  1636. */
  1637. if (delblks) {
  1638. /*
  1639. * Do the reservations first. Unreservation can't fail.
  1640. */
  1641. ASSERT(udq_delblks || gdq_delblks || pdq_delblks);
  1642. ASSERT(udq_unres || gdq_unres || pdq_unres);
  1643. error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  1644. udq_delblks, gdq_delblks, pdq_delblks,
  1645. (xfs_qcnt_t)delblks, 0,
  1646. flags | blkflags | prjflags);
  1647. if (error)
  1648. return error;
  1649. xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  1650. udq_unres, gdq_unres, pdq_unres,
  1651. -((xfs_qcnt_t)delblks), 0, blkflags);
  1652. }
  1653. return 0;
  1654. }
  1655. int
  1656. xfs_qm_vop_rename_dqattach(
  1657. struct xfs_inode **i_tab)
  1658. {
  1659. struct xfs_mount *mp = i_tab[0]->i_mount;
  1660. int i;
  1661. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1662. return 0;
  1663. for (i = 0; (i < 4 && i_tab[i]); i++) {
  1664. struct xfs_inode *ip = i_tab[i];
  1665. int error;
  1666. /*
  1667. * Watch out for duplicate entries in the table.
  1668. */
  1669. if (i == 0 || ip != i_tab[i-1]) {
  1670. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1671. error = xfs_qm_dqattach(ip, 0);
  1672. if (error)
  1673. return error;
  1674. }
  1675. }
  1676. }
  1677. return 0;
  1678. }
  1679. void
  1680. xfs_qm_vop_create_dqattach(
  1681. struct xfs_trans *tp,
  1682. struct xfs_inode *ip,
  1683. struct xfs_dquot *udqp,
  1684. struct xfs_dquot *gdqp,
  1685. struct xfs_dquot *pdqp)
  1686. {
  1687. struct xfs_mount *mp = tp->t_mountp;
  1688. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1689. return;
  1690. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1691. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1692. if (udqp && XFS_IS_UQUOTA_ON(mp)) {
  1693. ASSERT(ip->i_udquot == NULL);
  1694. ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
  1695. ip->i_udquot = xfs_qm_dqhold(udqp);
  1696. xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
  1697. }
  1698. if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
  1699. ASSERT(ip->i_gdquot == NULL);
  1700. ASSERT(ip->i_d.di_gid == be32_to_cpu(gdqp->q_core.d_id));
  1701. ip->i_gdquot = xfs_qm_dqhold(gdqp);
  1702. xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
  1703. }
  1704. if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
  1705. ASSERT(ip->i_pdquot == NULL);
  1706. ASSERT(xfs_get_projid(ip) == be32_to_cpu(pdqp->q_core.d_id));
  1707. ip->i_pdquot = xfs_qm_dqhold(pdqp);
  1708. xfs_trans_mod_dquot(tp, pdqp, XFS_TRANS_DQ_ICOUNT, 1);
  1709. }
  1710. }