xfs_qm.c 46 KB

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