xfs_qm.c 51 KB

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