quota.c 39 KB

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