quota.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  5. */
  6. /*
  7. * Quota change tags are associated with each transaction that allocates or
  8. * deallocates space. Those changes are accumulated locally to each node (in a
  9. * per-node file) and then are periodically synced to the quota file. This
  10. * avoids the bottleneck of constantly touching the quota file, but introduces
  11. * fuzziness in the current usage value of IDs that are being used on different
  12. * nodes in the cluster simultaneously. So, it is possible for a user on
  13. * multiple nodes to overrun their quota, but that overrun is controlable.
  14. * Since quota tags are part of transactions, there is no need for a quota check
  15. * program to be run on node crashes or anything like that.
  16. *
  17. * There are couple of knobs that let the administrator manage the quota
  18. * fuzziness. "quota_quantum" sets the maximum time a quota change can be
  19. * sitting on one node before being synced to the quota file. (The default is
  20. * 60 seconds.) Another knob, "quota_scale" controls how quickly the frequency
  21. * of quota file syncs increases as the user moves closer to their limit. The
  22. * more frequent the syncs, the more accurate the quota enforcement, but that
  23. * means that there is more contention between the nodes for the quota file.
  24. * The default value is one. This sets the maximum theoretical quota overrun
  25. * (with infinite node with infinite bandwidth) to twice the user's limit. (In
  26. * practice, the maximum overrun you see should be much less.) A "quota_scale"
  27. * number greater than one makes quota syncs more frequent and reduces the
  28. * maximum overrun. Numbers less than one (but greater than zero) make quota
  29. * syncs less frequent.
  30. *
  31. * GFS quotas also use per-ID Lock Value Blocks (LVBs) to cache the contents of
  32. * the quota file, so it is not being constantly read.
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/sched.h>
  36. #include <linux/slab.h>
  37. #include <linux/mm.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/completion.h>
  40. #include <linux/buffer_head.h>
  41. #include <linux/sort.h>
  42. #include <linux/fs.h>
  43. #include <linux/bio.h>
  44. #include <linux/gfs2_ondisk.h>
  45. #include <linux/kthread.h>
  46. #include <linux/freezer.h>
  47. #include <linux/quota.h>
  48. #include <linux/dqblk_xfs.h>
  49. #include <linux/lockref.h>
  50. #include <linux/list_lru.h>
  51. #include <linux/rcupdate.h>
  52. #include <linux/rculist_bl.h>
  53. #include <linux/bit_spinlock.h>
  54. #include <linux/jhash.h>
  55. #include <linux/vmalloc.h>
  56. #include "gfs2.h"
  57. #include "incore.h"
  58. #include "bmap.h"
  59. #include "glock.h"
  60. #include "glops.h"
  61. #include "log.h"
  62. #include "meta_io.h"
  63. #include "quota.h"
  64. #include "rgrp.h"
  65. #include "super.h"
  66. #include "trans.h"
  67. #include "inode.h"
  68. #include "util.h"
  69. #define GFS2_QD_HASH_SHIFT 12
  70. #define GFS2_QD_HASH_SIZE BIT(GFS2_QD_HASH_SHIFT)
  71. #define GFS2_QD_HASH_MASK (GFS2_QD_HASH_SIZE - 1)
  72. /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
  73. /* -> sd_bitmap_lock */
  74. static DEFINE_SPINLOCK(qd_lock);
  75. struct list_lru gfs2_qd_lru;
  76. static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
  77. static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
  78. const struct kqid qid)
  79. {
  80. unsigned int h;
  81. h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
  82. h = jhash(&qid, sizeof(struct kqid), h);
  83. return h & GFS2_QD_HASH_MASK;
  84. }
  85. static inline void spin_lock_bucket(unsigned int hash)
  86. {
  87. hlist_bl_lock(&qd_hash_table[hash]);
  88. }
  89. static inline void spin_unlock_bucket(unsigned int hash)
  90. {
  91. hlist_bl_unlock(&qd_hash_table[hash]);
  92. }
  93. static void gfs2_qd_dealloc(struct rcu_head *rcu)
  94. {
  95. struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
  96. struct gfs2_sbd *sdp = qd->qd_sbd;
  97. kmem_cache_free(gfs2_quotad_cachep, qd);
  98. if (atomic_dec_and_test(&sdp->sd_quota_count))
  99. wake_up(&sdp->sd_kill_wait);
  100. }
  101. static void gfs2_qd_dispose(struct gfs2_quota_data *qd)
  102. {
  103. struct gfs2_sbd *sdp = qd->qd_sbd;
  104. spin_lock(&qd_lock);
  105. list_del(&qd->qd_list);
  106. spin_unlock(&qd_lock);
  107. spin_lock_bucket(qd->qd_hash);
  108. hlist_bl_del_rcu(&qd->qd_hlist);
  109. spin_unlock_bucket(qd->qd_hash);
  110. if (!gfs2_withdrawn(sdp)) {
  111. gfs2_assert_warn(sdp, !qd->qd_change);
  112. gfs2_assert_warn(sdp, !qd->qd_slot_ref);
  113. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  114. }
  115. gfs2_glock_put(qd->qd_gl);
  116. call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
  117. }
  118. static void gfs2_qd_list_dispose(struct list_head *list)
  119. {
  120. struct gfs2_quota_data *qd;
  121. while (!list_empty(list)) {
  122. qd = list_first_entry(list, struct gfs2_quota_data, qd_lru);
  123. list_del(&qd->qd_lru);
  124. gfs2_qd_dispose(qd);
  125. }
  126. }
  127. static enum lru_status gfs2_qd_isolate(struct list_head *item,
  128. struct list_lru_one *lru, void *arg)
  129. {
  130. struct list_head *dispose = arg;
  131. struct gfs2_quota_data *qd =
  132. list_entry(item, struct gfs2_quota_data, qd_lru);
  133. enum lru_status status;
  134. if (!spin_trylock(&qd->qd_lockref.lock))
  135. return LRU_SKIP;
  136. status = LRU_SKIP;
  137. if (qd->qd_lockref.count == 0) {
  138. lockref_mark_dead(&qd->qd_lockref);
  139. list_lru_isolate_move(lru, &qd->qd_lru, dispose);
  140. status = LRU_REMOVED;
  141. }
  142. spin_unlock(&qd->qd_lockref.lock);
  143. return status;
  144. }
  145. static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
  146. struct shrink_control *sc)
  147. {
  148. LIST_HEAD(dispose);
  149. unsigned long freed;
  150. if (!(sc->gfp_mask & __GFP_FS))
  151. return SHRINK_STOP;
  152. freed = list_lru_shrink_walk(&gfs2_qd_lru, sc,
  153. gfs2_qd_isolate, &dispose);
  154. gfs2_qd_list_dispose(&dispose);
  155. return freed;
  156. }
  157. static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
  158. struct shrink_control *sc)
  159. {
  160. return vfs_pressure_ratio(list_lru_shrink_count(&gfs2_qd_lru, sc));
  161. }
  162. static struct shrinker *gfs2_qd_shrinker;
  163. int __init gfs2_qd_shrinker_init(void)
  164. {
  165. gfs2_qd_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "gfs2-qd");
  166. if (!gfs2_qd_shrinker)
  167. return -ENOMEM;
  168. gfs2_qd_shrinker->count_objects = gfs2_qd_shrink_count;
  169. gfs2_qd_shrinker->scan_objects = gfs2_qd_shrink_scan;
  170. shrinker_register(gfs2_qd_shrinker);
  171. return 0;
  172. }
  173. void gfs2_qd_shrinker_exit(void)
  174. {
  175. shrinker_free(gfs2_qd_shrinker);
  176. }
  177. static u64 qd2index(struct gfs2_quota_data *qd)
  178. {
  179. struct kqid qid = qd->qd_id;
  180. return (2 * (u64)from_kqid(&init_user_ns, qid)) +
  181. ((qid.type == USRQUOTA) ? 0 : 1);
  182. }
  183. static u64 qd2offset(struct gfs2_quota_data *qd)
  184. {
  185. return qd2index(qd) * sizeof(struct gfs2_quota);
  186. }
  187. static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
  188. {
  189. struct gfs2_quota_data *qd;
  190. int error;
  191. qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
  192. if (!qd)
  193. return NULL;
  194. qd->qd_sbd = sdp;
  195. lockref_init(&qd->qd_lockref);
  196. qd->qd_id = qid;
  197. qd->qd_slot = -1;
  198. INIT_LIST_HEAD(&qd->qd_lru);
  199. qd->qd_hash = hash;
  200. error = gfs2_glock_get(sdp, qd2index(qd),
  201. &gfs2_quota_glops, CREATE, &qd->qd_gl);
  202. if (error)
  203. goto fail;
  204. return qd;
  205. fail:
  206. kmem_cache_free(gfs2_quotad_cachep, qd);
  207. return NULL;
  208. }
  209. static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
  210. const struct gfs2_sbd *sdp,
  211. struct kqid qid)
  212. {
  213. struct gfs2_quota_data *qd;
  214. struct hlist_bl_node *h;
  215. hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
  216. if (!qid_eq(qd->qd_id, qid))
  217. continue;
  218. if (qd->qd_sbd != sdp)
  219. continue;
  220. if (lockref_get_not_dead(&qd->qd_lockref)) {
  221. list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru);
  222. return qd;
  223. }
  224. }
  225. return NULL;
  226. }
  227. static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
  228. struct gfs2_quota_data **qdp)
  229. {
  230. struct gfs2_quota_data *qd, *new_qd;
  231. unsigned int hash = gfs2_qd_hash(sdp, qid);
  232. rcu_read_lock();
  233. *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
  234. rcu_read_unlock();
  235. if (qd)
  236. return 0;
  237. new_qd = qd_alloc(hash, sdp, qid);
  238. if (!new_qd)
  239. return -ENOMEM;
  240. spin_lock(&qd_lock);
  241. spin_lock_bucket(hash);
  242. *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
  243. if (qd == NULL) {
  244. *qdp = new_qd;
  245. list_add(&new_qd->qd_list, &sdp->sd_quota_list);
  246. hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
  247. atomic_inc(&sdp->sd_quota_count);
  248. }
  249. spin_unlock_bucket(hash);
  250. spin_unlock(&qd_lock);
  251. if (qd) {
  252. gfs2_glock_put(new_qd->qd_gl);
  253. kmem_cache_free(gfs2_quotad_cachep, new_qd);
  254. }
  255. return 0;
  256. }
  257. static void __qd_hold(struct gfs2_quota_data *qd)
  258. {
  259. struct gfs2_sbd *sdp = qd->qd_sbd;
  260. gfs2_assert(sdp, qd->qd_lockref.count > 0);
  261. qd->qd_lockref.count++;
  262. }
  263. static void qd_put(struct gfs2_quota_data *qd)
  264. {
  265. struct gfs2_sbd *sdp;
  266. if (lockref_put_or_lock(&qd->qd_lockref))
  267. return;
  268. BUG_ON(__lockref_is_dead(&qd->qd_lockref));
  269. sdp = qd->qd_sbd;
  270. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
  271. lockref_mark_dead(&qd->qd_lockref);
  272. spin_unlock(&qd->qd_lockref.lock);
  273. list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru);
  274. gfs2_qd_dispose(qd);
  275. return;
  276. }
  277. qd->qd_lockref.count = 0;
  278. list_lru_add_obj(&gfs2_qd_lru, &qd->qd_lru);
  279. spin_unlock(&qd->qd_lockref.lock);
  280. }
  281. static int slot_get(struct gfs2_quota_data *qd)
  282. {
  283. struct gfs2_sbd *sdp = qd->qd_sbd;
  284. unsigned int bit;
  285. int error = 0;
  286. spin_lock(&sdp->sd_bitmap_lock);
  287. if (qd->qd_slot_ref == 0) {
  288. bit = find_first_zero_bit(sdp->sd_quota_bitmap,
  289. sdp->sd_quota_slots);
  290. if (bit >= sdp->sd_quota_slots) {
  291. error = -ENOSPC;
  292. goto out;
  293. }
  294. set_bit(bit, sdp->sd_quota_bitmap);
  295. qd->qd_slot = bit;
  296. }
  297. qd->qd_slot_ref++;
  298. out:
  299. spin_unlock(&sdp->sd_bitmap_lock);
  300. return error;
  301. }
  302. static void slot_hold(struct gfs2_quota_data *qd)
  303. {
  304. struct gfs2_sbd *sdp = qd->qd_sbd;
  305. spin_lock(&sdp->sd_bitmap_lock);
  306. gfs2_assert(sdp, qd->qd_slot_ref);
  307. qd->qd_slot_ref++;
  308. spin_unlock(&sdp->sd_bitmap_lock);
  309. }
  310. static void slot_put(struct gfs2_quota_data *qd)
  311. {
  312. struct gfs2_sbd *sdp = qd->qd_sbd;
  313. spin_lock(&sdp->sd_bitmap_lock);
  314. gfs2_assert(sdp, qd->qd_slot_ref);
  315. if (!--qd->qd_slot_ref) {
  316. BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
  317. qd->qd_slot = -1;
  318. }
  319. spin_unlock(&sdp->sd_bitmap_lock);
  320. }
  321. static int bh_get(struct gfs2_quota_data *qd)
  322. {
  323. struct gfs2_sbd *sdp = qd->qd_sbd;
  324. struct inode *inode = sdp->sd_qc_inode;
  325. struct gfs2_inode *ip = GFS2_I(inode);
  326. unsigned int block, offset;
  327. struct buffer_head *bh = NULL;
  328. struct iomap iomap = { };
  329. int error;
  330. spin_lock(&qd->qd_lockref.lock);
  331. if (qd->qd_bh_count) {
  332. qd->qd_bh_count++;
  333. spin_unlock(&qd->qd_lockref.lock);
  334. return 0;
  335. }
  336. spin_unlock(&qd->qd_lockref.lock);
  337. block = qd->qd_slot / sdp->sd_qc_per_block;
  338. offset = qd->qd_slot % sdp->sd_qc_per_block;
  339. error = gfs2_iomap_get(inode,
  340. (loff_t)block << inode->i_blkbits,
  341. i_blocksize(inode), &iomap);
  342. if (error)
  343. return error;
  344. error = -ENOENT;
  345. if (iomap.type != IOMAP_MAPPED)
  346. return error;
  347. error = gfs2_meta_read(ip->i_gl, iomap.addr >> inode->i_blkbits,
  348. DIO_WAIT, 0, &bh);
  349. if (error)
  350. return error;
  351. error = -EIO;
  352. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  353. goto out;
  354. spin_lock(&qd->qd_lockref.lock);
  355. if (qd->qd_bh == NULL) {
  356. qd->qd_bh = bh;
  357. qd->qd_bh_qc = (struct gfs2_quota_change *)
  358. (bh->b_data + sizeof(struct gfs2_meta_header) +
  359. offset * sizeof(struct gfs2_quota_change));
  360. bh = NULL;
  361. }
  362. qd->qd_bh_count++;
  363. spin_unlock(&qd->qd_lockref.lock);
  364. error = 0;
  365. out:
  366. brelse(bh);
  367. return error;
  368. }
  369. static void bh_put(struct gfs2_quota_data *qd)
  370. {
  371. struct gfs2_sbd *sdp = qd->qd_sbd;
  372. struct buffer_head *bh = NULL;
  373. spin_lock(&qd->qd_lockref.lock);
  374. gfs2_assert(sdp, qd->qd_bh_count);
  375. if (!--qd->qd_bh_count) {
  376. bh = qd->qd_bh;
  377. qd->qd_bh = NULL;
  378. qd->qd_bh_qc = NULL;
  379. }
  380. spin_unlock(&qd->qd_lockref.lock);
  381. brelse(bh);
  382. }
  383. static bool qd_grab_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
  384. u64 sync_gen)
  385. {
  386. bool ret = false;
  387. spin_lock(&qd->qd_lockref.lock);
  388. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  389. !test_bit(QDF_CHANGE, &qd->qd_flags) ||
  390. qd->qd_sync_gen >= sync_gen)
  391. goto out;
  392. if (__lockref_is_dead(&qd->qd_lockref))
  393. goto out;
  394. qd->qd_lockref.count++;
  395. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  396. set_bit(QDF_LOCKED, &qd->qd_flags);
  397. qd->qd_change_sync = qd->qd_change;
  398. slot_hold(qd);
  399. ret = true;
  400. out:
  401. spin_unlock(&qd->qd_lockref.lock);
  402. return ret;
  403. }
  404. static void qd_ungrab_sync(struct gfs2_quota_data *qd)
  405. {
  406. clear_bit(QDF_LOCKED, &qd->qd_flags);
  407. slot_put(qd);
  408. qd_put(qd);
  409. }
  410. static void qdsb_put(struct gfs2_quota_data *qd)
  411. {
  412. bh_put(qd);
  413. slot_put(qd);
  414. qd_put(qd);
  415. }
  416. static void qd_unlock(struct gfs2_quota_data *qd)
  417. {
  418. spin_lock(&qd->qd_lockref.lock);
  419. gfs2_assert_warn(qd->qd_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
  420. clear_bit(QDF_LOCKED, &qd->qd_flags);
  421. spin_unlock(&qd->qd_lockref.lock);
  422. qdsb_put(qd);
  423. }
  424. static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
  425. struct gfs2_quota_data **qdp)
  426. {
  427. int error;
  428. error = qd_get(sdp, qid, qdp);
  429. if (error)
  430. return error;
  431. error = slot_get(*qdp);
  432. if (error)
  433. goto fail;
  434. error = bh_get(*qdp);
  435. if (error)
  436. goto fail_slot;
  437. return 0;
  438. fail_slot:
  439. slot_put(*qdp);
  440. fail:
  441. qd_put(*qdp);
  442. return error;
  443. }
  444. /**
  445. * gfs2_qa_get - make sure we have a quota allocations data structure,
  446. * if necessary
  447. * @ip: the inode for this reservation
  448. */
  449. int gfs2_qa_get(struct gfs2_inode *ip)
  450. {
  451. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  452. struct inode *inode = &ip->i_inode;
  453. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  454. return 0;
  455. spin_lock(&inode->i_lock);
  456. if (ip->i_qadata == NULL) {
  457. struct gfs2_qadata *tmp;
  458. spin_unlock(&inode->i_lock);
  459. tmp = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
  460. if (!tmp)
  461. return -ENOMEM;
  462. spin_lock(&inode->i_lock);
  463. if (ip->i_qadata == NULL)
  464. ip->i_qadata = tmp;
  465. else
  466. kmem_cache_free(gfs2_qadata_cachep, tmp);
  467. }
  468. ip->i_qadata->qa_ref++;
  469. spin_unlock(&inode->i_lock);
  470. return 0;
  471. }
  472. void gfs2_qa_put(struct gfs2_inode *ip)
  473. {
  474. struct inode *inode = &ip->i_inode;
  475. spin_lock(&inode->i_lock);
  476. if (ip->i_qadata && --ip->i_qadata->qa_ref == 0) {
  477. kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
  478. ip->i_qadata = NULL;
  479. }
  480. spin_unlock(&inode->i_lock);
  481. }
  482. int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  483. {
  484. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  485. struct gfs2_quota_data **qd;
  486. int error;
  487. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  488. return 0;
  489. error = gfs2_qa_get(ip);
  490. if (error)
  491. return error;
  492. qd = ip->i_qadata->qa_qd;
  493. if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
  494. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags))) {
  495. error = -EIO;
  496. gfs2_qa_put(ip);
  497. goto out;
  498. }
  499. error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
  500. if (error)
  501. goto out_unhold;
  502. ip->i_qadata->qa_qd_num++;
  503. qd++;
  504. error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
  505. if (error)
  506. goto out_unhold;
  507. ip->i_qadata->qa_qd_num++;
  508. qd++;
  509. if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
  510. !uid_eq(uid, ip->i_inode.i_uid)) {
  511. error = qdsb_get(sdp, make_kqid_uid(uid), qd);
  512. if (error)
  513. goto out_unhold;
  514. ip->i_qadata->qa_qd_num++;
  515. qd++;
  516. }
  517. if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
  518. !gid_eq(gid, ip->i_inode.i_gid)) {
  519. error = qdsb_get(sdp, make_kqid_gid(gid), qd);
  520. if (error)
  521. goto out_unhold;
  522. ip->i_qadata->qa_qd_num++;
  523. qd++;
  524. }
  525. out_unhold:
  526. if (error)
  527. gfs2_quota_unhold(ip);
  528. out:
  529. return error;
  530. }
  531. void gfs2_quota_unhold(struct gfs2_inode *ip)
  532. {
  533. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  534. u32 x;
  535. if (ip->i_qadata == NULL)
  536. return;
  537. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  538. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  539. qdsb_put(ip->i_qadata->qa_qd[x]);
  540. ip->i_qadata->qa_qd[x] = NULL;
  541. }
  542. ip->i_qadata->qa_qd_num = 0;
  543. gfs2_qa_put(ip);
  544. }
  545. static int sort_qd(const void *a, const void *b)
  546. {
  547. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  548. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  549. if (qid_lt(qd_a->qd_id, qd_b->qd_id))
  550. return -1;
  551. if (qid_lt(qd_b->qd_id, qd_a->qd_id))
  552. return 1;
  553. return 0;
  554. }
  555. static void do_qc(struct gfs2_quota_data *qd, s64 change)
  556. {
  557. struct gfs2_sbd *sdp = qd->qd_sbd;
  558. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  559. struct gfs2_quota_change *qc = qd->qd_bh_qc;
  560. bool needs_put = false;
  561. s64 x;
  562. gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
  563. /*
  564. * The QDF_CHANGE flag indicates that the slot in the quota change file
  565. * is used. Here, we use the value of qc->qc_change when the slot is
  566. * used, and we assume a value of 0 otherwise.
  567. */
  568. spin_lock(&qd->qd_lockref.lock);
  569. x = 0;
  570. if (test_bit(QDF_CHANGE, &qd->qd_flags))
  571. x = be64_to_cpu(qc->qc_change);
  572. x += change;
  573. qd->qd_change += change;
  574. if (!x && test_bit(QDF_CHANGE, &qd->qd_flags)) {
  575. /* The slot in the quota change file becomes unused. */
  576. clear_bit(QDF_CHANGE, &qd->qd_flags);
  577. qc->qc_flags = 0;
  578. qc->qc_id = 0;
  579. needs_put = true;
  580. } else if (x && !test_bit(QDF_CHANGE, &qd->qd_flags)) {
  581. /* The slot in the quota change file becomes used. */
  582. set_bit(QDF_CHANGE, &qd->qd_flags);
  583. __qd_hold(qd);
  584. slot_hold(qd);
  585. qc->qc_flags = 0;
  586. if (qd->qd_id.type == USRQUOTA)
  587. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  588. qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
  589. }
  590. qc->qc_change = cpu_to_be64(x);
  591. spin_unlock(&qd->qd_lockref.lock);
  592. if (needs_put) {
  593. slot_put(qd);
  594. qd_put(qd);
  595. }
  596. if (change < 0) /* Reset quiet flag if we freed some blocks */
  597. clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
  598. }
  599. static int gfs2_write_buf_to_page(struct gfs2_sbd *sdp, unsigned long index,
  600. unsigned off, void *buf, unsigned bytes)
  601. {
  602. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  603. struct inode *inode = &ip->i_inode;
  604. struct address_space *mapping = inode->i_mapping;
  605. struct folio *folio;
  606. struct buffer_head *bh;
  607. u64 blk;
  608. unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0;
  609. unsigned to_write = bytes, pg_off = off;
  610. blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift);
  611. boff = off % bsize;
  612. folio = filemap_grab_folio(mapping, index);
  613. if (IS_ERR(folio))
  614. return PTR_ERR(folio);
  615. bh = folio_buffers(folio);
  616. if (!bh)
  617. bh = create_empty_buffers(folio, bsize, 0);
  618. for (;;) {
  619. /* Find the beginning block within the folio */
  620. if (pg_off >= ((bnum * bsize) + bsize)) {
  621. bh = bh->b_this_page;
  622. bnum++;
  623. blk++;
  624. continue;
  625. }
  626. if (!buffer_mapped(bh)) {
  627. gfs2_block_map(inode, blk, bh, 1);
  628. if (!buffer_mapped(bh))
  629. goto unlock_out;
  630. /* If it's a newly allocated disk block, zero it */
  631. if (buffer_new(bh))
  632. folio_zero_range(folio, bnum * bsize,
  633. bh->b_size);
  634. }
  635. if (folio_test_uptodate(folio))
  636. set_buffer_uptodate(bh);
  637. if (bh_read(bh, REQ_META | REQ_PRIO) < 0)
  638. goto unlock_out;
  639. gfs2_trans_add_data(ip->i_gl, bh);
  640. /* If we need to write to the next block as well */
  641. if (to_write > (bsize - boff)) {
  642. pg_off += (bsize - boff);
  643. to_write -= (bsize - boff);
  644. boff = pg_off % bsize;
  645. continue;
  646. }
  647. break;
  648. }
  649. /* Write to the folio, now that we have setup the buffer(s) */
  650. memcpy_to_folio(folio, off, buf, bytes);
  651. flush_dcache_folio(folio);
  652. folio_unlock(folio);
  653. folio_put(folio);
  654. return 0;
  655. unlock_out:
  656. folio_unlock(folio);
  657. folio_put(folio);
  658. return -EIO;
  659. }
  660. static int gfs2_write_disk_quota(struct gfs2_sbd *sdp, struct gfs2_quota *qp,
  661. loff_t loc)
  662. {
  663. unsigned long pg_beg;
  664. unsigned pg_off, nbytes, overflow = 0;
  665. int error;
  666. void *ptr;
  667. nbytes = sizeof(struct gfs2_quota);
  668. pg_beg = loc >> PAGE_SHIFT;
  669. pg_off = offset_in_page(loc);
  670. /* If the quota straddles a page boundary, split the write in two */
  671. if ((pg_off + nbytes) > PAGE_SIZE)
  672. overflow = (pg_off + nbytes) - PAGE_SIZE;
  673. ptr = qp;
  674. error = gfs2_write_buf_to_page(sdp, pg_beg, pg_off, ptr,
  675. nbytes - overflow);
  676. /* If there's an overflow, write the remaining bytes to the next page */
  677. if (!error && overflow)
  678. error = gfs2_write_buf_to_page(sdp, pg_beg + 1, 0,
  679. ptr + nbytes - overflow,
  680. overflow);
  681. return error;
  682. }
  683. /**
  684. * gfs2_adjust_quota - adjust record of current block usage
  685. * @sdp: The superblock
  686. * @loc: Offset of the entry in the quota file
  687. * @change: The amount of usage change to record
  688. * @qd: The quota data
  689. * @fdq: The updated limits to record
  690. *
  691. * This function was mostly borrowed from gfs2_block_truncate_page which was
  692. * in turn mostly borrowed from ext3
  693. *
  694. * Returns: 0 or -ve on error
  695. */
  696. static int gfs2_adjust_quota(struct gfs2_sbd *sdp, loff_t loc,
  697. s64 change, struct gfs2_quota_data *qd,
  698. struct qc_dqblk *fdq)
  699. {
  700. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  701. struct inode *inode = &ip->i_inode;
  702. struct gfs2_quota q;
  703. int err;
  704. u64 size;
  705. if (gfs2_is_stuffed(ip)) {
  706. err = gfs2_unstuff_dinode(ip);
  707. if (err)
  708. return err;
  709. }
  710. memset(&q, 0, sizeof(struct gfs2_quota));
  711. err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
  712. if (err < 0)
  713. return err;
  714. loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */
  715. be64_add_cpu(&q.qu_value, change);
  716. if (((s64)be64_to_cpu(q.qu_value)) < 0)
  717. q.qu_value = 0; /* Never go negative on quota usage */
  718. spin_lock(&qd->qd_lockref.lock);
  719. qd->qd_qb.qb_value = q.qu_value;
  720. if (fdq) {
  721. if (fdq->d_fieldmask & QC_SPC_SOFT) {
  722. q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
  723. qd->qd_qb.qb_warn = q.qu_warn;
  724. }
  725. if (fdq->d_fieldmask & QC_SPC_HARD) {
  726. q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
  727. qd->qd_qb.qb_limit = q.qu_limit;
  728. }
  729. if (fdq->d_fieldmask & QC_SPACE) {
  730. q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
  731. qd->qd_qb.qb_value = q.qu_value;
  732. }
  733. }
  734. spin_unlock(&qd->qd_lockref.lock);
  735. err = gfs2_write_disk_quota(sdp, &q, loc);
  736. if (!err) {
  737. size = loc + sizeof(struct gfs2_quota);
  738. if (size > inode->i_size)
  739. i_size_write(inode, size);
  740. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  741. mark_inode_dirty(inode);
  742. set_bit(QDF_REFRESH, &qd->qd_flags);
  743. }
  744. return err;
  745. }
  746. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda,
  747. u64 sync_gen)
  748. {
  749. struct gfs2_sbd *sdp = (*qda)->qd_sbd;
  750. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  751. struct gfs2_alloc_parms ap = {};
  752. unsigned int data_blocks, ind_blocks;
  753. struct gfs2_holder *ghs, i_gh;
  754. unsigned int qx, x;
  755. struct gfs2_quota_data *qd;
  756. unsigned reserved;
  757. loff_t offset;
  758. unsigned int nalloc = 0, blocks;
  759. int error;
  760. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  761. &data_blocks, &ind_blocks);
  762. ghs = kmalloc_objs(struct gfs2_holder, num_qd, GFP_NOFS);
  763. if (!ghs)
  764. return -ENOMEM;
  765. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  766. inode_lock(&ip->i_inode);
  767. for (qx = 0; qx < num_qd; qx++) {
  768. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  769. GL_NOCACHE, &ghs[qx]);
  770. if (error)
  771. goto out_dq;
  772. }
  773. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  774. if (error)
  775. goto out_dq;
  776. for (x = 0; x < num_qd; x++) {
  777. offset = qd2offset(qda[x]);
  778. if (gfs2_write_alloc_required(ip, offset,
  779. sizeof(struct gfs2_quota)))
  780. nalloc++;
  781. }
  782. /*
  783. * 1 blk for unstuffing inode if stuffed. We add this extra
  784. * block to the reservation unconditionally. If the inode
  785. * doesn't need unstuffing, the block will be released to the
  786. * rgrp since it won't be allocated during the transaction
  787. */
  788. /* +3 in the end for unstuffing block, inode size update block
  789. * and another block in case quota straddles page boundary and
  790. * two blocks need to be updated instead of 1 */
  791. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  792. reserved = 1 + (nalloc * (data_blocks + ind_blocks));
  793. ap.target = reserved;
  794. error = gfs2_inplace_reserve(ip, &ap);
  795. if (error)
  796. goto out_alloc;
  797. if (nalloc)
  798. blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
  799. error = gfs2_trans_begin(sdp, blocks, 0);
  800. if (error)
  801. goto out_ipres;
  802. for (x = 0; x < num_qd; x++) {
  803. qd = qda[x];
  804. offset = qd2offset(qd);
  805. error = gfs2_adjust_quota(sdp, offset, qd->qd_change_sync, qd,
  806. NULL);
  807. if (error)
  808. goto out_end_trans;
  809. do_qc(qd, -qd->qd_change_sync);
  810. set_bit(QDF_REFRESH, &qd->qd_flags);
  811. }
  812. out_end_trans:
  813. gfs2_trans_end(sdp);
  814. out_ipres:
  815. gfs2_inplace_release(ip);
  816. out_alloc:
  817. gfs2_glock_dq_uninit(&i_gh);
  818. out_dq:
  819. while (qx--)
  820. gfs2_glock_dq_uninit(&ghs[qx]);
  821. inode_unlock(&ip->i_inode);
  822. kfree(ghs);
  823. gfs2_log_flush(glock_sbd(ip->i_gl), ip->i_gl,
  824. GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
  825. if (!error) {
  826. for (x = 0; x < num_qd; x++) {
  827. qd = qda[x];
  828. spin_lock(&qd->qd_lockref.lock);
  829. if (qd->qd_sync_gen < sync_gen)
  830. qd->qd_sync_gen = sync_gen;
  831. spin_unlock(&qd->qd_lockref.lock);
  832. }
  833. }
  834. return error;
  835. }
  836. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  837. {
  838. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  839. struct gfs2_quota q;
  840. struct gfs2_quota_lvb *qlvb;
  841. loff_t pos;
  842. int error;
  843. memset(&q, 0, sizeof(struct gfs2_quota));
  844. pos = qd2offset(qd);
  845. error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
  846. if (error < 0)
  847. return error;
  848. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  849. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  850. qlvb->__pad = 0;
  851. qlvb->qb_limit = q.qu_limit;
  852. qlvb->qb_warn = q.qu_warn;
  853. qlvb->qb_value = q.qu_value;
  854. spin_lock(&qd->qd_lockref.lock);
  855. qd->qd_qb = *qlvb;
  856. spin_unlock(&qd->qd_lockref.lock);
  857. return 0;
  858. }
  859. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  860. struct gfs2_holder *q_gh)
  861. {
  862. struct gfs2_sbd *sdp = qd->qd_sbd;
  863. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  864. struct gfs2_holder i_gh;
  865. int error;
  866. gfs2_assert_warn(sdp, sdp == glock_sbd(qd->qd_gl));
  867. restart:
  868. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  869. if (error)
  870. return error;
  871. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  872. force_refresh = FORCE;
  873. spin_lock(&qd->qd_lockref.lock);
  874. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  875. spin_unlock(&qd->qd_lockref.lock);
  876. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  877. gfs2_glock_dq_uninit(q_gh);
  878. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  879. GL_NOCACHE, q_gh);
  880. if (error)
  881. return error;
  882. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  883. if (error)
  884. goto fail;
  885. error = update_qd(sdp, qd);
  886. if (error)
  887. goto fail_gunlock;
  888. gfs2_glock_dq_uninit(&i_gh);
  889. gfs2_glock_dq_uninit(q_gh);
  890. force_refresh = 0;
  891. goto restart;
  892. }
  893. return 0;
  894. fail_gunlock:
  895. gfs2_glock_dq_uninit(&i_gh);
  896. fail:
  897. gfs2_glock_dq_uninit(q_gh);
  898. return error;
  899. }
  900. int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  901. {
  902. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  903. struct gfs2_quota_data *qd;
  904. u32 x;
  905. int error;
  906. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  907. return 0;
  908. error = gfs2_quota_hold(ip, uid, gid);
  909. if (error)
  910. return error;
  911. sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
  912. sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  913. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  914. qd = ip->i_qadata->qa_qd[x];
  915. error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
  916. if (error)
  917. break;
  918. }
  919. if (!error)
  920. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  921. else {
  922. while (x--)
  923. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  924. gfs2_quota_unhold(ip);
  925. }
  926. return error;
  927. }
  928. static bool need_sync(struct gfs2_quota_data *qd)
  929. {
  930. struct gfs2_sbd *sdp = qd->qd_sbd;
  931. struct gfs2_tune *gt = &sdp->sd_tune;
  932. s64 value, change, limit;
  933. unsigned int num, den;
  934. int ret = false;
  935. spin_lock(&qd->qd_lockref.lock);
  936. if (!qd->qd_qb.qb_limit)
  937. goto out;
  938. change = qd->qd_change;
  939. if (change <= 0)
  940. goto out;
  941. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  942. limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
  943. if (value >= limit)
  944. goto out;
  945. spin_lock(&gt->gt_spin);
  946. num = gt->gt_quota_scale_num;
  947. den = gt->gt_quota_scale_den;
  948. spin_unlock(&gt->gt_spin);
  949. change *= gfs2_jindex_size(sdp) * num;
  950. change = div_s64(change, den);
  951. if (value + change < limit)
  952. goto out;
  953. ret = true;
  954. out:
  955. spin_unlock(&qd->qd_lockref.lock);
  956. return ret;
  957. }
  958. void gfs2_quota_unlock(struct gfs2_inode *ip)
  959. {
  960. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  961. struct gfs2_quota_data *qda[2 * GFS2_MAXQUOTAS];
  962. unsigned int count = 0;
  963. u32 x;
  964. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  965. return;
  966. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  967. struct gfs2_quota_data *qd;
  968. bool sync;
  969. int error;
  970. qd = ip->i_qadata->qa_qd[x];
  971. sync = need_sync(qd);
  972. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  973. if (!sync)
  974. continue;
  975. spin_lock(&qd_lock);
  976. sync = qd_grab_sync(sdp, qd, U64_MAX);
  977. spin_unlock(&qd_lock);
  978. if (!sync)
  979. continue;
  980. gfs2_assert_warn(sdp, qd->qd_change_sync);
  981. error = bh_get(qd);
  982. if (error) {
  983. qd_ungrab_sync(qd);
  984. continue;
  985. }
  986. qda[count++] = qd;
  987. }
  988. if (count) {
  989. u64 sync_gen = READ_ONCE(sdp->sd_quota_sync_gen);
  990. do_sync(count, qda, sync_gen);
  991. for (x = 0; x < count; x++)
  992. qd_unlock(qda[x]);
  993. }
  994. gfs2_quota_unhold(ip);
  995. }
  996. #define MAX_LINE 256
  997. static void print_message(struct gfs2_quota_data *qd, char *type)
  998. {
  999. struct gfs2_sbd *sdp = qd->qd_sbd;
  1000. if (sdp->sd_args.ar_quota != GFS2_QUOTA_QUIET) {
  1001. fs_info(sdp, "quota %s for %s %u\n",
  1002. type,
  1003. (qd->qd_id.type == USRQUOTA) ? "user" : "group",
  1004. from_kqid(&init_user_ns, qd->qd_id));
  1005. }
  1006. }
  1007. /**
  1008. * gfs2_quota_check - check if allocating new blocks will exceed quota
  1009. * @ip: The inode for which this check is being performed
  1010. * @uid: The uid to check against
  1011. * @gid: The gid to check against
  1012. * @ap: The allocation parameters. ap->target contains the requested
  1013. * blocks. ap->min_target, if set, contains the minimum blks
  1014. * requested.
  1015. *
  1016. * Returns: 0 on success.
  1017. * min_req = ap->min_target ? ap->min_target : ap->target;
  1018. * quota must allow at least min_req blks for success and
  1019. * ap->allowed is set to the number of blocks allowed
  1020. *
  1021. * -EDQUOT otherwise, quota violation. ap->allowed is set to number
  1022. * of blocks available.
  1023. */
  1024. int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
  1025. struct gfs2_alloc_parms *ap)
  1026. {
  1027. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1028. struct gfs2_quota_data *qd;
  1029. s64 value, warn, limit;
  1030. u32 x;
  1031. int error = 0;
  1032. ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
  1033. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  1034. return 0;
  1035. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  1036. qd = ip->i_qadata->qa_qd[x];
  1037. if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1038. qid_eq(qd->qd_id, make_kqid_gid(gid))))
  1039. continue;
  1040. spin_lock(&qd->qd_lockref.lock);
  1041. warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
  1042. limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
  1043. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  1044. value += qd->qd_change;
  1045. spin_unlock(&qd->qd_lockref.lock);
  1046. if (limit > 0 && (limit - value) < ap->allowed)
  1047. ap->allowed = limit - value;
  1048. /* If we can't meet the target */
  1049. if (limit && limit < (value + (s64)ap->target)) {
  1050. /* If no min_target specified or we don't meet
  1051. * min_target, return -EDQUOT */
  1052. if (!ap->min_target || ap->min_target > ap->allowed) {
  1053. if (!test_and_set_bit(QDF_QMSG_QUIET,
  1054. &qd->qd_flags)) {
  1055. print_message(qd, "exceeded");
  1056. quota_send_warning(qd->qd_id,
  1057. sdp->sd_vfs->s_dev,
  1058. QUOTA_NL_BHARDWARN);
  1059. }
  1060. error = -EDQUOT;
  1061. break;
  1062. }
  1063. } else if (warn && warn < value &&
  1064. time_after_eq(jiffies, qd->qd_last_warn +
  1065. gfs2_tune_get(sdp, gt_quota_warn_period)
  1066. * HZ)) {
  1067. quota_send_warning(qd->qd_id,
  1068. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  1069. print_message(qd, "warning");
  1070. error = 0;
  1071. qd->qd_last_warn = jiffies;
  1072. }
  1073. }
  1074. return error;
  1075. }
  1076. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  1077. kuid_t uid, kgid_t gid)
  1078. {
  1079. struct gfs2_quota_data *qd;
  1080. u32 x;
  1081. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1082. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ||
  1083. gfs2_assert_warn(sdp, change))
  1084. return;
  1085. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  1086. return;
  1087. if (gfs2_assert_withdraw(sdp, ip->i_qadata &&
  1088. ip->i_qadata->qa_ref > 0))
  1089. return;
  1090. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  1091. qd = ip->i_qadata->qa_qd[x];
  1092. if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1093. qid_eq(qd->qd_id, make_kqid_gid(gid))) {
  1094. do_qc(qd, change);
  1095. }
  1096. }
  1097. }
  1098. int gfs2_quota_sync(struct super_block *sb, int type)
  1099. {
  1100. struct gfs2_sbd *sdp = sb->s_fs_info;
  1101. struct gfs2_quota_data **qda;
  1102. unsigned int max_qd = PAGE_SIZE / sizeof(struct gfs2_holder);
  1103. u64 sync_gen;
  1104. int error = 0;
  1105. if (sb_rdonly(sdp->sd_vfs))
  1106. return 0;
  1107. qda = kzalloc_objs(struct gfs2_quota_data *, max_qd);
  1108. if (!qda)
  1109. return -ENOMEM;
  1110. mutex_lock(&sdp->sd_quota_sync_mutex);
  1111. sync_gen = sdp->sd_quota_sync_gen + 1;
  1112. do {
  1113. struct gfs2_quota_data *iter;
  1114. unsigned int num_qd = 0;
  1115. unsigned int x;
  1116. spin_lock(&qd_lock);
  1117. list_for_each_entry(iter, &sdp->sd_quota_list, qd_list) {
  1118. if (qd_grab_sync(sdp, iter, sync_gen)) {
  1119. qda[num_qd++] = iter;
  1120. if (num_qd == max_qd)
  1121. break;
  1122. }
  1123. }
  1124. spin_unlock(&qd_lock);
  1125. if (!num_qd)
  1126. break;
  1127. for (x = 0; x < num_qd; x++) {
  1128. error = bh_get(qda[x]);
  1129. if (!error)
  1130. continue;
  1131. while (x < num_qd)
  1132. qd_ungrab_sync(qda[--num_qd]);
  1133. break;
  1134. }
  1135. if (!error) {
  1136. WRITE_ONCE(sdp->sd_quota_sync_gen, sync_gen);
  1137. error = do_sync(num_qd, qda, sync_gen);
  1138. }
  1139. for (x = 0; x < num_qd; x++)
  1140. qd_unlock(qda[x]);
  1141. } while (!error);
  1142. mutex_unlock(&sdp->sd_quota_sync_mutex);
  1143. kfree(qda);
  1144. return error;
  1145. }
  1146. int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
  1147. {
  1148. struct gfs2_quota_data *qd;
  1149. struct gfs2_holder q_gh;
  1150. int error;
  1151. error = qd_get(sdp, qid, &qd);
  1152. if (error)
  1153. return error;
  1154. error = do_glock(qd, FORCE, &q_gh);
  1155. if (!error)
  1156. gfs2_glock_dq_uninit(&q_gh);
  1157. qd_put(qd);
  1158. return error;
  1159. }
  1160. int gfs2_quota_init(struct gfs2_sbd *sdp)
  1161. {
  1162. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  1163. u64 size = i_size_read(sdp->sd_qc_inode);
  1164. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  1165. unsigned int x, slot = 0;
  1166. unsigned int found = 0;
  1167. unsigned int hash;
  1168. unsigned int bm_size;
  1169. struct buffer_head *bh;
  1170. u64 dblock;
  1171. u32 extlen = 0;
  1172. int error;
  1173. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  1174. return -EIO;
  1175. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  1176. bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
  1177. bm_size *= sizeof(unsigned long);
  1178. error = -ENOMEM;
  1179. sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
  1180. if (sdp->sd_quota_bitmap == NULL)
  1181. sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
  1182. __GFP_ZERO);
  1183. if (!sdp->sd_quota_bitmap)
  1184. return error;
  1185. for (x = 0; x < blocks; x++) {
  1186. struct gfs2_quota_change *qc;
  1187. unsigned int y;
  1188. if (!extlen) {
  1189. extlen = 32;
  1190. error = gfs2_get_extent(&ip->i_inode, x, &dblock, &extlen);
  1191. if (error)
  1192. goto fail;
  1193. }
  1194. error = -EIO;
  1195. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1196. if (!bh)
  1197. goto fail;
  1198. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  1199. goto fail_brelse;
  1200. qc = (struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
  1201. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1202. y++, slot++) {
  1203. struct gfs2_quota_data *old_qd, *qd;
  1204. s64 qc_change = be64_to_cpu(qc->qc_change);
  1205. u32 qc_flags = be32_to_cpu(qc->qc_flags);
  1206. enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
  1207. USRQUOTA : GRPQUOTA;
  1208. struct kqid qc_id = make_kqid(&init_user_ns, qtype,
  1209. be32_to_cpu(qc->qc_id));
  1210. qc++;
  1211. if (!qc_change)
  1212. continue;
  1213. hash = gfs2_qd_hash(sdp, qc_id);
  1214. qd = qd_alloc(hash, sdp, qc_id);
  1215. if (qd == NULL)
  1216. goto fail_brelse;
  1217. qd->qd_lockref.count = 0;
  1218. set_bit(QDF_CHANGE, &qd->qd_flags);
  1219. qd->qd_change = qc_change;
  1220. qd->qd_slot = slot;
  1221. qd->qd_slot_ref = 1;
  1222. spin_lock(&qd_lock);
  1223. spin_lock_bucket(hash);
  1224. old_qd = gfs2_qd_search_bucket(hash, sdp, qc_id);
  1225. if (old_qd) {
  1226. fs_err(sdp, "Corruption found in quota_change%u"
  1227. "file: duplicate identifier in "
  1228. "slot %u\n",
  1229. sdp->sd_jdesc->jd_jid, slot);
  1230. spin_unlock_bucket(hash);
  1231. spin_unlock(&qd_lock);
  1232. qd_put(old_qd);
  1233. gfs2_glock_put(qd->qd_gl);
  1234. kmem_cache_free(gfs2_quotad_cachep, qd);
  1235. /* zero out the duplicate slot */
  1236. lock_buffer(bh);
  1237. memset(qc, 0, sizeof(*qc));
  1238. mark_buffer_dirty(bh);
  1239. unlock_buffer(bh);
  1240. continue;
  1241. }
  1242. BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
  1243. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1244. atomic_inc(&sdp->sd_quota_count);
  1245. hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
  1246. spin_unlock_bucket(hash);
  1247. spin_unlock(&qd_lock);
  1248. found++;
  1249. }
  1250. if (buffer_dirty(bh))
  1251. sync_dirty_buffer(bh);
  1252. brelse(bh);
  1253. dblock++;
  1254. extlen--;
  1255. }
  1256. if (found)
  1257. fs_info(sdp, "found %u quota changes\n", found);
  1258. return 0;
  1259. fail_brelse:
  1260. if (buffer_dirty(bh))
  1261. sync_dirty_buffer(bh);
  1262. brelse(bh);
  1263. fail:
  1264. gfs2_quota_cleanup(sdp);
  1265. return error;
  1266. }
  1267. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1268. {
  1269. struct gfs2_quota_data *qd;
  1270. LIST_HEAD(dispose);
  1271. int count;
  1272. BUG_ON(!test_bit(SDF_NORECOVERY, &sdp->sd_flags) &&
  1273. test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  1274. spin_lock(&qd_lock);
  1275. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  1276. spin_lock(&qd->qd_lockref.lock);
  1277. if (qd->qd_lockref.count != 0) {
  1278. spin_unlock(&qd->qd_lockref.lock);
  1279. continue;
  1280. }
  1281. lockref_mark_dead(&qd->qd_lockref);
  1282. spin_unlock(&qd->qd_lockref.lock);
  1283. list_lru_del_obj(&gfs2_qd_lru, &qd->qd_lru);
  1284. list_add(&qd->qd_lru, &dispose);
  1285. }
  1286. spin_unlock(&qd_lock);
  1287. gfs2_qd_list_dispose(&dispose);
  1288. wait_event_timeout(sdp->sd_kill_wait,
  1289. (count = atomic_read(&sdp->sd_quota_count)) == 0,
  1290. HZ * 60);
  1291. if (count != 0)
  1292. fs_err(sdp, "%d left-over quota data objects\n", count);
  1293. kvfree(sdp->sd_quota_bitmap);
  1294. sdp->sd_quota_bitmap = NULL;
  1295. }
  1296. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1297. {
  1298. if (error == 0 || error == -EROFS)
  1299. return;
  1300. if (!gfs2_withdrawn(sdp)) {
  1301. if (!cmpxchg(&sdp->sd_log_error, 0, error))
  1302. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1303. wake_up(&sdp->sd_logd_waitq);
  1304. }
  1305. }
  1306. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1307. if (!sdp->sd_statfs_force_sync) {
  1308. sdp->sd_statfs_force_sync = 1;
  1309. wake_up(&sdp->sd_quota_wait);
  1310. }
  1311. }
  1312. /**
  1313. * gfs2_quotad - Write cached quota changes into the quota file
  1314. * @data: Pointer to GFS2 superblock
  1315. *
  1316. */
  1317. int gfs2_quotad(void *data)
  1318. {
  1319. struct gfs2_sbd *sdp = data;
  1320. unsigned long now = jiffies;
  1321. unsigned long statfs_deadline = now;
  1322. unsigned long quotad_deadline = now;
  1323. set_freezable();
  1324. while (!kthread_should_stop()) {
  1325. unsigned long t;
  1326. if (gfs2_withdrawn(sdp))
  1327. break;
  1328. now = jiffies;
  1329. if (sdp->sd_statfs_force_sync ||
  1330. time_after(now, statfs_deadline)) {
  1331. unsigned int quantum;
  1332. int error;
  1333. /* Update the master statfs file */
  1334. error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1335. quotad_error(sdp, "statfs", error);
  1336. quantum = gfs2_tune_get(sdp, gt_statfs_quantum);
  1337. statfs_deadline = now + quantum * HZ;
  1338. }
  1339. if (time_after(now, quotad_deadline)) {
  1340. unsigned int quantum;
  1341. int error;
  1342. /* Update the quota file */
  1343. error = gfs2_quota_sync(sdp->sd_vfs, 0);
  1344. quotad_error(sdp, "sync", error);
  1345. quantum = gfs2_tune_get(sdp, gt_quota_quantum);
  1346. quotad_deadline = now + quantum * HZ;
  1347. }
  1348. t = min(statfs_deadline - now, quotad_deadline - now);
  1349. wait_event_freezable_timeout(sdp->sd_quota_wait,
  1350. sdp->sd_statfs_force_sync ||
  1351. gfs2_withdrawn(sdp) ||
  1352. kthread_should_stop(),
  1353. t);
  1354. if (sdp->sd_statfs_force_sync)
  1355. t = 0;
  1356. }
  1357. return 0;
  1358. }
  1359. static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
  1360. {
  1361. struct gfs2_sbd *sdp = sb->s_fs_info;
  1362. memset(state, 0, sizeof(*state));
  1363. switch (sdp->sd_args.ar_quota) {
  1364. case GFS2_QUOTA_QUIET:
  1365. fallthrough;
  1366. case GFS2_QUOTA_ON:
  1367. state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1368. state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1369. fallthrough;
  1370. case GFS2_QUOTA_ACCOUNT:
  1371. state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
  1372. QCI_SYSFILE;
  1373. state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
  1374. QCI_SYSFILE;
  1375. break;
  1376. case GFS2_QUOTA_OFF:
  1377. break;
  1378. }
  1379. if (sdp->sd_quota_inode) {
  1380. state->s_state[USRQUOTA].ino =
  1381. GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1382. state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
  1383. }
  1384. state->s_state[USRQUOTA].nextents = 1; /* unsupported */
  1385. state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
  1386. state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
  1387. return 0;
  1388. }
  1389. static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
  1390. struct qc_dqblk *fdq)
  1391. {
  1392. struct gfs2_sbd *sdp = sb->s_fs_info;
  1393. struct gfs2_quota_lvb *qlvb;
  1394. struct gfs2_quota_data *qd;
  1395. struct gfs2_holder q_gh;
  1396. int error;
  1397. memset(fdq, 0, sizeof(*fdq));
  1398. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1399. return -ESRCH; /* Crazy XFS error code */
  1400. if ((qid.type != USRQUOTA) &&
  1401. (qid.type != GRPQUOTA))
  1402. return -EINVAL;
  1403. error = qd_get(sdp, qid, &qd);
  1404. if (error)
  1405. return error;
  1406. error = do_glock(qd, FORCE, &q_gh);
  1407. if (error)
  1408. goto out;
  1409. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  1410. fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
  1411. fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
  1412. fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
  1413. gfs2_glock_dq_uninit(&q_gh);
  1414. out:
  1415. qd_put(qd);
  1416. return error;
  1417. }
  1418. /* GFS2 only supports a subset of the XFS fields */
  1419. #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
  1420. static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
  1421. struct qc_dqblk *fdq)
  1422. {
  1423. struct gfs2_sbd *sdp = sb->s_fs_info;
  1424. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1425. struct gfs2_quota_data *qd;
  1426. struct gfs2_holder q_gh, i_gh;
  1427. unsigned int data_blocks, ind_blocks;
  1428. unsigned int blocks = 0;
  1429. int alloc_required;
  1430. loff_t offset;
  1431. int error;
  1432. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1433. return -ESRCH; /* Crazy XFS error code */
  1434. if ((qid.type != USRQUOTA) &&
  1435. (qid.type != GRPQUOTA))
  1436. return -EINVAL;
  1437. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1438. return -EINVAL;
  1439. error = qd_get(sdp, qid, &qd);
  1440. if (error)
  1441. return error;
  1442. error = gfs2_qa_get(ip);
  1443. if (error)
  1444. goto out_put;
  1445. inode_lock(&ip->i_inode);
  1446. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1447. if (error)
  1448. goto out_unlockput;
  1449. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1450. if (error)
  1451. goto out_q;
  1452. /* Check for existing entry, if none then alloc new blocks */
  1453. error = update_qd(sdp, qd);
  1454. if (error)
  1455. goto out_i;
  1456. /* If nothing has changed, this is a no-op */
  1457. if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
  1458. ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1459. fdq->d_fieldmask ^= QC_SPC_SOFT;
  1460. if ((fdq->d_fieldmask & QC_SPC_HARD) &&
  1461. ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1462. fdq->d_fieldmask ^= QC_SPC_HARD;
  1463. if ((fdq->d_fieldmask & QC_SPACE) &&
  1464. ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1465. fdq->d_fieldmask ^= QC_SPACE;
  1466. if (fdq->d_fieldmask == 0)
  1467. goto out_i;
  1468. offset = qd2offset(qd);
  1469. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1470. if (gfs2_is_stuffed(ip))
  1471. alloc_required = 1;
  1472. if (alloc_required) {
  1473. struct gfs2_alloc_parms ap = {};
  1474. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1475. &data_blocks, &ind_blocks);
  1476. blocks = 1 + data_blocks + ind_blocks;
  1477. ap.target = blocks;
  1478. error = gfs2_inplace_reserve(ip, &ap);
  1479. if (error)
  1480. goto out_i;
  1481. blocks += gfs2_rg_blocks(ip, blocks);
  1482. }
  1483. /* Some quotas span block boundaries and can update two blocks,
  1484. adding an extra block to the transaction to handle such quotas */
  1485. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1486. if (error)
  1487. goto out_release;
  1488. /* Apply changes */
  1489. error = gfs2_adjust_quota(sdp, offset, 0, qd, fdq);
  1490. if (!error)
  1491. clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
  1492. gfs2_trans_end(sdp);
  1493. out_release:
  1494. if (alloc_required)
  1495. gfs2_inplace_release(ip);
  1496. out_i:
  1497. gfs2_glock_dq_uninit(&i_gh);
  1498. out_q:
  1499. gfs2_glock_dq_uninit(&q_gh);
  1500. out_unlockput:
  1501. gfs2_qa_put(ip);
  1502. inode_unlock(&ip->i_inode);
  1503. out_put:
  1504. qd_put(qd);
  1505. return error;
  1506. }
  1507. const struct quotactl_ops gfs2_quotactl_ops = {
  1508. .quota_sync = gfs2_quota_sync,
  1509. .get_state = gfs2_quota_get_state,
  1510. .get_dqblk = gfs2_get_dqblk,
  1511. .set_dqblk = gfs2_set_dqblk,
  1512. };
  1513. void __init gfs2_quota_hash_init(void)
  1514. {
  1515. unsigned i;
  1516. for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
  1517. INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
  1518. }