xfs_qm.c 48 KB

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