quota.c 38 KB

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