quota.c 38 KB

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