xfs_qm.c 48 KB

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