quota.c 38 KB

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