xfs_qm.c 51 KB

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