quota.c 38 KB

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