xfs_qm.c 63 KB

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