quota.c 27 KB

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