xfs_qm.c 46 KB

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