xfs_qm.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_ialloc.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_rtalloc.h"
  36. #include "xfs_error.h"
  37. #include "xfs_bmap.h"
  38. #include "xfs_attr.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_trans_space.h"
  41. #include "xfs_utils.h"
  42. #include "xfs_qm.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * The global quota manager. There is only one of these for the entire
  46. * system, _not_ one per file system. XQM keeps track of the overall
  47. * quota functionality, including maintaining the freelist and hash
  48. * tables of dquots.
  49. */
  50. struct mutex xfs_Gqm_lock;
  51. struct xfs_qm *xfs_Gqm;
  52. uint ndquot;
  53. kmem_zone_t *qm_dqzone;
  54. kmem_zone_t *qm_dqtrxzone;
  55. STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
  56. STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
  57. STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
  58. STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
  59. STATIC int xfs_qm_shake(struct shrinker *, int, gfp_t);
  60. static struct shrinker xfs_qm_shaker = {
  61. .shrink = xfs_qm_shake,
  62. .seeks = DEFAULT_SEEKS,
  63. };
  64. #ifdef DEBUG
  65. extern struct mutex qcheck_lock;
  66. #endif
  67. #ifdef QUOTADEBUG
  68. static void
  69. xfs_qm_dquot_list_print(
  70. struct xfs_mount *mp)
  71. {
  72. xfs_dquot_t *dqp;
  73. int i = 0;
  74. list_for_each_entry(dqp, &mp->m_quotainfo->qi_dqlist_lock, qi_mplist) {
  75. xfs_debug(mp, " %d. \"%d (%s)\" "
  76. "bcnt = %lld, icnt = %lld, refs = %d",
  77. i++, be32_to_cpu(dqp->q_core.d_id),
  78. DQFLAGTO_TYPESTR(dqp),
  79. (long long)be64_to_cpu(dqp->q_core.d_bcount),
  80. (long long)be64_to_cpu(dqp->q_core.d_icount),
  81. dqp->q_nrefs);
  82. }
  83. }
  84. #else
  85. static void xfs_qm_dquot_list_print(struct xfs_mount *mp) { }
  86. #endif
  87. /*
  88. * Initialize the XQM structure.
  89. * Note that there is not one quota manager per file system.
  90. */
  91. STATIC struct xfs_qm *
  92. xfs_Gqm_init(void)
  93. {
  94. xfs_dqhash_t *udqhash, *gdqhash;
  95. xfs_qm_t *xqm;
  96. size_t hsize;
  97. uint i;
  98. /*
  99. * Initialize the dquot hash tables.
  100. */
  101. udqhash = kmem_zalloc_greedy(&hsize,
  102. XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
  103. XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t));
  104. if (!udqhash)
  105. goto out;
  106. gdqhash = kmem_zalloc_large(hsize);
  107. if (!gdqhash)
  108. goto out_free_udqhash;
  109. hsize /= sizeof(xfs_dqhash_t);
  110. ndquot = hsize << 8;
  111. xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
  112. xqm->qm_dqhashmask = hsize - 1;
  113. xqm->qm_usr_dqhtable = udqhash;
  114. xqm->qm_grp_dqhtable = gdqhash;
  115. ASSERT(xqm->qm_usr_dqhtable != NULL);
  116. ASSERT(xqm->qm_grp_dqhtable != NULL);
  117. for (i = 0; i < hsize; i++) {
  118. xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
  119. xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
  120. }
  121. /*
  122. * Freelist of all dquots of all file systems
  123. */
  124. INIT_LIST_HEAD(&xqm->qm_dqfrlist);
  125. xqm->qm_dqfrlist_cnt = 0;
  126. mutex_init(&xqm->qm_dqfrlist_lock);
  127. /*
  128. * dquot zone. we register our own low-memory callback.
  129. */
  130. if (!qm_dqzone) {
  131. xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
  132. "xfs_dquots");
  133. qm_dqzone = xqm->qm_dqzone;
  134. } else
  135. xqm->qm_dqzone = qm_dqzone;
  136. register_shrinker(&xfs_qm_shaker);
  137. /*
  138. * The t_dqinfo portion of transactions.
  139. */
  140. if (!qm_dqtrxzone) {
  141. xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
  142. "xfs_dqtrx");
  143. qm_dqtrxzone = xqm->qm_dqtrxzone;
  144. } else
  145. xqm->qm_dqtrxzone = qm_dqtrxzone;
  146. atomic_set(&xqm->qm_totaldquots, 0);
  147. xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
  148. xqm->qm_nrefs = 0;
  149. #ifdef DEBUG
  150. mutex_init(&qcheck_lock);
  151. #endif
  152. return xqm;
  153. out_free_udqhash:
  154. kmem_free_large(udqhash);
  155. out:
  156. return NULL;
  157. }
  158. /*
  159. * Destroy the global quota manager when its reference count goes to zero.
  160. */
  161. STATIC void
  162. xfs_qm_destroy(
  163. struct xfs_qm *xqm)
  164. {
  165. struct xfs_dquot *dqp, *n;
  166. int hsize, i;
  167. ASSERT(xqm != NULL);
  168. ASSERT(xqm->qm_nrefs == 0);
  169. unregister_shrinker(&xfs_qm_shaker);
  170. hsize = xqm->qm_dqhashmask + 1;
  171. for (i = 0; i < hsize; i++) {
  172. xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
  173. xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
  174. }
  175. kmem_free_large(xqm->qm_usr_dqhtable);
  176. kmem_free_large(xqm->qm_grp_dqhtable);
  177. xqm->qm_usr_dqhtable = NULL;
  178. xqm->qm_grp_dqhtable = NULL;
  179. xqm->qm_dqhashmask = 0;
  180. /* frlist cleanup */
  181. mutex_lock(&xqm->qm_dqfrlist_lock);
  182. list_for_each_entry_safe(dqp, n, &xqm->qm_dqfrlist, q_freelist) {
  183. xfs_dqlock(dqp);
  184. #ifdef QUOTADEBUG
  185. xfs_debug(dqp->q_mount, "FREELIST destroy 0x%p", dqp);
  186. #endif
  187. list_del_init(&dqp->q_freelist);
  188. xfs_Gqm->qm_dqfrlist_cnt--;
  189. xfs_dqunlock(dqp);
  190. xfs_qm_dqdestroy(dqp);
  191. }
  192. mutex_unlock(&xqm->qm_dqfrlist_lock);
  193. mutex_destroy(&xqm->qm_dqfrlist_lock);
  194. #ifdef DEBUG
  195. mutex_destroy(&qcheck_lock);
  196. #endif
  197. kmem_free(xqm);
  198. }
  199. /*
  200. * Called at mount time to let XQM know that another file system is
  201. * starting quotas. This isn't crucial information as the individual mount
  202. * structures are pretty independent, but it helps the XQM keep a
  203. * global view of what's going on.
  204. */
  205. /* ARGSUSED */
  206. STATIC int
  207. xfs_qm_hold_quotafs_ref(
  208. struct xfs_mount *mp)
  209. {
  210. /*
  211. * Need to lock the xfs_Gqm structure for things like this. For example,
  212. * the structure could disappear between the entry to this routine and
  213. * a HOLD operation if not locked.
  214. */
  215. mutex_lock(&xfs_Gqm_lock);
  216. if (!xfs_Gqm) {
  217. xfs_Gqm = xfs_Gqm_init();
  218. if (!xfs_Gqm) {
  219. mutex_unlock(&xfs_Gqm_lock);
  220. return ENOMEM;
  221. }
  222. }
  223. /*
  224. * We can keep a list of all filesystems with quotas mounted for
  225. * debugging and statistical purposes, but ...
  226. * Just take a reference and get out.
  227. */
  228. xfs_Gqm->qm_nrefs++;
  229. mutex_unlock(&xfs_Gqm_lock);
  230. return 0;
  231. }
  232. /*
  233. * Release the reference that a filesystem took at mount time,
  234. * so that we know when we need to destroy the entire quota manager.
  235. */
  236. /* ARGSUSED */
  237. STATIC void
  238. xfs_qm_rele_quotafs_ref(
  239. struct xfs_mount *mp)
  240. {
  241. xfs_dquot_t *dqp, *n;
  242. ASSERT(xfs_Gqm);
  243. ASSERT(xfs_Gqm->qm_nrefs > 0);
  244. /*
  245. * Go thru the freelist and destroy all inactive dquots.
  246. */
  247. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  248. list_for_each_entry_safe(dqp, n, &xfs_Gqm->qm_dqfrlist, q_freelist) {
  249. xfs_dqlock(dqp);
  250. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  251. ASSERT(dqp->q_mount == NULL);
  252. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  253. ASSERT(list_empty(&dqp->q_hashlist));
  254. ASSERT(list_empty(&dqp->q_mplist));
  255. list_del_init(&dqp->q_freelist);
  256. xfs_Gqm->qm_dqfrlist_cnt--;
  257. xfs_dqunlock(dqp);
  258. xfs_qm_dqdestroy(dqp);
  259. } else {
  260. xfs_dqunlock(dqp);
  261. }
  262. }
  263. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  264. /*
  265. * Destroy the entire XQM. If somebody mounts with quotaon, this'll
  266. * be restarted.
  267. */
  268. mutex_lock(&xfs_Gqm_lock);
  269. if (--xfs_Gqm->qm_nrefs == 0) {
  270. xfs_qm_destroy(xfs_Gqm);
  271. xfs_Gqm = NULL;
  272. }
  273. mutex_unlock(&xfs_Gqm_lock);
  274. }
  275. /*
  276. * Just destroy the quotainfo structure.
  277. */
  278. void
  279. xfs_qm_unmount(
  280. struct xfs_mount *mp)
  281. {
  282. if (mp->m_quotainfo) {
  283. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
  284. xfs_qm_destroy_quotainfo(mp);
  285. }
  286. }
  287. /*
  288. * This is called from xfs_mountfs to start quotas and initialize all
  289. * necessary data structures like quotainfo. This is also responsible for
  290. * running a quotacheck as necessary. We are guaranteed that the superblock
  291. * is consistently read in at this point.
  292. *
  293. * If we fail here, the mount will continue with quota turned off. We don't
  294. * need to inidicate success or failure at all.
  295. */
  296. void
  297. xfs_qm_mount_quotas(
  298. xfs_mount_t *mp)
  299. {
  300. int error = 0;
  301. uint sbf;
  302. /*
  303. * If quotas on realtime volumes is not supported, we disable
  304. * quotas immediately.
  305. */
  306. if (mp->m_sb.sb_rextents) {
  307. xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
  308. mp->m_qflags = 0;
  309. goto write_changes;
  310. }
  311. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  312. /*
  313. * Allocate the quotainfo structure inside the mount struct, and
  314. * create quotainode(s), and change/rev superblock if necessary.
  315. */
  316. error = xfs_qm_init_quotainfo(mp);
  317. if (error) {
  318. /*
  319. * We must turn off quotas.
  320. */
  321. ASSERT(mp->m_quotainfo == NULL);
  322. mp->m_qflags = 0;
  323. goto write_changes;
  324. }
  325. /*
  326. * If any of the quotas are not consistent, do a quotacheck.
  327. */
  328. if (XFS_QM_NEED_QUOTACHECK(mp)) {
  329. error = xfs_qm_quotacheck(mp);
  330. if (error) {
  331. /* Quotacheck failed and disabled quotas. */
  332. return;
  333. }
  334. }
  335. /*
  336. * If one type of quotas is off, then it will lose its
  337. * quotachecked status, since we won't be doing accounting for
  338. * that type anymore.
  339. */
  340. if (!XFS_IS_UQUOTA_ON(mp))
  341. mp->m_qflags &= ~XFS_UQUOTA_CHKD;
  342. if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
  343. mp->m_qflags &= ~XFS_OQUOTA_CHKD;
  344. write_changes:
  345. /*
  346. * We actually don't have to acquire the m_sb_lock at all.
  347. * This can only be called from mount, and that's single threaded. XXX
  348. */
  349. spin_lock(&mp->m_sb_lock);
  350. sbf = mp->m_sb.sb_qflags;
  351. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  352. spin_unlock(&mp->m_sb_lock);
  353. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  354. if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
  355. /*
  356. * We could only have been turning quotas off.
  357. * We aren't in very good shape actually because
  358. * the incore structures are convinced that quotas are
  359. * off, but the on disk superblock doesn't know that !
  360. */
  361. ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
  362. xfs_alert(mp, "%s: Superblock update failed!",
  363. __func__);
  364. }
  365. }
  366. if (error) {
  367. xfs_warn(mp, "Failed to initialize disk quotas.");
  368. return;
  369. }
  370. #ifdef QUOTADEBUG
  371. if (XFS_IS_QUOTA_ON(mp))
  372. xfs_qm_internalqcheck(mp);
  373. #endif
  374. }
  375. /*
  376. * Called from the vfsops layer.
  377. */
  378. void
  379. xfs_qm_unmount_quotas(
  380. xfs_mount_t *mp)
  381. {
  382. /*
  383. * Release the dquots that root inode, et al might be holding,
  384. * before we flush quotas and blow away the quotainfo structure.
  385. */
  386. ASSERT(mp->m_rootip);
  387. xfs_qm_dqdetach(mp->m_rootip);
  388. if (mp->m_rbmip)
  389. xfs_qm_dqdetach(mp->m_rbmip);
  390. if (mp->m_rsumip)
  391. xfs_qm_dqdetach(mp->m_rsumip);
  392. /*
  393. * Release the quota inodes.
  394. */
  395. if (mp->m_quotainfo) {
  396. if (mp->m_quotainfo->qi_uquotaip) {
  397. IRELE(mp->m_quotainfo->qi_uquotaip);
  398. mp->m_quotainfo->qi_uquotaip = NULL;
  399. }
  400. if (mp->m_quotainfo->qi_gquotaip) {
  401. IRELE(mp->m_quotainfo->qi_gquotaip);
  402. mp->m_quotainfo->qi_gquotaip = NULL;
  403. }
  404. }
  405. }
  406. /*
  407. * Flush all dquots of the given file system to disk. The dquots are
  408. * _not_ purged from memory here, just their data written to disk.
  409. */
  410. STATIC int
  411. xfs_qm_dqflush_all(
  412. struct xfs_mount *mp,
  413. int sync_mode)
  414. {
  415. struct xfs_quotainfo *q = mp->m_quotainfo;
  416. int recl;
  417. struct xfs_dquot *dqp;
  418. int niters;
  419. int error;
  420. if (!q)
  421. return 0;
  422. niters = 0;
  423. again:
  424. mutex_lock(&q->qi_dqlist_lock);
  425. list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
  426. xfs_dqlock(dqp);
  427. if (! XFS_DQ_IS_DIRTY(dqp)) {
  428. xfs_dqunlock(dqp);
  429. continue;
  430. }
  431. /* XXX a sentinel would be better */
  432. recl = q->qi_dqreclaims;
  433. if (!xfs_dqflock_nowait(dqp)) {
  434. /*
  435. * If we can't grab the flush lock then check
  436. * to see if the dquot has been flushed delayed
  437. * write. If so, grab its buffer and send it
  438. * out immediately. We'll be able to acquire
  439. * the flush lock when the I/O completes.
  440. */
  441. xfs_qm_dqflock_pushbuf_wait(dqp);
  442. }
  443. /*
  444. * Let go of the mplist lock. We don't want to hold it
  445. * across a disk write.
  446. */
  447. mutex_unlock(&q->qi_dqlist_lock);
  448. error = xfs_qm_dqflush(dqp, sync_mode);
  449. xfs_dqunlock(dqp);
  450. if (error)
  451. return error;
  452. mutex_lock(&q->qi_dqlist_lock);
  453. if (recl != q->qi_dqreclaims) {
  454. mutex_unlock(&q->qi_dqlist_lock);
  455. /* XXX restart limit */
  456. goto again;
  457. }
  458. }
  459. mutex_unlock(&q->qi_dqlist_lock);
  460. /* return ! busy */
  461. return 0;
  462. }
  463. /*
  464. * Release the group dquot pointers the user dquots may be
  465. * carrying around as a hint. mplist is locked on entry and exit.
  466. */
  467. STATIC void
  468. xfs_qm_detach_gdquots(
  469. struct xfs_mount *mp)
  470. {
  471. struct xfs_quotainfo *q = mp->m_quotainfo;
  472. struct xfs_dquot *dqp, *gdqp;
  473. int nrecl;
  474. again:
  475. ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
  476. list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
  477. xfs_dqlock(dqp);
  478. if ((gdqp = dqp->q_gdquot)) {
  479. xfs_dqlock(gdqp);
  480. dqp->q_gdquot = NULL;
  481. }
  482. xfs_dqunlock(dqp);
  483. if (gdqp) {
  484. /*
  485. * Can't hold the mplist lock across a dqput.
  486. * XXXmust convert to marker based iterations here.
  487. */
  488. nrecl = q->qi_dqreclaims;
  489. mutex_unlock(&q->qi_dqlist_lock);
  490. xfs_qm_dqput(gdqp);
  491. mutex_lock(&q->qi_dqlist_lock);
  492. if (nrecl != q->qi_dqreclaims)
  493. goto again;
  494. }
  495. }
  496. }
  497. /*
  498. * Go through all the incore dquots of this file system and take them
  499. * off the mplist and hashlist, if the dquot type matches the dqtype
  500. * parameter. This is used when turning off quota accounting for
  501. * users and/or groups, as well as when the filesystem is unmounting.
  502. */
  503. STATIC int
  504. xfs_qm_dqpurge_int(
  505. struct xfs_mount *mp,
  506. uint flags)
  507. {
  508. struct xfs_quotainfo *q = mp->m_quotainfo;
  509. struct xfs_dquot *dqp, *n;
  510. uint dqtype;
  511. int nrecl;
  512. int nmisses;
  513. if (!q)
  514. return 0;
  515. dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
  516. dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
  517. dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
  518. mutex_lock(&q->qi_dqlist_lock);
  519. /*
  520. * In the first pass through all incore dquots of this filesystem,
  521. * we release the group dquot pointers the user dquots may be
  522. * carrying around as a hint. We need to do this irrespective of
  523. * what's being turned off.
  524. */
  525. xfs_qm_detach_gdquots(mp);
  526. again:
  527. nmisses = 0;
  528. ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
  529. /*
  530. * Try to get rid of all of the unwanted dquots. The idea is to
  531. * get them off mplist and hashlist, but leave them on freelist.
  532. */
  533. list_for_each_entry_safe(dqp, n, &q->qi_dqlist, q_mplist) {
  534. /*
  535. * It's OK to look at the type without taking dqlock here.
  536. * We're holding the mplist lock here, and that's needed for
  537. * a dqreclaim.
  538. */
  539. if ((dqp->dq_flags & dqtype) == 0)
  540. continue;
  541. if (!mutex_trylock(&dqp->q_hash->qh_lock)) {
  542. nrecl = q->qi_dqreclaims;
  543. mutex_unlock(&q->qi_dqlist_lock);
  544. mutex_lock(&dqp->q_hash->qh_lock);
  545. mutex_lock(&q->qi_dqlist_lock);
  546. /*
  547. * XXXTheoretically, we can get into a very long
  548. * ping pong game here.
  549. * No one can be adding dquots to the mplist at
  550. * this point, but somebody might be taking things off.
  551. */
  552. if (nrecl != q->qi_dqreclaims) {
  553. mutex_unlock(&dqp->q_hash->qh_lock);
  554. goto again;
  555. }
  556. }
  557. /*
  558. * Take the dquot off the mplist and hashlist. It may remain on
  559. * freelist in INACTIVE state.
  560. */
  561. nmisses += xfs_qm_dqpurge(dqp);
  562. }
  563. mutex_unlock(&q->qi_dqlist_lock);
  564. return nmisses;
  565. }
  566. int
  567. xfs_qm_dqpurge_all(
  568. xfs_mount_t *mp,
  569. uint flags)
  570. {
  571. int ndquots;
  572. /*
  573. * Purge the dquot cache.
  574. * None of the dquots should really be busy at this point.
  575. */
  576. if (mp->m_quotainfo) {
  577. while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
  578. delay(ndquots * 10);
  579. }
  580. }
  581. return 0;
  582. }
  583. STATIC int
  584. xfs_qm_dqattach_one(
  585. xfs_inode_t *ip,
  586. xfs_dqid_t id,
  587. uint type,
  588. uint doalloc,
  589. xfs_dquot_t *udqhint, /* hint */
  590. xfs_dquot_t **IO_idqpp)
  591. {
  592. xfs_dquot_t *dqp;
  593. int error;
  594. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  595. error = 0;
  596. /*
  597. * See if we already have it in the inode itself. IO_idqpp is
  598. * &i_udquot or &i_gdquot. This made the code look weird, but
  599. * made the logic a lot simpler.
  600. */
  601. dqp = *IO_idqpp;
  602. if (dqp) {
  603. trace_xfs_dqattach_found(dqp);
  604. return 0;
  605. }
  606. /*
  607. * udqhint is the i_udquot field in inode, and is non-NULL only
  608. * when the type arg is group/project. Its purpose is to save a
  609. * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
  610. * the user dquot.
  611. */
  612. if (udqhint) {
  613. ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
  614. xfs_dqlock(udqhint);
  615. /*
  616. * No need to take dqlock to look at the id.
  617. *
  618. * The ID can't change until it gets reclaimed, and it won't
  619. * be reclaimed as long as we have a ref from inode and we
  620. * hold the ilock.
  621. */
  622. dqp = udqhint->q_gdquot;
  623. if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
  624. xfs_dqlock(dqp);
  625. XFS_DQHOLD(dqp);
  626. ASSERT(*IO_idqpp == NULL);
  627. *IO_idqpp = dqp;
  628. xfs_dqunlock(dqp);
  629. xfs_dqunlock(udqhint);
  630. return 0;
  631. }
  632. /*
  633. * We can't hold a dquot lock when we call the dqget code.
  634. * We'll deadlock in no time, because of (not conforming to)
  635. * lock ordering - the inodelock comes before any dquot lock,
  636. * and we may drop and reacquire the ilock in xfs_qm_dqget().
  637. */
  638. xfs_dqunlock(udqhint);
  639. }
  640. /*
  641. * Find the dquot from somewhere. This bumps the
  642. * reference count of dquot and returns it locked.
  643. * This can return ENOENT if dquot didn't exist on
  644. * disk and we didn't ask it to allocate;
  645. * ESRCH if quotas got turned off suddenly.
  646. */
  647. error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp);
  648. if (error)
  649. return error;
  650. trace_xfs_dqattach_get(dqp);
  651. /*
  652. * dqget may have dropped and re-acquired the ilock, but it guarantees
  653. * that the dquot returned is the one that should go in the inode.
  654. */
  655. *IO_idqpp = dqp;
  656. xfs_dqunlock(dqp);
  657. return 0;
  658. }
  659. /*
  660. * Given a udquot and gdquot, attach a ptr to the group dquot in the
  661. * udquot as a hint for future lookups. The idea sounds simple, but the
  662. * execution isn't, because the udquot might have a group dquot attached
  663. * already and getting rid of that gets us into lock ordering constraints.
  664. * The process is complicated more by the fact that the dquots may or may not
  665. * be locked on entry.
  666. */
  667. STATIC void
  668. xfs_qm_dqattach_grouphint(
  669. xfs_dquot_t *udq,
  670. xfs_dquot_t *gdq)
  671. {
  672. xfs_dquot_t *tmp;
  673. xfs_dqlock(udq);
  674. if ((tmp = udq->q_gdquot)) {
  675. if (tmp == gdq) {
  676. xfs_dqunlock(udq);
  677. return;
  678. }
  679. udq->q_gdquot = NULL;
  680. /*
  681. * We can't keep any dqlocks when calling dqrele,
  682. * because the freelist lock comes before dqlocks.
  683. */
  684. xfs_dqunlock(udq);
  685. /*
  686. * we took a hard reference once upon a time in dqget,
  687. * so give it back when the udquot no longer points at it
  688. * dqput() does the unlocking of the dquot.
  689. */
  690. xfs_qm_dqrele(tmp);
  691. xfs_dqlock(udq);
  692. xfs_dqlock(gdq);
  693. } else {
  694. ASSERT(XFS_DQ_IS_LOCKED(udq));
  695. xfs_dqlock(gdq);
  696. }
  697. ASSERT(XFS_DQ_IS_LOCKED(udq));
  698. ASSERT(XFS_DQ_IS_LOCKED(gdq));
  699. /*
  700. * Somebody could have attached a gdquot here,
  701. * when we dropped the uqlock. If so, just do nothing.
  702. */
  703. if (udq->q_gdquot == NULL) {
  704. XFS_DQHOLD(gdq);
  705. udq->q_gdquot = gdq;
  706. }
  707. xfs_dqunlock(gdq);
  708. xfs_dqunlock(udq);
  709. }
  710. /*
  711. * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
  712. * into account.
  713. * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
  714. * Inode may get unlocked and relocked in here, and the caller must deal with
  715. * the consequences.
  716. */
  717. int
  718. xfs_qm_dqattach_locked(
  719. xfs_inode_t *ip,
  720. uint flags)
  721. {
  722. xfs_mount_t *mp = ip->i_mount;
  723. uint nquotas = 0;
  724. int error = 0;
  725. if (!XFS_IS_QUOTA_RUNNING(mp) ||
  726. !XFS_IS_QUOTA_ON(mp) ||
  727. !XFS_NOT_DQATTACHED(mp, ip) ||
  728. ip->i_ino == mp->m_sb.sb_uquotino ||
  729. ip->i_ino == mp->m_sb.sb_gquotino)
  730. return 0;
  731. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  732. if (XFS_IS_UQUOTA_ON(mp)) {
  733. error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
  734. flags & XFS_QMOPT_DQALLOC,
  735. NULL, &ip->i_udquot);
  736. if (error)
  737. goto done;
  738. nquotas++;
  739. }
  740. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  741. if (XFS_IS_OQUOTA_ON(mp)) {
  742. error = XFS_IS_GQUOTA_ON(mp) ?
  743. xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
  744. flags & XFS_QMOPT_DQALLOC,
  745. ip->i_udquot, &ip->i_gdquot) :
  746. xfs_qm_dqattach_one(ip, xfs_get_projid(ip), XFS_DQ_PROJ,
  747. flags & XFS_QMOPT_DQALLOC,
  748. ip->i_udquot, &ip->i_gdquot);
  749. /*
  750. * Don't worry about the udquot that we may have
  751. * attached above. It'll get detached, if not already.
  752. */
  753. if (error)
  754. goto done;
  755. nquotas++;
  756. }
  757. /*
  758. * Attach this group quota to the user quota as a hint.
  759. * This WON'T, in general, result in a thrash.
  760. */
  761. if (nquotas == 2) {
  762. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  763. ASSERT(ip->i_udquot);
  764. ASSERT(ip->i_gdquot);
  765. /*
  766. * We may or may not have the i_udquot locked at this point,
  767. * but this check is OK since we don't depend on the i_gdquot to
  768. * be accurate 100% all the time. It is just a hint, and this
  769. * will succeed in general.
  770. */
  771. if (ip->i_udquot->q_gdquot == ip->i_gdquot)
  772. goto done;
  773. /*
  774. * Attach i_gdquot to the gdquot hint inside the i_udquot.
  775. */
  776. xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
  777. }
  778. done:
  779. #ifdef QUOTADEBUG
  780. if (! error) {
  781. if (XFS_IS_UQUOTA_ON(mp))
  782. ASSERT(ip->i_udquot);
  783. if (XFS_IS_OQUOTA_ON(mp))
  784. ASSERT(ip->i_gdquot);
  785. }
  786. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  787. #endif
  788. return error;
  789. }
  790. int
  791. xfs_qm_dqattach(
  792. struct xfs_inode *ip,
  793. uint flags)
  794. {
  795. int error;
  796. xfs_ilock(ip, XFS_ILOCK_EXCL);
  797. error = xfs_qm_dqattach_locked(ip, flags);
  798. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  799. return error;
  800. }
  801. /*
  802. * Release dquots (and their references) if any.
  803. * The inode should be locked EXCL except when this's called by
  804. * xfs_ireclaim.
  805. */
  806. void
  807. xfs_qm_dqdetach(
  808. xfs_inode_t *ip)
  809. {
  810. if (!(ip->i_udquot || ip->i_gdquot))
  811. return;
  812. trace_xfs_dquot_dqdetach(ip);
  813. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
  814. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
  815. if (ip->i_udquot) {
  816. xfs_qm_dqrele(ip->i_udquot);
  817. ip->i_udquot = NULL;
  818. }
  819. if (ip->i_gdquot) {
  820. xfs_qm_dqrele(ip->i_gdquot);
  821. ip->i_gdquot = NULL;
  822. }
  823. }
  824. int
  825. xfs_qm_sync(
  826. struct xfs_mount *mp,
  827. int flags)
  828. {
  829. struct xfs_quotainfo *q = mp->m_quotainfo;
  830. int recl, restarts;
  831. struct xfs_dquot *dqp;
  832. int error;
  833. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  834. return 0;
  835. restarts = 0;
  836. again:
  837. mutex_lock(&q->qi_dqlist_lock);
  838. /*
  839. * dqpurge_all() also takes the mplist lock and iterate thru all dquots
  840. * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
  841. * when we have the mplist lock, we know that dquots will be consistent
  842. * as long as we have it locked.
  843. */
  844. if (!XFS_IS_QUOTA_ON(mp)) {
  845. mutex_unlock(&q->qi_dqlist_lock);
  846. return 0;
  847. }
  848. ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
  849. list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
  850. /*
  851. * If this is vfs_sync calling, then skip the dquots that
  852. * don't 'seem' to be dirty. ie. don't acquire dqlock.
  853. * This is very similar to what xfs_sync does with inodes.
  854. */
  855. if (flags & SYNC_TRYLOCK) {
  856. if (!XFS_DQ_IS_DIRTY(dqp))
  857. continue;
  858. if (!xfs_qm_dqlock_nowait(dqp))
  859. continue;
  860. } else {
  861. xfs_dqlock(dqp);
  862. }
  863. /*
  864. * Now, find out for sure if this dquot is dirty or not.
  865. */
  866. if (! XFS_DQ_IS_DIRTY(dqp)) {
  867. xfs_dqunlock(dqp);
  868. continue;
  869. }
  870. /* XXX a sentinel would be better */
  871. recl = q->qi_dqreclaims;
  872. if (!xfs_dqflock_nowait(dqp)) {
  873. if (flags & SYNC_TRYLOCK) {
  874. xfs_dqunlock(dqp);
  875. continue;
  876. }
  877. /*
  878. * If we can't grab the flush lock then if the caller
  879. * really wanted us to give this our best shot, so
  880. * see if we can give a push to the buffer before we wait
  881. * on the flush lock. At this point, we know that
  882. * even though the dquot is being flushed,
  883. * it has (new) dirty data.
  884. */
  885. xfs_qm_dqflock_pushbuf_wait(dqp);
  886. }
  887. /*
  888. * Let go of the mplist lock. We don't want to hold it
  889. * across a disk write
  890. */
  891. mutex_unlock(&q->qi_dqlist_lock);
  892. error = xfs_qm_dqflush(dqp, flags);
  893. xfs_dqunlock(dqp);
  894. if (error && XFS_FORCED_SHUTDOWN(mp))
  895. return 0; /* Need to prevent umount failure */
  896. else if (error)
  897. return error;
  898. mutex_lock(&q->qi_dqlist_lock);
  899. if (recl != q->qi_dqreclaims) {
  900. if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
  901. break;
  902. mutex_unlock(&q->qi_dqlist_lock);
  903. goto again;
  904. }
  905. }
  906. mutex_unlock(&q->qi_dqlist_lock);
  907. return 0;
  908. }
  909. /*
  910. * The hash chains and the mplist use the same xfs_dqhash structure as
  911. * their list head, but we can take the mplist qh_lock and one of the
  912. * hash qh_locks at the same time without any problem as they aren't
  913. * related.
  914. */
  915. static struct lock_class_key xfs_quota_mplist_class;
  916. /*
  917. * This initializes all the quota information that's kept in the
  918. * mount structure
  919. */
  920. STATIC int
  921. xfs_qm_init_quotainfo(
  922. xfs_mount_t *mp)
  923. {
  924. xfs_quotainfo_t *qinf;
  925. int error;
  926. xfs_dquot_t *dqp;
  927. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  928. /*
  929. * Tell XQM that we exist as soon as possible.
  930. */
  931. if ((error = xfs_qm_hold_quotafs_ref(mp))) {
  932. return error;
  933. }
  934. qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
  935. /*
  936. * See if quotainodes are setup, and if not, allocate them,
  937. * and change the superblock accordingly.
  938. */
  939. if ((error = xfs_qm_init_quotainos(mp))) {
  940. kmem_free(qinf);
  941. mp->m_quotainfo = NULL;
  942. return error;
  943. }
  944. INIT_LIST_HEAD(&qinf->qi_dqlist);
  945. mutex_init(&qinf->qi_dqlist_lock);
  946. lockdep_set_class(&qinf->qi_dqlist_lock, &xfs_quota_mplist_class);
  947. qinf->qi_dqreclaims = 0;
  948. /* mutex used to serialize quotaoffs */
  949. mutex_init(&qinf->qi_quotaofflock);
  950. /* Precalc some constants */
  951. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  952. ASSERT(qinf->qi_dqchunklen);
  953. qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
  954. do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
  955. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  956. /*
  957. * We try to get the limits from the superuser's limits fields.
  958. * This is quite hacky, but it is standard quota practice.
  959. * We look at the USR dquot with id == 0 first, but if user quotas
  960. * are not enabled we goto the GRP dquot with id == 0.
  961. * We don't really care to keep separate default limits for user
  962. * and group quotas, at least not at this point.
  963. */
  964. error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
  965. XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
  966. (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
  967. XFS_DQ_PROJ),
  968. XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
  969. &dqp);
  970. if (! error) {
  971. xfs_disk_dquot_t *ddqp = &dqp->q_core;
  972. /*
  973. * The warnings and timers set the grace period given to
  974. * a user or group before he or she can not perform any
  975. * more writing. If it is zero, a default is used.
  976. */
  977. qinf->qi_btimelimit = ddqp->d_btimer ?
  978. be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
  979. qinf->qi_itimelimit = ddqp->d_itimer ?
  980. be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
  981. qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
  982. be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
  983. qinf->qi_bwarnlimit = ddqp->d_bwarns ?
  984. be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
  985. qinf->qi_iwarnlimit = ddqp->d_iwarns ?
  986. be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
  987. qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
  988. be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
  989. qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
  990. qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
  991. qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
  992. qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
  993. qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
  994. qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
  995. /*
  996. * We sent the XFS_QMOPT_DQSUSER flag to dqget because
  997. * we don't want this dquot cached. We haven't done a
  998. * quotacheck yet, and quotacheck doesn't like incore dquots.
  999. */
  1000. xfs_qm_dqdestroy(dqp);
  1001. } else {
  1002. qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
  1003. qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
  1004. qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
  1005. qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
  1006. qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
  1007. qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
  1008. }
  1009. return 0;
  1010. }
  1011. /*
  1012. * Gets called when unmounting a filesystem or when all quotas get
  1013. * turned off.
  1014. * This purges the quota inodes, destroys locks and frees itself.
  1015. */
  1016. void
  1017. xfs_qm_destroy_quotainfo(
  1018. xfs_mount_t *mp)
  1019. {
  1020. xfs_quotainfo_t *qi;
  1021. qi = mp->m_quotainfo;
  1022. ASSERT(qi != NULL);
  1023. ASSERT(xfs_Gqm != NULL);
  1024. /*
  1025. * Release the reference that XQM kept, so that we know
  1026. * when the XQM structure should be freed. We cannot assume
  1027. * that xfs_Gqm is non-null after this point.
  1028. */
  1029. xfs_qm_rele_quotafs_ref(mp);
  1030. ASSERT(list_empty(&qi->qi_dqlist));
  1031. mutex_destroy(&qi->qi_dqlist_lock);
  1032. if (qi->qi_uquotaip) {
  1033. IRELE(qi->qi_uquotaip);
  1034. qi->qi_uquotaip = NULL; /* paranoia */
  1035. }
  1036. if (qi->qi_gquotaip) {
  1037. IRELE(qi->qi_gquotaip);
  1038. qi->qi_gquotaip = NULL;
  1039. }
  1040. mutex_destroy(&qi->qi_quotaofflock);
  1041. kmem_free(qi);
  1042. mp->m_quotainfo = NULL;
  1043. }
  1044. /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
  1045. /* ARGSUSED */
  1046. STATIC void
  1047. xfs_qm_list_init(
  1048. xfs_dqlist_t *list,
  1049. char *str,
  1050. int n)
  1051. {
  1052. mutex_init(&list->qh_lock);
  1053. INIT_LIST_HEAD(&list->qh_list);
  1054. list->qh_version = 0;
  1055. list->qh_nelems = 0;
  1056. }
  1057. STATIC void
  1058. xfs_qm_list_destroy(
  1059. xfs_dqlist_t *list)
  1060. {
  1061. mutex_destroy(&(list->qh_lock));
  1062. }
  1063. /*
  1064. * Create an inode and return with a reference already taken, but unlocked
  1065. * This is how we create quota inodes
  1066. */
  1067. STATIC int
  1068. xfs_qm_qino_alloc(
  1069. xfs_mount_t *mp,
  1070. xfs_inode_t **ip,
  1071. __int64_t sbfields,
  1072. uint flags)
  1073. {
  1074. xfs_trans_t *tp;
  1075. int error;
  1076. int committed;
  1077. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
  1078. if ((error = xfs_trans_reserve(tp,
  1079. XFS_QM_QINOCREATE_SPACE_RES(mp),
  1080. XFS_CREATE_LOG_RES(mp), 0,
  1081. XFS_TRANS_PERM_LOG_RES,
  1082. XFS_CREATE_LOG_COUNT))) {
  1083. xfs_trans_cancel(tp, 0);
  1084. return error;
  1085. }
  1086. error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, 1, ip, &committed);
  1087. if (error) {
  1088. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  1089. XFS_TRANS_ABORT);
  1090. return error;
  1091. }
  1092. /*
  1093. * Make the changes in the superblock, and log those too.
  1094. * sbfields arg may contain fields other than *QUOTINO;
  1095. * VERSIONNUM for example.
  1096. */
  1097. spin_lock(&mp->m_sb_lock);
  1098. if (flags & XFS_QMOPT_SBVERSION) {
  1099. ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
  1100. ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1101. XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
  1102. (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1103. XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
  1104. xfs_sb_version_addquota(&mp->m_sb);
  1105. mp->m_sb.sb_uquotino = NULLFSINO;
  1106. mp->m_sb.sb_gquotino = NULLFSINO;
  1107. /* qflags will get updated _after_ quotacheck */
  1108. mp->m_sb.sb_qflags = 0;
  1109. }
  1110. if (flags & XFS_QMOPT_UQUOTA)
  1111. mp->m_sb.sb_uquotino = (*ip)->i_ino;
  1112. else
  1113. mp->m_sb.sb_gquotino = (*ip)->i_ino;
  1114. spin_unlock(&mp->m_sb_lock);
  1115. xfs_mod_sb(tp, sbfields);
  1116. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
  1117. xfs_alert(mp, "%s failed (error %d)!", __func__, error);
  1118. return error;
  1119. }
  1120. return 0;
  1121. }
  1122. STATIC void
  1123. xfs_qm_reset_dqcounts(
  1124. xfs_mount_t *mp,
  1125. xfs_buf_t *bp,
  1126. xfs_dqid_t id,
  1127. uint type)
  1128. {
  1129. xfs_disk_dquot_t *ddq;
  1130. int j;
  1131. trace_xfs_reset_dqcounts(bp, _RET_IP_);
  1132. /*
  1133. * Reset all counters and timers. They'll be
  1134. * started afresh by xfs_qm_quotacheck.
  1135. */
  1136. #ifdef DEBUG
  1137. j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  1138. do_div(j, sizeof(xfs_dqblk_t));
  1139. ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
  1140. #endif
  1141. ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
  1142. for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
  1143. /*
  1144. * Do a sanity check, and if needed, repair the dqblk. Don't
  1145. * output any warnings because it's perfectly possible to
  1146. * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
  1147. */
  1148. (void) xfs_qm_dqcheck(mp, ddq, id+j, type, XFS_QMOPT_DQREPAIR,
  1149. "xfs_quotacheck");
  1150. ddq->d_bcount = 0;
  1151. ddq->d_icount = 0;
  1152. ddq->d_rtbcount = 0;
  1153. ddq->d_btimer = 0;
  1154. ddq->d_itimer = 0;
  1155. ddq->d_rtbtimer = 0;
  1156. ddq->d_bwarns = 0;
  1157. ddq->d_iwarns = 0;
  1158. ddq->d_rtbwarns = 0;
  1159. ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
  1160. }
  1161. }
  1162. STATIC int
  1163. xfs_qm_dqiter_bufs(
  1164. xfs_mount_t *mp,
  1165. xfs_dqid_t firstid,
  1166. xfs_fsblock_t bno,
  1167. xfs_filblks_t blkcnt,
  1168. uint flags)
  1169. {
  1170. xfs_buf_t *bp;
  1171. int error;
  1172. int notcommitted;
  1173. int incr;
  1174. int type;
  1175. ASSERT(blkcnt > 0);
  1176. notcommitted = 0;
  1177. incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
  1178. XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
  1179. type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
  1180. (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
  1181. error = 0;
  1182. /*
  1183. * Blkcnt arg can be a very big number, and might even be
  1184. * larger than the log itself. So, we have to break it up into
  1185. * manageable-sized transactions.
  1186. * Note that we don't start a permanent transaction here; we might
  1187. * not be able to get a log reservation for the whole thing up front,
  1188. * and we don't really care to either, because we just discard
  1189. * everything if we were to crash in the middle of this loop.
  1190. */
  1191. while (blkcnt--) {
  1192. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  1193. XFS_FSB_TO_DADDR(mp, bno),
  1194. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  1195. if (error)
  1196. break;
  1197. xfs_qm_reset_dqcounts(mp, bp, firstid, type);
  1198. xfs_bdwrite(mp, bp);
  1199. /*
  1200. * goto the next block.
  1201. */
  1202. bno++;
  1203. firstid += mp->m_quotainfo->qi_dqperchunk;
  1204. }
  1205. return error;
  1206. }
  1207. /*
  1208. * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
  1209. * caller supplied function for every chunk of dquots that we find.
  1210. */
  1211. STATIC int
  1212. xfs_qm_dqiterate(
  1213. xfs_mount_t *mp,
  1214. xfs_inode_t *qip,
  1215. uint flags)
  1216. {
  1217. xfs_bmbt_irec_t *map;
  1218. int i, nmaps; /* number of map entries */
  1219. int error; /* return value */
  1220. xfs_fileoff_t lblkno;
  1221. xfs_filblks_t maxlblkcnt;
  1222. xfs_dqid_t firstid;
  1223. xfs_fsblock_t rablkno;
  1224. xfs_filblks_t rablkcnt;
  1225. error = 0;
  1226. /*
  1227. * This looks racy, but we can't keep an inode lock across a
  1228. * trans_reserve. But, this gets called during quotacheck, and that
  1229. * happens only at mount time which is single threaded.
  1230. */
  1231. if (qip->i_d.di_nblocks == 0)
  1232. return 0;
  1233. map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
  1234. lblkno = 0;
  1235. maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1236. do {
  1237. nmaps = XFS_DQITER_MAP_SIZE;
  1238. /*
  1239. * We aren't changing the inode itself. Just changing
  1240. * some of its data. No new blocks are added here, and
  1241. * the inode is never added to the transaction.
  1242. */
  1243. xfs_ilock(qip, XFS_ILOCK_SHARED);
  1244. error = xfs_bmapi(NULL, qip, lblkno,
  1245. maxlblkcnt - lblkno,
  1246. XFS_BMAPI_METADATA,
  1247. NULL,
  1248. 0, map, &nmaps, NULL);
  1249. xfs_iunlock(qip, XFS_ILOCK_SHARED);
  1250. if (error)
  1251. break;
  1252. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  1253. for (i = 0; i < nmaps; i++) {
  1254. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  1255. ASSERT(map[i].br_blockcount);
  1256. lblkno += map[i].br_blockcount;
  1257. if (map[i].br_startblock == HOLESTARTBLOCK)
  1258. continue;
  1259. firstid = (xfs_dqid_t) map[i].br_startoff *
  1260. mp->m_quotainfo->qi_dqperchunk;
  1261. /*
  1262. * Do a read-ahead on the next extent.
  1263. */
  1264. if ((i+1 < nmaps) &&
  1265. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  1266. rablkcnt = map[i+1].br_blockcount;
  1267. rablkno = map[i+1].br_startblock;
  1268. while (rablkcnt--) {
  1269. xfs_buf_readahead(mp->m_ddev_targp,
  1270. XFS_FSB_TO_DADDR(mp, rablkno),
  1271. mp->m_quotainfo->qi_dqchunklen);
  1272. rablkno++;
  1273. }
  1274. }
  1275. /*
  1276. * Iterate thru all the blks in the extent and
  1277. * reset the counters of all the dquots inside them.
  1278. */
  1279. if ((error = xfs_qm_dqiter_bufs(mp,
  1280. firstid,
  1281. map[i].br_startblock,
  1282. map[i].br_blockcount,
  1283. flags))) {
  1284. break;
  1285. }
  1286. }
  1287. if (error)
  1288. break;
  1289. } while (nmaps > 0);
  1290. kmem_free(map);
  1291. return error;
  1292. }
  1293. /*
  1294. * Called by dqusage_adjust in doing a quotacheck.
  1295. *
  1296. * Given the inode, and a dquot id this updates both the incore dqout as well
  1297. * as the buffer copy. This is so that once the quotacheck is done, we can
  1298. * just log all the buffers, as opposed to logging numerous updates to
  1299. * individual dquots.
  1300. */
  1301. STATIC int
  1302. xfs_qm_quotacheck_dqadjust(
  1303. struct xfs_inode *ip,
  1304. xfs_dqid_t id,
  1305. uint type,
  1306. xfs_qcnt_t nblks,
  1307. xfs_qcnt_t rtblks)
  1308. {
  1309. struct xfs_mount *mp = ip->i_mount;
  1310. struct xfs_dquot *dqp;
  1311. int error;
  1312. error = xfs_qm_dqget(mp, ip, id, type,
  1313. XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN, &dqp);
  1314. if (error) {
  1315. /*
  1316. * Shouldn't be able to turn off quotas here.
  1317. */
  1318. ASSERT(error != ESRCH);
  1319. ASSERT(error != ENOENT);
  1320. return error;
  1321. }
  1322. trace_xfs_dqadjust(dqp);
  1323. /*
  1324. * Adjust the inode count and the block count to reflect this inode's
  1325. * resource usage.
  1326. */
  1327. be64_add_cpu(&dqp->q_core.d_icount, 1);
  1328. dqp->q_res_icount++;
  1329. if (nblks) {
  1330. be64_add_cpu(&dqp->q_core.d_bcount, nblks);
  1331. dqp->q_res_bcount += nblks;
  1332. }
  1333. if (rtblks) {
  1334. be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
  1335. dqp->q_res_rtbcount += rtblks;
  1336. }
  1337. /*
  1338. * Set default limits, adjust timers (since we changed usages)
  1339. *
  1340. * There are no timers for the default values set in the root dquot.
  1341. */
  1342. if (dqp->q_core.d_id) {
  1343. xfs_qm_adjust_dqlimits(mp, &dqp->q_core);
  1344. xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
  1345. }
  1346. dqp->dq_flags |= XFS_DQ_DIRTY;
  1347. xfs_qm_dqput(dqp);
  1348. return 0;
  1349. }
  1350. STATIC int
  1351. xfs_qm_get_rtblks(
  1352. xfs_inode_t *ip,
  1353. xfs_qcnt_t *O_rtblks)
  1354. {
  1355. xfs_filblks_t rtblks; /* total rt blks */
  1356. xfs_extnum_t idx; /* extent record index */
  1357. xfs_ifork_t *ifp; /* inode fork pointer */
  1358. xfs_extnum_t nextents; /* number of extent entries */
  1359. int error;
  1360. ASSERT(XFS_IS_REALTIME_INODE(ip));
  1361. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1362. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1363. if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
  1364. return error;
  1365. }
  1366. rtblks = 0;
  1367. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1368. for (idx = 0; idx < nextents; idx++)
  1369. rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
  1370. *O_rtblks = (xfs_qcnt_t)rtblks;
  1371. return 0;
  1372. }
  1373. /*
  1374. * callback routine supplied to bulkstat(). Given an inumber, find its
  1375. * dquots and update them to account for resources taken by that inode.
  1376. */
  1377. /* ARGSUSED */
  1378. STATIC int
  1379. xfs_qm_dqusage_adjust(
  1380. xfs_mount_t *mp, /* mount point for filesystem */
  1381. xfs_ino_t ino, /* inode number to get data for */
  1382. void __user *buffer, /* not used */
  1383. int ubsize, /* not used */
  1384. int *ubused, /* not used */
  1385. int *res) /* result code value */
  1386. {
  1387. xfs_inode_t *ip;
  1388. xfs_qcnt_t nblks, rtblks = 0;
  1389. int error;
  1390. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1391. /*
  1392. * rootino must have its resources accounted for, not so with the quota
  1393. * inodes.
  1394. */
  1395. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1396. *res = BULKSTAT_RV_NOTHING;
  1397. return XFS_ERROR(EINVAL);
  1398. }
  1399. /*
  1400. * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
  1401. * interface expects the inode to be exclusively locked because that's
  1402. * the case in all other instances. It's OK that we do this because
  1403. * quotacheck is done only at mount time.
  1404. */
  1405. error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip);
  1406. if (error) {
  1407. *res = BULKSTAT_RV_NOTHING;
  1408. return error;
  1409. }
  1410. ASSERT(ip->i_delayed_blks == 0);
  1411. if (XFS_IS_REALTIME_INODE(ip)) {
  1412. /*
  1413. * Walk thru the extent list and count the realtime blocks.
  1414. */
  1415. error = xfs_qm_get_rtblks(ip, &rtblks);
  1416. if (error)
  1417. goto error0;
  1418. }
  1419. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
  1420. /*
  1421. * Add the (disk blocks and inode) resources occupied by this
  1422. * inode to its dquots. We do this adjustment in the incore dquot,
  1423. * and also copy the changes to its buffer.
  1424. * We don't care about putting these changes in a transaction
  1425. * envelope because if we crash in the middle of a 'quotacheck'
  1426. * we have to start from the beginning anyway.
  1427. * Once we're done, we'll log all the dquot bufs.
  1428. *
  1429. * The *QUOTA_ON checks below may look pretty racy, but quotachecks
  1430. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1431. */
  1432. if (XFS_IS_UQUOTA_ON(mp)) {
  1433. error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_uid,
  1434. XFS_DQ_USER, nblks, rtblks);
  1435. if (error)
  1436. goto error0;
  1437. }
  1438. if (XFS_IS_GQUOTA_ON(mp)) {
  1439. error = xfs_qm_quotacheck_dqadjust(ip, ip->i_d.di_gid,
  1440. XFS_DQ_GROUP, nblks, rtblks);
  1441. if (error)
  1442. goto error0;
  1443. }
  1444. if (XFS_IS_PQUOTA_ON(mp)) {
  1445. error = xfs_qm_quotacheck_dqadjust(ip, xfs_get_projid(ip),
  1446. XFS_DQ_PROJ, nblks, rtblks);
  1447. if (error)
  1448. goto error0;
  1449. }
  1450. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1451. IRELE(ip);
  1452. *res = BULKSTAT_RV_DIDONE;
  1453. return 0;
  1454. error0:
  1455. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1456. IRELE(ip);
  1457. *res = BULKSTAT_RV_GIVEUP;
  1458. return error;
  1459. }
  1460. /*
  1461. * Walk thru all the filesystem inodes and construct a consistent view
  1462. * of the disk quota world. If the quotacheck fails, disable quotas.
  1463. */
  1464. int
  1465. xfs_qm_quotacheck(
  1466. xfs_mount_t *mp)
  1467. {
  1468. int done, count, error;
  1469. xfs_ino_t lastino;
  1470. size_t structsz;
  1471. xfs_inode_t *uip, *gip;
  1472. uint flags;
  1473. count = INT_MAX;
  1474. structsz = 1;
  1475. lastino = 0;
  1476. flags = 0;
  1477. ASSERT(mp->m_quotainfo->qi_uquotaip || mp->m_quotainfo->qi_gquotaip);
  1478. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1479. /*
  1480. * There should be no cached dquots. The (simplistic) quotacheck
  1481. * algorithm doesn't like that.
  1482. */
  1483. ASSERT(list_empty(&mp->m_quotainfo->qi_dqlist));
  1484. xfs_notice(mp, "Quotacheck needed: Please wait.");
  1485. /*
  1486. * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
  1487. * their counters to zero. We need a clean slate.
  1488. * We don't log our changes till later.
  1489. */
  1490. uip = mp->m_quotainfo->qi_uquotaip;
  1491. if (uip) {
  1492. error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA);
  1493. if (error)
  1494. goto error_return;
  1495. flags |= XFS_UQUOTA_CHKD;
  1496. }
  1497. gip = mp->m_quotainfo->qi_gquotaip;
  1498. if (gip) {
  1499. error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
  1500. XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
  1501. if (error)
  1502. goto error_return;
  1503. flags |= XFS_OQUOTA_CHKD;
  1504. }
  1505. do {
  1506. /*
  1507. * Iterate thru all the inodes in the file system,
  1508. * adjusting the corresponding dquot counters in core.
  1509. */
  1510. error = xfs_bulkstat(mp, &lastino, &count,
  1511. xfs_qm_dqusage_adjust,
  1512. structsz, NULL, &done);
  1513. if (error)
  1514. break;
  1515. } while (!done);
  1516. /*
  1517. * We've made all the changes that we need to make incore.
  1518. * Flush them down to disk buffers if everything was updated
  1519. * successfully.
  1520. */
  1521. if (!error)
  1522. error = xfs_qm_dqflush_all(mp, 0);
  1523. /*
  1524. * We can get this error if we couldn't do a dquot allocation inside
  1525. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1526. * dirty dquots that might be cached, we just want to get rid of them
  1527. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1528. * at this point (because we intentionally didn't in dqget_noattach).
  1529. */
  1530. if (error) {
  1531. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
  1532. goto error_return;
  1533. }
  1534. /*
  1535. * We didn't log anything, because if we crashed, we'll have to
  1536. * start the quotacheck from scratch anyway. However, we must make
  1537. * sure that our dquot changes are secure before we put the
  1538. * quotacheck'd stamp on the superblock. So, here we do a synchronous
  1539. * flush.
  1540. */
  1541. XFS_bflush(mp->m_ddev_targp);
  1542. /*
  1543. * If one type of quotas is off, then it will lose its
  1544. * quotachecked status, since we won't be doing accounting for
  1545. * that type anymore.
  1546. */
  1547. mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
  1548. mp->m_qflags |= flags;
  1549. xfs_qm_dquot_list_print(mp);
  1550. error_return:
  1551. if (error) {
  1552. xfs_warn(mp,
  1553. "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
  1554. error);
  1555. /*
  1556. * We must turn off quotas.
  1557. */
  1558. ASSERT(mp->m_quotainfo != NULL);
  1559. ASSERT(xfs_Gqm != NULL);
  1560. xfs_qm_destroy_quotainfo(mp);
  1561. if (xfs_mount_reset_sbqflags(mp)) {
  1562. xfs_warn(mp,
  1563. "Quotacheck: Failed to reset quota flags.");
  1564. }
  1565. } else
  1566. xfs_notice(mp, "Quotacheck: Done.");
  1567. return (error);
  1568. }
  1569. /*
  1570. * This is called after the superblock has been read in and we're ready to
  1571. * iget the quota inodes.
  1572. */
  1573. STATIC int
  1574. xfs_qm_init_quotainos(
  1575. xfs_mount_t *mp)
  1576. {
  1577. xfs_inode_t *uip, *gip;
  1578. int error;
  1579. __int64_t sbflags;
  1580. uint flags;
  1581. ASSERT(mp->m_quotainfo);
  1582. uip = gip = NULL;
  1583. sbflags = 0;
  1584. flags = 0;
  1585. /*
  1586. * Get the uquota and gquota inodes
  1587. */
  1588. if (xfs_sb_version_hasquota(&mp->m_sb)) {
  1589. if (XFS_IS_UQUOTA_ON(mp) &&
  1590. mp->m_sb.sb_uquotino != NULLFSINO) {
  1591. ASSERT(mp->m_sb.sb_uquotino > 0);
  1592. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  1593. 0, 0, &uip)))
  1594. return XFS_ERROR(error);
  1595. }
  1596. if (XFS_IS_OQUOTA_ON(mp) &&
  1597. mp->m_sb.sb_gquotino != NULLFSINO) {
  1598. ASSERT(mp->m_sb.sb_gquotino > 0);
  1599. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  1600. 0, 0, &gip))) {
  1601. if (uip)
  1602. IRELE(uip);
  1603. return XFS_ERROR(error);
  1604. }
  1605. }
  1606. } else {
  1607. flags |= XFS_QMOPT_SBVERSION;
  1608. sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1609. XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
  1610. }
  1611. /*
  1612. * Create the two inodes, if they don't exist already. The changes
  1613. * made above will get added to a transaction and logged in one of
  1614. * the qino_alloc calls below. If the device is readonly,
  1615. * temporarily switch to read-write to do this.
  1616. */
  1617. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1618. if ((error = xfs_qm_qino_alloc(mp, &uip,
  1619. sbflags | XFS_SB_UQUOTINO,
  1620. flags | XFS_QMOPT_UQUOTA)))
  1621. return XFS_ERROR(error);
  1622. flags &= ~XFS_QMOPT_SBVERSION;
  1623. }
  1624. if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
  1625. flags |= (XFS_IS_GQUOTA_ON(mp) ?
  1626. XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
  1627. error = xfs_qm_qino_alloc(mp, &gip,
  1628. sbflags | XFS_SB_GQUOTINO, flags);
  1629. if (error) {
  1630. if (uip)
  1631. IRELE(uip);
  1632. return XFS_ERROR(error);
  1633. }
  1634. }
  1635. mp->m_quotainfo->qi_uquotaip = uip;
  1636. mp->m_quotainfo->qi_gquotaip = gip;
  1637. return 0;
  1638. }
  1639. /*
  1640. * Just pop the least recently used dquot off the freelist and
  1641. * recycle it. The returned dquot is locked.
  1642. */
  1643. STATIC xfs_dquot_t *
  1644. xfs_qm_dqreclaim_one(void)
  1645. {
  1646. xfs_dquot_t *dqpout;
  1647. xfs_dquot_t *dqp;
  1648. int restarts;
  1649. int startagain;
  1650. restarts = 0;
  1651. dqpout = NULL;
  1652. /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
  1653. again:
  1654. startagain = 0;
  1655. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  1656. list_for_each_entry(dqp, &xfs_Gqm->qm_dqfrlist, q_freelist) {
  1657. struct xfs_mount *mp = dqp->q_mount;
  1658. xfs_dqlock(dqp);
  1659. /*
  1660. * We are racing with dqlookup here. Naturally we don't
  1661. * want to reclaim a dquot that lookup wants. We release the
  1662. * freelist lock and start over, so that lookup will grab
  1663. * both the dquot and the freelistlock.
  1664. */
  1665. if (dqp->dq_flags & XFS_DQ_WANT) {
  1666. ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
  1667. trace_xfs_dqreclaim_want(dqp);
  1668. XQM_STATS_INC(xqmstats.xs_qm_dqwants);
  1669. restarts++;
  1670. startagain = 1;
  1671. goto dqunlock;
  1672. }
  1673. /*
  1674. * If the dquot is inactive, we are assured that it is
  1675. * not on the mplist or the hashlist, and that makes our
  1676. * life easier.
  1677. */
  1678. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  1679. ASSERT(mp == NULL);
  1680. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  1681. ASSERT(list_empty(&dqp->q_hashlist));
  1682. ASSERT(list_empty(&dqp->q_mplist));
  1683. list_del_init(&dqp->q_freelist);
  1684. xfs_Gqm->qm_dqfrlist_cnt--;
  1685. dqpout = dqp;
  1686. XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
  1687. goto dqunlock;
  1688. }
  1689. ASSERT(dqp->q_hash);
  1690. ASSERT(!list_empty(&dqp->q_mplist));
  1691. /*
  1692. * Try to grab the flush lock. If this dquot is in the process
  1693. * of getting flushed to disk, we don't want to reclaim it.
  1694. */
  1695. if (!xfs_dqflock_nowait(dqp))
  1696. goto dqunlock;
  1697. /*
  1698. * We have the flush lock so we know that this is not in the
  1699. * process of being flushed. So, if this is dirty, flush it
  1700. * DELWRI so that we don't get a freelist infested with
  1701. * dirty dquots.
  1702. */
  1703. if (XFS_DQ_IS_DIRTY(dqp)) {
  1704. int error;
  1705. trace_xfs_dqreclaim_dirty(dqp);
  1706. /*
  1707. * We flush it delayed write, so don't bother
  1708. * releasing the freelist lock.
  1709. */
  1710. error = xfs_qm_dqflush(dqp, 0);
  1711. if (error) {
  1712. xfs_warn(mp, "%s: dquot %p flush failed",
  1713. __func__, dqp);
  1714. }
  1715. goto dqunlock;
  1716. }
  1717. /*
  1718. * We're trying to get the hashlock out of order. This races
  1719. * with dqlookup; so, we giveup and goto the next dquot if
  1720. * we couldn't get the hashlock. This way, we won't starve
  1721. * a dqlookup process that holds the hashlock that is
  1722. * waiting for the freelist lock.
  1723. */
  1724. if (!mutex_trylock(&dqp->q_hash->qh_lock)) {
  1725. restarts++;
  1726. goto dqfunlock;
  1727. }
  1728. /*
  1729. * This races with dquot allocation code as well as dqflush_all
  1730. * and reclaim code. So, if we failed to grab the mplist lock,
  1731. * giveup everything and start over.
  1732. */
  1733. if (!mutex_trylock(&mp->m_quotainfo->qi_dqlist_lock)) {
  1734. restarts++;
  1735. startagain = 1;
  1736. goto qhunlock;
  1737. }
  1738. ASSERT(dqp->q_nrefs == 0);
  1739. list_del_init(&dqp->q_mplist);
  1740. mp->m_quotainfo->qi_dquots--;
  1741. mp->m_quotainfo->qi_dqreclaims++;
  1742. list_del_init(&dqp->q_hashlist);
  1743. dqp->q_hash->qh_version++;
  1744. list_del_init(&dqp->q_freelist);
  1745. xfs_Gqm->qm_dqfrlist_cnt--;
  1746. dqpout = dqp;
  1747. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  1748. qhunlock:
  1749. mutex_unlock(&dqp->q_hash->qh_lock);
  1750. dqfunlock:
  1751. xfs_dqfunlock(dqp);
  1752. dqunlock:
  1753. xfs_dqunlock(dqp);
  1754. if (dqpout)
  1755. break;
  1756. if (restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1757. break;
  1758. if (startagain) {
  1759. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  1760. goto again;
  1761. }
  1762. }
  1763. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  1764. return dqpout;
  1765. }
  1766. /*
  1767. * Traverse the freelist of dquots and attempt to reclaim a maximum of
  1768. * 'howmany' dquots. This operation races with dqlookup(), and attempts to
  1769. * favor the lookup function ...
  1770. */
  1771. STATIC int
  1772. xfs_qm_shake_freelist(
  1773. int howmany)
  1774. {
  1775. int nreclaimed = 0;
  1776. xfs_dquot_t *dqp;
  1777. if (howmany <= 0)
  1778. return 0;
  1779. while (nreclaimed < howmany) {
  1780. dqp = xfs_qm_dqreclaim_one();
  1781. if (!dqp)
  1782. return nreclaimed;
  1783. xfs_qm_dqdestroy(dqp);
  1784. nreclaimed++;
  1785. }
  1786. return nreclaimed;
  1787. }
  1788. /*
  1789. * The kmem_shake interface is invoked when memory is running low.
  1790. */
  1791. /* ARGSUSED */
  1792. STATIC int
  1793. xfs_qm_shake(
  1794. struct shrinker *shrink,
  1795. int nr_to_scan,
  1796. gfp_t gfp_mask)
  1797. {
  1798. int ndqused, nfree, n;
  1799. if (!kmem_shake_allow(gfp_mask))
  1800. return 0;
  1801. if (!xfs_Gqm)
  1802. return 0;
  1803. nfree = xfs_Gqm->qm_dqfrlist_cnt; /* free dquots */
  1804. /* incore dquots in all f/s's */
  1805. ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
  1806. ASSERT(ndqused >= 0);
  1807. if (nfree <= ndqused && nfree < ndquot)
  1808. return 0;
  1809. ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
  1810. n = nfree - ndqused - ndquot; /* # over target */
  1811. return xfs_qm_shake_freelist(MAX(nfree, n));
  1812. }
  1813. /*------------------------------------------------------------------*/
  1814. /*
  1815. * Return a new incore dquot. Depending on the number of
  1816. * dquots in the system, we either allocate a new one on the kernel heap,
  1817. * or reclaim a free one.
  1818. * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
  1819. * to reclaim an existing one from the freelist.
  1820. */
  1821. boolean_t
  1822. xfs_qm_dqalloc_incore(
  1823. xfs_dquot_t **O_dqpp)
  1824. {
  1825. xfs_dquot_t *dqp;
  1826. /*
  1827. * Check against high water mark to see if we want to pop
  1828. * a nincompoop dquot off the freelist.
  1829. */
  1830. if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
  1831. /*
  1832. * Try to recycle a dquot from the freelist.
  1833. */
  1834. if ((dqp = xfs_qm_dqreclaim_one())) {
  1835. XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
  1836. /*
  1837. * Just zero the core here. The rest will get
  1838. * reinitialized by caller. XXX we shouldn't even
  1839. * do this zero ...
  1840. */
  1841. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  1842. *O_dqpp = dqp;
  1843. return B_FALSE;
  1844. }
  1845. XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
  1846. }
  1847. /*
  1848. * Allocate a brand new dquot on the kernel heap and return it
  1849. * to the caller to initialize.
  1850. */
  1851. ASSERT(xfs_Gqm->qm_dqzone != NULL);
  1852. *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
  1853. atomic_inc(&xfs_Gqm->qm_totaldquots);
  1854. return B_TRUE;
  1855. }
  1856. /*
  1857. * Start a transaction and write the incore superblock changes to
  1858. * disk. flags parameter indicates which fields have changed.
  1859. */
  1860. int
  1861. xfs_qm_write_sb_changes(
  1862. xfs_mount_t *mp,
  1863. __int64_t flags)
  1864. {
  1865. xfs_trans_t *tp;
  1866. int error;
  1867. #ifdef QUOTADEBUG
  1868. xfs_notice(mp, "Writing superblock quota changes");
  1869. #endif
  1870. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  1871. if ((error = xfs_trans_reserve(tp, 0,
  1872. mp->m_sb.sb_sectsize + 128, 0,
  1873. 0,
  1874. XFS_DEFAULT_LOG_COUNT))) {
  1875. xfs_trans_cancel(tp, 0);
  1876. return error;
  1877. }
  1878. xfs_mod_sb(tp, flags);
  1879. error = xfs_trans_commit(tp, 0);
  1880. return error;
  1881. }
  1882. /* --------------- utility functions for vnodeops ---------------- */
  1883. /*
  1884. * Given an inode, a uid, gid and prid make sure that we have
  1885. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  1886. * quotas by creating this file.
  1887. * This also attaches dquot(s) to the given inode after locking it,
  1888. * and returns the dquots corresponding to the uid and/or gid.
  1889. *
  1890. * in : inode (unlocked)
  1891. * out : udquot, gdquot with references taken and unlocked
  1892. */
  1893. int
  1894. xfs_qm_vop_dqalloc(
  1895. struct xfs_inode *ip,
  1896. uid_t uid,
  1897. gid_t gid,
  1898. prid_t prid,
  1899. uint flags,
  1900. struct xfs_dquot **O_udqpp,
  1901. struct xfs_dquot **O_gdqpp)
  1902. {
  1903. struct xfs_mount *mp = ip->i_mount;
  1904. struct xfs_dquot *uq, *gq;
  1905. int error;
  1906. uint lockflags;
  1907. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1908. return 0;
  1909. lockflags = XFS_ILOCK_EXCL;
  1910. xfs_ilock(ip, lockflags);
  1911. if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
  1912. gid = ip->i_d.di_gid;
  1913. /*
  1914. * Attach the dquot(s) to this inode, doing a dquot allocation
  1915. * if necessary. The dquot(s) will not be locked.
  1916. */
  1917. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1918. error = xfs_qm_dqattach_locked(ip, XFS_QMOPT_DQALLOC);
  1919. if (error) {
  1920. xfs_iunlock(ip, lockflags);
  1921. return error;
  1922. }
  1923. }
  1924. uq = gq = NULL;
  1925. if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
  1926. if (ip->i_d.di_uid != uid) {
  1927. /*
  1928. * What we need is the dquot that has this uid, and
  1929. * if we send the inode to dqget, the uid of the inode
  1930. * takes priority over what's sent in the uid argument.
  1931. * We must unlock inode here before calling dqget if
  1932. * we're not sending the inode, because otherwise
  1933. * we'll deadlock by doing trans_reserve while
  1934. * holding ilock.
  1935. */
  1936. xfs_iunlock(ip, lockflags);
  1937. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
  1938. XFS_DQ_USER,
  1939. XFS_QMOPT_DQALLOC |
  1940. XFS_QMOPT_DOWARN,
  1941. &uq))) {
  1942. ASSERT(error != ENOENT);
  1943. return error;
  1944. }
  1945. /*
  1946. * Get the ilock in the right order.
  1947. */
  1948. xfs_dqunlock(uq);
  1949. lockflags = XFS_ILOCK_SHARED;
  1950. xfs_ilock(ip, lockflags);
  1951. } else {
  1952. /*
  1953. * Take an extra reference, because we'll return
  1954. * this to caller
  1955. */
  1956. ASSERT(ip->i_udquot);
  1957. uq = ip->i_udquot;
  1958. xfs_dqlock(uq);
  1959. XFS_DQHOLD(uq);
  1960. xfs_dqunlock(uq);
  1961. }
  1962. }
  1963. if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
  1964. if (ip->i_d.di_gid != gid) {
  1965. xfs_iunlock(ip, lockflags);
  1966. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
  1967. XFS_DQ_GROUP,
  1968. XFS_QMOPT_DQALLOC |
  1969. XFS_QMOPT_DOWARN,
  1970. &gq))) {
  1971. if (uq)
  1972. xfs_qm_dqrele(uq);
  1973. ASSERT(error != ENOENT);
  1974. return error;
  1975. }
  1976. xfs_dqunlock(gq);
  1977. lockflags = XFS_ILOCK_SHARED;
  1978. xfs_ilock(ip, lockflags);
  1979. } else {
  1980. ASSERT(ip->i_gdquot);
  1981. gq = ip->i_gdquot;
  1982. xfs_dqlock(gq);
  1983. XFS_DQHOLD(gq);
  1984. xfs_dqunlock(gq);
  1985. }
  1986. } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
  1987. if (xfs_get_projid(ip) != prid) {
  1988. xfs_iunlock(ip, lockflags);
  1989. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
  1990. XFS_DQ_PROJ,
  1991. XFS_QMOPT_DQALLOC |
  1992. XFS_QMOPT_DOWARN,
  1993. &gq))) {
  1994. if (uq)
  1995. xfs_qm_dqrele(uq);
  1996. ASSERT(error != ENOENT);
  1997. return (error);
  1998. }
  1999. xfs_dqunlock(gq);
  2000. lockflags = XFS_ILOCK_SHARED;
  2001. xfs_ilock(ip, lockflags);
  2002. } else {
  2003. ASSERT(ip->i_gdquot);
  2004. gq = ip->i_gdquot;
  2005. xfs_dqlock(gq);
  2006. XFS_DQHOLD(gq);
  2007. xfs_dqunlock(gq);
  2008. }
  2009. }
  2010. if (uq)
  2011. trace_xfs_dquot_dqalloc(ip);
  2012. xfs_iunlock(ip, lockflags);
  2013. if (O_udqpp)
  2014. *O_udqpp = uq;
  2015. else if (uq)
  2016. xfs_qm_dqrele(uq);
  2017. if (O_gdqpp)
  2018. *O_gdqpp = gq;
  2019. else if (gq)
  2020. xfs_qm_dqrele(gq);
  2021. return 0;
  2022. }
  2023. /*
  2024. * Actually transfer ownership, and do dquot modifications.
  2025. * These were already reserved.
  2026. */
  2027. xfs_dquot_t *
  2028. xfs_qm_vop_chown(
  2029. xfs_trans_t *tp,
  2030. xfs_inode_t *ip,
  2031. xfs_dquot_t **IO_olddq,
  2032. xfs_dquot_t *newdq)
  2033. {
  2034. xfs_dquot_t *prevdq;
  2035. uint bfield = XFS_IS_REALTIME_INODE(ip) ?
  2036. XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
  2037. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2038. ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
  2039. /* old dquot */
  2040. prevdq = *IO_olddq;
  2041. ASSERT(prevdq);
  2042. ASSERT(prevdq != newdq);
  2043. xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
  2044. xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
  2045. /* the sparkling new dquot */
  2046. xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
  2047. xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
  2048. /*
  2049. * Take an extra reference, because the inode
  2050. * is going to keep this dquot pointer even
  2051. * after the trans_commit.
  2052. */
  2053. xfs_dqlock(newdq);
  2054. XFS_DQHOLD(newdq);
  2055. xfs_dqunlock(newdq);
  2056. *IO_olddq = newdq;
  2057. return prevdq;
  2058. }
  2059. /*
  2060. * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
  2061. */
  2062. int
  2063. xfs_qm_vop_chown_reserve(
  2064. xfs_trans_t *tp,
  2065. xfs_inode_t *ip,
  2066. xfs_dquot_t *udqp,
  2067. xfs_dquot_t *gdqp,
  2068. uint flags)
  2069. {
  2070. xfs_mount_t *mp = ip->i_mount;
  2071. uint delblks, blkflags, prjflags = 0;
  2072. xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
  2073. int error;
  2074. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2075. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  2076. delblks = ip->i_delayed_blks;
  2077. delblksudq = delblksgdq = unresudq = unresgdq = NULL;
  2078. blkflags = XFS_IS_REALTIME_INODE(ip) ?
  2079. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
  2080. if (XFS_IS_UQUOTA_ON(mp) && udqp &&
  2081. ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
  2082. delblksudq = udqp;
  2083. /*
  2084. * If there are delayed allocation blocks, then we have to
  2085. * unreserve those from the old dquot, and add them to the
  2086. * new dquot.
  2087. */
  2088. if (delblks) {
  2089. ASSERT(ip->i_udquot);
  2090. unresudq = ip->i_udquot;
  2091. }
  2092. }
  2093. if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
  2094. if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
  2095. xfs_get_projid(ip) != be32_to_cpu(gdqp->q_core.d_id))
  2096. prjflags = XFS_QMOPT_ENOSPC;
  2097. if (prjflags ||
  2098. (XFS_IS_GQUOTA_ON(ip->i_mount) &&
  2099. ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
  2100. delblksgdq = gdqp;
  2101. if (delblks) {
  2102. ASSERT(ip->i_gdquot);
  2103. unresgdq = ip->i_gdquot;
  2104. }
  2105. }
  2106. }
  2107. if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
  2108. delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
  2109. flags | blkflags | prjflags)))
  2110. return (error);
  2111. /*
  2112. * Do the delayed blks reservations/unreservations now. Since, these
  2113. * are done without the help of a transaction, if a reservation fails
  2114. * its previous reservations won't be automatically undone by trans
  2115. * code. So, we have to do it manually here.
  2116. */
  2117. if (delblks) {
  2118. /*
  2119. * Do the reservations first. Unreservation can't fail.
  2120. */
  2121. ASSERT(delblksudq || delblksgdq);
  2122. ASSERT(unresudq || unresgdq);
  2123. if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2124. delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
  2125. flags | blkflags | prjflags)))
  2126. return (error);
  2127. xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2128. unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
  2129. blkflags);
  2130. }
  2131. return (0);
  2132. }
  2133. int
  2134. xfs_qm_vop_rename_dqattach(
  2135. struct xfs_inode **i_tab)
  2136. {
  2137. struct xfs_mount *mp = i_tab[0]->i_mount;
  2138. int i;
  2139. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  2140. return 0;
  2141. for (i = 0; (i < 4 && i_tab[i]); i++) {
  2142. struct xfs_inode *ip = i_tab[i];
  2143. int error;
  2144. /*
  2145. * Watch out for duplicate entries in the table.
  2146. */
  2147. if (i == 0 || ip != i_tab[i-1]) {
  2148. if (XFS_NOT_DQATTACHED(mp, ip)) {
  2149. error = xfs_qm_dqattach(ip, 0);
  2150. if (error)
  2151. return error;
  2152. }
  2153. }
  2154. }
  2155. return 0;
  2156. }
  2157. void
  2158. xfs_qm_vop_create_dqattach(
  2159. struct xfs_trans *tp,
  2160. struct xfs_inode *ip,
  2161. struct xfs_dquot *udqp,
  2162. struct xfs_dquot *gdqp)
  2163. {
  2164. struct xfs_mount *mp = tp->t_mountp;
  2165. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  2166. return;
  2167. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2168. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  2169. if (udqp) {
  2170. xfs_dqlock(udqp);
  2171. XFS_DQHOLD(udqp);
  2172. xfs_dqunlock(udqp);
  2173. ASSERT(ip->i_udquot == NULL);
  2174. ip->i_udquot = udqp;
  2175. ASSERT(XFS_IS_UQUOTA_ON(mp));
  2176. ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
  2177. xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
  2178. }
  2179. if (gdqp) {
  2180. xfs_dqlock(gdqp);
  2181. XFS_DQHOLD(gdqp);
  2182. xfs_dqunlock(gdqp);
  2183. ASSERT(ip->i_gdquot == NULL);
  2184. ip->i_gdquot = gdqp;
  2185. ASSERT(XFS_IS_OQUOTA_ON(mp));
  2186. ASSERT((XFS_IS_GQUOTA_ON(mp) ?
  2187. ip->i_d.di_gid : xfs_get_projid(ip)) ==
  2188. be32_to_cpu(gdqp->q_core.d_id));
  2189. xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
  2190. }
  2191. }