xfs_qm.c 46 KB

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