quota.c 38 KB

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