xfs_qm.c 46 KB

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