quota.c 27 KB

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