quota.c 31 KB

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