quota.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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_inode.i_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_inode.i_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_inode.i_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_inode.i_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 = be64_to_cpu(qc->qc_change) + change;
  443. qc->qc_change = cpu_to_be64(x);
  444. spin_lock(&sdp->sd_quota_spin);
  445. qd->qd_change = x;
  446. spin_unlock(&sdp->sd_quota_spin);
  447. if (!x) {
  448. gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
  449. clear_bit(QDF_CHANGE, &qd->qd_flags);
  450. qc->qc_flags = 0;
  451. qc->qc_id = 0;
  452. slot_put(qd);
  453. qd_put(qd);
  454. } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
  455. qd_hold(qd);
  456. slot_hold(qd);
  457. }
  458. mutex_unlock(&sdp->sd_quota_mutex);
  459. }
  460. /**
  461. * gfs2_adjust_quota
  462. *
  463. * This function was mostly borrowed from gfs2_block_truncate_page which was
  464. * in turn mostly borrowed from ext3
  465. */
  466. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  467. s64 change, struct gfs2_quota_data *qd)
  468. {
  469. struct inode *inode = &ip->i_inode;
  470. struct address_space *mapping = inode->i_mapping;
  471. unsigned long index = loc >> PAGE_CACHE_SHIFT;
  472. unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
  473. unsigned blocksize, iblock, pos;
  474. struct buffer_head *bh;
  475. struct page *page;
  476. void *kaddr;
  477. __be64 *ptr;
  478. s64 value;
  479. int err = -EIO;
  480. page = grab_cache_page(mapping, index);
  481. if (!page)
  482. return -ENOMEM;
  483. blocksize = inode->i_sb->s_blocksize;
  484. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  485. if (!page_has_buffers(page))
  486. create_empty_buffers(page, blocksize, 0);
  487. bh = page_buffers(page);
  488. pos = blocksize;
  489. while (offset >= pos) {
  490. bh = bh->b_this_page;
  491. iblock++;
  492. pos += blocksize;
  493. }
  494. if (!buffer_mapped(bh)) {
  495. gfs2_get_block(inode, iblock, bh, 1);
  496. if (!buffer_mapped(bh))
  497. goto unlock;
  498. }
  499. if (PageUptodate(page))
  500. set_buffer_uptodate(bh);
  501. if (!buffer_uptodate(bh)) {
  502. ll_rw_block(READ_META, 1, &bh);
  503. wait_on_buffer(bh);
  504. if (!buffer_uptodate(bh))
  505. goto unlock;
  506. }
  507. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  508. kaddr = kmap_atomic(page, KM_USER0);
  509. ptr = kaddr + offset;
  510. value = (s64)be64_to_cpu(*ptr) + change;
  511. *ptr = cpu_to_be64(value);
  512. flush_dcache_page(page);
  513. kunmap_atomic(kaddr, KM_USER0);
  514. err = 0;
  515. qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
  516. qd->qd_qb.qb_value = cpu_to_be64(value);
  517. unlock:
  518. unlock_page(page);
  519. page_cache_release(page);
  520. return err;
  521. }
  522. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  523. {
  524. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  525. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  526. unsigned int data_blocks, ind_blocks;
  527. struct gfs2_holder *ghs, i_gh;
  528. unsigned int qx, x;
  529. struct gfs2_quota_data *qd;
  530. loff_t offset;
  531. unsigned int nalloc = 0;
  532. struct gfs2_alloc *al = NULL;
  533. int error;
  534. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  535. &data_blocks, &ind_blocks);
  536. ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_KERNEL);
  537. if (!ghs)
  538. return -ENOMEM;
  539. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  540. for (qx = 0; qx < num_qd; qx++) {
  541. error = gfs2_glock_nq_init(qda[qx]->qd_gl,
  542. LM_ST_EXCLUSIVE,
  543. GL_NOCACHE, &ghs[qx]);
  544. if (error)
  545. goto out;
  546. }
  547. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  548. if (error)
  549. goto out;
  550. for (x = 0; x < num_qd; x++) {
  551. int alloc_required;
  552. offset = qd2offset(qda[x]);
  553. error = gfs2_write_alloc_required(ip, offset,
  554. sizeof(struct gfs2_quota),
  555. &alloc_required);
  556. if (error)
  557. goto out_gunlock;
  558. if (alloc_required)
  559. nalloc++;
  560. }
  561. if (nalloc) {
  562. al = gfs2_alloc_get(ip);
  563. al->al_requested = nalloc * (data_blocks + ind_blocks);
  564. error = gfs2_inplace_reserve(ip);
  565. if (error)
  566. goto out_alloc;
  567. error = gfs2_trans_begin(sdp,
  568. al->al_rgd->rd_ri.ri_length +
  569. num_qd * data_blocks +
  570. nalloc * ind_blocks +
  571. RES_DINODE + num_qd +
  572. RES_STATFS, 0);
  573. if (error)
  574. goto out_ipres;
  575. } else {
  576. error = gfs2_trans_begin(sdp,
  577. num_qd * data_blocks +
  578. RES_DINODE + num_qd, 0);
  579. if (error)
  580. goto out_gunlock;
  581. }
  582. for (x = 0; x < num_qd; x++) {
  583. qd = qda[x];
  584. offset = qd2offset(qd);
  585. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
  586. (struct gfs2_quota_data *)
  587. qd->qd_gl->gl_lvb);
  588. if (error)
  589. goto out_end_trans;
  590. do_qc(qd, -qd->qd_change_sync);
  591. }
  592. error = 0;
  593. out_end_trans:
  594. gfs2_trans_end(sdp);
  595. out_ipres:
  596. if (nalloc)
  597. gfs2_inplace_release(ip);
  598. out_alloc:
  599. if (nalloc)
  600. gfs2_alloc_put(ip);
  601. out_gunlock:
  602. gfs2_glock_dq_uninit(&i_gh);
  603. out:
  604. while (qx--)
  605. gfs2_glock_dq_uninit(&ghs[qx]);
  606. kfree(ghs);
  607. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  608. return error;
  609. }
  610. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  611. struct gfs2_holder *q_gh)
  612. {
  613. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  614. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  615. struct gfs2_holder i_gh;
  616. struct gfs2_quota_host q;
  617. char buf[sizeof(struct gfs2_quota)];
  618. struct file_ra_state ra_state;
  619. int error;
  620. struct gfs2_quota_lvb *qlvb;
  621. file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
  622. restart:
  623. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  624. if (error)
  625. return error;
  626. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  627. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  628. loff_t pos;
  629. gfs2_glock_dq_uninit(q_gh);
  630. error = gfs2_glock_nq_init(qd->qd_gl,
  631. LM_ST_EXCLUSIVE, GL_NOCACHE,
  632. q_gh);
  633. if (error)
  634. return error;
  635. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  636. if (error)
  637. goto fail;
  638. memset(buf, 0, sizeof(struct gfs2_quota));
  639. pos = qd2offset(qd);
  640. error = gfs2_internal_read(ip, &ra_state, buf,
  641. &pos, sizeof(struct gfs2_quota));
  642. if (error < 0)
  643. goto fail_gunlock;
  644. gfs2_glock_dq_uninit(&i_gh);
  645. gfs2_quota_in(&q, buf);
  646. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  647. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  648. qlvb->__pad = 0;
  649. qlvb->qb_limit = cpu_to_be64(q.qu_limit);
  650. qlvb->qb_warn = cpu_to_be64(q.qu_warn);
  651. qlvb->qb_value = cpu_to_be64(q.qu_value);
  652. qd->qd_qb = *qlvb;
  653. if (gfs2_glock_is_blocking(qd->qd_gl)) {
  654. gfs2_glock_dq_uninit(q_gh);
  655. force_refresh = 0;
  656. goto restart;
  657. }
  658. }
  659. return 0;
  660. fail_gunlock:
  661. gfs2_glock_dq_uninit(&i_gh);
  662. fail:
  663. gfs2_glock_dq_uninit(q_gh);
  664. return error;
  665. }
  666. int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
  667. {
  668. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  669. struct gfs2_alloc *al = &ip->i_alloc;
  670. unsigned int x;
  671. int error = 0;
  672. gfs2_quota_hold(ip, uid, gid);
  673. if (capable(CAP_SYS_RESOURCE) ||
  674. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  675. return 0;
  676. sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
  677. sort_qd, NULL);
  678. for (x = 0; x < al->al_qd_num; x++) {
  679. error = do_glock(al->al_qd[x], NO_FORCE, &al->al_qd_ghs[x]);
  680. if (error)
  681. break;
  682. }
  683. if (!error)
  684. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  685. else {
  686. while (x--)
  687. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  688. gfs2_quota_unhold(ip);
  689. }
  690. return error;
  691. }
  692. static int need_sync(struct gfs2_quota_data *qd)
  693. {
  694. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  695. struct gfs2_tune *gt = &sdp->sd_tune;
  696. s64 value;
  697. unsigned int num, den;
  698. int do_sync = 1;
  699. if (!qd->qd_qb.qb_limit)
  700. return 0;
  701. spin_lock(&sdp->sd_quota_spin);
  702. value = qd->qd_change;
  703. spin_unlock(&sdp->sd_quota_spin);
  704. spin_lock(&gt->gt_spin);
  705. num = gt->gt_quota_scale_num;
  706. den = gt->gt_quota_scale_den;
  707. spin_unlock(&gt->gt_spin);
  708. if (value < 0)
  709. do_sync = 0;
  710. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  711. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  712. do_sync = 0;
  713. else {
  714. value *= gfs2_jindex_size(sdp) * num;
  715. do_div(value, den);
  716. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  717. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  718. do_sync = 0;
  719. }
  720. return do_sync;
  721. }
  722. void gfs2_quota_unlock(struct gfs2_inode *ip)
  723. {
  724. struct gfs2_alloc *al = &ip->i_alloc;
  725. struct gfs2_quota_data *qda[4];
  726. unsigned int count = 0;
  727. unsigned int x;
  728. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  729. goto out;
  730. for (x = 0; x < al->al_qd_num; x++) {
  731. struct gfs2_quota_data *qd;
  732. int sync;
  733. qd = al->al_qd[x];
  734. sync = need_sync(qd);
  735. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  736. if (sync && qd_trylock(qd))
  737. qda[count++] = qd;
  738. }
  739. if (count) {
  740. do_sync(count, qda);
  741. for (x = 0; x < count; x++)
  742. qd_unlock(qda[x]);
  743. }
  744. out:
  745. gfs2_quota_unhold(ip);
  746. }
  747. #define MAX_LINE 256
  748. static int print_message(struct gfs2_quota_data *qd, char *type)
  749. {
  750. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  751. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\r\n",
  752. sdp->sd_fsname, type,
  753. (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
  754. qd->qd_id);
  755. return 0;
  756. }
  757. int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
  758. {
  759. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  760. struct gfs2_alloc *al = &ip->i_alloc;
  761. struct gfs2_quota_data *qd;
  762. s64 value;
  763. unsigned int x;
  764. int error = 0;
  765. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  766. return 0;
  767. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  768. return 0;
  769. for (x = 0; x < al->al_qd_num; x++) {
  770. qd = al->al_qd[x];
  771. if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  772. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
  773. continue;
  774. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  775. spin_lock(&sdp->sd_quota_spin);
  776. value += qd->qd_change;
  777. spin_unlock(&sdp->sd_quota_spin);
  778. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  779. print_message(qd, "exceeded");
  780. error = -EDQUOT;
  781. break;
  782. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  783. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  784. time_after_eq(jiffies, qd->qd_last_warn +
  785. gfs2_tune_get(sdp,
  786. gt_quota_warn_period) * HZ)) {
  787. error = print_message(qd, "warning");
  788. qd->qd_last_warn = jiffies;
  789. }
  790. }
  791. return error;
  792. }
  793. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  794. u32 uid, u32 gid)
  795. {
  796. struct gfs2_alloc *al = &ip->i_alloc;
  797. struct gfs2_quota_data *qd;
  798. unsigned int x;
  799. unsigned int found = 0;
  800. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  801. return;
  802. if (ip->i_di.di_flags & GFS2_DIF_SYSTEM)
  803. return;
  804. for (x = 0; x < al->al_qd_num; x++) {
  805. qd = al->al_qd[x];
  806. if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  807. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
  808. do_qc(qd, change);
  809. found++;
  810. }
  811. }
  812. }
  813. int gfs2_quota_sync(struct gfs2_sbd *sdp)
  814. {
  815. struct gfs2_quota_data **qda;
  816. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  817. unsigned int num_qd;
  818. unsigned int x;
  819. int error = 0;
  820. sdp->sd_quota_sync_gen++;
  821. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  822. if (!qda)
  823. return -ENOMEM;
  824. do {
  825. num_qd = 0;
  826. for (;;) {
  827. error = qd_fish(sdp, qda + num_qd);
  828. if (error || !qda[num_qd])
  829. break;
  830. if (++num_qd == max_qd)
  831. break;
  832. }
  833. if (num_qd) {
  834. if (!error)
  835. error = do_sync(num_qd, qda);
  836. if (!error)
  837. for (x = 0; x < num_qd; x++)
  838. qda[x]->qd_sync_gen =
  839. sdp->sd_quota_sync_gen;
  840. for (x = 0; x < num_qd; x++)
  841. qd_unlock(qda[x]);
  842. }
  843. } while (!error && num_qd == max_qd);
  844. kfree(qda);
  845. return error;
  846. }
  847. int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
  848. {
  849. struct gfs2_quota_data *qd;
  850. struct gfs2_holder q_gh;
  851. int error;
  852. error = qd_get(sdp, user, id, CREATE, &qd);
  853. if (error)
  854. return error;
  855. error = do_glock(qd, FORCE, &q_gh);
  856. if (!error)
  857. gfs2_glock_dq_uninit(&q_gh);
  858. qd_put(qd);
  859. return error;
  860. }
  861. int gfs2_quota_init(struct gfs2_sbd *sdp)
  862. {
  863. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  864. unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
  865. unsigned int x, slot = 0;
  866. unsigned int found = 0;
  867. u64 dblock;
  868. u32 extlen = 0;
  869. int error;
  870. if (!ip->i_di.di_size || ip->i_di.di_size > (64 << 20) ||
  871. ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
  872. gfs2_consist_inode(ip);
  873. return -EIO;
  874. }
  875. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  876. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  877. error = -ENOMEM;
  878. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  879. sizeof(unsigned char *), GFP_KERNEL);
  880. if (!sdp->sd_quota_bitmap)
  881. return error;
  882. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  883. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
  884. if (!sdp->sd_quota_bitmap[x])
  885. goto fail;
  886. }
  887. for (x = 0; x < blocks; x++) {
  888. struct buffer_head *bh;
  889. unsigned int y;
  890. if (!extlen) {
  891. int new = 0;
  892. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  893. if (error)
  894. goto fail;
  895. }
  896. error = -EIO;
  897. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  898. if (!bh)
  899. goto fail;
  900. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  901. brelse(bh);
  902. goto fail;
  903. }
  904. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  905. y++, slot++) {
  906. struct gfs2_quota_change_host qc;
  907. struct gfs2_quota_data *qd;
  908. gfs2_quota_change_in(&qc, bh->b_data +
  909. sizeof(struct gfs2_meta_header) +
  910. y * sizeof(struct gfs2_quota_change));
  911. if (!qc.qc_change)
  912. continue;
  913. error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
  914. qc.qc_id, &qd);
  915. if (error) {
  916. brelse(bh);
  917. goto fail;
  918. }
  919. set_bit(QDF_CHANGE, &qd->qd_flags);
  920. qd->qd_change = qc.qc_change;
  921. qd->qd_slot = slot;
  922. qd->qd_slot_count = 1;
  923. qd->qd_last_touched = jiffies;
  924. spin_lock(&sdp->sd_quota_spin);
  925. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  926. list_add(&qd->qd_list, &sdp->sd_quota_list);
  927. atomic_inc(&sdp->sd_quota_count);
  928. spin_unlock(&sdp->sd_quota_spin);
  929. found++;
  930. }
  931. brelse(bh);
  932. dblock++;
  933. extlen--;
  934. }
  935. if (found)
  936. fs_info(sdp, "found %u quota changes\n", found);
  937. return 0;
  938. fail:
  939. gfs2_quota_cleanup(sdp);
  940. return error;
  941. }
  942. void gfs2_quota_scan(struct gfs2_sbd *sdp)
  943. {
  944. struct gfs2_quota_data *qd, *safe;
  945. LIST_HEAD(dead);
  946. spin_lock(&sdp->sd_quota_spin);
  947. list_for_each_entry_safe(qd, safe, &sdp->sd_quota_list, qd_list) {
  948. if (!qd->qd_count &&
  949. time_after_eq(jiffies, qd->qd_last_touched +
  950. gfs2_tune_get(sdp, gt_quota_cache_secs) * HZ)) {
  951. list_move(&qd->qd_list, &dead);
  952. gfs2_assert_warn(sdp,
  953. atomic_read(&sdp->sd_quota_count) > 0);
  954. atomic_dec(&sdp->sd_quota_count);
  955. }
  956. }
  957. spin_unlock(&sdp->sd_quota_spin);
  958. while (!list_empty(&dead)) {
  959. qd = list_entry(dead.next, struct gfs2_quota_data, qd_list);
  960. list_del(&qd->qd_list);
  961. gfs2_assert_warn(sdp, !qd->qd_change);
  962. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  963. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  964. gfs2_lvb_unhold(qd->qd_gl);
  965. kfree(qd);
  966. }
  967. }
  968. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  969. {
  970. struct list_head *head = &sdp->sd_quota_list;
  971. struct gfs2_quota_data *qd;
  972. unsigned int x;
  973. spin_lock(&sdp->sd_quota_spin);
  974. while (!list_empty(head)) {
  975. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  976. if (qd->qd_count > 1 ||
  977. (qd->qd_count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  978. list_move(&qd->qd_list, head);
  979. spin_unlock(&sdp->sd_quota_spin);
  980. schedule();
  981. spin_lock(&sdp->sd_quota_spin);
  982. continue;
  983. }
  984. list_del(&qd->qd_list);
  985. atomic_dec(&sdp->sd_quota_count);
  986. spin_unlock(&sdp->sd_quota_spin);
  987. if (!qd->qd_count) {
  988. gfs2_assert_warn(sdp, !qd->qd_change);
  989. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  990. } else
  991. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  992. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  993. gfs2_lvb_unhold(qd->qd_gl);
  994. kfree(qd);
  995. spin_lock(&sdp->sd_quota_spin);
  996. }
  997. spin_unlock(&sdp->sd_quota_spin);
  998. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  999. if (sdp->sd_quota_bitmap) {
  1000. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1001. kfree(sdp->sd_quota_bitmap[x]);
  1002. kfree(sdp->sd_quota_bitmap);
  1003. }
  1004. }