xfs_qm.c 47 KB

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