quota.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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_alloc *al = ip->i_alloc;
  406. struct gfs2_quota_data **qd = al->al_qd;
  407. int error;
  408. if (gfs2_assert_warn(sdp, !al->al_qd_num) ||
  409. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
  410. return -EIO;
  411. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  412. return 0;
  413. error = qdsb_get(sdp, QUOTA_USER, ip->i_inode.i_uid, qd);
  414. if (error)
  415. goto out;
  416. al->al_qd_num++;
  417. qd++;
  418. error = qdsb_get(sdp, QUOTA_GROUP, ip->i_inode.i_gid, qd);
  419. if (error)
  420. goto out;
  421. al->al_qd_num++;
  422. qd++;
  423. if (uid != NO_QUOTA_CHANGE && uid != ip->i_inode.i_uid) {
  424. error = qdsb_get(sdp, QUOTA_USER, uid, qd);
  425. if (error)
  426. goto out;
  427. al->al_qd_num++;
  428. qd++;
  429. }
  430. if (gid != NO_QUOTA_CHANGE && gid != ip->i_inode.i_gid) {
  431. error = qdsb_get(sdp, QUOTA_GROUP, gid, qd);
  432. if (error)
  433. goto out;
  434. al->al_qd_num++;
  435. qd++;
  436. }
  437. out:
  438. if (error)
  439. gfs2_quota_unhold(ip);
  440. return error;
  441. }
  442. void gfs2_quota_unhold(struct gfs2_inode *ip)
  443. {
  444. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  445. struct gfs2_alloc *al = ip->i_alloc;
  446. unsigned int x;
  447. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  448. for (x = 0; x < al->al_qd_num; x++) {
  449. qdsb_put(al->al_qd[x]);
  450. al->al_qd[x] = NULL;
  451. }
  452. al->al_qd_num = 0;
  453. }
  454. static int sort_qd(const void *a, const void *b)
  455. {
  456. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  457. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  458. if (!test_bit(QDF_USER, &qd_a->qd_flags) !=
  459. !test_bit(QDF_USER, &qd_b->qd_flags)) {
  460. if (test_bit(QDF_USER, &qd_a->qd_flags))
  461. return -1;
  462. else
  463. return 1;
  464. }
  465. if (qd_a->qd_id < qd_b->qd_id)
  466. return -1;
  467. if (qd_a->qd_id > qd_b->qd_id)
  468. return 1;
  469. return 0;
  470. }
  471. static void do_qc(struct gfs2_quota_data *qd, s64 change)
  472. {
  473. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  474. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  475. struct gfs2_quota_change *qc = qd->qd_bh_qc;
  476. s64 x;
  477. mutex_lock(&sdp->sd_quota_mutex);
  478. gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
  479. if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
  480. qc->qc_change = 0;
  481. qc->qc_flags = 0;
  482. if (test_bit(QDF_USER, &qd->qd_flags))
  483. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  484. qc->qc_id = cpu_to_be32(qd->qd_id);
  485. }
  486. x = be64_to_cpu(qc->qc_change) + change;
  487. qc->qc_change = cpu_to_be64(x);
  488. spin_lock(&qd_lru_lock);
  489. qd->qd_change = x;
  490. spin_unlock(&qd_lru_lock);
  491. if (!x) {
  492. gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
  493. clear_bit(QDF_CHANGE, &qd->qd_flags);
  494. qc->qc_flags = 0;
  495. qc->qc_id = 0;
  496. slot_put(qd);
  497. qd_put(qd);
  498. } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
  499. qd_hold(qd);
  500. slot_hold(qd);
  501. }
  502. mutex_unlock(&sdp->sd_quota_mutex);
  503. }
  504. /**
  505. * gfs2_adjust_quota - adjust record of current block usage
  506. * @ip: The quota inode
  507. * @loc: Offset of the entry in the quota file
  508. * @change: The amount of usage change to record
  509. * @qd: The quota data
  510. * @fdq: The updated limits to record
  511. *
  512. * This function was mostly borrowed from gfs2_block_truncate_page which was
  513. * in turn mostly borrowed from ext3
  514. *
  515. * Returns: 0 or -ve on error
  516. */
  517. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  518. s64 change, struct gfs2_quota_data *qd,
  519. struct fs_disk_quota *fdq)
  520. {
  521. struct inode *inode = &ip->i_inode;
  522. struct gfs2_sbd *sdp = GFS2_SB(inode);
  523. struct address_space *mapping = inode->i_mapping;
  524. unsigned long index = loc >> PAGE_CACHE_SHIFT;
  525. unsigned offset = loc & (PAGE_CACHE_SIZE - 1);
  526. unsigned blocksize, iblock, pos;
  527. struct buffer_head *bh, *dibh;
  528. struct page *page;
  529. void *kaddr, *ptr;
  530. struct gfs2_quota q, *qp;
  531. int err, nbytes;
  532. u64 size;
  533. if (gfs2_is_stuffed(ip))
  534. gfs2_unstuff_dinode(ip, NULL);
  535. memset(&q, 0, sizeof(struct gfs2_quota));
  536. err = gfs2_internal_read(ip, NULL, (char *)&q, &loc, sizeof(q));
  537. if (err < 0)
  538. return err;
  539. err = -EIO;
  540. qp = &q;
  541. qp->qu_value = be64_to_cpu(qp->qu_value);
  542. qp->qu_value += change;
  543. qp->qu_value = cpu_to_be64(qp->qu_value);
  544. qd->qd_qb.qb_value = qp->qu_value;
  545. if (fdq) {
  546. if (fdq->d_fieldmask & FS_DQ_BSOFT) {
  547. qp->qu_warn = cpu_to_be64(fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift);
  548. qd->qd_qb.qb_warn = qp->qu_warn;
  549. }
  550. if (fdq->d_fieldmask & FS_DQ_BHARD) {
  551. qp->qu_limit = cpu_to_be64(fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift);
  552. qd->qd_qb.qb_limit = qp->qu_limit;
  553. }
  554. if (fdq->d_fieldmask & FS_DQ_BCOUNT) {
  555. qp->qu_value = cpu_to_be64(fdq->d_bcount >> sdp->sd_fsb2bb_shift);
  556. qd->qd_qb.qb_value = qp->qu_value;
  557. }
  558. }
  559. /* Write the quota into the quota file on disk */
  560. ptr = qp;
  561. nbytes = sizeof(struct gfs2_quota);
  562. get_a_page:
  563. page = grab_cache_page(mapping, index);
  564. if (!page)
  565. return -ENOMEM;
  566. blocksize = inode->i_sb->s_blocksize;
  567. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  568. if (!page_has_buffers(page))
  569. create_empty_buffers(page, blocksize, 0);
  570. bh = page_buffers(page);
  571. pos = blocksize;
  572. while (offset >= pos) {
  573. bh = bh->b_this_page;
  574. iblock++;
  575. pos += blocksize;
  576. }
  577. if (!buffer_mapped(bh)) {
  578. gfs2_block_map(inode, iblock, bh, 1);
  579. if (!buffer_mapped(bh))
  580. goto unlock_out;
  581. /* If it's a newly allocated disk block for quota, zero it */
  582. if (buffer_new(bh))
  583. zero_user(page, pos - blocksize, bh->b_size);
  584. }
  585. if (PageUptodate(page))
  586. set_buffer_uptodate(bh);
  587. if (!buffer_uptodate(bh)) {
  588. ll_rw_block(READ_META, 1, &bh);
  589. wait_on_buffer(bh);
  590. if (!buffer_uptodate(bh))
  591. goto unlock_out;
  592. }
  593. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  594. kaddr = kmap_atomic(page, KM_USER0);
  595. if (offset + sizeof(struct gfs2_quota) > PAGE_CACHE_SIZE)
  596. nbytes = PAGE_CACHE_SIZE - offset;
  597. memcpy(kaddr + offset, ptr, nbytes);
  598. flush_dcache_page(page);
  599. kunmap_atomic(kaddr, KM_USER0);
  600. unlock_page(page);
  601. page_cache_release(page);
  602. /* If quota straddles page boundary, we need to update the rest of the
  603. * quota at the beginning of the next page */
  604. if ((offset + sizeof(struct gfs2_quota)) > PAGE_CACHE_SIZE) {
  605. ptr = ptr + nbytes;
  606. nbytes = sizeof(struct gfs2_quota) - nbytes;
  607. offset = 0;
  608. index++;
  609. goto get_a_page;
  610. }
  611. /* Update the disk inode timestamp and size (if extended) */
  612. err = gfs2_meta_inode_buffer(ip, &dibh);
  613. if (err)
  614. goto out;
  615. size = loc + sizeof(struct gfs2_quota);
  616. if (size > inode->i_size)
  617. i_size_write(inode, size);
  618. inode->i_mtime = inode->i_atime = CURRENT_TIME;
  619. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  620. gfs2_dinode_out(ip, dibh->b_data);
  621. brelse(dibh);
  622. mark_inode_dirty(inode);
  623. out:
  624. return err;
  625. unlock_out:
  626. unlock_page(page);
  627. page_cache_release(page);
  628. return err;
  629. }
  630. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  631. {
  632. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
  633. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  634. unsigned int data_blocks, ind_blocks;
  635. struct gfs2_holder *ghs, i_gh;
  636. unsigned int qx, x;
  637. struct gfs2_quota_data *qd;
  638. loff_t offset;
  639. unsigned int nalloc = 0, blocks;
  640. struct gfs2_alloc *al = NULL;
  641. int 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. al = gfs2_alloc_get(ip);
  665. if (!al) {
  666. error = -ENOMEM;
  667. goto out_gunlock;
  668. }
  669. /*
  670. * 1 blk for unstuffing inode if stuffed. We add this extra
  671. * block to the reservation unconditionally. If the inode
  672. * doesn't need unstuffing, the block will be released to the
  673. * rgrp since it won't be allocated during the transaction
  674. */
  675. al->al_requested = 1;
  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. if (nalloc)
  681. al->al_requested += nalloc * (data_blocks + ind_blocks);
  682. error = gfs2_inplace_reserve(ip);
  683. if (error)
  684. goto out_alloc;
  685. if (nalloc)
  686. blocks += gfs2_rg_blocks(al) + 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_alloc_put(ip);
  706. out_gunlock:
  707. gfs2_glock_dq_uninit(&i_gh);
  708. out:
  709. while (qx--)
  710. gfs2_glock_dq_uninit(&ghs[qx]);
  711. mutex_unlock(&ip->i_inode.i_mutex);
  712. kfree(ghs);
  713. gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
  714. return error;
  715. }
  716. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  717. {
  718. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  719. struct gfs2_quota q;
  720. struct gfs2_quota_lvb *qlvb;
  721. loff_t pos;
  722. int error;
  723. memset(&q, 0, sizeof(struct gfs2_quota));
  724. pos = qd2offset(qd);
  725. error = gfs2_internal_read(ip, NULL, (char *)&q, &pos, sizeof(q));
  726. if (error < 0)
  727. return error;
  728. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  729. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  730. qlvb->__pad = 0;
  731. qlvb->qb_limit = q.qu_limit;
  732. qlvb->qb_warn = q.qu_warn;
  733. qlvb->qb_value = q.qu_value;
  734. qd->qd_qb = *qlvb;
  735. return 0;
  736. }
  737. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  738. struct gfs2_holder *q_gh)
  739. {
  740. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  741. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  742. struct gfs2_holder i_gh;
  743. int error;
  744. restart:
  745. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  746. if (error)
  747. return error;
  748. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  749. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  750. gfs2_glock_dq_uninit(q_gh);
  751. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  752. GL_NOCACHE, q_gh);
  753. if (error)
  754. return error;
  755. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  756. if (error)
  757. goto fail;
  758. error = update_qd(sdp, qd);
  759. if (error)
  760. goto fail_gunlock;
  761. gfs2_glock_dq_uninit(&i_gh);
  762. gfs2_glock_dq_uninit(q_gh);
  763. force_refresh = 0;
  764. goto restart;
  765. }
  766. return 0;
  767. fail_gunlock:
  768. gfs2_glock_dq_uninit(&i_gh);
  769. fail:
  770. gfs2_glock_dq_uninit(q_gh);
  771. return error;
  772. }
  773. int gfs2_quota_lock(struct gfs2_inode *ip, u32 uid, u32 gid)
  774. {
  775. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  776. struct gfs2_alloc *al = ip->i_alloc;
  777. struct gfs2_quota_data *qd;
  778. unsigned int x;
  779. int error = 0;
  780. gfs2_quota_hold(ip, uid, gid);
  781. if (capable(CAP_SYS_RESOURCE) ||
  782. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  783. return 0;
  784. sort(al->al_qd, al->al_qd_num, sizeof(struct gfs2_quota_data *),
  785. sort_qd, NULL);
  786. for (x = 0; x < al->al_qd_num; x++) {
  787. int force = NO_FORCE;
  788. qd = al->al_qd[x];
  789. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  790. force = FORCE;
  791. error = do_glock(qd, force, &al->al_qd_ghs[x]);
  792. if (error)
  793. break;
  794. }
  795. if (!error)
  796. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  797. else {
  798. while (x--)
  799. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  800. gfs2_quota_unhold(ip);
  801. }
  802. return error;
  803. }
  804. static int need_sync(struct gfs2_quota_data *qd)
  805. {
  806. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  807. struct gfs2_tune *gt = &sdp->sd_tune;
  808. s64 value;
  809. unsigned int num, den;
  810. int do_sync = 1;
  811. if (!qd->qd_qb.qb_limit)
  812. return 0;
  813. spin_lock(&qd_lru_lock);
  814. value = qd->qd_change;
  815. spin_unlock(&qd_lru_lock);
  816. spin_lock(&gt->gt_spin);
  817. num = gt->gt_quota_scale_num;
  818. den = gt->gt_quota_scale_den;
  819. spin_unlock(&gt->gt_spin);
  820. if (value < 0)
  821. do_sync = 0;
  822. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  823. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  824. do_sync = 0;
  825. else {
  826. value *= gfs2_jindex_size(sdp) * num;
  827. value = div_s64(value, den);
  828. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  829. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  830. do_sync = 0;
  831. }
  832. return do_sync;
  833. }
  834. void gfs2_quota_unlock(struct gfs2_inode *ip)
  835. {
  836. struct gfs2_alloc *al = ip->i_alloc;
  837. struct gfs2_quota_data *qda[4];
  838. unsigned int count = 0;
  839. unsigned int x;
  840. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  841. goto out;
  842. for (x = 0; x < al->al_qd_num; x++) {
  843. struct gfs2_quota_data *qd;
  844. int sync;
  845. qd = al->al_qd[x];
  846. sync = need_sync(qd);
  847. gfs2_glock_dq_uninit(&al->al_qd_ghs[x]);
  848. if (sync && qd_trylock(qd))
  849. qda[count++] = qd;
  850. }
  851. if (count) {
  852. do_sync(count, qda);
  853. for (x = 0; x < count; x++)
  854. qd_unlock(qda[x]);
  855. }
  856. out:
  857. gfs2_quota_unhold(ip);
  858. }
  859. #define MAX_LINE 256
  860. static int print_message(struct gfs2_quota_data *qd, char *type)
  861. {
  862. struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
  863. printk(KERN_INFO "GFS2: fsid=%s: quota %s for %s %u\n",
  864. sdp->sd_fsname, type,
  865. (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
  866. qd->qd_id);
  867. return 0;
  868. }
  869. int gfs2_quota_check(struct gfs2_inode *ip, u32 uid, u32 gid)
  870. {
  871. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  872. struct gfs2_alloc *al = ip->i_alloc;
  873. struct gfs2_quota_data *qd;
  874. s64 value;
  875. unsigned int x;
  876. int error = 0;
  877. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  878. return 0;
  879. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  880. return 0;
  881. for (x = 0; x < al->al_qd_num; x++) {
  882. qd = al->al_qd[x];
  883. if (!((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  884. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))))
  885. continue;
  886. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  887. spin_lock(&qd_lru_lock);
  888. value += qd->qd_change;
  889. spin_unlock(&qd_lru_lock);
  890. if (be64_to_cpu(qd->qd_qb.qb_limit) && (s64)be64_to_cpu(qd->qd_qb.qb_limit) < value) {
  891. print_message(qd, "exceeded");
  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_BHARDWARN);
  895. error = -EDQUOT;
  896. break;
  897. } else if (be64_to_cpu(qd->qd_qb.qb_warn) &&
  898. (s64)be64_to_cpu(qd->qd_qb.qb_warn) < value &&
  899. time_after_eq(jiffies, qd->qd_last_warn +
  900. gfs2_tune_get(sdp,
  901. gt_quota_warn_period) * HZ)) {
  902. quota_send_warning(test_bit(QDF_USER, &qd->qd_flags) ?
  903. USRQUOTA : GRPQUOTA, qd->qd_id,
  904. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  905. error = print_message(qd, "warning");
  906. qd->qd_last_warn = jiffies;
  907. }
  908. }
  909. return error;
  910. }
  911. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  912. u32 uid, u32 gid)
  913. {
  914. struct gfs2_alloc *al = ip->i_alloc;
  915. struct gfs2_quota_data *qd;
  916. unsigned int x;
  917. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), change))
  918. return;
  919. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  920. return;
  921. for (x = 0; x < al->al_qd_num; x++) {
  922. qd = al->al_qd[x];
  923. if ((qd->qd_id == uid && test_bit(QDF_USER, &qd->qd_flags)) ||
  924. (qd->qd_id == gid && !test_bit(QDF_USER, &qd->qd_flags))) {
  925. do_qc(qd, change);
  926. }
  927. }
  928. }
  929. int gfs2_quota_sync(struct super_block *sb, int type, int wait)
  930. {
  931. struct gfs2_sbd *sdp = sb->s_fs_info;
  932. struct gfs2_quota_data **qda;
  933. unsigned int max_qd = gfs2_tune_get(sdp, gt_quota_simul_sync);
  934. unsigned int num_qd;
  935. unsigned int x;
  936. int error = 0;
  937. sdp->sd_quota_sync_gen++;
  938. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  939. if (!qda)
  940. return -ENOMEM;
  941. do {
  942. num_qd = 0;
  943. for (;;) {
  944. error = qd_fish(sdp, qda + num_qd);
  945. if (error || !qda[num_qd])
  946. break;
  947. if (++num_qd == max_qd)
  948. break;
  949. }
  950. if (num_qd) {
  951. if (!error)
  952. error = do_sync(num_qd, qda);
  953. if (!error)
  954. for (x = 0; x < num_qd; x++)
  955. qda[x]->qd_sync_gen =
  956. sdp->sd_quota_sync_gen;
  957. for (x = 0; x < num_qd; x++)
  958. qd_unlock(qda[x]);
  959. }
  960. } while (!error && num_qd == max_qd);
  961. kfree(qda);
  962. return error;
  963. }
  964. static int gfs2_quota_sync_timeo(struct super_block *sb, int type)
  965. {
  966. return gfs2_quota_sync(sb, type, 0);
  967. }
  968. int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id)
  969. {
  970. struct gfs2_quota_data *qd;
  971. struct gfs2_holder q_gh;
  972. int error;
  973. error = qd_get(sdp, user, id, &qd);
  974. if (error)
  975. return error;
  976. error = do_glock(qd, FORCE, &q_gh);
  977. if (!error)
  978. gfs2_glock_dq_uninit(&q_gh);
  979. qd_put(qd);
  980. return error;
  981. }
  982. static void gfs2_quota_change_in(struct gfs2_quota_change_host *qc, const void *buf)
  983. {
  984. const struct gfs2_quota_change *str = buf;
  985. qc->qc_change = be64_to_cpu(str->qc_change);
  986. qc->qc_flags = be32_to_cpu(str->qc_flags);
  987. qc->qc_id = be32_to_cpu(str->qc_id);
  988. }
  989. int gfs2_quota_init(struct gfs2_sbd *sdp)
  990. {
  991. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  992. u64 size = i_size_read(sdp->sd_qc_inode);
  993. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  994. unsigned int x, slot = 0;
  995. unsigned int found = 0;
  996. u64 dblock;
  997. u32 extlen = 0;
  998. int error;
  999. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  1000. return -EIO;
  1001. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  1002. sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
  1003. error = -ENOMEM;
  1004. sdp->sd_quota_bitmap = kcalloc(sdp->sd_quota_chunks,
  1005. sizeof(unsigned char *), GFP_NOFS);
  1006. if (!sdp->sd_quota_bitmap)
  1007. return error;
  1008. for (x = 0; x < sdp->sd_quota_chunks; x++) {
  1009. sdp->sd_quota_bitmap[x] = kzalloc(PAGE_SIZE, GFP_NOFS);
  1010. if (!sdp->sd_quota_bitmap[x])
  1011. goto fail;
  1012. }
  1013. for (x = 0; x < blocks; x++) {
  1014. struct buffer_head *bh;
  1015. unsigned int y;
  1016. if (!extlen) {
  1017. int new = 0;
  1018. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1019. if (error)
  1020. goto fail;
  1021. }
  1022. error = -EIO;
  1023. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1024. if (!bh)
  1025. goto fail;
  1026. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1027. brelse(bh);
  1028. goto fail;
  1029. }
  1030. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1031. y++, slot++) {
  1032. struct gfs2_quota_change_host qc;
  1033. struct gfs2_quota_data *qd;
  1034. gfs2_quota_change_in(&qc, bh->b_data +
  1035. sizeof(struct gfs2_meta_header) +
  1036. y * sizeof(struct gfs2_quota_change));
  1037. if (!qc.qc_change)
  1038. continue;
  1039. error = qd_alloc(sdp, (qc.qc_flags & GFS2_QCF_USER),
  1040. qc.qc_id, &qd);
  1041. if (error) {
  1042. brelse(bh);
  1043. goto fail;
  1044. }
  1045. set_bit(QDF_CHANGE, &qd->qd_flags);
  1046. qd->qd_change = qc.qc_change;
  1047. qd->qd_slot = slot;
  1048. qd->qd_slot_count = 1;
  1049. spin_lock(&qd_lru_lock);
  1050. gfs2_icbit_munge(sdp, sdp->sd_quota_bitmap, slot, 1);
  1051. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1052. atomic_inc(&sdp->sd_quota_count);
  1053. spin_unlock(&qd_lru_lock);
  1054. found++;
  1055. }
  1056. brelse(bh);
  1057. dblock++;
  1058. extlen--;
  1059. }
  1060. if (found)
  1061. fs_info(sdp, "found %u quota changes\n", found);
  1062. return 0;
  1063. fail:
  1064. gfs2_quota_cleanup(sdp);
  1065. return error;
  1066. }
  1067. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1068. {
  1069. struct list_head *head = &sdp->sd_quota_list;
  1070. struct gfs2_quota_data *qd;
  1071. unsigned int x;
  1072. spin_lock(&qd_lru_lock);
  1073. while (!list_empty(head)) {
  1074. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1075. if (atomic_read(&qd->qd_count) > 1 ||
  1076. (atomic_read(&qd->qd_count) &&
  1077. !test_bit(QDF_CHANGE, &qd->qd_flags))) {
  1078. list_move(&qd->qd_list, head);
  1079. spin_unlock(&qd_lru_lock);
  1080. schedule();
  1081. spin_lock(&qd_lru_lock);
  1082. continue;
  1083. }
  1084. list_del(&qd->qd_list);
  1085. /* Also remove if this qd exists in the reclaim list */
  1086. if (!list_empty(&qd->qd_reclaim)) {
  1087. list_del_init(&qd->qd_reclaim);
  1088. atomic_dec(&qd_lru_count);
  1089. }
  1090. atomic_dec(&sdp->sd_quota_count);
  1091. spin_unlock(&qd_lru_lock);
  1092. if (!atomic_read(&qd->qd_count)) {
  1093. gfs2_assert_warn(sdp, !qd->qd_change);
  1094. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1095. } else
  1096. gfs2_assert_warn(sdp, qd->qd_slot_count == 1);
  1097. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1098. gfs2_glock_put(qd->qd_gl);
  1099. kmem_cache_free(gfs2_quotad_cachep, qd);
  1100. spin_lock(&qd_lru_lock);
  1101. }
  1102. spin_unlock(&qd_lru_lock);
  1103. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1104. if (sdp->sd_quota_bitmap) {
  1105. for (x = 0; x < sdp->sd_quota_chunks; x++)
  1106. kfree(sdp->sd_quota_bitmap[x]);
  1107. kfree(sdp->sd_quota_bitmap);
  1108. }
  1109. }
  1110. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1111. {
  1112. if (error == 0 || error == -EROFS)
  1113. return;
  1114. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1115. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1116. }
  1117. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1118. int (*fxn)(struct super_block *sb, int type),
  1119. unsigned long t, unsigned long *timeo,
  1120. unsigned int *new_timeo)
  1121. {
  1122. if (t >= *timeo) {
  1123. int error = fxn(sdp->sd_vfs, 0);
  1124. quotad_error(sdp, msg, error);
  1125. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1126. } else {
  1127. *timeo -= t;
  1128. }
  1129. }
  1130. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1131. {
  1132. struct gfs2_inode *ip;
  1133. while(1) {
  1134. ip = NULL;
  1135. spin_lock(&sdp->sd_trunc_lock);
  1136. if (!list_empty(&sdp->sd_trunc_list)) {
  1137. ip = list_entry(sdp->sd_trunc_list.next,
  1138. struct gfs2_inode, i_trunc_list);
  1139. list_del_init(&ip->i_trunc_list);
  1140. }
  1141. spin_unlock(&sdp->sd_trunc_lock);
  1142. if (ip == NULL)
  1143. return;
  1144. gfs2_glock_finish_truncate(ip);
  1145. }
  1146. }
  1147. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1148. if (!sdp->sd_statfs_force_sync) {
  1149. sdp->sd_statfs_force_sync = 1;
  1150. wake_up(&sdp->sd_quota_wait);
  1151. }
  1152. }
  1153. /**
  1154. * gfs2_quotad - Write cached quota changes into the quota file
  1155. * @sdp: Pointer to GFS2 superblock
  1156. *
  1157. */
  1158. int gfs2_quotad(void *data)
  1159. {
  1160. struct gfs2_sbd *sdp = data;
  1161. struct gfs2_tune *tune = &sdp->sd_tune;
  1162. unsigned long statfs_timeo = 0;
  1163. unsigned long quotad_timeo = 0;
  1164. unsigned long t = 0;
  1165. DEFINE_WAIT(wait);
  1166. int empty;
  1167. while (!kthread_should_stop()) {
  1168. /* Update the master statfs file */
  1169. if (sdp->sd_statfs_force_sync) {
  1170. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1171. quotad_error(sdp, "statfs", error);
  1172. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1173. }
  1174. else
  1175. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1176. &statfs_timeo,
  1177. &tune->gt_statfs_quantum);
  1178. /* Update quota file */
  1179. quotad_check_timeo(sdp, "sync", gfs2_quota_sync_timeo, t,
  1180. &quotad_timeo, &tune->gt_quota_quantum);
  1181. /* Check for & recover partially truncated inodes */
  1182. quotad_check_trunc_list(sdp);
  1183. if (freezing(current))
  1184. refrigerator();
  1185. t = min(quotad_timeo, statfs_timeo);
  1186. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1187. spin_lock(&sdp->sd_trunc_lock);
  1188. empty = list_empty(&sdp->sd_trunc_list);
  1189. spin_unlock(&sdp->sd_trunc_lock);
  1190. if (empty && !sdp->sd_statfs_force_sync)
  1191. t -= schedule_timeout(t);
  1192. else
  1193. t = 0;
  1194. finish_wait(&sdp->sd_quota_wait, &wait);
  1195. }
  1196. return 0;
  1197. }
  1198. static int gfs2_quota_get_xstate(struct super_block *sb,
  1199. struct fs_quota_stat *fqs)
  1200. {
  1201. struct gfs2_sbd *sdp = sb->s_fs_info;
  1202. memset(fqs, 0, sizeof(struct fs_quota_stat));
  1203. fqs->qs_version = FS_QSTAT_VERSION;
  1204. switch (sdp->sd_args.ar_quota) {
  1205. case GFS2_QUOTA_ON:
  1206. fqs->qs_flags |= (FS_QUOTA_UDQ_ENFD | FS_QUOTA_GDQ_ENFD);
  1207. /*FALLTHRU*/
  1208. case GFS2_QUOTA_ACCOUNT:
  1209. fqs->qs_flags |= (FS_QUOTA_UDQ_ACCT | FS_QUOTA_GDQ_ACCT);
  1210. break;
  1211. case GFS2_QUOTA_OFF:
  1212. break;
  1213. }
  1214. if (sdp->sd_quota_inode) {
  1215. fqs->qs_uquota.qfs_ino = GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1216. fqs->qs_uquota.qfs_nblks = sdp->sd_quota_inode->i_blocks;
  1217. }
  1218. fqs->qs_uquota.qfs_nextents = 1; /* unsupported */
  1219. fqs->qs_gquota = fqs->qs_uquota; /* its the same inode in both cases */
  1220. fqs->qs_incoredqs = atomic_read(&qd_lru_count);
  1221. return 0;
  1222. }
  1223. static int gfs2_get_dqblk(struct super_block *sb, int type, qid_t id,
  1224. struct fs_disk_quota *fdq)
  1225. {
  1226. struct gfs2_sbd *sdp = sb->s_fs_info;
  1227. struct gfs2_quota_lvb *qlvb;
  1228. struct gfs2_quota_data *qd;
  1229. struct gfs2_holder q_gh;
  1230. int error;
  1231. memset(fdq, 0, sizeof(struct fs_disk_quota));
  1232. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1233. return -ESRCH; /* Crazy XFS error code */
  1234. if (type == USRQUOTA)
  1235. type = QUOTA_USER;
  1236. else if (type == GRPQUOTA)
  1237. type = QUOTA_GROUP;
  1238. else
  1239. return -EINVAL;
  1240. error = qd_get(sdp, type, id, &qd);
  1241. if (error)
  1242. return error;
  1243. error = do_glock(qd, FORCE, &q_gh);
  1244. if (error)
  1245. goto out;
  1246. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lvb;
  1247. fdq->d_version = FS_DQUOT_VERSION;
  1248. fdq->d_flags = (type == QUOTA_USER) ? FS_USER_QUOTA : FS_GROUP_QUOTA;
  1249. fdq->d_id = id;
  1250. fdq->d_blk_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_fsb2bb_shift;
  1251. fdq->d_blk_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_fsb2bb_shift;
  1252. fdq->d_bcount = be64_to_cpu(qlvb->qb_value) << sdp->sd_fsb2bb_shift;
  1253. gfs2_glock_dq_uninit(&q_gh);
  1254. out:
  1255. qd_put(qd);
  1256. return error;
  1257. }
  1258. /* GFS2 only supports a subset of the XFS fields */
  1259. #define GFS2_FIELDMASK (FS_DQ_BSOFT|FS_DQ_BHARD|FS_DQ_BCOUNT)
  1260. static int gfs2_set_dqblk(struct super_block *sb, int type, qid_t id,
  1261. struct fs_disk_quota *fdq)
  1262. {
  1263. struct gfs2_sbd *sdp = sb->s_fs_info;
  1264. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1265. struct gfs2_quota_data *qd;
  1266. struct gfs2_holder q_gh, i_gh;
  1267. unsigned int data_blocks, ind_blocks;
  1268. unsigned int blocks = 0;
  1269. int alloc_required;
  1270. struct gfs2_alloc *al;
  1271. loff_t offset;
  1272. int error;
  1273. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1274. return -ESRCH; /* Crazy XFS error code */
  1275. switch(type) {
  1276. case USRQUOTA:
  1277. type = QUOTA_USER;
  1278. if (fdq->d_flags != FS_USER_QUOTA)
  1279. return -EINVAL;
  1280. break;
  1281. case GRPQUOTA:
  1282. type = QUOTA_GROUP;
  1283. if (fdq->d_flags != FS_GROUP_QUOTA)
  1284. return -EINVAL;
  1285. break;
  1286. default:
  1287. return -EINVAL;
  1288. }
  1289. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1290. return -EINVAL;
  1291. if (fdq->d_id != id)
  1292. return -EINVAL;
  1293. error = qd_get(sdp, type, id, &qd);
  1294. if (error)
  1295. return error;
  1296. mutex_lock(&ip->i_inode.i_mutex);
  1297. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1298. if (error)
  1299. goto out_put;
  1300. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1301. if (error)
  1302. goto out_q;
  1303. /* Check for existing entry, if none then alloc new blocks */
  1304. error = update_qd(sdp, qd);
  1305. if (error)
  1306. goto out_i;
  1307. /* If nothing has changed, this is a no-op */
  1308. if ((fdq->d_fieldmask & FS_DQ_BSOFT) &&
  1309. ((fdq->d_blk_softlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1310. fdq->d_fieldmask ^= FS_DQ_BSOFT;
  1311. if ((fdq->d_fieldmask & FS_DQ_BHARD) &&
  1312. ((fdq->d_blk_hardlimit >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1313. fdq->d_fieldmask ^= FS_DQ_BHARD;
  1314. if ((fdq->d_fieldmask & FS_DQ_BCOUNT) &&
  1315. ((fdq->d_bcount >> sdp->sd_fsb2bb_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1316. fdq->d_fieldmask ^= FS_DQ_BCOUNT;
  1317. if (fdq->d_fieldmask == 0)
  1318. goto out_i;
  1319. offset = qd2offset(qd);
  1320. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1321. if (gfs2_is_stuffed(ip))
  1322. alloc_required = 1;
  1323. if (alloc_required) {
  1324. al = gfs2_alloc_get(ip);
  1325. if (al == NULL)
  1326. goto out_i;
  1327. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1328. &data_blocks, &ind_blocks);
  1329. blocks = al->al_requested = 1 + data_blocks + ind_blocks;
  1330. error = gfs2_inplace_reserve(ip);
  1331. if (error)
  1332. goto out_alloc;
  1333. blocks += gfs2_rg_blocks(al);
  1334. }
  1335. /* Some quotas span block boundaries and can update two blocks,
  1336. adding an extra block to the transaction to handle such quotas */
  1337. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1338. if (error)
  1339. goto out_release;
  1340. /* Apply changes */
  1341. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1342. gfs2_trans_end(sdp);
  1343. out_release:
  1344. if (alloc_required) {
  1345. gfs2_inplace_release(ip);
  1346. out_alloc:
  1347. gfs2_alloc_put(ip);
  1348. }
  1349. out_i:
  1350. gfs2_glock_dq_uninit(&i_gh);
  1351. out_q:
  1352. gfs2_glock_dq_uninit(&q_gh);
  1353. out_put:
  1354. mutex_unlock(&ip->i_inode.i_mutex);
  1355. qd_put(qd);
  1356. return error;
  1357. }
  1358. const struct quotactl_ops gfs2_quotactl_ops = {
  1359. .quota_sync = gfs2_quota_sync,
  1360. .get_xstate = gfs2_quota_get_xstate,
  1361. .get_dqblk = gfs2_get_dqblk,
  1362. .set_dqblk = gfs2_set_dqblk,
  1363. };