xfs_qm.c 50 KB

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