xfs_qm.c 51 KB

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