quota.c 28 KB

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