xfs_qm.c 47 KB

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