quota.c 27 KB

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