quota.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 to 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 "gfs2.h"
  49. #include "incore.h"
  50. #include "bmap.h"
  51. #include "glock.h"
  52. #include "glops.h"
  53. #include "log.h"
  54. #include "meta_io.h"
  55. #include "quota.h"
  56. #include "rgrp.h"
  57. #include "super.h"
  58. #include "trans.h"
  59. #include "inode.h"
  60. #include "ops_address.h"
  61. #include "util.h"
  62. #define QUOTA_USER 1
  63. #define QUOTA_GROUP 0
  64. struct gfs2_quota_host {
  65. u64 qu_limit;
  66. u64 qu_warn;
  67. s64 qu_value;
  68. u32 qu_ll_next;
  69. };
  70. struct gfs2_quota_change_host {
  71. u64 qc_change;
  72. u32 qc_flags; /* GFS2_QCF_... */
  73. u32 qc_id;
  74. };
  75. static LIST_HEAD(qd_lru_list);
  76. static atomic_t qd_lru_count = ATOMIC_INIT(0);
  77. static spinlock_t qd_lru_lock = SPIN_LOCK_UNLOCKED;
  78. int gfs2_shrink_qd_memory(int nr, gfp_t gfp_mask)
  79. {
  80. struct gfs2_quota_data *qd;
  81. struct gfs2_sbd *sdp;
  82. if (nr == 0)
  83. goto out;
  84. if (!(gfp_mask & __GFP_FS))
  85. return -1;
  86. spin_lock(&qd_lru_lock);
  87. while (nr && !list_empty(&qd_lru_list)) {
  88. qd = list_entry(qd_lru_list.next,
  89. struct gfs2_quota_data, qd_reclaim);
  90. sdp = qd->qd_gl->gl_sbd;
  91. /* Free from the filesystem-specific list */
  92. list_del(&qd->qd_list);
  93. gfs2_assert_warn(sdp, !qd->qd_change);
  94. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  95. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  96. gfs2_glock_put(qd->qd_gl);
  97. atomic_dec(&sdp->sd_quota_count);
  98. /* Delete it from the common reclaim list */
  99. list_del_init(&qd->qd_reclaim);
  100. atomic_dec(&qd_lru_count);
  101. spin_unlock(&qd_lru_lock);
  102. kmem_cache_free(gfs2_quotad_cachep, qd);
  103. spin_lock(&qd_lru_lock);
  104. nr--;
  105. }
  106. spin_unlock(&qd_lru_lock);
  107. out:
  108. return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
  109. }
  110. static u64 qd2offset(struct gfs2_quota_data *qd)
  111. {
  112. u64 offset;
  113. offset = 2 * (u64)qd->qd_id + !test_bit(QDF_USER, &qd->qd_flags);
  114. offset *= sizeof(struct gfs2_quota);
  115. return offset;
  116. }
  117. static int qd_alloc(struct gfs2_sbd *sdp, int user, u32 id,
  118. struct gfs2_quota_data **qdp)
  119. {
  120. struct gfs2_quota_data *qd;
  121. int error;
  122. qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
  123. if (!qd)
  124. return -ENOMEM;
  125. atomic_set(&qd->qd_count, 1);
  126. qd->qd_id = id;
  127. if (user)
  128. set_bit(QDF_USER, &qd->qd_flags);
  129. qd->qd_slot = -1;
  130. INIT_LIST_HEAD(&qd->qd_reclaim);
  131. error = gfs2_glock_get(sdp, 2 * (u64)id + !user,
  132. &gfs2_quota_glops, CREATE, &qd->qd_gl);
  133. if (error)
  134. goto fail;
  135. *qdp = qd;
  136. return 0;
  137. fail:
  138. kmem_cache_free(gfs2_quotad_cachep, qd);
  139. return error;
  140. }
  141. static int qd_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
  142. struct gfs2_quota_data **qdp)
  143. {
  144. struct gfs2_quota_data *qd = NULL, *new_qd = NULL;
  145. int error, found;
  146. *qdp = NULL;
  147. for (;;) {
  148. found = 0;
  149. spin_lock(&qd_lru_lock);
  150. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  151. if (qd->qd_id == id &&
  152. !test_bit(QDF_USER, &qd->qd_flags) == !user) {
  153. if (!atomic_read(&qd->qd_count) &&
  154. !list_empty(&qd->qd_reclaim)) {
  155. /* Remove it from reclaim list */
  156. list_del_init(&qd->qd_reclaim);
  157. atomic_dec(&qd_lru_count);
  158. }
  159. atomic_inc(&qd->qd_count);
  160. found = 1;
  161. break;
  162. }
  163. }
  164. if (!found)
  165. qd = NULL;
  166. if (!qd && new_qd) {
  167. qd = new_qd;
  168. list_add(&qd->qd_list, &sdp->sd_quota_list);
  169. atomic_inc(&sdp->sd_quota_count);
  170. new_qd = NULL;
  171. }
  172. spin_unlock(&qd_lru_lock);
  173. if (qd || !create) {
  174. if (new_qd) {
  175. gfs2_glock_put(new_qd->qd_gl);
  176. kmem_cache_free(gfs2_quotad_cachep, new_qd);
  177. }
  178. *qdp = qd;
  179. return 0;
  180. }
  181. error = qd_alloc(sdp, user, id, &new_qd);
  182. if (error)
  183. return error;
  184. }
  185. }
  186. static void qd_hold(struct gfs2_quota_data *qd)
  187. {
  188. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  189. gfs2_assert(sdp, atomic_read(&qd->qd_count));
  190. atomic_inc(&qd->qd_count);
  191. }
  192. static void qd_put(struct gfs2_quota_data *qd)
  193. {
  194. if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
  195. /* Add to the reclaim list */
  196. list_add_tail(&qd->qd_reclaim, &qd_lru_list);
  197. atomic_inc(&qd_lru_count);
  198. spin_unlock(&qd_lru_lock);
  199. }
  200. }
  201. static int slot_get(struct gfs2_quota_data *qd)
  202. {
  203. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  204. unsigned int c, o = 0, b;
  205. unsigned char byte = 0;
  206. spin_lock(&qd_lru_lock);
  207. if (qd->qd_slot_count++) {
  208. spin_unlock(&qd_lru_lock);
  209. return 0;
  210. }
  211. for (c = 0; c < sdp->sd_quota_chunks; c++)
  212. for (o = 0; o < PAGE_SIZE; o++) {
  213. byte = sdp->sd_quota_bitmap[c][o];
  214. if (byte != 0xFF)
  215. goto found;
  216. }
  217. goto fail;
  218. found:
  219. for (b = 0; b < 8; b++)
  220. if (!(byte & (1 << b)))
  221. break;
  222. qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
  223. if (qd->qd_slot >= sdp->sd_quota_slots)
  224. goto fail;
  225. sdp->sd_quota_bitmap[c][o] |= 1 << b;
  226. spin_unlock(&qd_lru_lock);
  227. return 0;
  228. fail:
  229. qd->qd_slot_count--;
  230. spin_unlock(&qd_lru_lock);
  231. return -ENOSPC;
  232. }
  233. static void slot_hold(struct gfs2_quota_data *qd)
  234. {
  235. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  236. spin_lock(&qd_lru_lock);
  237. gfs2_assert(sdp, qd->qd_slot_count);
  238. qd->qd_slot_count++;
  239. spin_unlock(&qd_lru_lock);
  240. }
  241. static void slot_put(struct gfs2_quota_data *qd)
  242. {
  243. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  244. spin_lock(&qd_lru_lock);
  245. gfs2_assert(sdp, qd->qd_slot_count);
  246. if (!--qd->qd_slot_count) {
  247. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
  248. qd->qd_slot = -1;
  249. }
  250. spin_unlock(&qd_lru_lock);
  251. }
  252. static int bh_get(struct gfs2_quota_data *qd)
  253. {
  254. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  255. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  256. unsigned int block, offset;
  257. struct buffer_head *bh;
  258. int error;
  259. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  260. mutex_lock(&sdp->sd_quota_mutex);
  261. if (qd->qd_bh_count++) {
  262. mutex_unlock(&sdp->sd_quota_mutex);
  263. return 0;
  264. }
  265. block = qd->qd_slot / sdp->sd_qc_per_block;
  266. offset = qd->qd_slot % sdp->sd_qc_per_block;
  267. bh_map.b_size = 1 << ip->i_inode.i_blkbits;
  268. error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
  269. if (error)
  270. goto fail;
  271. error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
  272. if (error)
  273. goto fail;
  274. error = -EIO;
  275. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  276. goto fail_brelse;
  277. qd->qd_bh = bh;
  278. qd->qd_bh_qc = (struct gfs2_quota_change *)
  279. (bh->b_data + sizeof(struct gfs2_meta_header) +
  280. offset * sizeof(struct gfs2_quota_change));
  281. mutex_unlock(&sdp->sd_quota_mutex);
  282. return 0;
  283. fail_brelse:
  284. brelse(bh);
  285. fail:
  286. qd->qd_bh_count--;
  287. mutex_unlock(&sdp->sd_quota_mutex);
  288. return error;
  289. }
  290. static void bh_put(struct gfs2_quota_data *qd)
  291. {
  292. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  293. mutex_lock(&sdp->sd_quota_mutex);
  294. gfs2_assert(sdp, qd->qd_bh_count);
  295. if (!--qd->qd_bh_count) {
  296. brelse(qd->qd_bh);
  297. qd->qd_bh = NULL;
  298. qd->qd_bh_qc = NULL;
  299. }
  300. mutex_unlock(&sdp->sd_quota_mutex);
  301. }
  302. static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
  303. {
  304. struct gfs2_quota_data *qd = NULL;
  305. int error;
  306. int found = 0;
  307. *qdp = NULL;
  308. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  309. return 0;
  310. spin_lock(&qd_lru_lock);
  311. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  312. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  313. !test_bit(QDF_CHANGE, &qd->qd_flags) ||
  314. qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
  315. continue;
  316. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  317. set_bit(QDF_LOCKED, &qd->qd_flags);
  318. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  319. atomic_inc(&qd->qd_count);
  320. qd->qd_change_sync = qd->qd_change;
  321. gfs2_assert_warn(sdp, qd->qd_slot_count);
  322. qd->qd_slot_count++;
  323. found = 1;
  324. break;
  325. }
  326. if (!found)
  327. qd = NULL;
  328. spin_unlock(&qd_lru_lock);
  329. if (qd) {
  330. gfs2_assert_warn(sdp, qd->qd_change_sync);
  331. error = bh_get(qd);
  332. if (error) {
  333. clear_bit(QDF_LOCKED, &qd->qd_flags);
  334. slot_put(qd);
  335. qd_put(qd);
  336. return error;
  337. }
  338. }
  339. *qdp = qd;
  340. return 0;
  341. }
  342. static int qd_trylock(struct gfs2_quota_data *qd)
  343. {
  344. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  345. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  346. return 0;
  347. spin_lock(&qd_lru_lock);
  348. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  349. !test_bit(QDF_CHANGE, &qd->qd_flags)) {
  350. spin_unlock(&qd_lru_lock);
  351. return 0;
  352. }
  353. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  354. set_bit(QDF_LOCKED, &qd->qd_flags);
  355. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  356. atomic_inc(&qd->qd_count);
  357. qd->qd_change_sync = qd->qd_change;
  358. gfs2_assert_warn(sdp, qd->qd_slot_count);
  359. qd->qd_slot_count++;
  360. spin_unlock(&qd_lru_lock);
  361. gfs2_assert_warn(sdp, qd->qd_change_sync);
  362. if (bh_get(qd)) {
  363. clear_bit(QDF_LOCKED, &qd->qd_flags);
  364. slot_put(qd);
  365. qd_put(qd);
  366. return 0;
  367. }
  368. return 1;
  369. }
  370. static void qd_unlock(struct gfs2_quota_data *qd)
  371. {
  372. gfs2_assert_warn(qd->qd_gl->gl_sbd,
  373. test_bit(QDF_LOCKED, &qd->qd_flags));
  374. clear_bit(QDF_LOCKED, &qd->qd_flags);
  375. bh_put(qd);
  376. slot_put(qd);
  377. qd_put(qd);
  378. }
  379. static int qdsb_get(struct gfs2_sbd *sdp, int user, u32 id, int create,
  380. struct gfs2_quota_data **qdp)
  381. {
  382. int error;
  383. error = qd_get(sdp, user, id, create, qdp);
  384. if (error)
  385. return error;
  386. error = slot_get(*qdp);
  387. if (error)
  388. goto fail;
  389. error = bh_get(*qdp);
  390. if (error)
  391. goto fail_slot;
  392. return 0;
  393. fail_slot:
  394. slot_put(*qdp);
  395. fail:
  396. qd_put(*qdp);
  397. return error;
  398. }
  399. static void qdsb_put(struct gfs2_quota_data *qd)
  400. {
  401. bh_put(qd);
  402. slot_put(qd);
  403. qd_put(qd);
  404. }
  405. int gfs2_quota_hold(struct gfs2_inode *ip, u32 uid, u32 gid)
  406. {
  407. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  408. struct gfs2_alloc *al = ip->i_alloc;
  409. struct gfs2_quota_data **qd = al->al_qd;
  410. int error;
  411. if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
  412. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
  413. return -EIO;
  414. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  415. return 0;
  416. error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, CREATE, qd);
  417. if (error)
  418. goto out;
  419. al->al_qd_num++;
  420. qd++;
  421. error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, CREATE, qd);
  422. if (error)
  423. goto out;
  424. al->al_qd_num++;
  425. qd++;
  426. if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
  427. error = qdsb_get(sdp, QUOTA_USER, uid, CREATE, qd);
  428. if (error)
  429. goto out;
  430. al->al_qd_num++;
  431. qd++;
  432. }
  433. if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
  434. error = qdsb_get(sdp, QUOTA_GROUP, gid, CREATE, qd);
  435. if (error)
  436. goto out;
  437. al->al_qd_num++;
  438. qd++;
  439. }
  440. out:
  441. if (error)
  442. gfs2_quota_unhold(ip);
  443. return error;
  444. }
  445. void gfs2_quota_unhold(struct gfs2_inode *ip)
  446. {
  447. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  448. struct gfs2_alloc *al = ip->i_alloc;
  449. unsigned int x;
  450. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  451. for (x = 0; x < al->al_qd_num; x++) {
  452. qdsb_put(al->al_qd[x]);
  453. al->al_qd[x] = NULL;
  454. }
  455. al->al_qd_num = 0;
  456. }
  457. static int sort_qd(const void *a, const void *b)
  458. {
  459. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  460. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  461. if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
  462. !test_bit(QDF_USER, &qd_b->qd_flags)) {
  463. if (test_bit(QDF_USER, &qd_a->qd_flags))
  464. return -1;
  465. else
  466. return 1;
  467. }
  468. if (qd_a->qd_id < qd_b->qd_id)
  469. return -1;
  470. if (qd_a->qd_id > qd_b->qd_id)
  471. return 1;
  472. return 0;
  473. }
  474. static void do_qc(struct gfs2_quota_data *qd, s64 change)
  475. {
  476. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  477. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  478. struct gfs2_quota_change *qc = qd->qd_bh_qc;
  479. s64 x;
  480. mutex_lock(&sdp->sd_quota_mutex);
  481. gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
  482. if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
  483. qc->qc_change = 0;
  484. qc->qc_flags = 0;
  485. if (test_bit(QDF_USER, &qd->qd_flags))
  486. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  487. qc->qc_id = cpu_to_be32(qd->qd_id);
  488. }
  489. x = be64_to_cpu(qc->qc_change) + change;
  490. qc->qc_change = cpu_to_be64(x);
  491. spin_lock(&qd_lru_lock);
  492. qd->qd_change = x;
  493. spin_unlock(&qd_lru_lock);
  494. if (!x) {
  495. gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
  496. clear_bit(QDF_CHANGE, &qd->qd_flags);
  497. qc->qc_flags = 0;
  498. qc->qc_id = 0;
  499. slot_put(qd);
  500. qd_put(qd);
  501. } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
  502. qd_hold(qd);
  503. slot_hold(qd);
  504. }
  505. mutex_unlock(&sdp->sd_quota_mutex);
  506. }
  507. static void gfs2_quota_in(struct gfs2_quota_host *qu, const void *buf)
  508. {
  509. const struct gfs2_quota *str = buf;
  510. qu->qu_limit = be64_to_cpu(str->qu_limit);
  511. qu->qu_warn = be64_to_cpu(str->qu_warn);
  512. qu->qu_value = be64_to_cpu(str->qu_value);
  513. qu->qu_ll_next = be32_to_cpu(str->qu_ll_next);
  514. }
  515. static void gfs2_quota_out(const struct gfs2_quota_host *qu, void *buf)
  516. {
  517. struct gfs2_quota *str = buf;
  518. str->qu_limit = cpu_to_be64(qu->qu_limit);
  519. str->qu_warn = cpu_to_be64(qu->qu_warn);
  520. str->qu_value = cpu_to_be64(qu->qu_value);
  521. str->qu_ll_next = cpu_to_be32(qu->qu_ll_next);
  522. memset(&str->qu_reserved, 0, sizeof(str->qu_reserved));
  523. }
  524. /**
  525. * gfs2_adjust_quota
  526. *
  527. * This function was mostly borrowed from gfs2_block_truncate_page which was
  528. * in turn mostly borrowed from ext3
  529. */
  530. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  531. s64 change, struct gfs2_quota_data *qd)
  532. {
  533. struct inode *inode = &ip->i_inode;
  534. struct address_space *mapping = inode->i_mapping;
  535. unsigned long index = loc >> PAGE_CACHE_SHIFT;
  536. unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
  537. unsigned blocksize, iblock, pos;
  538. struct buffer_head *bh;
  539. struct page *page;
  540. void *kaddr;
  541. char *ptr;
  542. struct gfs2_quota_host qp;
  543. s64 value;
  544. int err = -EIO;
  545. if (gfs2_is_stuffed(ip))
  546. gfs2_unstuff_dinode(ip, NULL);
  547. page = grab_cache_page(mapping, index);
  548. if (!page)
  549. return -ENOMEM;
  550. blocksize = inode->i_sb->s_blocksize;
  551. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  552. if (!page_has_buffers(page))
  553. create_empty_buffers(page, blocksize, 0);
  554. bh = page_buffers(page);
  555. pos = blocksize;
  556. while (offset >= pos) {
  557. bh = bh->b_this_page;
  558. iblock++;
  559. pos += blocksize;
  560. }
  561. if (!buffer_mapped(bh)) {
  562. gfs2_block_map(inode, iblock, bh, 1);
  563. if (!buffer_mapped(bh))
  564. goto unlock;
  565. }
  566. if (PageUptodate(page))
  567. set_buffer_uptodate(bh);
  568. if (!buffer_uptodate(bh)) {
  569. ll_rw_block(READ_META, 1, &bh);
  570. wait_on_buffer(bh);
  571. if (!buffer_uptodate(bh))
  572. goto unlock;
  573. }
  574. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  575. kaddr = kmap_atomic(page, KM_USER0);
  576. ptr = kaddr + offset;
  577. gfs2_quota_in(&qp, ptr);
  578. qp.qu_value += change;
  579. value = qp.qu_value;
  580. gfs2_quota_out(&qp, ptr);
  581. flush_dcache_page(page);
  582. kunmap_atomic(kaddr, KM_USER0);
  583. err = 0;
  584. qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
  585. qd->qd_qb.qb_value = cpu_to_be64(value);
  586. ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_magic = cpu_to_be32(GFS2_MAGIC);
  587. ((struct gfs2_quota_lvb*)(qd->qd_gl->gl_lvb))->qb_value = cpu_to_be64(value);
  588. unlock:
  589. unlock_page(page);
  590. page_cache_release(page);
  591. return err;
  592. }
  593. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  594. {
  595. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  596. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  597. unsigned int data_blocks, ind_blocks;
  598. struct gfs2_holder *ghs, i_gh;
  599. unsigned int qx, x;
  600. struct gfs2_quota_data *qd;
  601. loff_t offset;
  602. unsigned int nalloc = 0, blocks;
  603. struct gfs2_alloc *al = NULL;
  604. int error;
  605. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  606. &data_blocks, &ind_blocks);
  607. ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
  608. if (!ghs)
  609. return -ENOMEM;
  610. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  611. for (qx = 0; qx < num_qd; qx++) {
  612. error = gfs2_glock_nq_init(qda[qx]->qd_gl,
  613. LM_ST_EXCLUSIVE,
  614. GL_NOCACHE, &ghs[qx]);
  615. if (error)
  616. goto out;
  617. }
  618. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  619. if (error)
  620. goto out;
  621. for (x = 0; x < num_qd; x++) {
  622. int alloc_required;
  623. offset = qd2offset(qda[x]);
  624. error = gfs2_write_alloc_required(ip, offset,
  625. sizeof(struct gfs2_quota),
  626. &alloc_required);
  627. if (error)
  628. goto out_gunlock;
  629. if (alloc_required)
  630. nalloc++;
  631. }
  632. al = gfs2_alloc_get(ip);
  633. if (!al) {
  634. error = -ENOMEM;
  635. goto out_gunlock;
  636. }
  637. /*
  638. * 1 blk for unstuffing inode if stuffed. We add this extra
  639. * block to the reservation unconditionally. If the inode
  640. * doesn't need unstuffing, the block will be released to the
  641. * rgrp since it won't be allocated during the transaction
  642. */
  643. al->al_requested = 1;
  644. /* +1 in the end for block requested above for unstuffing */
  645. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 1;
  646. if (nalloc)
  647. al->al_requested += nalloc * (data_blocks + ind_blocks);
  648. error = gfs2_inplace_reserve(ip);
  649. if (error)
  650. goto out_alloc;
  651. if (nalloc)
  652. blocks += al->al_rgd->rd_length + nalloc * ind_blocks + RES_STATFS;
  653. error = gfs2_trans_begin(sdp, blocks, 0);
  654. if (error)
  655. goto out_ipres;
  656. for (x = 0; x < num_qd; x++) {
  657. qd = qda[x];
  658. offset = qd2offset(qd);
  659. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
  660. (struct gfs2_quota_data *)
  661. qd);
  662. if (error)
  663. goto out_end_trans;
  664. do_qc(qd, -qd->qd_change_sync);
  665. }
  666. error = 0;
  667. out_end_trans:
  668. gfs2_trans_end(sdp);
  669. out_ipres:
  670. gfs2_inplace_release(ip);
  671. out_alloc:
  672. gfs2_alloc_put(ip);
  673. out_gunlock:
  674. gfs2_glock_dq_uninit(&i_gh);
  675. out:
  676. while (qx--)
  677. gfs2_glock_dq_uninit(&ghs[qx]);
  678. kfree(ghs);
  679. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  680. return error;
  681. }
  682. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  683. struct gfs2_holder *q_gh)
  684. {
  685. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  686. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  687. struct gfs2_holder i_gh;
  688. struct gfs2_quota_host q;
  689. char buf[sizeof(struct gfs2_quota)];
  690. int error;
  691. struct gfs2_quota_lvb *qlvb;
  692. restart:
  693. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  694. if (error)
  695. return error;
  696. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  697. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  698. loff_t pos;
  699. gfs2_glock_dq_uninit(q_gh);
  700. error = gfs2_glock_nq_init(qd->qd_gl,
  701. LM_ST_EXCLUSIVE, GL_NOCACHE,
  702. q_gh);
  703. if (error)
  704. return error;
  705. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  706. if (error)
  707. goto fail;
  708. memset(buf, 0, sizeof(struct gfs2_quota));
  709. pos = qd2offset(qd);
  710. error = gfs2_internal_read(ip, NULL, buf, &pos,
  711. sizeof(struct gfs2_quota));
  712. if (error < 0)
  713. goto fail_gunlock;
  714. gfs2_glock_dq_uninit(&i_gh);
  715. gfs2_quota_in(&q, buf);
  716. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  717. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  718. qlvb->__pad = 0;
  719. qlvb->qb_limit = cpu_to_be64(q.qu_limit);
  720. qlvb->qb_warn = cpu_to_be64(q.qu_warn);
  721. qlvb->qb_value = cpu_to_be64(q.qu_value);
  722. qd->qd_qb = *qlvb;
  723. if (gfs2_glock_is_blocking(qd->qd_gl)) {
  724. gfs2_glock_dq_uninit(q_gh);
  725. force_refresh = 0;
  726. goto restart;
  727. }
  728. }
  729. return 0;
  730. fail_gunlock:
  731. gfs2_glock_dq_uninit(&i_gh);
  732. fail:
  733. gfs2_glock_dq_uninit(q_gh);
  734. return error;
  735. }
  736. int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
  737. {
  738. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  739. struct gfs2_alloc *al = ip->i_alloc;
  740. unsigned int x;
  741. int error = 0;
  742. gfs2_quota_hold(ip, uid, gid);
  743. if (capable(CAP_SYS_RESOURCE) ||
  744. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  745. return 0;
  746. sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
  747. sort_qd, NULL);
  748. for (x = 0; x < al->al_qd_num; x++) {
  749. error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
  750. if (error)
  751. break;
  752. }
  753. if (!error)
  754. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  755. else {
  756. while (x--)
  757. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  758. gfs2_quota_unhold(ip);
  759. }
  760. return error;
  761. }
  762. static int need_sync(struct gfs2_quota_data *qd)
  763. {
  764. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  765. struct gfs2_tune *gt = &sdp->sd_tune;
  766. s64 value;
  767. unsigned int num, den;
  768. int do_sync = 1;
  769. if (!qd->qd_qb.qb_limit)
  770. return 0;
  771. spin_lock(&qd_lru_lock);
  772. value = qd->qd_change;
  773. spin_unlock(&qd_lru_lock);
  774. spin_lock(&gt->gt_spin);
  775. num = gt->gt_quota_scale_num;
  776. den = gt->gt_quota_scale_den;
  777. spin_unlock(&gt->gt_spin);
  778. if (value < 0)
  779. do_sync = 0;
  780. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  781. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  782. do_sync = 0;
  783. else {
  784. value *= gfs2_jindex_size(sdp) * num;
  785. value = div_s64(value, den);
  786. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  787. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  788. do_sync = 0;
  789. }
  790. return do_sync;
  791. }
  792. void gfs2_quota_unlock(struct gfs2_inode *ip)
  793. {
  794. struct gfs2_alloc *al = ip->i_alloc;
  795. struct gfs2_quota_data *qda[4];
  796. unsigned int count = 0;
  797. unsigned int x;
  798. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  799. goto out;
  800. for (x = 0; x < al->al_qd_num; x++) {
  801. struct gfs2_quota_data *qd;
  802. int sync;
  803. qd = al->al_qd[x];
  804. sync = need_sync(qd);
  805. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  806. if (sync && qd_trylock(qd))
  807. qda[count++] = qd;
  808. }
  809. if (count) {
  810. do_sync(count, qda);
  811. for (x = 0; x < count; x++)
  812. qd_unlock(qda[x]);
  813. }
  814. out:
  815. gfs2_quota_unhold(ip);
  816. }
  817. #define MAX_LINE 256
  818. static int print_message(struct gfs2_quota_data *qd, char *type)
  819. {
  820. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  821. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
  822. sdp->sd_fsname, type,
  823. (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
  824. qd->qd_id);
  825. return 0;
  826. }
  827. int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
  828. {
  829. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  830. struct gfs2_alloc *al = ip->i_alloc;
  831. struct gfs2_quota_data *qd;
  832. s64 value;
  833. unsigned int x;
  834. int error = 0;
  835. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  836. return 0;
  837. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  838. return 0;
  839. for (x = 0; x < al->al_qd_num; x++) {
  840. qd = al->al_qd[x];
  841. if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  842. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
  843. continue;
  844. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  845. spin_lock(&qd_lru_lock);
  846. value += qd->qd_change;
  847. spin_unlock(&qd_lru_lock);
  848. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  849. print_message(qd, "exceeded");
  850. error = -EDQUOT;
  851. break;
  852. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  853. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  854. time_after_eq(jiffies, qd->qd_last_warn +
  855. gfs2_tune_get(sdp,
  856. gt_quota_warn_period) * HZ)) {
  857. error = print_message(qd, "warning");
  858. qd->qd_last_warn = jiffies;
  859. }
  860. }
  861. return error;
  862. }
  863. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  864. u32 uid, u32 gid)
  865. {
  866. struct gfs2_alloc *al = ip->i_alloc;
  867. struct gfs2_quota_data *qd;
  868. unsigned int x;
  869. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  870. return;
  871. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  872. return;
  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. do_qc(qd, change);
  878. }
  879. }
  880. }
  881. int gfs2_quota_sync(struct gfs2_sbd *sdp)
  882. {
  883. struct gfs2_quota_data **qda;
  884. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  885. unsigned int num_qd;
  886. unsigned int x;
  887. int error = 0;
  888. sdp->sd_quota_sync_gen++;
  889. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  890. if (!qda)
  891. return -ENOMEM;
  892. do {
  893. num_qd = 0;
  894. for (;;) {
  895. error = qd_fish(sdp, qda + num_qd);
  896. if (error || !qda[num_qd])
  897. break;
  898. if (++num_qd == max_qd)
  899. break;
  900. }
  901. if (num_qd) {
  902. if (!error)
  903. error = do_sync(num_qd, qda);
  904. if (!error)
  905. for (x = 0; x < num_qd; x++)
  906. qda[x]->qd_sync_gen =
  907. sdp->sd_quota_sync_gen;
  908. for (x = 0; x < num_qd; x++)
  909. qd_unlock(qda[x]);
  910. }
  911. } while (!error && num_qd == max_qd);
  912. kfree(qda);
  913. return error;
  914. }
  915. int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
  916. {
  917. struct gfs2_quota_data *qd;
  918. struct gfs2_holder q_gh;
  919. int error;
  920. error = qd_get(sdp, user, id, CREATE, &qd);
  921. if (error)
  922. return error;
  923. error = do_glock(qd, FORCE, &q_gh);
  924. if (!error)
  925. gfs2_glock_dq_uninit(&q_gh);
  926. qd_put(qd);
  927. return error;
  928. }
  929. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  930. {
  931. const struct gfs2_quota_change *str = buf;
  932. qc->qc_change = be64_to_cpu(str->qc_change);
  933. qc->qc_flags = be32_to_cpu(str->qc_flags);
  934. qc->qc_id = be32_to_cpu(str->qc_id);
  935. }
  936. int gfs2_quota_init(struct gfs2_sbd *sdp)
  937. {
  938. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  939. unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
  940. unsigned int x, slot = 0;
  941. unsigned int found = 0;
  942. u64 dblock;
  943. u32 extlen = 0;
  944. int error;
  945. if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
  946. ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
  947. gfs2_consist_inode(ip);
  948. return -EIO;
  949. }
  950. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  951. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  952. error = -ENOMEM;
  953. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  954. sizeof(unsigned char *), GFP_NOFS);
  955. if (!sdp->sd_quota_bitmap)
  956. return error;
  957. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  958. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  959. if (!sdp->sd_quota_bitmap[x])
  960. goto fail;
  961. }
  962. for (x = 0; x < blocks; x++) {
  963. struct buffer_head *bh;
  964. unsigned int y;
  965. if (!extlen) {
  966. int new = 0;
  967. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  968. if (error)
  969. goto fail;
  970. }
  971. error = -EIO;
  972. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  973. if (!bh)
  974. goto fail;
  975. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  976. brelse(bh);
  977. goto fail;
  978. }
  979. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  980. y++, slot++) {
  981. struct gfs2_quota_change_host qc;
  982. struct gfs2_quota_data *qd;
  983. gfs2_quota_change_in(&qc, bh->b_data +
  984. sizeof(struct gfs2_meta_header) +
  985. y * sizeof(struct gfs2_quota_change));
  986. if (!qc.qc_change)
  987. continue;
  988. error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
  989. qc.qc_id, &qd);
  990. if (error) {
  991. brelse(bh);
  992. goto fail;
  993. }
  994. set_bit(QDF_CHANGE, &qd->qd_flags);
  995. qd->qd_change = qc.qc_change;
  996. qd->qd_slot = slot;
  997. qd->qd_slot_count = 1;
  998. spin_lock(&qd_lru_lock);
  999. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  1000. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1001. atomic_inc(&sdp->sd_quota_count);
  1002. spin_unlock(&qd_lru_lock);
  1003. found++;
  1004. }
  1005. brelse(bh);
  1006. dblock++;
  1007. extlen--;
  1008. }
  1009. if (found)
  1010. fs_info(sdp, "found %u quota changes\n", found);
  1011. return 0;
  1012. fail:
  1013. gfs2_quota_cleanup(sdp);
  1014. return error;
  1015. }
  1016. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1017. {
  1018. struct list_head *head = &sdp->sd_quota_list;
  1019. struct gfs2_quota_data *qd;
  1020. unsigned int x;
  1021. spin_lock(&qd_lru_lock);
  1022. while (!list_empty(head)) {
  1023. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1024. if (atomic_read(&qd->qd_count) > 1 ||
  1025. (atomic_read(&qd->qd_count) &&
  1026. !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1027. list_move(&qd->qd_list, head);
  1028. spin_unlock(&qd_lru_lock);
  1029. schedule();
  1030. spin_lock(&qd_lru_lock);
  1031. continue;
  1032. }
  1033. list_del(&qd->qd_list);
  1034. /* Also remove if this qd exists in the reclaim list */
  1035. if (!list_empty(&qd->qd_reclaim)) {
  1036. list_del_init(&qd->qd_reclaim);
  1037. atomic_dec(&qd_lru_count);
  1038. }
  1039. atomic_dec(&sdp->sd_quota_count);
  1040. spin_unlock(&qd_lru_lock);
  1041. if (!atomic_read(&qd->qd_count)) {
  1042. gfs2_assert_warn(sdp, !qd->qd_change);
  1043. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1044. } else
  1045. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1046. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1047. gfs2_glock_put(qd->qd_gl);
  1048. kmem_cache_free(gfs2_quotad_cachep, qd);
  1049. spin_lock(&qd_lru_lock);
  1050. }
  1051. spin_unlock(&qd_lru_lock);
  1052. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1053. if (sdp->sd_quota_bitmap) {
  1054. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1055. kfree(sdp->sd_quota_bitmap[x]);
  1056. kfree(sdp->sd_quota_bitmap);
  1057. }
  1058. }
  1059. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1060. {
  1061. if (error == 0 || error == -EROFS)
  1062. return;
  1063. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1064. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1065. }
  1066. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1067. int (*fxn)(struct gfs2_sbd *sdp),
  1068. unsigned long t, unsigned long *timeo,
  1069. unsigned int *new_timeo)
  1070. {
  1071. if (t >= *timeo) {
  1072. int error = fxn(sdp);
  1073. quotad_error(sdp, msg, error);
  1074. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1075. } else {
  1076. *timeo -= t;
  1077. }
  1078. }
  1079. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1080. {
  1081. struct gfs2_inode *ip;
  1082. while(1) {
  1083. ip = NULL;
  1084. spin_lock(&sdp->sd_trunc_lock);
  1085. if (!list_empty(&sdp->sd_trunc_list)) {
  1086. ip = list_entry(sdp->sd_trunc_list.next,
  1087. struct gfs2_inode, i_trunc_list);
  1088. list_del_init(&ip->i_trunc_list);
  1089. }
  1090. spin_unlock(&sdp->sd_trunc_lock);
  1091. if (ip == NULL)
  1092. return;
  1093. gfs2_glock_finish_truncate(ip);
  1094. }
  1095. }
  1096. /**
  1097. * gfs2_quotad - Write cached quota changes into the quota file
  1098. * @sdp: Pointer to GFS2 superblock
  1099. *
  1100. */
  1101. int gfs2_quotad(void *data)
  1102. {
  1103. struct gfs2_sbd *sdp = data;
  1104. struct gfs2_tune *tune = &sdp->sd_tune;
  1105. unsigned long statfs_timeo = 0;
  1106. unsigned long quotad_timeo = 0;
  1107. unsigned long t = 0;
  1108. DEFINE_WAIT(wait);
  1109. int empty;
  1110. while (!kthread_should_stop()) {
  1111. /* Update the master statfs file */
  1112. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1113. &statfs_timeo, &tune->gt_statfs_quantum);
  1114. /* Update quota file */
  1115. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1116. &quotad_timeo, &tune->gt_quota_quantum);
  1117. /* Check for & recover partially truncated inodes */
  1118. quotad_check_trunc_list(sdp);
  1119. if (freezing(current))
  1120. refrigerator();
  1121. t = min(quotad_timeo, statfs_timeo);
  1122. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_UNINTERRUPTIBLE);
  1123. spin_lock(&sdp->sd_trunc_lock);
  1124. empty = list_empty(&sdp->sd_trunc_list);
  1125. spin_unlock(&sdp->sd_trunc_lock);
  1126. if (empty)
  1127. t -= schedule_timeout(t);
  1128. else
  1129. t = 0;
  1130. finish_wait(&sdp->sd_quota_wait, &wait);
  1131. }
  1132. return 0;
  1133. }