xfs_qm.c 50 KB

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