xfs_qm.c 50 KB

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