quota.c 39 KB

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