xfs_qm.c 62 KB

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