quota.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Quota change tags are associated with each transaction that allocates or
  11. * deallocates space. Those changes are accumulated locally to each node (in a
  12. * per-node file) and then are periodically synced to the quota file. This
  13. * avoids the bottleneck of constantly touching the quota file, but introduces
  14. * fuzziness in the current usage value of IDs that are being used on different
  15. * nodes in the cluster simultaneously. So, it is possible for a user on
  16. * multiple nodes to overrun their quota, but that overrun is controlable.
  17. * Since quota tags are part of transactions, there is no need for a quota check
  18. * program to be run on node crashes or anything like that.
  19. *
  20. * There are couple of knobs that let the administrator manage the quota
  21. * fuzziness. "quota_quantum" sets the maximum time a quota change can be
  22. * sitting on one node before being synced to the quota file. (The default is
  23. * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency
  24. * of quota file syncs increases as the user moves closer to their limit. The
  25. * more frequent the syncs, the more accurate the quota enforcement, but that
  26. * means that there is more contention between the nodes for the quota file.
  27. * The default value is one. This sets the maximum theoretical quota overrun
  28. * (with infinite node with infinite bandwidth) to twice the user's limit. (In
  29. * practice, the maximum overrun you see should be much less.) A "quota_scale"
  30. * number greater than one makes quota syncs more frequent and reduces the
  31. * maximum overrun. Numbers less than one (but greater than zero) make quota
  32. * syncs less frequent.
  33. *
  34. * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of
  35. * the quota file, so it is not being constantly read.
  36. */
  37. #include <linux/sched.h>
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/completion.h>
  41. #include <linux/buffer_head.h>
  42. #include <linux/sort.h>
  43. #include <linux/fs.h>
  44. #include <linux/bio.h>
  45. #include <linux/gfs2_ondisk.h>
  46. #include <linux/kthread.h>
  47. #include <linux/freezer.h>
  48. #include <linux/quota.h>
  49. #include <linux/dqblk_xfs.h>
  50. #include "gfs2.h"
  51. #include "incore.h"
  52. #include "bmap.h"
  53. #include "glock.h"
  54. #include "glops.h"
  55. #include "log.h"
  56. #include "meta_io.h"
  57. #include "quota.h"
  58. #include "rgrp.h"
  59. #include "super.h"
  60. #include "trans.h"
  61. #include "inode.h"
  62. #include "util.h"
  63. #define QUOTA_USER 1
  64. #define QUOTA_GROUP 0
  65. struct gfs2_quota_change_host {
  66. u64 qc_change;
  67. u32 qc_flags; /* GFS2_QCF_... */
  68. u32 qc_id;
  69. };
  70. static LIST_HEAD(qd_lru_list);
  71. static atomic_t qd_lru_count = ATOMIC_INIT(0);
  72. static DEFINE_SPINLOCK(qd_lru_lock);
  73. int gfs2_shrink_qd_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  74. {
  75. struct gfs2_quota_data *qd;
  76. struct gfs2_sbd *sdp;
  77. if (nr == 0)
  78. goto out;
  79. if (!(gfp_mask & __GFP_FS))
  80. return -1;
  81. spin_lock(&qd_lru_lock);
  82. while (nr && !list_empty(&qd_lru_list)) {
  83. qd = list_entry(qd_lru_list.next,
  84. struct gfs2_quota_data, qd_reclaim);
  85. sdp = qd->qd_gl->gl_sbd;
  86. /* Free from the filesystem-specific list */
  87. list_del(&qd->qd_list);
  88. gfs2_assert_warn(sdp, !qd->qd_change);
  89. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  90. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  91. gfs2_glock_put(qd->qd_gl);
  92. atomic_dec(&sdp->sd_quota_count);
  93. /* Delete it from the common reclaim list */
  94. list_del_init(&qd->qd_reclaim);
  95. atomic_dec(&qd_lru_count);
  96. spin_unlock(&qd_lru_lock);
  97. kmem_cache_free(gfs2_quotad_cachep, qd);
  98. spin_lock(&qd_lru_lock);
  99. nr--;
  100. }
  101. spin_unlock(&qd_lru_lock);
  102. out:
  103. return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
  104. }
  105. static u64 qd2offset(struct gfs2_quota_data *qd)
  106. {
  107. u64 offset;
  108. offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
  109. offset *= sizeof(struct gfs2_quota);
  110. return offset;
  111. }
  112. static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
  113. struct gfs2_quota_data **qdp)
  114. {
  115. struct gfs2_quota_data *qd;
  116. int error;
  117. qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
  118. if (!qd)
  119. return -ENOMEM;
  120. atomic_set(&qd->qd_count, 1);
  121. qd->qd_id = id;
  122. if (user)
  123. set_bit(QDF_USER, &qd->qd_flags);
  124. qd->qd_slot = -1;
  125. INIT_LIST_HEAD(&qd->qd_reclaim);
  126. error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
  127. &gfs2_quota_glops, CREATE, &qd->qd_gl);
  128. if (error)
  129. goto fail;
  130. *qdp = qd;
  131. return 0;
  132. fail:
  133. kmem_cache_free(gfs2_quotad_cachep, qd);
  134. return error;
  135. }
  136. static int qd_get(struct gfs2_sbd *sdp, int user, u32 id,
  137. struct gfs2_quota_data **qdp)
  138. {
  139. struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
  140. int error, found;
  141. *qdp = NULL;
  142. for (;;) {
  143. found = 0;
  144. spin_lock(&qd_lru_lock);
  145. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  146. if (qd->qd_id == id &&
  147. !test_bit(QDF_USER, &qd->qd_flags) == !user) {
  148. if (!atomic_read(&qd->qd_count) &&
  149. !list_empty(&qd->qd_reclaim)) {
  150. /* Remove it from reclaim list */
  151. list_del_init(&qd->qd_reclaim);
  152. atomic_dec(&qd_lru_count);
  153. }
  154. atomic_inc(&qd->qd_count);
  155. found = 1;
  156. break;
  157. }
  158. }
  159. if (!found)
  160. qd = NULL;
  161. if (!qd && new_qd) {
  162. qd = new_qd;
  163. list_add(&qd->qd_list, &sdp->sd_quota_list);
  164. atomic_inc(&sdp->sd_quota_count);
  165. new_qd = NULL;
  166. }
  167. spin_unlock(&qd_lru_lock);
  168. if (qd) {
  169. if (new_qd) {
  170. gfs2_glock_put(new_qd->qd_gl);
  171. kmem_cache_free(gfs2_quotad_cachep, new_qd);
  172. }
  173. *qdp = qd;
  174. return 0;
  175. }
  176. error = qd_alloc(sdp, user, id, &new_qd);
  177. if (error)
  178. return error;
  179. }
  180. }
  181. static void qd_hold(struct gfs2_quota_data *qd)
  182. {
  183. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  184. gfs2_assert(sdp, atomic_read(&qd->qd_count));
  185. atomic_inc(&qd->qd_count);
  186. }
  187. static void qd_put(struct gfs2_quota_data *qd)
  188. {
  189. if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
  190. /* Add to the reclaim list */
  191. list_add_tail(&qd->qd_reclaim, &qd_lru_list);
  192. atomic_inc(&qd_lru_count);
  193. spin_unlock(&qd_lru_lock);
  194. }
  195. }
  196. static int slot_get(struct gfs2_quota_data *qd)
  197. {
  198. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  199. unsigned int c, o = 0, b;
  200. unsigned char byte = 0;
  201. spin_lock(&qd_lru_lock);
  202. if (qd->qd_slot_count++) {
  203. spin_unlock(&qd_lru_lock);
  204. return 0;
  205. }
  206. for (c = 0; c < sdp->sd_quota_chunks; c++)
  207. for (o = 0; o < PAGE_SIZE; o++) {
  208. byte = sdp->sd_quota_bitmap[c][o];
  209. if (byte != 0xFF)
  210. goto found;
  211. }
  212. goto fail;
  213. found:
  214. for (b = 0; b < 8; b++)
  215. if (!(byte & (1 << b)))
  216. break;
  217. qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
  218. if (qd->qd_slot >= sdp->sd_quota_slots)
  219. goto fail;
  220. sdp->sd_quota_bitmap[c][o] |= 1 << b;
  221. spin_unlock(&qd_lru_lock);
  222. return 0;
  223. fail:
  224. qd->qd_slot_count--;
  225. spin_unlock(&qd_lru_lock);
  226. return -ENOSPC;
  227. }
  228. static void slot_hold(struct gfs2_quota_data *qd)
  229. {
  230. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  231. spin_lock(&qd_lru_lock);
  232. gfs2_assert(sdp, qd->qd_slot_count);
  233. qd->qd_slot_count++;
  234. spin_unlock(&qd_lru_lock);
  235. }
  236. static void slot_put(struct gfs2_quota_data *qd)
  237. {
  238. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  239. spin_lock(&qd_lru_lock);
  240. gfs2_assert(sdp, qd->qd_slot_count);
  241. if (!--qd->qd_slot_count) {
  242. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
  243. qd->qd_slot = -1;
  244. }
  245. spin_unlock(&qd_lru_lock);
  246. }
  247. static int bh_get(struct gfs2_quota_data *qd)
  248. {
  249. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  250. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  251. unsigned int block, offset;
  252. struct buffer_head *bh;
  253. int error;
  254. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  255. mutex_lock(&sdp->sd_quota_mutex);
  256. if (qd->qd_bh_count++) {
  257. mutex_unlock(&sdp->sd_quota_mutex);
  258. return 0;
  259. }
  260. block = qd->qd_slot / sdp->sd_qc_per_block;
  261. offset = qd->qd_slot % sdp->sd_qc_per_block;
  262. bh_map.b_size = 1 << ip->i_inode.i_blkbits;
  263. error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
  264. if (error)
  265. goto fail;
  266. error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
  267. if (error)
  268. goto fail;
  269. error = -EIO;
  270. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  271. goto fail_brelse;
  272. qd->qd_bh = bh;
  273. qd->qd_bh_qc = (struct gfs2_quota_change *)
  274. (bh->b_data + sizeof(struct gfs2_meta_header) +
  275. offset * sizeof(struct gfs2_quota_change));
  276. mutex_unlock(&sdp->sd_quota_mutex);
  277. return 0;
  278. fail_brelse:
  279. brelse(bh);
  280. fail:
  281. qd->qd_bh_count--;
  282. mutex_unlock(&sdp->sd_quota_mutex);
  283. return error;
  284. }
  285. static void bh_put(struct gfs2_quota_data *qd)
  286. {
  287. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  288. mutex_lock(&sdp->sd_quota_mutex);
  289. gfs2_assert(sdp, qd->qd_bh_count);
  290. if (!--qd->qd_bh_count) {
  291. brelse(qd->qd_bh);
  292. qd->qd_bh = NULL;
  293. qd->qd_bh_qc = NULL;
  294. }
  295. mutex_unlock(&sdp->sd_quota_mutex);
  296. }
  297. static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
  298. {
  299. struct gfs2_quota_data *qd = NULL;
  300. int error;
  301. int found = 0;
  302. *qdp = NULL;
  303. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  304. return 0;
  305. spin_lock(&qd_lru_lock);
  306. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  307. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  308. !test_bit(QDF_CHANGE, &qd->qd_flags) ||
  309. qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
  310. continue;
  311. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  312. set_bit(QDF_LOCKED, &qd->qd_flags);
  313. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  314. atomic_inc(&qd->qd_count);
  315. qd->qd_change_sync = qd->qd_change;
  316. gfs2_assert_warn(sdp, qd->qd_slot_count);
  317. qd->qd_slot_count++;
  318. found = 1;
  319. break;
  320. }
  321. if (!found)
  322. qd = NULL;
  323. spin_unlock(&qd_lru_lock);
  324. if (qd) {
  325. gfs2_assert_warn(sdp, qd->qd_change_sync);
  326. error = bh_get(qd);
  327. if (error) {
  328. clear_bit(QDF_LOCKED, &qd->qd_flags);
  329. slot_put(qd);
  330. qd_put(qd);
  331. return error;
  332. }
  333. }
  334. *qdp = qd;
  335. return 0;
  336. }
  337. static int qd_trylock(struct gfs2_quota_data *qd)
  338. {
  339. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  340. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  341. return 0;
  342. spin_lock(&qd_lru_lock);
  343. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  344. !test_bit(QDF_CHANGE, &qd->qd_flags)) {
  345. spin_unlock(&qd_lru_lock);
  346. return 0;
  347. }
  348. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  349. set_bit(QDF_LOCKED, &qd->qd_flags);
  350. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  351. atomic_inc(&qd->qd_count);
  352. qd->qd_change_sync = qd->qd_change;
  353. gfs2_assert_warn(sdp, qd->qd_slot_count);
  354. qd->qd_slot_count++;
  355. spin_unlock(&qd_lru_lock);
  356. gfs2_assert_warn(sdp, qd->qd_change_sync);
  357. if (bh_get(qd)) {
  358. clear_bit(QDF_LOCKED, &qd->qd_flags);
  359. slot_put(qd);
  360. qd_put(qd);
  361. return 0;
  362. }
  363. return 1;
  364. }
  365. static void qd_unlock(struct gfs2_quota_data *qd)
  366. {
  367. gfs2_assert_warn(qd->qd_gl->gl_sbd,
  368. test_bit(QDF_LOCKED, &qd->qd_flags));
  369. clear_bit(QDF_LOCKED, &qd->qd_flags);
  370. bh_put(qd);
  371. slot_put(qd);
  372. qd_put(qd);
  373. }
  374. static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id,
  375. struct gfs2_quota_data **qdp)
  376. {
  377. int error;
  378. error = qd_get(sdp, user, id, qdp);
  379. if (error)
  380. return error;
  381. error = slot_get(*qdp);
  382. if (error)
  383. goto fail;
  384. error = bh_get(*qdp);
  385. if (error)
  386. goto fail_slot;
  387. return 0;
  388. fail_slot:
  389. slot_put(*qdp);
  390. fail:
  391. qd_put(*qdp);
  392. return error;
  393. }
  394. static void qdsb_put(struct gfs2_quota_data *qd)
  395. {
  396. bh_put(qd);
  397. slot_put(qd);
  398. qd_put(qd);
  399. }
  400. int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
  401. {
  402. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  403. struct gfs2_alloc *al = ip->i_alloc;
  404. struct gfs2_quota_data **qd = al->al_qd;
  405. int error;
  406. if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
  407. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
  408. return -EIO;
  409. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  410. return 0;
  411. error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, qd);
  412. if (error)
  413. goto out;
  414. al->al_qd_num++;
  415. qd++;
  416. error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, qd);
  417. if (error)
  418. goto out;
  419. al->al_qd_num++;
  420. qd++;
  421. if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
  422. error = qdsb_get(sdp, QUOTA_USER, uid, qd);
  423. if (error)
  424. goto out;
  425. al->al_qd_num++;
  426. qd++;
  427. }
  428. if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
  429. error = qdsb_get(sdp, QUOTA_GROUP, gid, qd);
  430. if (error)
  431. goto out;
  432. al->al_qd_num++;
  433. qd++;
  434. }
  435. out:
  436. if (error)
  437. gfs2_quota_unhold(ip);
  438. return error;
  439. }
  440. void gfs2_quota_unhold(struct gfs2_inode *ip)
  441. {
  442. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  443. struct gfs2_alloc *al = ip->i_alloc;
  444. unsigned int x;
  445. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  446. for (x = 0; x < al->al_qd_num; x++) {
  447. qdsb_put(al->al_qd[x]);
  448. al->al_qd[x] = NULL;
  449. }
  450. al->al_qd_num = 0;
  451. }
  452. static int sort_qd(const void *a, const void *b)
  453. {
  454. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  455. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  456. if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
  457. !test_bit(QDF_USER, &qd_b->qd_flags)) {
  458. if (test_bit(QDF_USER, &qd_a->qd_flags))
  459. return -1;
  460. else
  461. return 1;
  462. }
  463. if (qd_a->qd_id < qd_b->qd_id)
  464. return -1;
  465. if (qd_a->qd_id > qd_b->qd_id)
  466. return 1;
  467. return 0;
  468. }
  469. static void do_qc(struct gfs2_quota_data *qd, s64 change)
  470. {
  471. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  472. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  473. struct gfs2_quota_change *qc = qd->qd_bh_qc;
  474. s64 x;
  475. mutex_lock(&sdp->sd_quota_mutex);
  476. gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
  477. if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
  478. qc->qc_change = 0;
  479. qc->qc_flags = 0;
  480. if (test_bit(QDF_USER, &qd->qd_flags))
  481. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  482. qc->qc_id = cpu_to_be32(qd->qd_id);
  483. }
  484. x = be64_to_cpu(qc->qc_change) + change;
  485. qc->qc_change = cpu_to_be64(x);
  486. spin_lock(&qd_lru_lock);
  487. qd->qd_change = x;
  488. spin_unlock(&qd_lru_lock);
  489. if (!x) {
  490. gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
  491. clear_bit(QDF_CHANGE, &qd->qd_flags);
  492. qc->qc_flags = 0;
  493. qc->qc_id = 0;
  494. slot_put(qd);
  495. qd_put(qd);
  496. } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
  497. qd_hold(qd);
  498. slot_hold(qd);
  499. }
  500. mutex_unlock(&sdp->sd_quota_mutex);
  501. }
  502. /**
  503. * gfs2_adjust_quota - adjust record of current block usage
  504. * @ip: The quota inode
  505. * @loc: Offset of the entry in the quota file
  506. * @change: The amount of usage change to record
  507. * @qd: The quota data
  508. * @fdq: The updated limits to record
  509. *
  510. * This function was mostly borrowed from gfs2_block_truncate_page which was
  511. * in turn mostly borrowed from ext3
  512. *
  513. * Returns: 0 or -ve on error
  514. */
  515. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  516. s64 change, struct gfs2_quota_data *qd,
  517. struct fs_disk_quota *fdq)
  518. {
  519. struct inode *inode = &ip->i_inode;
  520. struct gfs2_sbd *sdp = GFS2_SB(inode);
  521. struct address_space *mapping = inode->i_mapping;
  522. unsigned long index = loc >> PAGE_CACHE_SHIFT;
  523. unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
  524. unsigned blocksize, iblock, pos;
  525. struct buffer_head *bh, *dibh;
  526. struct page *page;
  527. void *kaddr, *ptr;
  528. struct gfs2_quota q, *qp;
  529. int err, nbytes;
  530. u64 size;
  531. if (gfs2_is_stuffed(ip))
  532. gfs2_unstuff_dinode(ip, NULL);
  533. memset(&q, 0, sizeof(struct gfs2_quota));
  534. err = gfs2_internal_read(ip, NULL, (char *)&q, &loc, sizeof(q));
  535. if (err < 0)
  536. return err;
  537. err = -EIO;
  538. qp = &q;
  539. qp->qu_value = be64_to_cpu(qp->qu_value);
  540. qp->qu_value += change;
  541. qp->qu_value = cpu_to_be64(qp->qu_value);
  542. qd->qd_qb.qb_value = qp->qu_value;
  543. if (fdq) {
  544. if (fdq->d_fieldmask & FS_DQ_BSOFT) {
  545. qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
  546. qd->qd_qb.qb_warn = qp->qu_warn;
  547. }
  548. if (fdq->d_fieldmask & FS_DQ_BHARD) {
  549. qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
  550. qd->qd_qb.qb_limit = qp->qu_limit;
  551. }
  552. if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
  553. qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
  554. qd->qd_qb.qb_value = qp->qu_value;
  555. }
  556. }
  557. /* Write the quota into the quota file on disk */
  558. ptr = qp;
  559. nbytes = sizeof(struct gfs2_quota);
  560. get_a_page:
  561. page = grab_cache_page(mapping, index);
  562. if (!page)
  563. return -ENOMEM;
  564. blocksize = inode->i_sb->s_blocksize;
  565. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  566. if (!page_has_buffers(page))
  567. create_empty_buffers(page, blocksize, 0);
  568. bh = page_buffers(page);
  569. pos = blocksize;
  570. while (offset >= pos) {
  571. bh = bh->b_this_page;
  572. iblock++;
  573. pos += blocksize;
  574. }
  575. if (!buffer_mapped(bh)) {
  576. gfs2_block_map(inode, iblock, bh, 1);
  577. if (!buffer_mapped(bh))
  578. goto unlock_out;
  579. /* If it's a newly allocated disk block for quota, zero it */
  580. if (buffer_new(bh))
  581. zero_user(page, pos - blocksize, bh->b_size);
  582. }
  583. if (PageUptodate(page))
  584. set_buffer_uptodate(bh);
  585. if (!buffer_uptodate(bh)) {
  586. ll_rw_block(READ_META, 1, &bh);
  587. wait_on_buffer(bh);
  588. if (!buffer_uptodate(bh))
  589. goto unlock_out;
  590. }
  591. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  592. kaddr = kmap_atomic(page, KM_USER0);
  593. if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
  594. nbytes = PAGE_CACHE_SIZE - offset;
  595. memcpy(kaddr + offset, ptr, nbytes);
  596. flush_dcache_page(page);
  597. kunmap_atomic(kaddr, KM_USER0);
  598. unlock_page(page);
  599. page_cache_release(page);
  600. /* If quota straddles page boundary, we need to update the rest of the
  601. * quota at the beginning of the next page */
  602. if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
  603. ptr = ptr + nbytes;
  604. nbytes = sizeof(struct gfs2_quota) - nbytes;
  605. offset = 0;
  606. index++;
  607. goto get_a_page;
  608. }
  609. /* Update the disk inode timestamp and size (if extended) */
  610. err = gfs2_meta_inode_buffer(ip, &dibh);
  611. if (err)
  612. goto out;
  613. size = loc + sizeof(struct gfs2_quota);
  614. if (size > inode->i_size)
  615. i_size_write(inode, size);
  616. inode->i_mtime = inode->i_atime = CURRENT_TIME;
  617. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  618. gfs2_dinode_out(ip, dibh->b_data);
  619. brelse(dibh);
  620. mark_inode_dirty(inode);
  621. out:
  622. return err;
  623. unlock_out:
  624. unlock_page(page);
  625. page_cache_release(page);
  626. return err;
  627. }
  628. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  629. {
  630. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  631. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  632. unsigned int data_blocks, ind_blocks;
  633. struct gfs2_holder *ghs, i_gh;
  634. unsigned int qx, x;
  635. struct gfs2_quota_data *qd;
  636. loff_t offset;
  637. unsigned int nalloc = 0, blocks;
  638. struct gfs2_alloc *al = NULL;
  639. int error;
  640. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  641. &data_blocks, &ind_blocks);
  642. ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
  643. if (!ghs)
  644. return -ENOMEM;
  645. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  646. mutex_lock_nested(&ip->i_inode.i_mutex, I_MUTEX_QUOTA);
  647. for (qx = 0; qx < num_qd; qx++) {
  648. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  649. GL_NOCACHE, &ghs[qx]);
  650. if (error)
  651. goto out;
  652. }
  653. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  654. if (error)
  655. goto out;
  656. for (x = 0; x < num_qd; x++) {
  657. offset = qd2offset(qda[x]);
  658. if (gfs2_write_alloc_required(ip, offset,
  659. sizeof(struct gfs2_quota)))
  660. nalloc++;
  661. }
  662. al = gfs2_alloc_get(ip);
  663. if (!al) {
  664. error = -ENOMEM;
  665. goto out_gunlock;
  666. }
  667. /*
  668. * 1 blk for unstuffing inode if stuffed. We add this extra
  669. * block to the reservation unconditionally. If the inode
  670. * doesn't need unstuffing, the block will be released to the
  671. * rgrp since it won't be allocated during the transaction
  672. */
  673. al->al_requested = 1;
  674. /* +3 in the end for unstuffing block, inode size update block
  675. * and another block in case quota straddles page boundary and
  676. * two blocks need to be updated instead of 1 */
  677. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  678. if (nalloc)
  679. al->al_requested += nalloc * (data_blocks + ind_blocks);
  680. error = gfs2_inplace_reserve(ip);
  681. if (error)
  682. goto out_alloc;
  683. if (nalloc)
  684. blocks += gfs2_rg_blocks(al) + nalloc * ind_blocks + RES_STATFS;
  685. error = gfs2_trans_begin(sdp, blocks, 0);
  686. if (error)
  687. goto out_ipres;
  688. for (x = 0; x < num_qd; x++) {
  689. qd = qda[x];
  690. offset = qd2offset(qd);
  691. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
  692. if (error)
  693. goto out_end_trans;
  694. do_qc(qd, -qd->qd_change_sync);
  695. }
  696. error = 0;
  697. out_end_trans:
  698. gfs2_trans_end(sdp);
  699. out_ipres:
  700. gfs2_inplace_release(ip);
  701. out_alloc:
  702. gfs2_alloc_put(ip);
  703. out_gunlock:
  704. gfs2_glock_dq_uninit(&i_gh);
  705. out:
  706. while (qx--)
  707. gfs2_glock_dq_uninit(&ghs[qx]);
  708. mutex_unlock(&ip->i_inode.i_mutex);
  709. kfree(ghs);
  710. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  711. return error;
  712. }
  713. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  714. {
  715. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  716. struct gfs2_quota q;
  717. struct gfs2_quota_lvb *qlvb;
  718. loff_t pos;
  719. int error;
  720. memset(&q, 0, sizeof(struct gfs2_quota));
  721. pos = qd2offset(qd);
  722. error = gfs2_internal_read(ip, NULL, (char *)&q, &pos, sizeof(q));
  723. if (error < 0)
  724. return error;
  725. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  726. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  727. qlvb->__pad = 0;
  728. qlvb->qb_limit = q.qu_limit;
  729. qlvb->qb_warn = q.qu_warn;
  730. qlvb->qb_value = q.qu_value;
  731. qd->qd_qb = *qlvb;
  732. return 0;
  733. }
  734. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  735. struct gfs2_holder *q_gh)
  736. {
  737. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  738. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  739. struct gfs2_holder i_gh;
  740. int error;
  741. restart:
  742. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  743. if (error)
  744. return error;
  745. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  746. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  747. gfs2_glock_dq_uninit(q_gh);
  748. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  749. GL_NOCACHE, q_gh);
  750. if (error)
  751. return error;
  752. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  753. if (error)
  754. goto fail;
  755. error = update_qd(sdp, qd);
  756. if (error)
  757. goto fail_gunlock;
  758. gfs2_glock_dq_uninit(&i_gh);
  759. gfs2_glock_dq_uninit(q_gh);
  760. force_refresh = 0;
  761. goto restart;
  762. }
  763. return 0;
  764. fail_gunlock:
  765. gfs2_glock_dq_uninit(&i_gh);
  766. fail:
  767. gfs2_glock_dq_uninit(q_gh);
  768. return error;
  769. }
  770. int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
  771. {
  772. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  773. struct gfs2_alloc *al = ip->i_alloc;
  774. unsigned int x;
  775. int error = 0;
  776. gfs2_quota_hold(ip, uid, gid);
  777. if (capable(CAP_SYS_RESOURCE) ||
  778. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  779. return 0;
  780. sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
  781. sort_qd, NULL);
  782. for (x = 0; x < al->al_qd_num; x++) {
  783. error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
  784. if (error)
  785. break;
  786. }
  787. if (!error)
  788. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  789. else {
  790. while (x--)
  791. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  792. gfs2_quota_unhold(ip);
  793. }
  794. return error;
  795. }
  796. static int need_sync(struct gfs2_quota_data *qd)
  797. {
  798. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  799. struct gfs2_tune *gt = &sdp->sd_tune;
  800. s64 value;
  801. unsigned int num, den;
  802. int do_sync = 1;
  803. if (!qd->qd_qb.qb_limit)
  804. return 0;
  805. spin_lock(&qd_lru_lock);
  806. value = qd->qd_change;
  807. spin_unlock(&qd_lru_lock);
  808. spin_lock(&gt->gt_spin);
  809. num = gt->gt_quota_scale_num;
  810. den = gt->gt_quota_scale_den;
  811. spin_unlock(&gt->gt_spin);
  812. if (value < 0)
  813. do_sync = 0;
  814. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  815. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  816. do_sync = 0;
  817. else {
  818. value *= gfs2_jindex_size(sdp) * num;
  819. value = div_s64(value, den);
  820. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  821. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  822. do_sync = 0;
  823. }
  824. return do_sync;
  825. }
  826. void gfs2_quota_unlock(struct gfs2_inode *ip)
  827. {
  828. struct gfs2_alloc *al = ip->i_alloc;
  829. struct gfs2_quota_data *qda[4];
  830. unsigned int count = 0;
  831. unsigned int x;
  832. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  833. goto out;
  834. for (x = 0; x < al->al_qd_num; x++) {
  835. struct gfs2_quota_data *qd;
  836. int sync;
  837. qd = al->al_qd[x];
  838. sync = need_sync(qd);
  839. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  840. if (sync && qd_trylock(qd))
  841. qda[count++] = qd;
  842. }
  843. if (count) {
  844. do_sync(count, qda);
  845. for (x = 0; x < count; x++)
  846. qd_unlock(qda[x]);
  847. }
  848. out:
  849. gfs2_quota_unhold(ip);
  850. }
  851. #define MAX_LINE 256
  852. static int print_message(struct gfs2_quota_data *qd, char *type)
  853. {
  854. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  855. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
  856. sdp->sd_fsname, type,
  857. (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
  858. qd->qd_id);
  859. return 0;
  860. }
  861. int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
  862. {
  863. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  864. struct gfs2_alloc *al = ip->i_alloc;
  865. struct gfs2_quota_data *qd;
  866. s64 value;
  867. unsigned int x;
  868. int error = 0;
  869. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  870. return 0;
  871. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  872. return 0;
  873. for (x = 0; x < al->al_qd_num; x++) {
  874. qd = al->al_qd[x];
  875. if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  876. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
  877. continue;
  878. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  879. spin_lock(&qd_lru_lock);
  880. value += qd->qd_change;
  881. spin_unlock(&qd_lru_lock);
  882. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  883. print_message(qd, "exceeded");
  884. quota_send_warning(test_bit(QDF_USER, &qd->qd_flags) ?
  885. USRQUOTA : GRPQUOTA, qd->qd_id,
  886. sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
  887. error = -EDQUOT;
  888. break;
  889. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  890. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  891. time_after_eq(jiffies, qd->qd_last_warn +
  892. gfs2_tune_get(sdp,
  893. gt_quota_warn_period) * HZ)) {
  894. quota_send_warning(test_bit(QDF_USER, &qd->qd_flags) ?
  895. USRQUOTA : GRPQUOTA, qd->qd_id,
  896. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  897. error = print_message(qd, "warning");
  898. qd->qd_last_warn = jiffies;
  899. }
  900. }
  901. return error;
  902. }
  903. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  904. u32 uid, u32 gid)
  905. {
  906. struct gfs2_alloc *al = ip->i_alloc;
  907. struct gfs2_quota_data *qd;
  908. unsigned int x;
  909. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  910. return;
  911. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  912. return;
  913. for (x = 0; x < al->al_qd_num; x++) {
  914. qd = al->al_qd[x];
  915. if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  916. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
  917. do_qc(qd, change);
  918. }
  919. }
  920. }
  921. int gfs2_quota_sync(struct super_block *sb, int type, int wait)
  922. {
  923. struct gfs2_sbd *sdp = sb->s_fs_info;
  924. struct gfs2_quota_data **qda;
  925. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  926. unsigned int num_qd;
  927. unsigned int x;
  928. int error = 0;
  929. sdp->sd_quota_sync_gen++;
  930. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  931. if (!qda)
  932. return -ENOMEM;
  933. do {
  934. num_qd = 0;
  935. for (;;) {
  936. error = qd_fish(sdp, qda + num_qd);
  937. if (error || !qda[num_qd])
  938. break;
  939. if (++num_qd == max_qd)
  940. break;
  941. }
  942. if (num_qd) {
  943. if (!error)
  944. error = do_sync(num_qd, qda);
  945. if (!error)
  946. for (x = 0; x < num_qd; x++)
  947. qda[x]->qd_sync_gen =
  948. sdp->sd_quota_sync_gen;
  949. for (x = 0; x < num_qd; x++)
  950. qd_unlock(qda[x]);
  951. }
  952. } while (!error && num_qd == max_qd);
  953. kfree(qda);
  954. return error;
  955. }
  956. static int gfs2_quota_sync_timeo(struct super_block *sb, int type)
  957. {
  958. return gfs2_quota_sync(sb, type, 0);
  959. }
  960. int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
  961. {
  962. struct gfs2_quota_data *qd;
  963. struct gfs2_holder q_gh;
  964. int error;
  965. error = qd_get(sdp, user, id, &qd);
  966. if (error)
  967. return error;
  968. error = do_glock(qd, FORCE, &q_gh);
  969. if (!error)
  970. gfs2_glock_dq_uninit(&q_gh);
  971. qd_put(qd);
  972. return error;
  973. }
  974. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  975. {
  976. const struct gfs2_quota_change *str = buf;
  977. qc->qc_change = be64_to_cpu(str->qc_change);
  978. qc->qc_flags = be32_to_cpu(str->qc_flags);
  979. qc->qc_id = be32_to_cpu(str->qc_id);
  980. }
  981. int gfs2_quota_init(struct gfs2_sbd *sdp)
  982. {
  983. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  984. u64 size = i_size_read(sdp->sd_qc_inode);
  985. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  986. unsigned int x, slot = 0;
  987. unsigned int found = 0;
  988. u64 dblock;
  989. u32 extlen = 0;
  990. int error;
  991. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  992. return -EIO;
  993. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  994. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  995. error = -ENOMEM;
  996. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  997. sizeof(unsigned char *), GFP_NOFS);
  998. if (!sdp->sd_quota_bitmap)
  999. return error;
  1000. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  1001. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  1002. if (!sdp->sd_quota_bitmap[x])
  1003. goto fail;
  1004. }
  1005. for (x = 0; x < blocks; x++) {
  1006. struct buffer_head *bh;
  1007. unsigned int y;
  1008. if (!extlen) {
  1009. int new = 0;
  1010. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1011. if (error)
  1012. goto fail;
  1013. }
  1014. error = -EIO;
  1015. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1016. if (!bh)
  1017. goto fail;
  1018. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1019. brelse(bh);
  1020. goto fail;
  1021. }
  1022. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1023. y++, slot++) {
  1024. struct gfs2_quota_change_host qc;
  1025. struct gfs2_quota_data *qd;
  1026. gfs2_quota_change_in(&qc, bh->b_data +
  1027. sizeof(struct gfs2_meta_header) +
  1028. y * sizeof(struct gfs2_quota_change));
  1029. if (!qc.qc_change)
  1030. continue;
  1031. error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
  1032. qc.qc_id, &qd);
  1033. if (error) {
  1034. brelse(bh);
  1035. goto fail;
  1036. }
  1037. set_bit(QDF_CHANGE, &qd->qd_flags);
  1038. qd->qd_change = qc.qc_change;
  1039. qd->qd_slot = slot;
  1040. qd->qd_slot_count = 1;
  1041. spin_lock(&qd_lru_lock);
  1042. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  1043. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1044. atomic_inc(&sdp->sd_quota_count);
  1045. spin_unlock(&qd_lru_lock);
  1046. found++;
  1047. }
  1048. brelse(bh);
  1049. dblock++;
  1050. extlen--;
  1051. }
  1052. if (found)
  1053. fs_info(sdp, "found %u quota changes\n", found);
  1054. return 0;
  1055. fail:
  1056. gfs2_quota_cleanup(sdp);
  1057. return error;
  1058. }
  1059. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1060. {
  1061. struct list_head *head = &sdp->sd_quota_list;
  1062. struct gfs2_quota_data *qd;
  1063. unsigned int x;
  1064. spin_lock(&qd_lru_lock);
  1065. while (!list_empty(head)) {
  1066. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1067. if (atomic_read(&qd->qd_count) > 1 ||
  1068. (atomic_read(&qd->qd_count) &&
  1069. !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1070. list_move(&qd->qd_list, head);
  1071. spin_unlock(&qd_lru_lock);
  1072. schedule();
  1073. spin_lock(&qd_lru_lock);
  1074. continue;
  1075. }
  1076. list_del(&qd->qd_list);
  1077. /* Also remove if this qd exists in the reclaim list */
  1078. if (!list_empty(&qd->qd_reclaim)) {
  1079. list_del_init(&qd->qd_reclaim);
  1080. atomic_dec(&qd_lru_count);
  1081. }
  1082. atomic_dec(&sdp->sd_quota_count);
  1083. spin_unlock(&qd_lru_lock);
  1084. if (!atomic_read(&qd->qd_count)) {
  1085. gfs2_assert_warn(sdp, !qd->qd_change);
  1086. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1087. } else
  1088. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1089. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1090. gfs2_glock_put(qd->qd_gl);
  1091. kmem_cache_free(gfs2_quotad_cachep, qd);
  1092. spin_lock(&qd_lru_lock);
  1093. }
  1094. spin_unlock(&qd_lru_lock);
  1095. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1096. if (sdp->sd_quota_bitmap) {
  1097. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1098. kfree(sdp->sd_quota_bitmap[x]);
  1099. kfree(sdp->sd_quota_bitmap);
  1100. }
  1101. }
  1102. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1103. {
  1104. if (error == 0 || error == -EROFS)
  1105. return;
  1106. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1107. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1108. }
  1109. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1110. int (*fxn)(struct super_block *sb, int type),
  1111. unsigned long t, unsigned long *timeo,
  1112. unsigned int *new_timeo)
  1113. {
  1114. if (t >= *timeo) {
  1115. int error = fxn(sdp->sd_vfs, 0);
  1116. quotad_error(sdp, msg, error);
  1117. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1118. } else {
  1119. *timeo -= t;
  1120. }
  1121. }
  1122. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1123. {
  1124. struct gfs2_inode *ip;
  1125. while(1) {
  1126. ip = NULL;
  1127. spin_lock(&sdp->sd_trunc_lock);
  1128. if (!list_empty(&sdp->sd_trunc_list)) {
  1129. ip = list_entry(sdp->sd_trunc_list.next,
  1130. struct gfs2_inode, i_trunc_list);
  1131. list_del_init(&ip->i_trunc_list);
  1132. }
  1133. spin_unlock(&sdp->sd_trunc_lock);
  1134. if (ip == NULL)
  1135. return;
  1136. gfs2_glock_finish_truncate(ip);
  1137. }
  1138. }
  1139. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1140. if (!sdp->sd_statfs_force_sync) {
  1141. sdp->sd_statfs_force_sync = 1;
  1142. wake_up(&sdp->sd_quota_wait);
  1143. }
  1144. }
  1145. /**
  1146. * gfs2_quotad - Write cached quota changes into the quota file
  1147. * @sdp: Pointer to GFS2 superblock
  1148. *
  1149. */
  1150. int gfs2_quotad(void *data)
  1151. {
  1152. struct gfs2_sbd *sdp = data;
  1153. struct gfs2_tune *tune = &sdp->sd_tune;
  1154. unsigned long statfs_timeo = 0;
  1155. unsigned long quotad_timeo = 0;
  1156. unsigned long t = 0;
  1157. DEFINE_WAIT(wait);
  1158. int empty;
  1159. while (!kthread_should_stop()) {
  1160. /* Update the master statfs file */
  1161. if (sdp->sd_statfs_force_sync) {
  1162. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1163. quotad_error(sdp, "statfs", error);
  1164. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1165. }
  1166. else
  1167. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1168. &statfs_timeo,
  1169. &tune->gt_statfs_quantum);
  1170. /* Update quota file */
  1171. quotad_check_timeo(sdp, "sync", gfs2_quota_sync_timeo, t,
  1172. &quotad_timeo, &tune->gt_quota_quantum);
  1173. /* Check for & recover partially truncated inodes */
  1174. quotad_check_trunc_list(sdp);
  1175. if (freezing(current))
  1176. refrigerator();
  1177. t = min(quotad_timeo, statfs_timeo);
  1178. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1179. spin_lock(&sdp->sd_trunc_lock);
  1180. empty = list_empty(&sdp->sd_trunc_list);
  1181. spin_unlock(&sdp->sd_trunc_lock);
  1182. if (empty && !sdp->sd_statfs_force_sync)
  1183. t -= schedule_timeout(t);
  1184. else
  1185. t = 0;
  1186. finish_wait(&sdp->sd_quota_wait, &wait);
  1187. }
  1188. return 0;
  1189. }
  1190. static int gfs2_quota_get_xstate(struct super_block *sb,
  1191. struct fs_quota_stat *fqs)
  1192. {
  1193. struct gfs2_sbd *sdp = sb->s_fs_info;
  1194. memset(fqs, 0, sizeof(struct fs_quota_stat));
  1195. fqs->qs_version = FS_QSTAT_VERSION;
  1196. switch (sdp->sd_args.ar_quota) {
  1197. case GFS2_QUOTA_ON:
  1198. fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
  1199. /*FALLTHRU*/
  1200. case GFS2_QUOTA_ACCOUNT:
  1201. fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
  1202. break;
  1203. case GFS2_QUOTA_OFF:
  1204. break;
  1205. }
  1206. if (sdp->sd_quota_inode) {
  1207. fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1208. fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
  1209. }
  1210. fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
  1211. fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
  1212. fqs->qs_incoredqs = atomic_read(&qd_lru_count);
  1213. return 0;
  1214. }
  1215. static int gfs2_get_dqblk(struct super_block *sb, int type, qid_t id,
  1216. struct fs_disk_quota *fdq)
  1217. {
  1218. struct gfs2_sbd *sdp = sb->s_fs_info;
  1219. struct gfs2_quota_lvb *qlvb;
  1220. struct gfs2_quota_data *qd;
  1221. struct gfs2_holder q_gh;
  1222. int error;
  1223. memset(fdq, 0, sizeof(struct fs_disk_quota));
  1224. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1225. return -ESRCH; /* Crazy XFS error code */
  1226. if (type == USRQUOTA)
  1227. type = QUOTA_USER;
  1228. else if (type == GRPQUOTA)
  1229. type = QUOTA_GROUP;
  1230. else
  1231. return -EINVAL;
  1232. error = qd_get(sdp, type, id, &qd);
  1233. if (error)
  1234. return error;
  1235. error = do_glock(qd, FORCE, &q_gh);
  1236. if (error)
  1237. goto out;
  1238. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  1239. fdq->d_version = FS_DQUOT_VERSION;
  1240. fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
  1241. fdq->d_id = id;
  1242. fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
  1243. fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
  1244. fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
  1245. gfs2_glock_dq_uninit(&q_gh);
  1246. out:
  1247. qd_put(qd);
  1248. return error;
  1249. }
  1250. /* GFS2 only supports a subset of the XFS fields */
  1251. #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
  1252. static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
  1253. struct fs_disk_quota *fdq)
  1254. {
  1255. struct gfs2_sbd *sdp = sb->s_fs_info;
  1256. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1257. struct gfs2_quota_data *qd;
  1258. struct gfs2_holder q_gh, i_gh;
  1259. unsigned int data_blocks, ind_blocks;
  1260. unsigned int blocks = 0;
  1261. int alloc_required;
  1262. struct gfs2_alloc *al;
  1263. loff_t offset;
  1264. int error;
  1265. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1266. return -ESRCH; /* Crazy XFS error code */
  1267. switch(type) {
  1268. case USRQUOTA:
  1269. type = QUOTA_USER;
  1270. if (fdq->d_flags != FS_USER_QUOTA)
  1271. return -EINVAL;
  1272. break;
  1273. case GRPQUOTA:
  1274. type = QUOTA_GROUP;
  1275. if (fdq->d_flags != FS_GROUP_QUOTA)
  1276. return -EINVAL;
  1277. break;
  1278. default:
  1279. return -EINVAL;
  1280. }
  1281. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1282. return -EINVAL;
  1283. if (fdq->d_id != id)
  1284. return -EINVAL;
  1285. error = qd_get(sdp, type, id, &qd);
  1286. if (error)
  1287. return error;
  1288. mutex_lock(&ip->i_inode.i_mutex);
  1289. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1290. if (error)
  1291. goto out_put;
  1292. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1293. if (error)
  1294. goto out_q;
  1295. /* Check for existing entry, if none then alloc new blocks */
  1296. error = update_qd(sdp, qd);
  1297. if (error)
  1298. goto out_i;
  1299. /* If nothing has changed, this is a no-op */
  1300. if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
  1301. ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1302. fdq->d_fieldmask ^= FS_DQ_BSOFT;
  1303. if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
  1304. ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1305. fdq->d_fieldmask ^= FS_DQ_BHARD;
  1306. if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
  1307. ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1308. fdq->d_fieldmask ^= FS_DQ_BCOUNT;
  1309. if (fdq->d_fieldmask == 0)
  1310. goto out_i;
  1311. offset = qd2offset(qd);
  1312. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1313. if (alloc_required) {
  1314. al = gfs2_alloc_get(ip);
  1315. if (al == NULL)
  1316. goto out_i;
  1317. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1318. &data_blocks, &ind_blocks);
  1319. blocks = al->al_requested = 1 + data_blocks + ind_blocks;
  1320. error = gfs2_inplace_reserve(ip);
  1321. if (error)
  1322. goto out_alloc;
  1323. blocks += gfs2_rg_blocks(al);
  1324. }
  1325. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 1, 0);
  1326. if (error)
  1327. goto out_release;
  1328. /* Apply changes */
  1329. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1330. gfs2_trans_end(sdp);
  1331. out_release:
  1332. if (alloc_required) {
  1333. gfs2_inplace_release(ip);
  1334. out_alloc:
  1335. gfs2_alloc_put(ip);
  1336. }
  1337. out_i:
  1338. gfs2_glock_dq_uninit(&i_gh);
  1339. out_q:
  1340. gfs2_glock_dq_uninit(&q_gh);
  1341. out_put:
  1342. mutex_unlock(&ip->i_inode.i_mutex);
  1343. qd_put(qd);
  1344. return error;
  1345. }
  1346. const struct quotactl_ops gfs2_quotactl_ops = {
  1347. .quota_sync = gfs2_quota_sync,
  1348. .get_xstate = gfs2_quota_get_xstate,
  1349. .get_dqblk = gfs2_get_dqblk,
  1350. .set_dqblk = gfs2_set_dqblk,
  1351. };