quota.c 27 KB

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