quota.c 28 KB

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