xfs_qm.c 47 KB

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