quota.c 38 KB

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