xfs_qm.c 46 KB

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