xfs_qm.c 50 KB

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