quota.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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 <linux/dqblk_xfs.h>
  49. #include "gfs2.h"
  50. #include "incore.h"
  51. #include "bmap.h"
  52. #include "glock.h"
  53. #include "glops.h"
  54. #include "log.h"
  55. #include "meta_io.h"
  56. #include "quota.h"
  57. #include "rgrp.h"
  58. #include "super.h"
  59. #include "trans.h"
  60. #include "inode.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 DEFINE_SPINLOCK(qd_lru_lock);
  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,
  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, 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, 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, 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, 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, LM_ST_EXCLUSIVE,
  701. GL_NOCACHE, q_gh);
  702. if (error)
  703. return error;
  704. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  705. if (error)
  706. goto fail;
  707. memset(buf, 0, sizeof(struct gfs2_quota));
  708. pos = qd2offset(qd);
  709. error = gfs2_internal_read(ip, NULL, buf, &pos,
  710. sizeof(struct gfs2_quota));
  711. if (error < 0)
  712. goto fail_gunlock;
  713. gfs2_glock_dq_uninit(&i_gh);
  714. gfs2_quota_in(&q, buf);
  715. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  716. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  717. qlvb->__pad = 0;
  718. qlvb->qb_limit = cpu_to_be64(q.qu_limit);
  719. qlvb->qb_warn = cpu_to_be64(q.qu_warn);
  720. qlvb->qb_value = cpu_to_be64(q.qu_value);
  721. qd->qd_qb = *qlvb;
  722. gfs2_glock_dq_uninit(q_gh);
  723. force_refresh = 0;
  724. goto restart;
  725. }
  726. return 0;
  727. fail_gunlock:
  728. gfs2_glock_dq_uninit(&i_gh);
  729. fail:
  730. gfs2_glock_dq_uninit(q_gh);
  731. return error;
  732. }
  733. int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
  734. {
  735. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  736. struct gfs2_alloc *al = ip->i_alloc;
  737. unsigned int x;
  738. int error = 0;
  739. gfs2_quota_hold(ip, uid, gid);
  740. if (capable(CAP_SYS_RESOURCE) ||
  741. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  742. return 0;
  743. sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
  744. sort_qd, NULL);
  745. for (x = 0; x < al->al_qd_num; x++) {
  746. error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
  747. if (error)
  748. break;
  749. }
  750. if (!error)
  751. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  752. else {
  753. while (x--)
  754. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  755. gfs2_quota_unhold(ip);
  756. }
  757. return error;
  758. }
  759. static int need_sync(struct gfs2_quota_data *qd)
  760. {
  761. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  762. struct gfs2_tune *gt = &sdp->sd_tune;
  763. s64 value;
  764. unsigned int num, den;
  765. int do_sync = 1;
  766. if (!qd->qd_qb.qb_limit)
  767. return 0;
  768. spin_lock(&qd_lru_lock);
  769. value = qd->qd_change;
  770. spin_unlock(&qd_lru_lock);
  771. spin_lock(&gt->gt_spin);
  772. num = gt->gt_quota_scale_num;
  773. den = gt->gt_quota_scale_den;
  774. spin_unlock(&gt->gt_spin);
  775. if (value < 0)
  776. do_sync = 0;
  777. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  778. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  779. do_sync = 0;
  780. else {
  781. value *= gfs2_jindex_size(sdp) * num;
  782. value = div_s64(value, den);
  783. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  784. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  785. do_sync = 0;
  786. }
  787. return do_sync;
  788. }
  789. void gfs2_quota_unlock(struct gfs2_inode *ip)
  790. {
  791. struct gfs2_alloc *al = ip->i_alloc;
  792. struct gfs2_quota_data *qda[4];
  793. unsigned int count = 0;
  794. unsigned int x;
  795. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  796. goto out;
  797. for (x = 0; x < al->al_qd_num; x++) {
  798. struct gfs2_quota_data *qd;
  799. int sync;
  800. qd = al->al_qd[x];
  801. sync = need_sync(qd);
  802. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  803. if (sync && qd_trylock(qd))
  804. qda[count++] = qd;
  805. }
  806. if (count) {
  807. do_sync(count, qda);
  808. for (x = 0; x < count; x++)
  809. qd_unlock(qda[x]);
  810. }
  811. out:
  812. gfs2_quota_unhold(ip);
  813. }
  814. #define MAX_LINE 256
  815. static int print_message(struct gfs2_quota_data *qd, char *type)
  816. {
  817. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  818. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
  819. sdp->sd_fsname, type,
  820. (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
  821. qd->qd_id);
  822. return 0;
  823. }
  824. int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
  825. {
  826. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  827. struct gfs2_alloc *al = ip->i_alloc;
  828. struct gfs2_quota_data *qd;
  829. s64 value;
  830. unsigned int x;
  831. int error = 0;
  832. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  833. return 0;
  834. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  835. return 0;
  836. for (x = 0; x < al->al_qd_num; x++) {
  837. qd = al->al_qd[x];
  838. if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  839. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
  840. continue;
  841. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  842. spin_lock(&qd_lru_lock);
  843. value += qd->qd_change;
  844. spin_unlock(&qd_lru_lock);
  845. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  846. print_message(qd, "exceeded");
  847. error = -EDQUOT;
  848. break;
  849. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  850. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  851. time_after_eq(jiffies, qd->qd_last_warn +
  852. gfs2_tune_get(sdp,
  853. gt_quota_warn_period) * HZ)) {
  854. error = print_message(qd, "warning");
  855. qd->qd_last_warn = jiffies;
  856. }
  857. }
  858. return error;
  859. }
  860. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  861. u32 uid, u32 gid)
  862. {
  863. struct gfs2_alloc *al = ip->i_alloc;
  864. struct gfs2_quota_data *qd;
  865. unsigned int x;
  866. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  867. return;
  868. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  869. return;
  870. for (x = 0; x < al->al_qd_num; x++) {
  871. qd = al->al_qd[x];
  872. if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  873. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
  874. do_qc(qd, change);
  875. }
  876. }
  877. }
  878. int gfs2_quota_sync(struct super_block *sb, int type)
  879. {
  880. struct gfs2_sbd *sdp = sb->s_fs_info;
  881. struct gfs2_quota_data **qda;
  882. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  883. unsigned int num_qd;
  884. unsigned int x;
  885. int error = 0;
  886. sdp->sd_quota_sync_gen++;
  887. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  888. if (!qda)
  889. return -ENOMEM;
  890. do {
  891. num_qd = 0;
  892. for (;;) {
  893. error = qd_fish(sdp, qda + num_qd);
  894. if (error || !qda[num_qd])
  895. break;
  896. if (++num_qd == max_qd)
  897. break;
  898. }
  899. if (num_qd) {
  900. if (!error)
  901. error = do_sync(num_qd, qda);
  902. if (!error)
  903. for (x = 0; x < num_qd; x++)
  904. qda[x]->qd_sync_gen =
  905. sdp->sd_quota_sync_gen;
  906. for (x = 0; x < num_qd; x++)
  907. qd_unlock(qda[x]);
  908. }
  909. } while (!error && num_qd == max_qd);
  910. kfree(qda);
  911. return error;
  912. }
  913. int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
  914. {
  915. struct gfs2_quota_data *qd;
  916. struct gfs2_holder q_gh;
  917. int error;
  918. error = qd_get(sdp, user, id, CREATE, &qd);
  919. if (error)
  920. return error;
  921. error = do_glock(qd, FORCE, &q_gh);
  922. if (!error)
  923. gfs2_glock_dq_uninit(&q_gh);
  924. qd_put(qd);
  925. return error;
  926. }
  927. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  928. {
  929. const struct gfs2_quota_change *str = buf;
  930. qc->qc_change = be64_to_cpu(str->qc_change);
  931. qc->qc_flags = be32_to_cpu(str->qc_flags);
  932. qc->qc_id = be32_to_cpu(str->qc_id);
  933. }
  934. int gfs2_quota_init(struct gfs2_sbd *sdp)
  935. {
  936. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  937. unsigned int blocks = ip->i_disksize >> sdp->sd_sb.sb_bsize_shift;
  938. unsigned int x, slot = 0;
  939. unsigned int found = 0;
  940. u64 dblock;
  941. u32 extlen = 0;
  942. int error;
  943. if (!ip->i_disksize || ip->i_disksize > (64 << 20) ||
  944. ip->i_disksize & (sdp->sd_sb.sb_bsize - 1)) {
  945. gfs2_consist_inode(ip);
  946. return -EIO;
  947. }
  948. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  949. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  950. error = -ENOMEM;
  951. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  952. sizeof(unsigned char *), GFP_NOFS);
  953. if (!sdp->sd_quota_bitmap)
  954. return error;
  955. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  956. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  957. if (!sdp->sd_quota_bitmap[x])
  958. goto fail;
  959. }
  960. for (x = 0; x < blocks; x++) {
  961. struct buffer_head *bh;
  962. unsigned int y;
  963. if (!extlen) {
  964. int new = 0;
  965. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  966. if (error)
  967. goto fail;
  968. }
  969. error = -EIO;
  970. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  971. if (!bh)
  972. goto fail;
  973. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  974. brelse(bh);
  975. goto fail;
  976. }
  977. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  978. y++, slot++) {
  979. struct gfs2_quota_change_host qc;
  980. struct gfs2_quota_data *qd;
  981. gfs2_quota_change_in(&qc, bh->b_data +
  982. sizeof(struct gfs2_meta_header) +
  983. y * sizeof(struct gfs2_quota_change));
  984. if (!qc.qc_change)
  985. continue;
  986. error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
  987. qc.qc_id, &qd);
  988. if (error) {
  989. brelse(bh);
  990. goto fail;
  991. }
  992. set_bit(QDF_CHANGE, &qd->qd_flags);
  993. qd->qd_change = qc.qc_change;
  994. qd->qd_slot = slot;
  995. qd->qd_slot_count = 1;
  996. spin_lock(&qd_lru_lock);
  997. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  998. list_add(&qd->qd_list, &sdp->sd_quota_list);
  999. atomic_inc(&sdp->sd_quota_count);
  1000. spin_unlock(&qd_lru_lock);
  1001. found++;
  1002. }
  1003. brelse(bh);
  1004. dblock++;
  1005. extlen--;
  1006. }
  1007. if (found)
  1008. fs_info(sdp, "found %u quota changes\n", found);
  1009. return 0;
  1010. fail:
  1011. gfs2_quota_cleanup(sdp);
  1012. return error;
  1013. }
  1014. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1015. {
  1016. struct list_head *head = &sdp->sd_quota_list;
  1017. struct gfs2_quota_data *qd;
  1018. unsigned int x;
  1019. spin_lock(&qd_lru_lock);
  1020. while (!list_empty(head)) {
  1021. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1022. if (atomic_read(&qd->qd_count) > 1 ||
  1023. (atomic_read(&qd->qd_count) &&
  1024. !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1025. list_move(&qd->qd_list, head);
  1026. spin_unlock(&qd_lru_lock);
  1027. schedule();
  1028. spin_lock(&qd_lru_lock);
  1029. continue;
  1030. }
  1031. list_del(&qd->qd_list);
  1032. /* Also remove if this qd exists in the reclaim list */
  1033. if (!list_empty(&qd->qd_reclaim)) {
  1034. list_del_init(&qd->qd_reclaim);
  1035. atomic_dec(&qd_lru_count);
  1036. }
  1037. atomic_dec(&sdp->sd_quota_count);
  1038. spin_unlock(&qd_lru_lock);
  1039. if (!atomic_read(&qd->qd_count)) {
  1040. gfs2_assert_warn(sdp, !qd->qd_change);
  1041. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1042. } else
  1043. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1044. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1045. gfs2_glock_put(qd->qd_gl);
  1046. kmem_cache_free(gfs2_quotad_cachep, qd);
  1047. spin_lock(&qd_lru_lock);
  1048. }
  1049. spin_unlock(&qd_lru_lock);
  1050. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1051. if (sdp->sd_quota_bitmap) {
  1052. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1053. kfree(sdp->sd_quota_bitmap[x]);
  1054. kfree(sdp->sd_quota_bitmap);
  1055. }
  1056. }
  1057. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1058. {
  1059. if (error == 0 || error == -EROFS)
  1060. return;
  1061. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1062. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1063. }
  1064. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1065. int (*fxn)(struct super_block *sb, int type),
  1066. unsigned long t, unsigned long *timeo,
  1067. unsigned int *new_timeo)
  1068. {
  1069. if (t >= *timeo) {
  1070. int error = fxn(sdp->sd_vfs, 0);
  1071. quotad_error(sdp, msg, error);
  1072. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1073. } else {
  1074. *timeo -= t;
  1075. }
  1076. }
  1077. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1078. {
  1079. struct gfs2_inode *ip;
  1080. while(1) {
  1081. ip = NULL;
  1082. spin_lock(&sdp->sd_trunc_lock);
  1083. if (!list_empty(&sdp->sd_trunc_list)) {
  1084. ip = list_entry(sdp->sd_trunc_list.next,
  1085. struct gfs2_inode, i_trunc_list);
  1086. list_del_init(&ip->i_trunc_list);
  1087. }
  1088. spin_unlock(&sdp->sd_trunc_lock);
  1089. if (ip == NULL)
  1090. return;
  1091. gfs2_glock_finish_truncate(ip);
  1092. }
  1093. }
  1094. /**
  1095. * gfs2_quotad - Write cached quota changes into the quota file
  1096. * @sdp: Pointer to GFS2 superblock
  1097. *
  1098. */
  1099. int gfs2_quotad(void *data)
  1100. {
  1101. struct gfs2_sbd *sdp = data;
  1102. struct gfs2_tune *tune = &sdp->sd_tune;
  1103. unsigned long statfs_timeo = 0;
  1104. unsigned long quotad_timeo = 0;
  1105. unsigned long t = 0;
  1106. DEFINE_WAIT(wait);
  1107. int empty;
  1108. while (!kthread_should_stop()) {
  1109. /* Update the master statfs file */
  1110. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1111. &statfs_timeo, &tune->gt_statfs_quantum);
  1112. /* Update quota file */
  1113. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1114. &quotad_timeo, &tune->gt_quota_quantum);
  1115. /* Check for & recover partially truncated inodes */
  1116. quotad_check_trunc_list(sdp);
  1117. if (freezing(current))
  1118. refrigerator();
  1119. t = min(quotad_timeo, statfs_timeo);
  1120. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1121. spin_lock(&sdp->sd_trunc_lock);
  1122. empty = list_empty(&sdp->sd_trunc_list);
  1123. spin_unlock(&sdp->sd_trunc_lock);
  1124. if (empty)
  1125. t -= schedule_timeout(t);
  1126. else
  1127. t = 0;
  1128. finish_wait(&sdp->sd_quota_wait, &wait);
  1129. }
  1130. return 0;
  1131. }
  1132. static int gfs2_quota_get_xstate(struct super_block *sb,
  1133. struct fs_quota_stat *fqs)
  1134. {
  1135. struct gfs2_sbd *sdp = sb->s_fs_info;
  1136. memset(fqs, 0, sizeof(struct fs_quota_stat));
  1137. fqs->qs_version = FS_QSTAT_VERSION;
  1138. if (sdp->sd_args.ar_quota == GFS2_QUOTA_ON)
  1139. fqs->qs_flags = (XFS_QUOTA_UDQ_ENFD | XFS_QUOTA_GDQ_ENFD);
  1140. else if (sdp->sd_args.ar_quota == GFS2_QUOTA_ACCOUNT)
  1141. fqs->qs_flags = (XFS_QUOTA_UDQ_ACCT | XFS_QUOTA_GDQ_ACCT);
  1142. if (sdp->sd_quota_inode) {
  1143. fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1144. fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
  1145. }
  1146. fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
  1147. fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
  1148. fqs->qs_incoredqs = atomic_read(&qd_lru_count);
  1149. return 0;
  1150. }
  1151. const struct quotactl_ops gfs2_quotactl_ops = {
  1152. .quota_sync = gfs2_quota_sync,
  1153. .get_xstate = gfs2_quota_get_xstate,
  1154. };