quota.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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 for 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/mm.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/completion.h>
  42. #include <linux/buffer_head.h>
  43. #include <linux/sort.h>
  44. #include <linux/fs.h>
  45. #include <linux/bio.h>
  46. #include <linux/gfs2_ondisk.h>
  47. #include <linux/kthread.h>
  48. #include <linux/freezer.h>
  49. #include <linux/quota.h>
  50. #include <linux/dqblk_xfs.h>
  51. #include "gfs2.h"
  52. #include "incore.h"
  53. #include "bmap.h"
  54. #include "glock.h"
  55. #include "glops.h"
  56. #include "log.h"
  57. #include "meta_io.h"
  58. #include "quota.h"
  59. #include "rgrp.h"
  60. #include "super.h"
  61. #include "trans.h"
  62. #include "inode.h"
  63. #include "util.h"
  64. struct gfs2_quota_change_host {
  65. u64 qc_change;
  66. u32 qc_flags; /* GFS2_QCF_... */
  67. struct kqid qc_id;
  68. };
  69. static LIST_HEAD(qd_lru_list);
  70. static atomic_t qd_lru_count = ATOMIC_INIT(0);
  71. static DEFINE_SPINLOCK(qd_lru_lock);
  72. int gfs2_shrink_qd_memory(struct shrinker *shrink, struct shrink_control *sc)
  73. {
  74. struct gfs2_quota_data *qd;
  75. struct gfs2_sbd *sdp;
  76. int nr_to_scan = sc->nr_to_scan;
  77. if (nr_to_scan == 0)
  78. goto out;
  79. if (!(sc->gfp_mask & __GFP_FS))
  80. return -1;
  81. spin_lock(&qd_lru_lock);
  82. while (nr_to_scan && !list_empty(&qd_lru_list)) {
  83. qd = list_entry(qd_lru_list.next,
  84. struct gfs2_quota_data, qd_reclaim);
  85. sdp = qd->qd_gl->gl_sbd;
  86. /* Free from the filesystem-specific list */
  87. list_del(&qd->qd_list);
  88. gfs2_assert_warn(sdp, !qd->qd_change);
  89. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  90. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  91. gfs2_glock_put(qd->qd_gl);
  92. atomic_dec(&sdp->sd_quota_count);
  93. /* Delete it from the common reclaim list */
  94. list_del_init(&qd->qd_reclaim);
  95. atomic_dec(&qd_lru_count);
  96. spin_unlock(&qd_lru_lock);
  97. kmem_cache_free(gfs2_quotad_cachep, qd);
  98. spin_lock(&qd_lru_lock);
  99. nr_to_scan--;
  100. }
  101. spin_unlock(&qd_lru_lock);
  102. out:
  103. return (atomic_read(&qd_lru_count) * sysctl_vfs_cache_pressure) / 100;
  104. }
  105. static u64 qd2index(struct gfs2_quota_data *qd)
  106. {
  107. struct kqid qid = qd->qd_id;
  108. return (2 * (u64)from_kqid(&init_user_ns, qid)) +
  109. ((qid.type == USRQUOTA) ? 0 : 1);
  110. }
  111. static u64 qd2offset(struct gfs2_quota_data *qd)
  112. {
  113. u64 offset;
  114. offset = qd2index(qd);
  115. offset *= sizeof(struct gfs2_quota);
  116. return offset;
  117. }
  118. static int qd_alloc(struct gfs2_sbd *sdp, struct kqid qid,
  119. struct gfs2_quota_data **qdp)
  120. {
  121. struct gfs2_quota_data *qd;
  122. int error;
  123. qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
  124. if (!qd)
  125. return -ENOMEM;
  126. atomic_set(&qd->qd_count, 1);
  127. qd->qd_id = qid;
  128. qd->qd_slot = -1;
  129. INIT_LIST_HEAD(&qd->qd_reclaim);
  130. error = gfs2_glock_get(sdp, qd2index(qd),
  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, struct kqid qid,
  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 (qid_eq(qd->qd_id, qid)) {
  151. if (!atomic_read(&qd->qd_count) &&
  152. !list_empty(&qd->qd_reclaim)) {
  153. /* Remove it from reclaim list */
  154. list_del_init(&qd->qd_reclaim);
  155. atomic_dec(&qd_lru_count);
  156. }
  157. atomic_inc(&qd->qd_count);
  158. found = 1;
  159. break;
  160. }
  161. }
  162. if (!found)
  163. qd = NULL;
  164. if (!qd && new_qd) {
  165. qd = new_qd;
  166. list_add(&qd->qd_list, &sdp->sd_quota_list);
  167. atomic_inc(&sdp->sd_quota_count);
  168. new_qd = NULL;
  169. }
  170. spin_unlock(&qd_lru_lock);
  171. if (qd) {
  172. if (new_qd) {
  173. gfs2_glock_put(new_qd->qd_gl);
  174. kmem_cache_free(gfs2_quotad_cachep, new_qd);
  175. }
  176. *qdp = qd;
  177. return 0;
  178. }
  179. error = qd_alloc(sdp, qid, &new_qd);
  180. if (error)
  181. return error;
  182. }
  183. }
  184. static void qd_hold(struct gfs2_quota_data *qd)
  185. {
  186. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  187. gfs2_assert(sdp, atomic_read(&qd->qd_count));
  188. atomic_inc(&qd->qd_count);
  189. }
  190. static void qd_put(struct gfs2_quota_data *qd)
  191. {
  192. if (atomic_dec_and_lock(&qd->qd_count, &qd_lru_lock)) {
  193. /* Add to the reclaim list */
  194. list_add_tail(&qd->qd_reclaim, &qd_lru_list);
  195. atomic_inc(&qd_lru_count);
  196. spin_unlock(&qd_lru_lock);
  197. }
  198. }
  199. static int slot_get(struct gfs2_quota_data *qd)
  200. {
  201. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  202. unsigned int c, o = 0, b;
  203. unsigned char byte = 0;
  204. spin_lock(&qd_lru_lock);
  205. if (qd->qd_slot_count++) {
  206. spin_unlock(&qd_lru_lock);
  207. return 0;
  208. }
  209. for (c = 0; c < sdp->sd_quota_chunks; c++)
  210. for (o = 0; o < PAGE_SIZE; o++) {
  211. byte = sdp->sd_quota_bitmap[c][o];
  212. if (byte != 0xFF)
  213. goto found;
  214. }
  215. goto fail;
  216. found:
  217. for (b = 0; b < 8; b++)
  218. if (!(byte & (1 << b)))
  219. break;
  220. qd->qd_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
  221. if (qd->qd_slot >= sdp->sd_quota_slots)
  222. goto fail;
  223. sdp->sd_quota_bitmap[c][o] |= 1 << b;
  224. spin_unlock(&qd_lru_lock);
  225. return 0;
  226. fail:
  227. qd->qd_slot_count--;
  228. spin_unlock(&qd_lru_lock);
  229. return -ENOSPC;
  230. }
  231. static void slot_hold(struct gfs2_quota_data *qd)
  232. {
  233. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  234. spin_lock(&qd_lru_lock);
  235. gfs2_assert(sdp, qd->qd_slot_count);
  236. qd->qd_slot_count++;
  237. spin_unlock(&qd_lru_lock);
  238. }
  239. static void slot_put(struct gfs2_quota_data *qd)
  240. {
  241. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  242. spin_lock(&qd_lru_lock);
  243. gfs2_assert(sdp, qd->qd_slot_count);
  244. if (!--qd->qd_slot_count) {
  245. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, qd->qd_slot, 0);
  246. qd->qd_slot = -1;
  247. }
  248. spin_unlock(&qd_lru_lock);
  249. }
  250. static int bh_get(struct gfs2_quota_data *qd)
  251. {
  252. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  253. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  254. unsigned int block, offset;
  255. struct buffer_head *bh;
  256. int error;
  257. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  258. mutex_lock(&sdp->sd_quota_mutex);
  259. if (qd->qd_bh_count++) {
  260. mutex_unlock(&sdp->sd_quota_mutex);
  261. return 0;
  262. }
  263. block = qd->qd_slot / sdp->sd_qc_per_block;
  264. offset = qd->qd_slot % sdp->sd_qc_per_block;
  265. bh_map.b_size = 1 << ip->i_inode.i_blkbits;
  266. error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
  267. if (error)
  268. goto fail;
  269. error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, &bh);
  270. if (error)
  271. goto fail;
  272. error = -EIO;
  273. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  274. goto fail_brelse;
  275. qd->qd_bh = bh;
  276. qd->qd_bh_qc = (struct gfs2_quota_change *)
  277. (bh->b_data + sizeof(struct gfs2_meta_header) +
  278. offset * sizeof(struct gfs2_quota_change));
  279. mutex_unlock(&sdp->sd_quota_mutex);
  280. return 0;
  281. fail_brelse:
  282. brelse(bh);
  283. fail:
  284. qd->qd_bh_count--;
  285. mutex_unlock(&sdp->sd_quota_mutex);
  286. return error;
  287. }
  288. static void bh_put(struct gfs2_quota_data *qd)
  289. {
  290. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  291. mutex_lock(&sdp->sd_quota_mutex);
  292. gfs2_assert(sdp, qd->qd_bh_count);
  293. if (!--qd->qd_bh_count) {
  294. brelse(qd->qd_bh);
  295. qd->qd_bh = NULL;
  296. qd->qd_bh_qc = NULL;
  297. }
  298. mutex_unlock(&sdp->sd_quota_mutex);
  299. }
  300. static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
  301. {
  302. struct gfs2_quota_data *qd = NULL;
  303. int error;
  304. int found = 0;
  305. *qdp = NULL;
  306. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  307. return 0;
  308. spin_lock(&qd_lru_lock);
  309. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  310. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  311. !test_bit(QDF_CHANGE, &qd->qd_flags) ||
  312. qd->qd_sync_gen >= sdp->sd_quota_sync_gen)
  313. continue;
  314. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  315. set_bit(QDF_LOCKED, &qd->qd_flags);
  316. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  317. atomic_inc(&qd->qd_count);
  318. qd->qd_change_sync = qd->qd_change;
  319. gfs2_assert_warn(sdp, qd->qd_slot_count);
  320. qd->qd_slot_count++;
  321. found = 1;
  322. break;
  323. }
  324. if (!found)
  325. qd = NULL;
  326. spin_unlock(&qd_lru_lock);
  327. if (qd) {
  328. gfs2_assert_warn(sdp, qd->qd_change_sync);
  329. error = bh_get(qd);
  330. if (error) {
  331. clear_bit(QDF_LOCKED, &qd->qd_flags);
  332. slot_put(qd);
  333. qd_put(qd);
  334. return error;
  335. }
  336. }
  337. *qdp = qd;
  338. return 0;
  339. }
  340. static int qd_trylock(struct gfs2_quota_data *qd)
  341. {
  342. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  343. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  344. return 0;
  345. spin_lock(&qd_lru_lock);
  346. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  347. !test_bit(QDF_CHANGE, &qd->qd_flags)) {
  348. spin_unlock(&qd_lru_lock);
  349. return 0;
  350. }
  351. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  352. set_bit(QDF_LOCKED, &qd->qd_flags);
  353. gfs2_assert_warn(sdp, atomic_read(&qd->qd_count));
  354. atomic_inc(&qd->qd_count);
  355. qd->qd_change_sync = qd->qd_change;
  356. gfs2_assert_warn(sdp, qd->qd_slot_count);
  357. qd->qd_slot_count++;
  358. spin_unlock(&qd_lru_lock);
  359. gfs2_assert_warn(sdp, qd->qd_change_sync);
  360. if (bh_get(qd)) {
  361. clear_bit(QDF_LOCKED, &qd->qd_flags);
  362. slot_put(qd);
  363. qd_put(qd);
  364. return 0;
  365. }
  366. return 1;
  367. }
  368. static void qd_unlock(struct gfs2_quota_data *qd)
  369. {
  370. gfs2_assert_warn(qd->qd_gl->gl_sbd,
  371. test_bit(QDF_LOCKED, &qd->qd_flags));
  372. clear_bit(QDF_LOCKED, &qd->qd_flags);
  373. bh_put(qd);
  374. slot_put(qd);
  375. qd_put(qd);
  376. }
  377. static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
  378. struct gfs2_quota_data **qdp)
  379. {
  380. int error;
  381. error = qd_get(sdp, qid, qdp);
  382. if (error)
  383. return error;
  384. error = slot_get(*qdp);
  385. if (error)
  386. goto fail;
  387. error = bh_get(*qdp);
  388. if (error)
  389. goto fail_slot;
  390. return 0;
  391. fail_slot:
  392. slot_put(*qdp);
  393. fail:
  394. qd_put(*qdp);
  395. return error;
  396. }
  397. static void qdsb_put(struct gfs2_quota_data *qd)
  398. {
  399. bh_put(qd);
  400. slot_put(qd);
  401. qd_put(qd);
  402. }
  403. int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  404. {
  405. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  406. struct gfs2_quota_data **qd;
  407. int error;
  408. if (ip->i_res == NULL) {
  409. error = gfs2_rs_alloc(ip);
  410. if (error)
  411. return error;
  412. }
  413. qd = ip->i_res->rs_qa_qd;
  414. if (gfs2_assert_warn(sdp, !ip->i_res->rs_qa_qd_num) ||
  415. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
  416. return -EIO;
  417. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  418. return 0;
  419. error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
  420. if (error)
  421. goto out;
  422. ip->i_res->rs_qa_qd_num++;
  423. qd++;
  424. error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
  425. if (error)
  426. goto out;
  427. ip->i_res->rs_qa_qd_num++;
  428. qd++;
  429. if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
  430. !uid_eq(uid, ip->i_inode.i_uid)) {
  431. error = qdsb_get(sdp, make_kqid_uid(uid), qd);
  432. if (error)
  433. goto out;
  434. ip->i_res->rs_qa_qd_num++;
  435. qd++;
  436. }
  437. if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
  438. !gid_eq(gid, ip->i_inode.i_gid)) {
  439. error = qdsb_get(sdp, make_kqid_gid(gid), qd);
  440. if (error)
  441. goto out;
  442. ip->i_res->rs_qa_qd_num++;
  443. qd++;
  444. }
  445. out:
  446. if (error)
  447. gfs2_quota_unhold(ip);
  448. return error;
  449. }
  450. void gfs2_quota_unhold(struct gfs2_inode *ip)
  451. {
  452. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  453. unsigned int x;
  454. if (ip->i_res == NULL)
  455. return;
  456. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  457. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  458. qdsb_put(ip->i_res->rs_qa_qd[x]);
  459. ip->i_res->rs_qa_qd[x] = NULL;
  460. }
  461. ip->i_res->rs_qa_qd_num = 0;
  462. }
  463. static int sort_qd(const void *a, const void *b)
  464. {
  465. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  466. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  467. if (qid_lt(qd_a->qd_id, qd_b->qd_id))
  468. return -1;
  469. if (qid_lt(qd_b->qd_id, qd_a->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_meta(ip->i_gl, qd->qd_bh);
  481. if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
  482. qc->qc_change = 0;
  483. qc->qc_flags = 0;
  484. if (qd->qd_id.type == USRQUOTA)
  485. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  486. qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, 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. /**
  507. * gfs2_adjust_quota - adjust record of current block usage
  508. * @ip: The quota inode
  509. * @loc: Offset of the entry in the quota file
  510. * @change: The amount of usage change to record
  511. * @qd: The quota data
  512. * @fdq: The updated limits to record
  513. *
  514. * This function was mostly borrowed from gfs2_block_truncate_page which was
  515. * in turn mostly borrowed from ext3
  516. *
  517. * Returns: 0 or -ve on error
  518. */
  519. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  520. s64 change, struct gfs2_quota_data *qd,
  521. struct fs_disk_quota *fdq)
  522. {
  523. struct inode *inode = &ip->i_inode;
  524. struct gfs2_sbd *sdp = GFS2_SB(inode);
  525. struct address_space *mapping = inode->i_mapping;
  526. unsigned long index = loc >> PAGE_CACHE_SHIFT;
  527. unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
  528. unsigned blocksize, iblock, pos;
  529. struct buffer_head *bh;
  530. struct page *page;
  531. void *kaddr, *ptr;
  532. struct gfs2_quota q, *qp;
  533. int err, nbytes;
  534. u64 size;
  535. if (gfs2_is_stuffed(ip)) {
  536. err = gfs2_unstuff_dinode(ip, NULL);
  537. if (err)
  538. return err;
  539. }
  540. memset(&q, 0, sizeof(struct gfs2_quota));
  541. err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
  542. if (err < 0)
  543. return err;
  544. err = -EIO;
  545. qp = &q;
  546. qp->qu_value = be64_to_cpu(qp->qu_value);
  547. qp->qu_value += change;
  548. qp->qu_value = cpu_to_be64(qp->qu_value);
  549. qd->qd_qb.qb_value = qp->qu_value;
  550. if (fdq) {
  551. if (fdq->d_fieldmask & FS_DQ_BSOFT) {
  552. qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
  553. qd->qd_qb.qb_warn = qp->qu_warn;
  554. }
  555. if (fdq->d_fieldmask & FS_DQ_BHARD) {
  556. qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
  557. qd->qd_qb.qb_limit = qp->qu_limit;
  558. }
  559. if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
  560. qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
  561. qd->qd_qb.qb_value = qp->qu_value;
  562. }
  563. }
  564. /* Write the quota into the quota file on disk */
  565. ptr = qp;
  566. nbytes = sizeof(struct gfs2_quota);
  567. get_a_page:
  568. page = find_or_create_page(mapping, index, GFP_NOFS);
  569. if (!page)
  570. return -ENOMEM;
  571. blocksize = inode->i_sb->s_blocksize;
  572. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  573. if (!page_has_buffers(page))
  574. create_empty_buffers(page, blocksize, 0);
  575. bh = page_buffers(page);
  576. pos = blocksize;
  577. while (offset >= pos) {
  578. bh = bh->b_this_page;
  579. iblock++;
  580. pos += blocksize;
  581. }
  582. if (!buffer_mapped(bh)) {
  583. gfs2_block_map(inode, iblock, bh, 1);
  584. if (!buffer_mapped(bh))
  585. goto unlock_out;
  586. /* If it's a newly allocated disk block for quota, zero it */
  587. if (buffer_new(bh))
  588. zero_user(page, pos - blocksize, bh->b_size);
  589. }
  590. if (PageUptodate(page))
  591. set_buffer_uptodate(bh);
  592. if (!buffer_uptodate(bh)) {
  593. ll_rw_block(READ | REQ_META, 1, &bh);
  594. wait_on_buffer(bh);
  595. if (!buffer_uptodate(bh))
  596. goto unlock_out;
  597. }
  598. gfs2_trans_add_data(ip->i_gl, bh);
  599. kaddr = kmap_atomic(page);
  600. if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
  601. nbytes = PAGE_CACHE_SIZE - offset;
  602. memcpy(kaddr + offset, ptr, nbytes);
  603. flush_dcache_page(page);
  604. kunmap_atomic(kaddr);
  605. unlock_page(page);
  606. page_cache_release(page);
  607. /* If quota straddles page boundary, we need to update the rest of the
  608. * quota at the beginning of the next page */
  609. if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
  610. ptr = ptr + nbytes;
  611. nbytes = sizeof(struct gfs2_quota) - nbytes;
  612. offset = 0;
  613. index++;
  614. goto get_a_page;
  615. }
  616. size = loc + sizeof(struct gfs2_quota);
  617. if (size > inode->i_size)
  618. i_size_write(inode, size);
  619. inode->i_mtime = inode->i_atime = CURRENT_TIME;
  620. mark_inode_dirty(inode);
  621. return 0;
  622. unlock_out:
  623. unlock_page(page);
  624. page_cache_release(page);
  625. return err;
  626. }
  627. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  628. {
  629. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  630. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  631. unsigned int data_blocks, ind_blocks;
  632. struct gfs2_holder *ghs, i_gh;
  633. unsigned int qx, x;
  634. struct gfs2_quota_data *qd;
  635. unsigned reserved;
  636. loff_t offset;
  637. unsigned int nalloc = 0, blocks;
  638. int error;
  639. error = gfs2_rs_alloc(ip);
  640. if (error)
  641. return error;
  642. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  643. &data_blocks, &ind_blocks);
  644. ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
  645. if (!ghs)
  646. return -ENOMEM;
  647. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  648. mutex_lock(&ip->i_inode.i_mutex);
  649. for (qx = 0; qx < num_qd; qx++) {
  650. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  651. GL_NOCACHE, &ghs[qx]);
  652. if (error)
  653. goto out;
  654. }
  655. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  656. if (error)
  657. goto out;
  658. for (x = 0; x < num_qd; x++) {
  659. offset = qd2offset(qda[x]);
  660. if (gfs2_write_alloc_required(ip, offset,
  661. sizeof(struct gfs2_quota)))
  662. nalloc++;
  663. }
  664. /*
  665. * 1 blk for unstuffing inode if stuffed. We add this extra
  666. * block to the reservation unconditionally. If the inode
  667. * doesn't need unstuffing, the block will be released to the
  668. * rgrp since it won't be allocated during the transaction
  669. */
  670. /* +3 in the end for unstuffing block, inode size update block
  671. * and another block in case quota straddles page boundary and
  672. * two blocks need to be updated instead of 1 */
  673. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  674. reserved = 1 + (nalloc * (data_blocks + ind_blocks));
  675. error = gfs2_inplace_reserve(ip, reserved, 0);
  676. if (error)
  677. goto out_alloc;
  678. if (nalloc)
  679. blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
  680. error = gfs2_trans_begin(sdp, blocks, 0);
  681. if (error)
  682. goto out_ipres;
  683. for (x = 0; x < num_qd; x++) {
  684. qd = qda[x];
  685. offset = qd2offset(qd);
  686. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
  687. if (error)
  688. goto out_end_trans;
  689. do_qc(qd, -qd->qd_change_sync);
  690. set_bit(QDF_REFRESH, &qd->qd_flags);
  691. }
  692. error = 0;
  693. out_end_trans:
  694. gfs2_trans_end(sdp);
  695. out_ipres:
  696. gfs2_inplace_release(ip);
  697. out_alloc:
  698. gfs2_glock_dq_uninit(&i_gh);
  699. out:
  700. while (qx--)
  701. gfs2_glock_dq_uninit(&ghs[qx]);
  702. mutex_unlock(&ip->i_inode.i_mutex);
  703. kfree(ghs);
  704. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  705. return error;
  706. }
  707. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  708. {
  709. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  710. struct gfs2_quota q;
  711. struct gfs2_quota_lvb *qlvb;
  712. loff_t pos;
  713. int error;
  714. memset(&q, 0, sizeof(struct gfs2_quota));
  715. pos = qd2offset(qd);
  716. error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
  717. if (error < 0)
  718. return error;
  719. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  720. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  721. qlvb->__pad = 0;
  722. qlvb->qb_limit = q.qu_limit;
  723. qlvb->qb_warn = q.qu_warn;
  724. qlvb->qb_value = q.qu_value;
  725. qd->qd_qb = *qlvb;
  726. return 0;
  727. }
  728. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  729. struct gfs2_holder *q_gh)
  730. {
  731. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  732. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  733. struct gfs2_holder i_gh;
  734. int error;
  735. restart:
  736. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  737. if (error)
  738. return error;
  739. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  740. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  741. gfs2_glock_dq_uninit(q_gh);
  742. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  743. GL_NOCACHE, q_gh);
  744. if (error)
  745. return error;
  746. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  747. if (error)
  748. goto fail;
  749. error = update_qd(sdp, qd);
  750. if (error)
  751. goto fail_gunlock;
  752. gfs2_glock_dq_uninit(&i_gh);
  753. gfs2_glock_dq_uninit(q_gh);
  754. force_refresh = 0;
  755. goto restart;
  756. }
  757. return 0;
  758. fail_gunlock:
  759. gfs2_glock_dq_uninit(&i_gh);
  760. fail:
  761. gfs2_glock_dq_uninit(q_gh);
  762. return error;
  763. }
  764. int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  765. {
  766. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  767. struct gfs2_quota_data *qd;
  768. unsigned int x;
  769. int error = 0;
  770. error = gfs2_quota_hold(ip, uid, gid);
  771. if (error)
  772. return error;
  773. if (capable(CAP_SYS_RESOURCE) ||
  774. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  775. return 0;
  776. sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
  777. sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  778. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  779. int force = NO_FORCE;
  780. qd = ip->i_res->rs_qa_qd[x];
  781. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  782. force = FORCE;
  783. error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
  784. if (error)
  785. break;
  786. }
  787. if (!error)
  788. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  789. else {
  790. while (x--)
  791. gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
  792. gfs2_quota_unhold(ip);
  793. }
  794. return error;
  795. }
  796. static int need_sync(struct gfs2_quota_data *qd)
  797. {
  798. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  799. struct gfs2_tune *gt = &sdp->sd_tune;
  800. s64 value;
  801. unsigned int num, den;
  802. int do_sync = 1;
  803. if (!qd->qd_qb.qb_limit)
  804. return 0;
  805. spin_lock(&qd_lru_lock);
  806. value = qd->qd_change;
  807. spin_unlock(&qd_lru_lock);
  808. spin_lock(&gt->gt_spin);
  809. num = gt->gt_quota_scale_num;
  810. den = gt->gt_quota_scale_den;
  811. spin_unlock(&gt->gt_spin);
  812. if (value < 0)
  813. do_sync = 0;
  814. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  815. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  816. do_sync = 0;
  817. else {
  818. value *= gfs2_jindex_size(sdp) * num;
  819. value = div_s64(value, den);
  820. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  821. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  822. do_sync = 0;
  823. }
  824. return do_sync;
  825. }
  826. void gfs2_quota_unlock(struct gfs2_inode *ip)
  827. {
  828. struct gfs2_quota_data *qda[4];
  829. unsigned int count = 0;
  830. unsigned int x;
  831. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  832. goto out;
  833. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  834. struct gfs2_quota_data *qd;
  835. int sync;
  836. qd = ip->i_res->rs_qa_qd[x];
  837. sync = need_sync(qd);
  838. gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
  839. if (sync && qd_trylock(qd))
  840. qda[count++] = qd;
  841. }
  842. if (count) {
  843. do_sync(count, qda);
  844. for (x = 0; x < count; x++)
  845. qd_unlock(qda[x]);
  846. }
  847. out:
  848. gfs2_quota_unhold(ip);
  849. }
  850. #define MAX_LINE 256
  851. static int print_message(struct gfs2_quota_data *qd, char *type)
  852. {
  853. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  854. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
  855. sdp->sd_fsname, type,
  856. (qd->qd_id.type == USRQUOTA) ? "user" : "group",
  857. from_kqid(&init_user_ns, qd->qd_id));
  858. return 0;
  859. }
  860. int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  861. {
  862. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  863. struct gfs2_quota_data *qd;
  864. s64 value;
  865. unsigned int x;
  866. int error = 0;
  867. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  868. return 0;
  869. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  870. return 0;
  871. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  872. qd = ip->i_res->rs_qa_qd[x];
  873. if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  874. qid_eq(qd->qd_id, make_kqid_gid(gid))))
  875. continue;
  876. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  877. spin_lock(&qd_lru_lock);
  878. value += qd->qd_change;
  879. spin_unlock(&qd_lru_lock);
  880. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  881. print_message(qd, "exceeded");
  882. quota_send_warning(qd->qd_id,
  883. sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
  884. error = -EDQUOT;
  885. break;
  886. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  887. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  888. time_after_eq(jiffies, qd->qd_last_warn +
  889. gfs2_tune_get(sdp,
  890. gt_quota_warn_period) * HZ)) {
  891. quota_send_warning(qd->qd_id,
  892. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  893. error = print_message(qd, "warning");
  894. qd->qd_last_warn = jiffies;
  895. }
  896. }
  897. return error;
  898. }
  899. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  900. kuid_t uid, kgid_t gid)
  901. {
  902. struct gfs2_quota_data *qd;
  903. unsigned int x;
  904. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  905. return;
  906. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  907. return;
  908. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  909. qd = ip->i_res->rs_qa_qd[x];
  910. if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  911. qid_eq(qd->qd_id, make_kqid_gid(gid))) {
  912. do_qc(qd, change);
  913. }
  914. }
  915. }
  916. int gfs2_quota_sync(struct super_block *sb, int type)
  917. {
  918. struct gfs2_sbd *sdp = sb->s_fs_info;
  919. struct gfs2_quota_data **qda;
  920. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  921. unsigned int num_qd;
  922. unsigned int x;
  923. int error = 0;
  924. sdp->sd_quota_sync_gen++;
  925. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  926. if (!qda)
  927. return -ENOMEM;
  928. do {
  929. num_qd = 0;
  930. for (;;) {
  931. error = qd_fish(sdp, qda + num_qd);
  932. if (error || !qda[num_qd])
  933. break;
  934. if (++num_qd == max_qd)
  935. break;
  936. }
  937. if (num_qd) {
  938. if (!error)
  939. error = do_sync(num_qd, qda);
  940. if (!error)
  941. for (x = 0; x < num_qd; x++)
  942. qda[x]->qd_sync_gen =
  943. sdp->sd_quota_sync_gen;
  944. for (x = 0; x < num_qd; x++)
  945. qd_unlock(qda[x]);
  946. }
  947. } while (!error && num_qd == max_qd);
  948. kfree(qda);
  949. return error;
  950. }
  951. int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
  952. {
  953. struct gfs2_quota_data *qd;
  954. struct gfs2_holder q_gh;
  955. int error;
  956. error = qd_get(sdp, qid, &qd);
  957. if (error)
  958. return error;
  959. error = do_glock(qd, FORCE, &q_gh);
  960. if (!error)
  961. gfs2_glock_dq_uninit(&q_gh);
  962. qd_put(qd);
  963. return error;
  964. }
  965. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  966. {
  967. const struct gfs2_quota_change *str = buf;
  968. qc->qc_change = be64_to_cpu(str->qc_change);
  969. qc->qc_flags = be32_to_cpu(str->qc_flags);
  970. qc->qc_id = make_kqid(&init_user_ns,
  971. (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA,
  972. be32_to_cpu(str->qc_id));
  973. }
  974. int gfs2_quota_init(struct gfs2_sbd *sdp)
  975. {
  976. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  977. u64 size = i_size_read(sdp->sd_qc_inode);
  978. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  979. unsigned int x, slot = 0;
  980. unsigned int found = 0;
  981. u64 dblock;
  982. u32 extlen = 0;
  983. int error;
  984. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  985. return -EIO;
  986. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  987. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  988. error = -ENOMEM;
  989. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  990. sizeof(unsigned char *), GFP_NOFS);
  991. if (!sdp->sd_quota_bitmap)
  992. return error;
  993. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  994. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  995. if (!sdp->sd_quota_bitmap[x])
  996. goto fail;
  997. }
  998. for (x = 0; x < blocks; x++) {
  999. struct buffer_head *bh;
  1000. unsigned int y;
  1001. if (!extlen) {
  1002. int new = 0;
  1003. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1004. if (error)
  1005. goto fail;
  1006. }
  1007. error = -EIO;
  1008. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1009. if (!bh)
  1010. goto fail;
  1011. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1012. brelse(bh);
  1013. goto fail;
  1014. }
  1015. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1016. y++, slot++) {
  1017. struct gfs2_quota_change_host qc;
  1018. struct gfs2_quota_data *qd;
  1019. gfs2_quota_change_in(&qc, bh->b_data +
  1020. sizeof(struct gfs2_meta_header) +
  1021. y * sizeof(struct gfs2_quota_change));
  1022. if (!qc.qc_change)
  1023. continue;
  1024. error = qd_alloc(sdp, qc.qc_id, &qd);
  1025. if (error) {
  1026. brelse(bh);
  1027. goto fail;
  1028. }
  1029. set_bit(QDF_CHANGE, &qd->qd_flags);
  1030. qd->qd_change = qc.qc_change;
  1031. qd->qd_slot = slot;
  1032. qd->qd_slot_count = 1;
  1033. spin_lock(&qd_lru_lock);
  1034. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  1035. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1036. atomic_inc(&sdp->sd_quota_count);
  1037. spin_unlock(&qd_lru_lock);
  1038. found++;
  1039. }
  1040. brelse(bh);
  1041. dblock++;
  1042. extlen--;
  1043. }
  1044. if (found)
  1045. fs_info(sdp, "found %u quota changes\n", found);
  1046. return 0;
  1047. fail:
  1048. gfs2_quota_cleanup(sdp);
  1049. return error;
  1050. }
  1051. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1052. {
  1053. struct list_head *head = &sdp->sd_quota_list;
  1054. struct gfs2_quota_data *qd;
  1055. unsigned int x;
  1056. spin_lock(&qd_lru_lock);
  1057. while (!list_empty(head)) {
  1058. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1059. if (atomic_read(&qd->qd_count) > 1 ||
  1060. (atomic_read(&qd->qd_count) &&
  1061. !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1062. list_move(&qd->qd_list, head);
  1063. spin_unlock(&qd_lru_lock);
  1064. schedule();
  1065. spin_lock(&qd_lru_lock);
  1066. continue;
  1067. }
  1068. list_del(&qd->qd_list);
  1069. /* Also remove if this qd exists in the reclaim list */
  1070. if (!list_empty(&qd->qd_reclaim)) {
  1071. list_del_init(&qd->qd_reclaim);
  1072. atomic_dec(&qd_lru_count);
  1073. }
  1074. atomic_dec(&sdp->sd_quota_count);
  1075. spin_unlock(&qd_lru_lock);
  1076. if (!atomic_read(&qd->qd_count)) {
  1077. gfs2_assert_warn(sdp, !qd->qd_change);
  1078. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1079. } else
  1080. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1081. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1082. gfs2_glock_put(qd->qd_gl);
  1083. kmem_cache_free(gfs2_quotad_cachep, qd);
  1084. spin_lock(&qd_lru_lock);
  1085. }
  1086. spin_unlock(&qd_lru_lock);
  1087. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1088. if (sdp->sd_quota_bitmap) {
  1089. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1090. kfree(sdp->sd_quota_bitmap[x]);
  1091. kfree(sdp->sd_quota_bitmap);
  1092. }
  1093. }
  1094. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1095. {
  1096. if (error == 0 || error == -EROFS)
  1097. return;
  1098. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1099. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1100. }
  1101. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1102. int (*fxn)(struct super_block *sb, int type),
  1103. unsigned long t, unsigned long *timeo,
  1104. unsigned int *new_timeo)
  1105. {
  1106. if (t >= *timeo) {
  1107. int error = fxn(sdp->sd_vfs, 0);
  1108. quotad_error(sdp, msg, error);
  1109. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1110. } else {
  1111. *timeo -= t;
  1112. }
  1113. }
  1114. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1115. {
  1116. struct gfs2_inode *ip;
  1117. while(1) {
  1118. ip = NULL;
  1119. spin_lock(&sdp->sd_trunc_lock);
  1120. if (!list_empty(&sdp->sd_trunc_list)) {
  1121. ip = list_entry(sdp->sd_trunc_list.next,
  1122. struct gfs2_inode, i_trunc_list);
  1123. list_del_init(&ip->i_trunc_list);
  1124. }
  1125. spin_unlock(&sdp->sd_trunc_lock);
  1126. if (ip == NULL)
  1127. return;
  1128. gfs2_glock_finish_truncate(ip);
  1129. }
  1130. }
  1131. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1132. if (!sdp->sd_statfs_force_sync) {
  1133. sdp->sd_statfs_force_sync = 1;
  1134. wake_up(&sdp->sd_quota_wait);
  1135. }
  1136. }
  1137. /**
  1138. * gfs2_quotad - Write cached quota changes into the quota file
  1139. * @sdp: Pointer to GFS2 superblock
  1140. *
  1141. */
  1142. int gfs2_quotad(void *data)
  1143. {
  1144. struct gfs2_sbd *sdp = data;
  1145. struct gfs2_tune *tune = &sdp->sd_tune;
  1146. unsigned long statfs_timeo = 0;
  1147. unsigned long quotad_timeo = 0;
  1148. unsigned long t = 0;
  1149. DEFINE_WAIT(wait);
  1150. int empty;
  1151. while (!kthread_should_stop()) {
  1152. /* Update the master statfs file */
  1153. if (sdp->sd_statfs_force_sync) {
  1154. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1155. quotad_error(sdp, "statfs", error);
  1156. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1157. }
  1158. else
  1159. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1160. &statfs_timeo,
  1161. &tune->gt_statfs_quantum);
  1162. /* Update quota file */
  1163. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1164. &quotad_timeo, &tune->gt_quota_quantum);
  1165. /* Check for & recover partially truncated inodes */
  1166. quotad_check_trunc_list(sdp);
  1167. try_to_freeze();
  1168. t = min(quotad_timeo, statfs_timeo);
  1169. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1170. spin_lock(&sdp->sd_trunc_lock);
  1171. empty = list_empty(&sdp->sd_trunc_list);
  1172. spin_unlock(&sdp->sd_trunc_lock);
  1173. if (empty && !sdp->sd_statfs_force_sync)
  1174. t -= schedule_timeout(t);
  1175. else
  1176. t = 0;
  1177. finish_wait(&sdp->sd_quota_wait, &wait);
  1178. }
  1179. return 0;
  1180. }
  1181. static int gfs2_quota_get_xstate(struct super_block *sb,
  1182. struct fs_quota_stat *fqs)
  1183. {
  1184. struct gfs2_sbd *sdp = sb->s_fs_info;
  1185. memset(fqs, 0, sizeof(struct fs_quota_stat));
  1186. fqs->qs_version = FS_QSTAT_VERSION;
  1187. switch (sdp->sd_args.ar_quota) {
  1188. case GFS2_QUOTA_ON:
  1189. fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
  1190. /*FALLTHRU*/
  1191. case GFS2_QUOTA_ACCOUNT:
  1192. fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
  1193. break;
  1194. case GFS2_QUOTA_OFF:
  1195. break;
  1196. }
  1197. if (sdp->sd_quota_inode) {
  1198. fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1199. fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
  1200. }
  1201. fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
  1202. fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
  1203. fqs->qs_incoredqs = atomic_read(&qd_lru_count);
  1204. return 0;
  1205. }
  1206. static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
  1207. struct fs_disk_quota *fdq)
  1208. {
  1209. struct gfs2_sbd *sdp = sb->s_fs_info;
  1210. struct gfs2_quota_lvb *qlvb;
  1211. struct gfs2_quota_data *qd;
  1212. struct gfs2_holder q_gh;
  1213. int error;
  1214. memset(fdq, 0, sizeof(struct fs_disk_quota));
  1215. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1216. return -ESRCH; /* Crazy XFS error code */
  1217. if ((qid.type != USRQUOTA) &&
  1218. (qid.type != GRPQUOTA))
  1219. return -EINVAL;
  1220. error = qd_get(sdp, qid, &qd);
  1221. if (error)
  1222. return error;
  1223. error = do_glock(qd, FORCE, &q_gh);
  1224. if (error)
  1225. goto out;
  1226. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  1227. fdq->d_version = FS_DQUOT_VERSION;
  1228. fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
  1229. fdq->d_id = from_kqid_munged(current_user_ns(), qid);
  1230. fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
  1231. fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
  1232. fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
  1233. gfs2_glock_dq_uninit(&q_gh);
  1234. out:
  1235. qd_put(qd);
  1236. return error;
  1237. }
  1238. /* GFS2 only supports a subset of the XFS fields */
  1239. #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
  1240. static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
  1241. struct fs_disk_quota *fdq)
  1242. {
  1243. struct gfs2_sbd *sdp = sb->s_fs_info;
  1244. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1245. struct gfs2_quota_data *qd;
  1246. struct gfs2_holder q_gh, i_gh;
  1247. unsigned int data_blocks, ind_blocks;
  1248. unsigned int blocks = 0;
  1249. int alloc_required;
  1250. loff_t offset;
  1251. int error;
  1252. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1253. return -ESRCH; /* Crazy XFS error code */
  1254. if ((qid.type != USRQUOTA) &&
  1255. (qid.type != GRPQUOTA))
  1256. return -EINVAL;
  1257. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1258. return -EINVAL;
  1259. error = qd_get(sdp, qid, &qd);
  1260. if (error)
  1261. return error;
  1262. error = gfs2_rs_alloc(ip);
  1263. if (error)
  1264. goto out_put;
  1265. mutex_lock(&ip->i_inode.i_mutex);
  1266. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1267. if (error)
  1268. goto out_unlockput;
  1269. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1270. if (error)
  1271. goto out_q;
  1272. /* Check for existing entry, if none then alloc new blocks */
  1273. error = update_qd(sdp, qd);
  1274. if (error)
  1275. goto out_i;
  1276. /* If nothing has changed, this is a no-op */
  1277. if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
  1278. ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1279. fdq->d_fieldmask ^= FS_DQ_BSOFT;
  1280. if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
  1281. ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1282. fdq->d_fieldmask ^= FS_DQ_BHARD;
  1283. if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
  1284. ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1285. fdq->d_fieldmask ^= FS_DQ_BCOUNT;
  1286. if (fdq->d_fieldmask == 0)
  1287. goto out_i;
  1288. offset = qd2offset(qd);
  1289. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1290. if (gfs2_is_stuffed(ip))
  1291. alloc_required = 1;
  1292. if (alloc_required) {
  1293. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1294. &data_blocks, &ind_blocks);
  1295. blocks = 1 + data_blocks + ind_blocks;
  1296. error = gfs2_inplace_reserve(ip, blocks, 0);
  1297. if (error)
  1298. goto out_i;
  1299. blocks += gfs2_rg_blocks(ip, blocks);
  1300. }
  1301. /* Some quotas span block boundaries and can update two blocks,
  1302. adding an extra block to the transaction to handle such quotas */
  1303. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1304. if (error)
  1305. goto out_release;
  1306. /* Apply changes */
  1307. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1308. gfs2_trans_end(sdp);
  1309. out_release:
  1310. if (alloc_required)
  1311. gfs2_inplace_release(ip);
  1312. out_i:
  1313. gfs2_glock_dq_uninit(&i_gh);
  1314. out_q:
  1315. gfs2_glock_dq_uninit(&q_gh);
  1316. out_unlockput:
  1317. mutex_unlock(&ip->i_inode.i_mutex);
  1318. out_put:
  1319. qd_put(qd);
  1320. return error;
  1321. }
  1322. const struct quotactl_ops gfs2_quotactl_ops = {
  1323. .quota_sync = gfs2_quota_sync,
  1324. .get_xstate = gfs2_quota_get_xstate,
  1325. .get_dqblk = gfs2_get_dqblk,
  1326. .set_dqblk = gfs2_set_dqblk,
  1327. };