quota.c 27 KB

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