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