quota.c 31 KB

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