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