xfs_qm.c 46 KB

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