xfs_qm.c 47 KB

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