xfs_qm.c 48 KB

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