quota.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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;
  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. be64_add_cpu(&q.qu_value, change);
  561. qd->qd_qb.qb_value = q.qu_value;
  562. if (fdq) {
  563. if (fdq->d_fieldmask & FS_DQ_BSOFT) {
  564. q.qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
  565. qd->qd_qb.qb_warn = q.qu_warn;
  566. }
  567. if (fdq->d_fieldmask & FS_DQ_BHARD) {
  568. q.qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
  569. qd->qd_qb.qb_limit = q.qu_limit;
  570. }
  571. if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
  572. q.qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
  573. qd->qd_qb.qb_value = q.qu_value;
  574. }
  575. }
  576. /* Write the quota into the quota file on disk */
  577. ptr = &q;
  578. nbytes = sizeof(struct gfs2_quota);
  579. get_a_page:
  580. page = find_or_create_page(mapping, index, GFP_NOFS);
  581. if (!page)
  582. return -ENOMEM;
  583. blocksize = inode->i_sb->s_blocksize;
  584. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  585. if (!page_has_buffers(page))
  586. create_empty_buffers(page, blocksize, 0);
  587. bh = page_buffers(page);
  588. pos = blocksize;
  589. while (offset >= pos) {
  590. bh = bh->b_this_page;
  591. iblock++;
  592. pos += blocksize;
  593. }
  594. if (!buffer_mapped(bh)) {
  595. gfs2_block_map(inode, iblock, bh, 1);
  596. if (!buffer_mapped(bh))
  597. goto unlock_out;
  598. /* If it's a newly allocated disk block for quota, zero it */
  599. if (buffer_new(bh))
  600. zero_user(page, pos - blocksize, bh->b_size);
  601. }
  602. if (PageUptodate(page))
  603. set_buffer_uptodate(bh);
  604. if (!buffer_uptodate(bh)) {
  605. ll_rw_block(READ | REQ_META, 1, &bh);
  606. wait_on_buffer(bh);
  607. if (!buffer_uptodate(bh))
  608. goto unlock_out;
  609. }
  610. gfs2_trans_add_data(ip->i_gl, bh);
  611. kaddr = kmap_atomic(page);
  612. if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
  613. nbytes = PAGE_CACHE_SIZE - offset;
  614. memcpy(kaddr + offset, ptr, nbytes);
  615. flush_dcache_page(page);
  616. kunmap_atomic(kaddr);
  617. unlock_page(page);
  618. page_cache_release(page);
  619. /* If quota straddles page boundary, we need to update the rest of the
  620. * quota at the beginning of the next page */
  621. if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
  622. ptr = ptr + nbytes;
  623. nbytes = sizeof(struct gfs2_quota) - nbytes;
  624. offset = 0;
  625. index++;
  626. goto get_a_page;
  627. }
  628. size = loc + sizeof(struct gfs2_quota);
  629. if (size > inode->i_size)
  630. i_size_write(inode, size);
  631. inode->i_mtime = inode->i_atime = CURRENT_TIME;
  632. mark_inode_dirty(inode);
  633. return 0;
  634. unlock_out:
  635. unlock_page(page);
  636. page_cache_release(page);
  637. return err;
  638. }
  639. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  640. {
  641. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  642. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  643. struct gfs2_alloc_parms ap = { .aflags = 0, };
  644. unsigned int data_blocks, ind_blocks;
  645. struct gfs2_holder *ghs, i_gh;
  646. unsigned int qx, x;
  647. struct gfs2_quota_data *qd;
  648. unsigned reserved;
  649. loff_t offset;
  650. unsigned int nalloc = 0, blocks;
  651. int error;
  652. error = gfs2_rs_alloc(ip);
  653. if (error)
  654. return error;
  655. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  656. &data_blocks, &ind_blocks);
  657. ghs = kcalloc(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
  658. if (!ghs)
  659. return -ENOMEM;
  660. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  661. mutex_lock(&ip->i_inode.i_mutex);
  662. for (qx = 0; qx < num_qd; qx++) {
  663. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  664. GL_NOCACHE, &ghs[qx]);
  665. if (error)
  666. goto out;
  667. }
  668. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  669. if (error)
  670. goto out;
  671. for (x = 0; x < num_qd; x++) {
  672. offset = qd2offset(qda[x]);
  673. if (gfs2_write_alloc_required(ip, offset,
  674. sizeof(struct gfs2_quota)))
  675. nalloc++;
  676. }
  677. /*
  678. * 1 blk for unstuffing inode if stuffed. We add this extra
  679. * block to the reservation unconditionally. If the inode
  680. * doesn't need unstuffing, the block will be released to the
  681. * rgrp since it won't be allocated during the transaction
  682. */
  683. /* +3 in the end for unstuffing block, inode size update block
  684. * and another block in case quota straddles page boundary and
  685. * two blocks need to be updated instead of 1 */
  686. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  687. reserved = 1 + (nalloc * (data_blocks + ind_blocks));
  688. ap.target = reserved;
  689. error = gfs2_inplace_reserve(ip, &ap);
  690. if (error)
  691. goto out_alloc;
  692. if (nalloc)
  693. blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
  694. error = gfs2_trans_begin(sdp, blocks, 0);
  695. if (error)
  696. goto out_ipres;
  697. for (x = 0; x < num_qd; x++) {
  698. qd = qda[x];
  699. offset = qd2offset(qd);
  700. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
  701. if (error)
  702. goto out_end_trans;
  703. do_qc(qd, -qd->qd_change_sync);
  704. set_bit(QDF_REFRESH, &qd->qd_flags);
  705. }
  706. error = 0;
  707. out_end_trans:
  708. gfs2_trans_end(sdp);
  709. out_ipres:
  710. gfs2_inplace_release(ip);
  711. out_alloc:
  712. gfs2_glock_dq_uninit(&i_gh);
  713. out:
  714. while (qx--)
  715. gfs2_glock_dq_uninit(&ghs[qx]);
  716. mutex_unlock(&ip->i_inode.i_mutex);
  717. kfree(ghs);
  718. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  719. return error;
  720. }
  721. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  722. {
  723. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  724. struct gfs2_quota q;
  725. struct gfs2_quota_lvb *qlvb;
  726. loff_t pos;
  727. int error;
  728. memset(&q, 0, sizeof(struct gfs2_quota));
  729. pos = qd2offset(qd);
  730. error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
  731. if (error < 0)
  732. return error;
  733. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  734. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  735. qlvb->__pad = 0;
  736. qlvb->qb_limit = q.qu_limit;
  737. qlvb->qb_warn = q.qu_warn;
  738. qlvb->qb_value = q.qu_value;
  739. qd->qd_qb = *qlvb;
  740. return 0;
  741. }
  742. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  743. struct gfs2_holder *q_gh)
  744. {
  745. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  746. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  747. struct gfs2_holder i_gh;
  748. int error;
  749. restart:
  750. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  751. if (error)
  752. return error;
  753. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  754. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  755. gfs2_glock_dq_uninit(q_gh);
  756. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  757. GL_NOCACHE, q_gh);
  758. if (error)
  759. return error;
  760. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  761. if (error)
  762. goto fail;
  763. error = update_qd(sdp, qd);
  764. if (error)
  765. goto fail_gunlock;
  766. gfs2_glock_dq_uninit(&i_gh);
  767. gfs2_glock_dq_uninit(q_gh);
  768. force_refresh = 0;
  769. goto restart;
  770. }
  771. return 0;
  772. fail_gunlock:
  773. gfs2_glock_dq_uninit(&i_gh);
  774. fail:
  775. gfs2_glock_dq_uninit(q_gh);
  776. return error;
  777. }
  778. int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  779. {
  780. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  781. struct gfs2_quota_data *qd;
  782. unsigned int x;
  783. int error = 0;
  784. error = gfs2_quota_hold(ip, uid, gid);
  785. if (error)
  786. return error;
  787. if (capable(CAP_SYS_RESOURCE) ||
  788. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  789. return 0;
  790. sort(ip->i_res->rs_qa_qd, ip->i_res->rs_qa_qd_num,
  791. sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  792. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  793. int force = NO_FORCE;
  794. qd = ip->i_res->rs_qa_qd[x];
  795. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  796. force = FORCE;
  797. error = do_glock(qd, force, &ip->i_res->rs_qa_qd_ghs[x]);
  798. if (error)
  799. break;
  800. }
  801. if (!error)
  802. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  803. else {
  804. while (x--)
  805. gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
  806. gfs2_quota_unhold(ip);
  807. }
  808. return error;
  809. }
  810. static int need_sync(struct gfs2_quota_data *qd)
  811. {
  812. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  813. struct gfs2_tune *gt = &sdp->sd_tune;
  814. s64 value;
  815. unsigned int num, den;
  816. int do_sync = 1;
  817. if (!qd->qd_qb.qb_limit)
  818. return 0;
  819. spin_lock(&qd_lock);
  820. value = qd->qd_change;
  821. spin_unlock(&qd_lock);
  822. spin_lock(&gt->gt_spin);
  823. num = gt->gt_quota_scale_num;
  824. den = gt->gt_quota_scale_den;
  825. spin_unlock(&gt->gt_spin);
  826. if (value < 0)
  827. do_sync = 0;
  828. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  829. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  830. do_sync = 0;
  831. else {
  832. value *= gfs2_jindex_size(sdp) * num;
  833. value = div_s64(value, den);
  834. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  835. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  836. do_sync = 0;
  837. }
  838. return do_sync;
  839. }
  840. void gfs2_quota_unlock(struct gfs2_inode *ip)
  841. {
  842. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  843. struct gfs2_quota_data *qda[4];
  844. unsigned int count = 0;
  845. unsigned int x;
  846. int found;
  847. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  848. goto out;
  849. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  850. struct gfs2_quota_data *qd;
  851. int sync;
  852. qd = ip->i_res->rs_qa_qd[x];
  853. sync = need_sync(qd);
  854. gfs2_glock_dq_uninit(&ip->i_res->rs_qa_qd_ghs[x]);
  855. if (!sync)
  856. continue;
  857. spin_lock(&qd_lock);
  858. found = qd_check_sync(sdp, qd, NULL);
  859. spin_unlock(&qd_lock);
  860. if (!found)
  861. continue;
  862. gfs2_assert_warn(sdp, qd->qd_change_sync);
  863. if (bh_get(qd)) {
  864. clear_bit(QDF_LOCKED, &qd->qd_flags);
  865. slot_put(qd);
  866. qd_put(qd);
  867. continue;
  868. }
  869. qda[count++] = qd;
  870. }
  871. if (count) {
  872. do_sync(count, qda);
  873. for (x = 0; x < count; x++)
  874. qd_unlock(qda[x]);
  875. }
  876. out:
  877. gfs2_quota_unhold(ip);
  878. }
  879. #define MAX_LINE 256
  880. static int print_message(struct gfs2_quota_data *qd, char *type)
  881. {
  882. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  883. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
  884. sdp->sd_fsname, type,
  885. (qd->qd_id.type == USRQUOTA) ? "user" : "group",
  886. from_kqid(&init_user_ns, qd->qd_id));
  887. return 0;
  888. }
  889. int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  890. {
  891. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  892. struct gfs2_quota_data *qd;
  893. s64 value;
  894. unsigned int x;
  895. int error = 0;
  896. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  897. return 0;
  898. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  899. return 0;
  900. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  901. qd = ip->i_res->rs_qa_qd[x];
  902. if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  903. qid_eq(qd->qd_id, make_kqid_gid(gid))))
  904. continue;
  905. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  906. spin_lock(&qd_lock);
  907. value += qd->qd_change;
  908. spin_unlock(&qd_lock);
  909. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  910. print_message(qd, "exceeded");
  911. quota_send_warning(qd->qd_id,
  912. sdp->sd_vfs->s_dev, QUOTA_NL_BHARDWARN);
  913. error = -EDQUOT;
  914. break;
  915. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  916. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  917. time_after_eq(jiffies, qd->qd_last_warn +
  918. gfs2_tune_get(sdp,
  919. gt_quota_warn_period) * HZ)) {
  920. quota_send_warning(qd->qd_id,
  921. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  922. error = print_message(qd, "warning");
  923. qd->qd_last_warn = jiffies;
  924. }
  925. }
  926. return error;
  927. }
  928. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  929. kuid_t uid, kgid_t gid)
  930. {
  931. struct gfs2_quota_data *qd;
  932. unsigned int x;
  933. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  934. return;
  935. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  936. return;
  937. for (x = 0; x < ip->i_res->rs_qa_qd_num; x++) {
  938. qd = ip->i_res->rs_qa_qd[x];
  939. if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  940. qid_eq(qd->qd_id, make_kqid_gid(gid))) {
  941. do_qc(qd, change);
  942. }
  943. }
  944. }
  945. int gfs2_quota_sync(struct super_block *sb, int type)
  946. {
  947. struct gfs2_sbd *sdp = sb->s_fs_info;
  948. struct gfs2_quota_data **qda;
  949. unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
  950. unsigned int num_qd;
  951. unsigned int x;
  952. int error = 0;
  953. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  954. if (!qda)
  955. return -ENOMEM;
  956. mutex_lock(&sdp->sd_quota_sync_mutex);
  957. sdp->sd_quota_sync_gen++;
  958. do {
  959. num_qd = 0;
  960. for (;;) {
  961. error = qd_fish(sdp, qda + num_qd);
  962. if (error || !qda[num_qd])
  963. break;
  964. if (++num_qd == max_qd)
  965. break;
  966. }
  967. if (num_qd) {
  968. if (!error)
  969. error = do_sync(num_qd, qda);
  970. if (!error)
  971. for (x = 0; x < num_qd; x++)
  972. qda[x]->qd_sync_gen =
  973. sdp->sd_quota_sync_gen;
  974. for (x = 0; x < num_qd; x++)
  975. qd_unlock(qda[x]);
  976. }
  977. } while (!error && num_qd == max_qd);
  978. mutex_unlock(&sdp->sd_quota_sync_mutex);
  979. kfree(qda);
  980. return error;
  981. }
  982. int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
  983. {
  984. struct gfs2_quota_data *qd;
  985. struct gfs2_holder q_gh;
  986. int error;
  987. error = qd_get(sdp, qid, &qd);
  988. if (error)
  989. return error;
  990. error = do_glock(qd, FORCE, &q_gh);
  991. if (!error)
  992. gfs2_glock_dq_uninit(&q_gh);
  993. qd_put(qd);
  994. return error;
  995. }
  996. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  997. {
  998. const struct gfs2_quota_change *str = buf;
  999. qc->qc_change = be64_to_cpu(str->qc_change);
  1000. qc->qc_flags = be32_to_cpu(str->qc_flags);
  1001. qc->qc_id = make_kqid(&init_user_ns,
  1002. (qc->qc_flags & GFS2_QCF_USER)?USRQUOTA:GRPQUOTA,
  1003. be32_to_cpu(str->qc_id));
  1004. }
  1005. int gfs2_quota_init(struct gfs2_sbd *sdp)
  1006. {
  1007. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  1008. u64 size = i_size_read(sdp->sd_qc_inode);
  1009. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  1010. unsigned int x, slot = 0;
  1011. unsigned int found = 0;
  1012. u64 dblock;
  1013. u32 extlen = 0;
  1014. int error;
  1015. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  1016. return -EIO;
  1017. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  1018. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  1019. error = -ENOMEM;
  1020. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  1021. sizeof(unsigned char *), GFP_NOFS);
  1022. if (!sdp->sd_quota_bitmap)
  1023. return error;
  1024. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  1025. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  1026. if (!sdp->sd_quota_bitmap[x])
  1027. goto fail;
  1028. }
  1029. for (x = 0; x < blocks; x++) {
  1030. struct buffer_head *bh;
  1031. unsigned int y;
  1032. if (!extlen) {
  1033. int new = 0;
  1034. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1035. if (error)
  1036. goto fail;
  1037. }
  1038. error = -EIO;
  1039. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1040. if (!bh)
  1041. goto fail;
  1042. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1043. brelse(bh);
  1044. goto fail;
  1045. }
  1046. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1047. y++, slot++) {
  1048. struct gfs2_quota_change_host qc;
  1049. struct gfs2_quota_data *qd;
  1050. gfs2_quota_change_in(&qc, bh->b_data +
  1051. sizeof(struct gfs2_meta_header) +
  1052. y * sizeof(struct gfs2_quota_change));
  1053. if (!qc.qc_change)
  1054. continue;
  1055. error = qd_alloc(sdp, qc.qc_id, &qd);
  1056. if (error) {
  1057. brelse(bh);
  1058. goto fail;
  1059. }
  1060. set_bit(QDF_CHANGE, &qd->qd_flags);
  1061. qd->qd_change = qc.qc_change;
  1062. qd->qd_slot = slot;
  1063. qd->qd_slot_count = 1;
  1064. spin_lock(&qd_lock);
  1065. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  1066. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1067. atomic_inc(&sdp->sd_quota_count);
  1068. spin_unlock(&qd_lock);
  1069. found++;
  1070. }
  1071. brelse(bh);
  1072. dblock++;
  1073. extlen--;
  1074. }
  1075. if (found)
  1076. fs_info(sdp, "found %u quota changes\n", found);
  1077. return 0;
  1078. fail:
  1079. gfs2_quota_cleanup(sdp);
  1080. return error;
  1081. }
  1082. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1083. {
  1084. struct list_head *head = &sdp->sd_quota_list;
  1085. struct gfs2_quota_data *qd;
  1086. unsigned int x;
  1087. spin_lock(&qd_lock);
  1088. while (!list_empty(head)) {
  1089. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1090. /*
  1091. * To be removed in due course... we should be able to
  1092. * ensure that all refs to the qd have done by this point
  1093. * so that this rather odd test is not required
  1094. */
  1095. spin_lock(&qd->qd_lockref.lock);
  1096. if (qd->qd_lockref.count > 1 ||
  1097. (qd->qd_lockref.count && !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1098. spin_unlock(&qd->qd_lockref.lock);
  1099. list_move(&qd->qd_list, head);
  1100. spin_unlock(&qd_lock);
  1101. schedule();
  1102. spin_lock(&qd_lock);
  1103. continue;
  1104. }
  1105. spin_unlock(&qd->qd_lockref.lock);
  1106. list_del(&qd->qd_list);
  1107. /* Also remove if this qd exists in the reclaim list */
  1108. list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
  1109. atomic_dec(&sdp->sd_quota_count);
  1110. spin_unlock(&qd_lock);
  1111. if (!qd->qd_lockref.count) {
  1112. gfs2_assert_warn(sdp, !qd->qd_change);
  1113. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1114. } else
  1115. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1116. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1117. gfs2_glock_put(qd->qd_gl);
  1118. kmem_cache_free(gfs2_quotad_cachep, qd);
  1119. spin_lock(&qd_lock);
  1120. }
  1121. spin_unlock(&qd_lock);
  1122. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1123. if (sdp->sd_quota_bitmap) {
  1124. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1125. kfree(sdp->sd_quota_bitmap[x]);
  1126. kfree(sdp->sd_quota_bitmap);
  1127. }
  1128. }
  1129. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1130. {
  1131. if (error == 0 || error == -EROFS)
  1132. return;
  1133. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1134. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1135. }
  1136. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1137. int (*fxn)(struct super_block *sb, int type),
  1138. unsigned long t, unsigned long *timeo,
  1139. unsigned int *new_timeo)
  1140. {
  1141. if (t >= *timeo) {
  1142. int error = fxn(sdp->sd_vfs, 0);
  1143. quotad_error(sdp, msg, error);
  1144. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1145. } else {
  1146. *timeo -= t;
  1147. }
  1148. }
  1149. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1150. {
  1151. struct gfs2_inode *ip;
  1152. while(1) {
  1153. ip = NULL;
  1154. spin_lock(&sdp->sd_trunc_lock);
  1155. if (!list_empty(&sdp->sd_trunc_list)) {
  1156. ip = list_entry(sdp->sd_trunc_list.next,
  1157. struct gfs2_inode, i_trunc_list);
  1158. list_del_init(&ip->i_trunc_list);
  1159. }
  1160. spin_unlock(&sdp->sd_trunc_lock);
  1161. if (ip == NULL)
  1162. return;
  1163. gfs2_glock_finish_truncate(ip);
  1164. }
  1165. }
  1166. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1167. if (!sdp->sd_statfs_force_sync) {
  1168. sdp->sd_statfs_force_sync = 1;
  1169. wake_up(&sdp->sd_quota_wait);
  1170. }
  1171. }
  1172. /**
  1173. * gfs2_quotad - Write cached quota changes into the quota file
  1174. * @sdp: Pointer to GFS2 superblock
  1175. *
  1176. */
  1177. int gfs2_quotad(void *data)
  1178. {
  1179. struct gfs2_sbd *sdp = data;
  1180. struct gfs2_tune *tune = &sdp->sd_tune;
  1181. unsigned long statfs_timeo = 0;
  1182. unsigned long quotad_timeo = 0;
  1183. unsigned long t = 0;
  1184. DEFINE_WAIT(wait);
  1185. int empty;
  1186. while (!kthread_should_stop()) {
  1187. /* Update the master statfs file */
  1188. if (sdp->sd_statfs_force_sync) {
  1189. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1190. quotad_error(sdp, "statfs", error);
  1191. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1192. }
  1193. else
  1194. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1195. &statfs_timeo,
  1196. &tune->gt_statfs_quantum);
  1197. /* Update quota file */
  1198. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1199. &quotad_timeo, &tune->gt_quota_quantum);
  1200. /* Check for & recover partially truncated inodes */
  1201. quotad_check_trunc_list(sdp);
  1202. try_to_freeze();
  1203. t = min(quotad_timeo, statfs_timeo);
  1204. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1205. spin_lock(&sdp->sd_trunc_lock);
  1206. empty = list_empty(&sdp->sd_trunc_list);
  1207. spin_unlock(&sdp->sd_trunc_lock);
  1208. if (empty && !sdp->sd_statfs_force_sync)
  1209. t -= schedule_timeout(t);
  1210. else
  1211. t = 0;
  1212. finish_wait(&sdp->sd_quota_wait, &wait);
  1213. }
  1214. return 0;
  1215. }
  1216. static int gfs2_quota_get_xstate(struct super_block *sb,
  1217. struct fs_quota_stat *fqs)
  1218. {
  1219. struct gfs2_sbd *sdp = sb->s_fs_info;
  1220. memset(fqs, 0, sizeof(struct fs_quota_stat));
  1221. fqs->qs_version = FS_QSTAT_VERSION;
  1222. switch (sdp->sd_args.ar_quota) {
  1223. case GFS2_QUOTA_ON:
  1224. fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
  1225. /*FALLTHRU*/
  1226. case GFS2_QUOTA_ACCOUNT:
  1227. fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
  1228. break;
  1229. case GFS2_QUOTA_OFF:
  1230. break;
  1231. }
  1232. if (sdp->sd_quota_inode) {
  1233. fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1234. fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
  1235. }
  1236. fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
  1237. fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
  1238. fqs->qs_incoredqs = list_lru_count(&gfs2_qd_lru);
  1239. return 0;
  1240. }
  1241. static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
  1242. struct fs_disk_quota *fdq)
  1243. {
  1244. struct gfs2_sbd *sdp = sb->s_fs_info;
  1245. struct gfs2_quota_lvb *qlvb;
  1246. struct gfs2_quota_data *qd;
  1247. struct gfs2_holder q_gh;
  1248. int error;
  1249. memset(fdq, 0, sizeof(struct fs_disk_quota));
  1250. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1251. return -ESRCH; /* Crazy XFS error code */
  1252. if ((qid.type != USRQUOTA) &&
  1253. (qid.type != GRPQUOTA))
  1254. return -EINVAL;
  1255. error = qd_get(sdp, qid, &qd);
  1256. if (error)
  1257. return error;
  1258. error = do_glock(qd, FORCE, &q_gh);
  1259. if (error)
  1260. goto out;
  1261. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  1262. fdq->d_version = FS_DQUOT_VERSION;
  1263. fdq->d_flags = (qid.type == USRQUOTA) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
  1264. fdq->d_id = from_kqid_munged(current_user_ns(), qid);
  1265. fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
  1266. fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
  1267. fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
  1268. gfs2_glock_dq_uninit(&q_gh);
  1269. out:
  1270. qd_put(qd);
  1271. return error;
  1272. }
  1273. /* GFS2 only supports a subset of the XFS fields */
  1274. #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
  1275. static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
  1276. struct fs_disk_quota *fdq)
  1277. {
  1278. struct gfs2_sbd *sdp = sb->s_fs_info;
  1279. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1280. struct gfs2_quota_data *qd;
  1281. struct gfs2_holder q_gh, i_gh;
  1282. unsigned int data_blocks, ind_blocks;
  1283. unsigned int blocks = 0;
  1284. int alloc_required;
  1285. loff_t offset;
  1286. int error;
  1287. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1288. return -ESRCH; /* Crazy XFS error code */
  1289. if ((qid.type != USRQUOTA) &&
  1290. (qid.type != GRPQUOTA))
  1291. return -EINVAL;
  1292. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1293. return -EINVAL;
  1294. error = qd_get(sdp, qid, &qd);
  1295. if (error)
  1296. return error;
  1297. error = gfs2_rs_alloc(ip);
  1298. if (error)
  1299. goto out_put;
  1300. mutex_lock(&ip->i_inode.i_mutex);
  1301. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1302. if (error)
  1303. goto out_unlockput;
  1304. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1305. if (error)
  1306. goto out_q;
  1307. /* Check for existing entry, if none then alloc new blocks */
  1308. error = update_qd(sdp, qd);
  1309. if (error)
  1310. goto out_i;
  1311. /* If nothing has changed, this is a no-op */
  1312. if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
  1313. ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1314. fdq->d_fieldmask ^= FS_DQ_BSOFT;
  1315. if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
  1316. ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1317. fdq->d_fieldmask ^= FS_DQ_BHARD;
  1318. if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
  1319. ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1320. fdq->d_fieldmask ^= FS_DQ_BCOUNT;
  1321. if (fdq->d_fieldmask == 0)
  1322. goto out_i;
  1323. offset = qd2offset(qd);
  1324. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1325. if (gfs2_is_stuffed(ip))
  1326. alloc_required = 1;
  1327. if (alloc_required) {
  1328. struct gfs2_alloc_parms ap = { .aflags = 0, };
  1329. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1330. &data_blocks, &ind_blocks);
  1331. blocks = 1 + data_blocks + ind_blocks;
  1332. ap.target = blocks;
  1333. error = gfs2_inplace_reserve(ip, &ap);
  1334. if (error)
  1335. goto out_i;
  1336. blocks += gfs2_rg_blocks(ip, blocks);
  1337. }
  1338. /* Some quotas span block boundaries and can update two blocks,
  1339. adding an extra block to the transaction to handle such quotas */
  1340. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1341. if (error)
  1342. goto out_release;
  1343. /* Apply changes */
  1344. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1345. gfs2_trans_end(sdp);
  1346. out_release:
  1347. if (alloc_required)
  1348. gfs2_inplace_release(ip);
  1349. out_i:
  1350. gfs2_glock_dq_uninit(&i_gh);
  1351. out_q:
  1352. gfs2_glock_dq_uninit(&q_gh);
  1353. out_unlockput:
  1354. mutex_unlock(&ip->i_inode.i_mutex);
  1355. out_put:
  1356. qd_put(qd);
  1357. return error;
  1358. }
  1359. const struct quotactl_ops gfs2_quotactl_ops = {
  1360. .quota_sync = gfs2_quota_sync,
  1361. .get_xstate = gfs2_quota_get_xstate,
  1362. .get_dqblk = gfs2_get_dqblk,
  1363. .set_dqblk = gfs2_set_dqblk,
  1364. };