quota.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/mm.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/completion.h>
  43. #include <linux/buffer_head.h>
  44. #include <linux/sort.h>
  45. #include <linux/fs.h>
  46. #include <linux/bio.h>
  47. #include <linux/gfs2_ondisk.h>
  48. #include <linux/kthread.h>
  49. #include <linux/freezer.h>
  50. #include <linux/quota.h>
  51. #include <linux/dqblk_xfs.h>
  52. #include <linux/lockref.h>
  53. #include <linux/list_lru.h>
  54. #include <linux/rcupdate.h>
  55. #include <linux/rculist_bl.h>
  56. #include <linux/bit_spinlock.h>
  57. #include <linux/jhash.h>
  58. #include <linux/vmalloc.h>
  59. #include "gfs2.h"
  60. #include "incore.h"
  61. #include "bmap.h"
  62. #include "glock.h"
  63. #include "glops.h"
  64. #include "log.h"
  65. #include "meta_io.h"
  66. #include "quota.h"
  67. #include "rgrp.h"
  68. #include "super.h"
  69. #include "trans.h"
  70. #include "inode.h"
  71. #include "util.h"
  72. #define GFS2_QD_HASH_SHIFT 12
  73. #define GFS2_QD_HASH_SIZE BIT(GFS2_QD_HASH_SHIFT)
  74. #define GFS2_QD_HASH_MASK (GFS2_QD_HASH_SIZE - 1)
  75. /* Lock order: qd_lock -> bucket lock -> qd->lockref.lock -> lru lock */
  76. /* -> sd_bitmap_lock */
  77. static DEFINE_SPINLOCK(qd_lock);
  78. struct list_lru gfs2_qd_lru;
  79. static struct hlist_bl_head qd_hash_table[GFS2_QD_HASH_SIZE];
  80. static unsigned int gfs2_qd_hash(const struct gfs2_sbd *sdp,
  81. const struct kqid qid)
  82. {
  83. unsigned int h;
  84. h = jhash(&sdp, sizeof(struct gfs2_sbd *), 0);
  85. h = jhash(&qid, sizeof(struct kqid), h);
  86. return h & GFS2_QD_HASH_MASK;
  87. }
  88. static inline void spin_lock_bucket(unsigned int hash)
  89. {
  90. hlist_bl_lock(&qd_hash_table[hash]);
  91. }
  92. static inline void spin_unlock_bucket(unsigned int hash)
  93. {
  94. hlist_bl_unlock(&qd_hash_table[hash]);
  95. }
  96. static void gfs2_qd_dealloc(struct rcu_head *rcu)
  97. {
  98. struct gfs2_quota_data *qd = container_of(rcu, struct gfs2_quota_data, qd_rcu);
  99. kmem_cache_free(gfs2_quotad_cachep, qd);
  100. }
  101. static void gfs2_qd_dispose(struct list_head *list)
  102. {
  103. struct gfs2_quota_data *qd;
  104. struct gfs2_sbd *sdp;
  105. while (!list_empty(list)) {
  106. qd = list_entry(list->next, struct gfs2_quota_data, qd_lru);
  107. sdp = qd->qd_gl->gl_name.ln_sbd;
  108. list_del(&qd->qd_lru);
  109. /* Free from the filesystem-specific list */
  110. spin_lock(&qd_lock);
  111. list_del(&qd->qd_list);
  112. spin_unlock(&qd_lock);
  113. spin_lock_bucket(qd->qd_hash);
  114. hlist_bl_del_rcu(&qd->qd_hlist);
  115. spin_unlock_bucket(qd->qd_hash);
  116. gfs2_assert_warn(sdp, !qd->qd_change);
  117. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  118. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  119. gfs2_glock_put(qd->qd_gl);
  120. atomic_dec(&sdp->sd_quota_count);
  121. /* Delete it from the common reclaim list */
  122. call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
  123. }
  124. }
  125. static enum lru_status gfs2_qd_isolate(struct list_head *item,
  126. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  127. {
  128. struct list_head *dispose = arg;
  129. struct gfs2_quota_data *qd = list_entry(item, struct gfs2_quota_data, qd_lru);
  130. if (!spin_trylock(&qd->qd_lockref.lock))
  131. return LRU_SKIP;
  132. if (qd->qd_lockref.count == 0) {
  133. lockref_mark_dead(&qd->qd_lockref);
  134. list_lru_isolate_move(lru, &qd->qd_lru, dispose);
  135. }
  136. spin_unlock(&qd->qd_lockref.lock);
  137. return LRU_REMOVED;
  138. }
  139. static unsigned long gfs2_qd_shrink_scan(struct shrinker *shrink,
  140. struct shrink_control *sc)
  141. {
  142. LIST_HEAD(dispose);
  143. unsigned long freed;
  144. if (!(sc->gfp_mask & __GFP_FS))
  145. return SHRINK_STOP;
  146. freed = list_lru_shrink_walk(&gfs2_qd_lru, sc,
  147. gfs2_qd_isolate, &dispose);
  148. gfs2_qd_dispose(&dispose);
  149. return freed;
  150. }
  151. static unsigned long gfs2_qd_shrink_count(struct shrinker *shrink,
  152. struct shrink_control *sc)
  153. {
  154. return vfs_pressure_ratio(list_lru_shrink_count(&gfs2_qd_lru, sc));
  155. }
  156. struct shrinker gfs2_qd_shrinker = {
  157. .count_objects = gfs2_qd_shrink_count,
  158. .scan_objects = gfs2_qd_shrink_scan,
  159. .seeks = DEFAULT_SEEKS,
  160. .flags = SHRINKER_NUMA_AWARE,
  161. };
  162. static u64 qd2index(struct gfs2_quota_data *qd)
  163. {
  164. struct kqid qid = qd->qd_id;
  165. return (2 * (u64)from_kqid(&init_user_ns, qid)) +
  166. ((qid.type == USRQUOTA) ? 0 : 1);
  167. }
  168. static u64 qd2offset(struct gfs2_quota_data *qd)
  169. {
  170. u64 offset;
  171. offset = qd2index(qd);
  172. offset *= sizeof(struct gfs2_quota);
  173. return offset;
  174. }
  175. static struct gfs2_quota_data *qd_alloc(unsigned hash, struct gfs2_sbd *sdp, struct kqid qid)
  176. {
  177. struct gfs2_quota_data *qd;
  178. int error;
  179. qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
  180. if (!qd)
  181. return NULL;
  182. qd->qd_sbd = sdp;
  183. qd->qd_lockref.count = 1;
  184. spin_lock_init(&qd->qd_lockref.lock);
  185. qd->qd_id = qid;
  186. qd->qd_slot = -1;
  187. INIT_LIST_HEAD(&qd->qd_lru);
  188. qd->qd_hash = hash;
  189. error = gfs2_glock_get(sdp, qd2index(qd),
  190. &gfs2_quota_glops, CREATE, &qd->qd_gl);
  191. if (error)
  192. goto fail;
  193. return qd;
  194. fail:
  195. kmem_cache_free(gfs2_quotad_cachep, qd);
  196. return NULL;
  197. }
  198. static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,
  199. const struct gfs2_sbd *sdp,
  200. struct kqid qid)
  201. {
  202. struct gfs2_quota_data *qd;
  203. struct hlist_bl_node *h;
  204. hlist_bl_for_each_entry_rcu(qd, h, &qd_hash_table[hash], qd_hlist) {
  205. if (!qid_eq(qd->qd_id, qid))
  206. continue;
  207. if (qd->qd_sbd != sdp)
  208. continue;
  209. if (lockref_get_not_dead(&qd->qd_lockref)) {
  210. list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
  211. return qd;
  212. }
  213. }
  214. return NULL;
  215. }
  216. static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
  217. struct gfs2_quota_data **qdp)
  218. {
  219. struct gfs2_quota_data *qd, *new_qd;
  220. unsigned int hash = gfs2_qd_hash(sdp, qid);
  221. rcu_read_lock();
  222. *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
  223. rcu_read_unlock();
  224. if (qd)
  225. return 0;
  226. new_qd = qd_alloc(hash, sdp, qid);
  227. if (!new_qd)
  228. return -ENOMEM;
  229. spin_lock(&qd_lock);
  230. spin_lock_bucket(hash);
  231. *qdp = qd = gfs2_qd_search_bucket(hash, sdp, qid);
  232. if (qd == NULL) {
  233. *qdp = new_qd;
  234. list_add(&new_qd->qd_list, &sdp->sd_quota_list);
  235. hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
  236. atomic_inc(&sdp->sd_quota_count);
  237. }
  238. spin_unlock_bucket(hash);
  239. spin_unlock(&qd_lock);
  240. if (qd) {
  241. gfs2_glock_put(new_qd->qd_gl);
  242. kmem_cache_free(gfs2_quotad_cachep, new_qd);
  243. }
  244. return 0;
  245. }
  246. static void qd_hold(struct gfs2_quota_data *qd)
  247. {
  248. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  249. gfs2_assert(sdp, !__lockref_is_dead(&qd->qd_lockref));
  250. lockref_get(&qd->qd_lockref);
  251. }
  252. static void qd_put(struct gfs2_quota_data *qd)
  253. {
  254. if (lockref_put_or_lock(&qd->qd_lockref))
  255. return;
  256. qd->qd_lockref.count = 0;
  257. list_lru_add(&gfs2_qd_lru, &qd->qd_lru);
  258. spin_unlock(&qd->qd_lockref.lock);
  259. }
  260. static int slot_get(struct gfs2_quota_data *qd)
  261. {
  262. struct gfs2_sbd *sdp = qd->qd_sbd;
  263. unsigned int bit;
  264. int error = 0;
  265. spin_lock(&sdp->sd_bitmap_lock);
  266. if (qd->qd_slot_count != 0)
  267. goto out;
  268. error = -ENOSPC;
  269. bit = find_first_zero_bit(sdp->sd_quota_bitmap, sdp->sd_quota_slots);
  270. if (bit < sdp->sd_quota_slots) {
  271. set_bit(bit, sdp->sd_quota_bitmap);
  272. qd->qd_slot = bit;
  273. error = 0;
  274. out:
  275. qd->qd_slot_count++;
  276. }
  277. spin_unlock(&sdp->sd_bitmap_lock);
  278. return error;
  279. }
  280. static void slot_hold(struct gfs2_quota_data *qd)
  281. {
  282. struct gfs2_sbd *sdp = qd->qd_sbd;
  283. spin_lock(&sdp->sd_bitmap_lock);
  284. gfs2_assert(sdp, qd->qd_slot_count);
  285. qd->qd_slot_count++;
  286. spin_unlock(&sdp->sd_bitmap_lock);
  287. }
  288. static void slot_put(struct gfs2_quota_data *qd)
  289. {
  290. struct gfs2_sbd *sdp = qd->qd_sbd;
  291. spin_lock(&sdp->sd_bitmap_lock);
  292. gfs2_assert(sdp, qd->qd_slot_count);
  293. if (!--qd->qd_slot_count) {
  294. BUG_ON(!test_and_clear_bit(qd->qd_slot, sdp->sd_quota_bitmap));
  295. qd->qd_slot = -1;
  296. }
  297. spin_unlock(&sdp->sd_bitmap_lock);
  298. }
  299. static int bh_get(struct gfs2_quota_data *qd)
  300. {
  301. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  302. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  303. unsigned int block, offset;
  304. struct buffer_head *bh;
  305. int error;
  306. struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
  307. mutex_lock(&sdp->sd_quota_mutex);
  308. if (qd->qd_bh_count++) {
  309. mutex_unlock(&sdp->sd_quota_mutex);
  310. return 0;
  311. }
  312. block = qd->qd_slot / sdp->sd_qc_per_block;
  313. offset = qd->qd_slot % sdp->sd_qc_per_block;
  314. bh_map.b_size = BIT(ip->i_inode.i_blkbits);
  315. error = gfs2_block_map(&ip->i_inode, block, &bh_map, 0);
  316. if (error)
  317. goto fail;
  318. error = gfs2_meta_read(ip->i_gl, bh_map.b_blocknr, DIO_WAIT, 0, &bh);
  319. if (error)
  320. goto fail;
  321. error = -EIO;
  322. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC))
  323. goto fail_brelse;
  324. qd->qd_bh = bh;
  325. qd->qd_bh_qc = (struct gfs2_quota_change *)
  326. (bh->b_data + sizeof(struct gfs2_meta_header) +
  327. offset * sizeof(struct gfs2_quota_change));
  328. mutex_unlock(&sdp->sd_quota_mutex);
  329. return 0;
  330. fail_brelse:
  331. brelse(bh);
  332. fail:
  333. qd->qd_bh_count--;
  334. mutex_unlock(&sdp->sd_quota_mutex);
  335. return error;
  336. }
  337. static void bh_put(struct gfs2_quota_data *qd)
  338. {
  339. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  340. mutex_lock(&sdp->sd_quota_mutex);
  341. gfs2_assert(sdp, qd->qd_bh_count);
  342. if (!--qd->qd_bh_count) {
  343. brelse(qd->qd_bh);
  344. qd->qd_bh = NULL;
  345. qd->qd_bh_qc = NULL;
  346. }
  347. mutex_unlock(&sdp->sd_quota_mutex);
  348. }
  349. static int qd_check_sync(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd,
  350. u64 *sync_gen)
  351. {
  352. if (test_bit(QDF_LOCKED, &qd->qd_flags) ||
  353. !test_bit(QDF_CHANGE, &qd->qd_flags) ||
  354. (sync_gen && (qd->qd_sync_gen >= *sync_gen)))
  355. return 0;
  356. if (!lockref_get_not_dead(&qd->qd_lockref))
  357. return 0;
  358. list_move_tail(&qd->qd_list, &sdp->sd_quota_list);
  359. set_bit(QDF_LOCKED, &qd->qd_flags);
  360. qd->qd_change_sync = qd->qd_change;
  361. slot_hold(qd);
  362. return 1;
  363. }
  364. static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
  365. {
  366. struct gfs2_quota_data *qd = NULL;
  367. int error;
  368. int found = 0;
  369. *qdp = NULL;
  370. if (sdp->sd_vfs->s_flags & MS_RDONLY)
  371. return 0;
  372. spin_lock(&qd_lock);
  373. list_for_each_entry(qd, &sdp->sd_quota_list, qd_list) {
  374. found = qd_check_sync(sdp, qd, &sdp->sd_quota_sync_gen);
  375. if (found)
  376. break;
  377. }
  378. if (!found)
  379. qd = NULL;
  380. spin_unlock(&qd_lock);
  381. if (qd) {
  382. gfs2_assert_warn(sdp, qd->qd_change_sync);
  383. error = bh_get(qd);
  384. if (error) {
  385. clear_bit(QDF_LOCKED, &qd->qd_flags);
  386. slot_put(qd);
  387. qd_put(qd);
  388. return error;
  389. }
  390. }
  391. *qdp = qd;
  392. return 0;
  393. }
  394. static void qd_unlock(struct gfs2_quota_data *qd)
  395. {
  396. gfs2_assert_warn(qd->qd_gl->gl_name.ln_sbd,
  397. test_bit(QDF_LOCKED, &qd->qd_flags));
  398. clear_bit(QDF_LOCKED, &qd->qd_flags);
  399. bh_put(qd);
  400. slot_put(qd);
  401. qd_put(qd);
  402. }
  403. static int qdsb_get(struct gfs2_sbd *sdp, struct kqid qid,
  404. struct gfs2_quota_data **qdp)
  405. {
  406. int error;
  407. error = qd_get(sdp, qid, qdp);
  408. if (error)
  409. return error;
  410. error = slot_get(*qdp);
  411. if (error)
  412. goto fail;
  413. error = bh_get(*qdp);
  414. if (error)
  415. goto fail_slot;
  416. return 0;
  417. fail_slot:
  418. slot_put(*qdp);
  419. fail:
  420. qd_put(*qdp);
  421. return error;
  422. }
  423. static void qdsb_put(struct gfs2_quota_data *qd)
  424. {
  425. bh_put(qd);
  426. slot_put(qd);
  427. qd_put(qd);
  428. }
  429. /**
  430. * gfs2_qa_alloc - make sure we have a quota allocations data structure,
  431. * if necessary
  432. * @ip: the inode for this reservation
  433. */
  434. int gfs2_qa_alloc(struct gfs2_inode *ip)
  435. {
  436. int error = 0;
  437. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  438. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  439. return 0;
  440. down_write(&ip->i_rw_mutex);
  441. if (ip->i_qadata == NULL) {
  442. ip->i_qadata = kmem_cache_zalloc(gfs2_qadata_cachep, GFP_NOFS);
  443. if (!ip->i_qadata)
  444. error = -ENOMEM;
  445. }
  446. up_write(&ip->i_rw_mutex);
  447. return error;
  448. }
  449. void gfs2_qa_delete(struct gfs2_inode *ip, atomic_t *wcount)
  450. {
  451. down_write(&ip->i_rw_mutex);
  452. if (ip->i_qadata && ((wcount == NULL) || (atomic_read(wcount) <= 1))) {
  453. kmem_cache_free(gfs2_qadata_cachep, ip->i_qadata);
  454. ip->i_qadata = NULL;
  455. }
  456. up_write(&ip->i_rw_mutex);
  457. }
  458. int gfs2_quota_hold(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  459. {
  460. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  461. struct gfs2_quota_data **qd;
  462. int error;
  463. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  464. return 0;
  465. if (ip->i_qadata == NULL) {
  466. error = gfs2_rsqa_alloc(ip);
  467. if (error)
  468. return error;
  469. }
  470. qd = ip->i_qadata->qa_qd;
  471. if (gfs2_assert_warn(sdp, !ip->i_qadata->qa_qd_num) ||
  472. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags)))
  473. return -EIO;
  474. error = qdsb_get(sdp, make_kqid_uid(ip->i_inode.i_uid), qd);
  475. if (error)
  476. goto out;
  477. ip->i_qadata->qa_qd_num++;
  478. qd++;
  479. error = qdsb_get(sdp, make_kqid_gid(ip->i_inode.i_gid), qd);
  480. if (error)
  481. goto out;
  482. ip->i_qadata->qa_qd_num++;
  483. qd++;
  484. if (!uid_eq(uid, NO_UID_QUOTA_CHANGE) &&
  485. !uid_eq(uid, ip->i_inode.i_uid)) {
  486. error = qdsb_get(sdp, make_kqid_uid(uid), qd);
  487. if (error)
  488. goto out;
  489. ip->i_qadata->qa_qd_num++;
  490. qd++;
  491. }
  492. if (!gid_eq(gid, NO_GID_QUOTA_CHANGE) &&
  493. !gid_eq(gid, ip->i_inode.i_gid)) {
  494. error = qdsb_get(sdp, make_kqid_gid(gid), qd);
  495. if (error)
  496. goto out;
  497. ip->i_qadata->qa_qd_num++;
  498. qd++;
  499. }
  500. out:
  501. if (error)
  502. gfs2_quota_unhold(ip);
  503. return error;
  504. }
  505. void gfs2_quota_unhold(struct gfs2_inode *ip)
  506. {
  507. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  508. u32 x;
  509. if (ip->i_qadata == NULL)
  510. return;
  511. gfs2_assert_warn(sdp, !test_bit(GIF_QD_LOCKED, &ip->i_flags));
  512. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  513. qdsb_put(ip->i_qadata->qa_qd[x]);
  514. ip->i_qadata->qa_qd[x] = NULL;
  515. }
  516. ip->i_qadata->qa_qd_num = 0;
  517. }
  518. static int sort_qd(const void *a, const void *b)
  519. {
  520. const struct gfs2_quota_data *qd_a = *(const struct gfs2_quota_data **)a;
  521. const struct gfs2_quota_data *qd_b = *(const struct gfs2_quota_data **)b;
  522. if (qid_lt(qd_a->qd_id, qd_b->qd_id))
  523. return -1;
  524. if (qid_lt(qd_b->qd_id, qd_a->qd_id))
  525. return 1;
  526. return 0;
  527. }
  528. static void do_qc(struct gfs2_quota_data *qd, s64 change)
  529. {
  530. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  531. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  532. struct gfs2_quota_change *qc = qd->qd_bh_qc;
  533. s64 x;
  534. mutex_lock(&sdp->sd_quota_mutex);
  535. gfs2_trans_add_meta(ip->i_gl, qd->qd_bh);
  536. if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
  537. qc->qc_change = 0;
  538. qc->qc_flags = 0;
  539. if (qd->qd_id.type == USRQUOTA)
  540. qc->qc_flags = cpu_to_be32(GFS2_QCF_USER);
  541. qc->qc_id = cpu_to_be32(from_kqid(&init_user_ns, qd->qd_id));
  542. }
  543. x = be64_to_cpu(qc->qc_change) + change;
  544. qc->qc_change = cpu_to_be64(x);
  545. spin_lock(&qd_lock);
  546. qd->qd_change = x;
  547. spin_unlock(&qd_lock);
  548. if (!x) {
  549. gfs2_assert_warn(sdp, test_bit(QDF_CHANGE, &qd->qd_flags));
  550. clear_bit(QDF_CHANGE, &qd->qd_flags);
  551. qc->qc_flags = 0;
  552. qc->qc_id = 0;
  553. slot_put(qd);
  554. qd_put(qd);
  555. } else if (!test_and_set_bit(QDF_CHANGE, &qd->qd_flags)) {
  556. qd_hold(qd);
  557. slot_hold(qd);
  558. }
  559. if (change < 0) /* Reset quiet flag if we freed some blocks */
  560. clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
  561. mutex_unlock(&sdp->sd_quota_mutex);
  562. }
  563. static int gfs2_write_buf_to_page(struct gfs2_inode *ip, unsigned long index,
  564. unsigned off, void *buf, unsigned bytes)
  565. {
  566. struct inode *inode = &ip->i_inode;
  567. struct gfs2_sbd *sdp = GFS2_SB(inode);
  568. struct address_space *mapping = inode->i_mapping;
  569. struct page *page;
  570. struct buffer_head *bh;
  571. void *kaddr;
  572. u64 blk;
  573. unsigned bsize = sdp->sd_sb.sb_bsize, bnum = 0, boff = 0;
  574. unsigned to_write = bytes, pg_off = off;
  575. int done = 0;
  576. blk = index << (PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift);
  577. boff = off % bsize;
  578. page = find_or_create_page(mapping, index, GFP_NOFS);
  579. if (!page)
  580. return -ENOMEM;
  581. if (!page_has_buffers(page))
  582. create_empty_buffers(page, bsize, 0);
  583. bh = page_buffers(page);
  584. while (!done) {
  585. /* Find the beginning block within the page */
  586. if (pg_off >= ((bnum * bsize) + bsize)) {
  587. bh = bh->b_this_page;
  588. bnum++;
  589. blk++;
  590. continue;
  591. }
  592. if (!buffer_mapped(bh)) {
  593. gfs2_block_map(inode, blk, bh, 1);
  594. if (!buffer_mapped(bh))
  595. goto unlock_out;
  596. /* If it's a newly allocated disk block, zero it */
  597. if (buffer_new(bh))
  598. zero_user(page, bnum * bsize, bh->b_size);
  599. }
  600. if (PageUptodate(page))
  601. set_buffer_uptodate(bh);
  602. if (!buffer_uptodate(bh)) {
  603. ll_rw_block(REQ_OP_READ, REQ_META, 1, &bh);
  604. wait_on_buffer(bh);
  605. if (!buffer_uptodate(bh))
  606. goto unlock_out;
  607. }
  608. gfs2_trans_add_data(ip->i_gl, bh);
  609. /* If we need to write to the next block as well */
  610. if (to_write > (bsize - boff)) {
  611. pg_off += (bsize - boff);
  612. to_write -= (bsize - boff);
  613. boff = pg_off % bsize;
  614. continue;
  615. }
  616. done = 1;
  617. }
  618. /* Write to the page, now that we have setup the buffer(s) */
  619. kaddr = kmap_atomic(page);
  620. memcpy(kaddr + off, buf, bytes);
  621. flush_dcache_page(page);
  622. kunmap_atomic(kaddr);
  623. unlock_page(page);
  624. put_page(page);
  625. return 0;
  626. unlock_out:
  627. unlock_page(page);
  628. put_page(page);
  629. return -EIO;
  630. }
  631. static int gfs2_write_disk_quota(struct gfs2_inode *ip, struct gfs2_quota *qp,
  632. loff_t loc)
  633. {
  634. unsigned long pg_beg;
  635. unsigned pg_off, nbytes, overflow = 0;
  636. int pg_oflow = 0, error;
  637. void *ptr;
  638. nbytes = sizeof(struct gfs2_quota);
  639. pg_beg = loc >> PAGE_SHIFT;
  640. pg_off = loc % PAGE_SIZE;
  641. /* If the quota straddles a page boundary, split the write in two */
  642. if ((pg_off + nbytes) > PAGE_SIZE) {
  643. pg_oflow = 1;
  644. overflow = (pg_off + nbytes) - PAGE_SIZE;
  645. }
  646. ptr = qp;
  647. error = gfs2_write_buf_to_page(ip, pg_beg, pg_off, ptr,
  648. nbytes - overflow);
  649. /* If there's an overflow, write the remaining bytes to the next page */
  650. if (!error && pg_oflow)
  651. error = gfs2_write_buf_to_page(ip, pg_beg + 1, 0,
  652. ptr + nbytes - overflow,
  653. overflow);
  654. return error;
  655. }
  656. /**
  657. * gfs2_adjust_quota - adjust record of current block usage
  658. * @ip: The quota inode
  659. * @loc: Offset of the entry in the quota file
  660. * @change: The amount of usage change to record
  661. * @qd: The quota data
  662. * @fdq: The updated limits to record
  663. *
  664. * This function was mostly borrowed from gfs2_block_truncate_page which was
  665. * in turn mostly borrowed from ext3
  666. *
  667. * Returns: 0 or -ve on error
  668. */
  669. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  670. s64 change, struct gfs2_quota_data *qd,
  671. struct qc_dqblk *fdq)
  672. {
  673. struct inode *inode = &ip->i_inode;
  674. struct gfs2_sbd *sdp = GFS2_SB(inode);
  675. struct gfs2_quota q;
  676. int err;
  677. u64 size;
  678. if (gfs2_is_stuffed(ip)) {
  679. err = gfs2_unstuff_dinode(ip, NULL);
  680. if (err)
  681. return err;
  682. }
  683. memset(&q, 0, sizeof(struct gfs2_quota));
  684. err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
  685. if (err < 0)
  686. return err;
  687. loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */
  688. err = -EIO;
  689. be64_add_cpu(&q.qu_value, change);
  690. if (((s64)be64_to_cpu(q.qu_value)) < 0)
  691. q.qu_value = 0; /* Never go negative on quota usage */
  692. qd->qd_qb.qb_value = q.qu_value;
  693. if (fdq) {
  694. if (fdq->d_fieldmask & QC_SPC_SOFT) {
  695. q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
  696. qd->qd_qb.qb_warn = q.qu_warn;
  697. }
  698. if (fdq->d_fieldmask & QC_SPC_HARD) {
  699. q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
  700. qd->qd_qb.qb_limit = q.qu_limit;
  701. }
  702. if (fdq->d_fieldmask & QC_SPACE) {
  703. q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
  704. qd->qd_qb.qb_value = q.qu_value;
  705. }
  706. }
  707. err = gfs2_write_disk_quota(ip, &q, loc);
  708. if (!err) {
  709. size = loc + sizeof(struct gfs2_quota);
  710. if (size > inode->i_size)
  711. i_size_write(inode, size);
  712. inode->i_mtime = inode->i_atime = current_time(inode);
  713. mark_inode_dirty(inode);
  714. set_bit(QDF_REFRESH, &qd->qd_flags);
  715. }
  716. return err;
  717. }
  718. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  719. {
  720. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_name.ln_sbd;
  721. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  722. struct gfs2_alloc_parms ap = { .aflags = 0, };
  723. unsigned int data_blocks, ind_blocks;
  724. struct gfs2_holder *ghs, i_gh;
  725. unsigned int qx, x;
  726. struct gfs2_quota_data *qd;
  727. unsigned reserved;
  728. loff_t offset;
  729. unsigned int nalloc = 0, blocks;
  730. int error;
  731. error = gfs2_rsqa_alloc(ip);
  732. if (error)
  733. return error;
  734. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  735. &data_blocks, &ind_blocks);
  736. ghs = kmalloc(num_qd * sizeof(struct gfs2_holder), GFP_NOFS);
  737. if (!ghs)
  738. return -ENOMEM;
  739. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  740. inode_lock(&ip->i_inode);
  741. for (qx = 0; qx < num_qd; qx++) {
  742. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  743. GL_NOCACHE, &ghs[qx]);
  744. if (error)
  745. goto out;
  746. }
  747. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  748. if (error)
  749. goto out;
  750. for (x = 0; x < num_qd; x++) {
  751. offset = qd2offset(qda[x]);
  752. if (gfs2_write_alloc_required(ip, offset,
  753. sizeof(struct gfs2_quota)))
  754. nalloc++;
  755. }
  756. /*
  757. * 1 blk for unstuffing inode if stuffed. We add this extra
  758. * block to the reservation unconditionally. If the inode
  759. * doesn't need unstuffing, the block will be released to the
  760. * rgrp since it won't be allocated during the transaction
  761. */
  762. /* +3 in the end for unstuffing block, inode size update block
  763. * and another block in case quota straddles page boundary and
  764. * two blocks need to be updated instead of 1 */
  765. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  766. reserved = 1 + (nalloc * (data_blocks + ind_blocks));
  767. ap.target = reserved;
  768. error = gfs2_inplace_reserve(ip, &ap);
  769. if (error)
  770. goto out_alloc;
  771. if (nalloc)
  772. blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
  773. error = gfs2_trans_begin(sdp, blocks, 0);
  774. if (error)
  775. goto out_ipres;
  776. for (x = 0; x < num_qd; x++) {
  777. qd = qda[x];
  778. offset = qd2offset(qd);
  779. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
  780. if (error)
  781. goto out_end_trans;
  782. do_qc(qd, -qd->qd_change_sync);
  783. set_bit(QDF_REFRESH, &qd->qd_flags);
  784. }
  785. error = 0;
  786. out_end_trans:
  787. gfs2_trans_end(sdp);
  788. out_ipres:
  789. gfs2_inplace_release(ip);
  790. out_alloc:
  791. gfs2_glock_dq_uninit(&i_gh);
  792. out:
  793. while (qx--)
  794. gfs2_glock_dq_uninit(&ghs[qx]);
  795. inode_unlock(&ip->i_inode);
  796. kfree(ghs);
  797. gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl, NORMAL_FLUSH);
  798. return error;
  799. }
  800. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  801. {
  802. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  803. struct gfs2_quota q;
  804. struct gfs2_quota_lvb *qlvb;
  805. loff_t pos;
  806. int error;
  807. memset(&q, 0, sizeof(struct gfs2_quota));
  808. pos = qd2offset(qd);
  809. error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
  810. if (error < 0)
  811. return error;
  812. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  813. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  814. qlvb->__pad = 0;
  815. qlvb->qb_limit = q.qu_limit;
  816. qlvb->qb_warn = q.qu_warn;
  817. qlvb->qb_value = q.qu_value;
  818. qd->qd_qb = *qlvb;
  819. return 0;
  820. }
  821. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  822. struct gfs2_holder *q_gh)
  823. {
  824. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  825. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  826. struct gfs2_holder i_gh;
  827. int error;
  828. restart:
  829. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  830. if (error)
  831. return error;
  832. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  833. force_refresh = FORCE;
  834. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  835. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  836. gfs2_glock_dq_uninit(q_gh);
  837. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  838. GL_NOCACHE, q_gh);
  839. if (error)
  840. return error;
  841. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  842. if (error)
  843. goto fail;
  844. error = update_qd(sdp, qd);
  845. if (error)
  846. goto fail_gunlock;
  847. gfs2_glock_dq_uninit(&i_gh);
  848. gfs2_glock_dq_uninit(q_gh);
  849. force_refresh = 0;
  850. goto restart;
  851. }
  852. return 0;
  853. fail_gunlock:
  854. gfs2_glock_dq_uninit(&i_gh);
  855. fail:
  856. gfs2_glock_dq_uninit(q_gh);
  857. return error;
  858. }
  859. int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  860. {
  861. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  862. struct gfs2_quota_data *qd;
  863. u32 x;
  864. int error = 0;
  865. if (capable(CAP_SYS_RESOURCE) ||
  866. sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  867. return 0;
  868. error = gfs2_quota_hold(ip, uid, gid);
  869. if (error)
  870. return error;
  871. sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
  872. sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  873. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  874. qd = ip->i_qadata->qa_qd[x];
  875. error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
  876. if (error)
  877. break;
  878. }
  879. if (!error)
  880. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  881. else {
  882. while (x--)
  883. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  884. gfs2_quota_unhold(ip);
  885. }
  886. return error;
  887. }
  888. static int need_sync(struct gfs2_quota_data *qd)
  889. {
  890. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  891. struct gfs2_tune *gt = &sdp->sd_tune;
  892. s64 value;
  893. unsigned int num, den;
  894. int do_sync = 1;
  895. if (!qd->qd_qb.qb_limit)
  896. return 0;
  897. spin_lock(&qd_lock);
  898. value = qd->qd_change;
  899. spin_unlock(&qd_lock);
  900. spin_lock(&gt->gt_spin);
  901. num = gt->gt_quota_scale_num;
  902. den = gt->gt_quota_scale_den;
  903. spin_unlock(&gt->gt_spin);
  904. if (value < 0)
  905. do_sync = 0;
  906. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  907. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  908. do_sync = 0;
  909. else {
  910. value *= gfs2_jindex_size(sdp) * num;
  911. value = div_s64(value, den);
  912. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  913. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  914. do_sync = 0;
  915. }
  916. return do_sync;
  917. }
  918. void gfs2_quota_unlock(struct gfs2_inode *ip)
  919. {
  920. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  921. struct gfs2_quota_data *qda[4];
  922. unsigned int count = 0;
  923. u32 x;
  924. int found;
  925. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  926. goto out;
  927. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  928. struct gfs2_quota_data *qd;
  929. int sync;
  930. qd = ip->i_qadata->qa_qd[x];
  931. sync = need_sync(qd);
  932. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  933. if (!sync)
  934. continue;
  935. spin_lock(&qd_lock);
  936. found = qd_check_sync(sdp, qd, NULL);
  937. spin_unlock(&qd_lock);
  938. if (!found)
  939. continue;
  940. gfs2_assert_warn(sdp, qd->qd_change_sync);
  941. if (bh_get(qd)) {
  942. clear_bit(QDF_LOCKED, &qd->qd_flags);
  943. slot_put(qd);
  944. qd_put(qd);
  945. continue;
  946. }
  947. qda[count++] = qd;
  948. }
  949. if (count) {
  950. do_sync(count, qda);
  951. for (x = 0; x < count; x++)
  952. qd_unlock(qda[x]);
  953. }
  954. out:
  955. gfs2_quota_unhold(ip);
  956. }
  957. #define MAX_LINE 256
  958. static int print_message(struct gfs2_quota_data *qd, char *type)
  959. {
  960. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  961. fs_info(sdp, "quota %s for %s %u\n",
  962. type,
  963. (qd->qd_id.type == USRQUOTA) ? "user" : "group",
  964. from_kqid(&init_user_ns, qd->qd_id));
  965. return 0;
  966. }
  967. /**
  968. * gfs2_quota_check - check if allocating new blocks will exceed quota
  969. * @ip: The inode for which this check is being performed
  970. * @uid: The uid to check against
  971. * @gid: The gid to check against
  972. * @ap: The allocation parameters. ap->target contains the requested
  973. * blocks. ap->min_target, if set, contains the minimum blks
  974. * requested.
  975. *
  976. * Returns: 0 on success.
  977. * min_req = ap->min_target ? ap->min_target : ap->target;
  978. * quota must allow atleast min_req blks for success and
  979. * ap->allowed is set to the number of blocks allowed
  980. *
  981. * -EDQUOT otherwise, quota violation. ap->allowed is set to number
  982. * of blocks available.
  983. */
  984. int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
  985. struct gfs2_alloc_parms *ap)
  986. {
  987. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  988. struct gfs2_quota_data *qd;
  989. s64 value, warn, limit;
  990. u32 x;
  991. int error = 0;
  992. ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
  993. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  994. return 0;
  995. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  996. return 0;
  997. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  998. qd = ip->i_qadata->qa_qd[x];
  999. if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1000. qid_eq(qd->qd_id, make_kqid_gid(gid))))
  1001. continue;
  1002. warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
  1003. limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
  1004. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  1005. spin_lock(&qd_lock);
  1006. value += qd->qd_change;
  1007. spin_unlock(&qd_lock);
  1008. if (limit > 0 && (limit - value) < ap->allowed)
  1009. ap->allowed = limit - value;
  1010. /* If we can't meet the target */
  1011. if (limit && limit < (value + (s64)ap->target)) {
  1012. /* If no min_target specified or we don't meet
  1013. * min_target, return -EDQUOT */
  1014. if (!ap->min_target || ap->min_target > ap->allowed) {
  1015. if (!test_and_set_bit(QDF_QMSG_QUIET,
  1016. &qd->qd_flags)) {
  1017. print_message(qd, "exceeded");
  1018. quota_send_warning(qd->qd_id,
  1019. sdp->sd_vfs->s_dev,
  1020. QUOTA_NL_BHARDWARN);
  1021. }
  1022. error = -EDQUOT;
  1023. break;
  1024. }
  1025. } else if (warn && warn < value &&
  1026. time_after_eq(jiffies, qd->qd_last_warn +
  1027. gfs2_tune_get(sdp, gt_quota_warn_period)
  1028. * HZ)) {
  1029. quota_send_warning(qd->qd_id,
  1030. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  1031. error = print_message(qd, "warning");
  1032. qd->qd_last_warn = jiffies;
  1033. }
  1034. }
  1035. return error;
  1036. }
  1037. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  1038. kuid_t uid, kgid_t gid)
  1039. {
  1040. struct gfs2_quota_data *qd;
  1041. u32 x;
  1042. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1043. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON ||
  1044. gfs2_assert_warn(sdp, change))
  1045. return;
  1046. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  1047. return;
  1048. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  1049. qd = ip->i_qadata->qa_qd[x];
  1050. if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1051. qid_eq(qd->qd_id, make_kqid_gid(gid))) {
  1052. do_qc(qd, change);
  1053. }
  1054. }
  1055. }
  1056. int gfs2_quota_sync(struct super_block *sb, int type)
  1057. {
  1058. struct gfs2_sbd *sdp = sb->s_fs_info;
  1059. struct gfs2_quota_data **qda;
  1060. unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
  1061. unsigned int num_qd;
  1062. unsigned int x;
  1063. int error = 0;
  1064. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  1065. if (!qda)
  1066. return -ENOMEM;
  1067. mutex_lock(&sdp->sd_quota_sync_mutex);
  1068. sdp->sd_quota_sync_gen++;
  1069. do {
  1070. num_qd = 0;
  1071. for (;;) {
  1072. error = qd_fish(sdp, qda + num_qd);
  1073. if (error || !qda[num_qd])
  1074. break;
  1075. if (++num_qd == max_qd)
  1076. break;
  1077. }
  1078. if (num_qd) {
  1079. if (!error)
  1080. error = do_sync(num_qd, qda);
  1081. if (!error)
  1082. for (x = 0; x < num_qd; x++)
  1083. qda[x]->qd_sync_gen =
  1084. sdp->sd_quota_sync_gen;
  1085. for (x = 0; x < num_qd; x++)
  1086. qd_unlock(qda[x]);
  1087. }
  1088. } while (!error && num_qd == max_qd);
  1089. mutex_unlock(&sdp->sd_quota_sync_mutex);
  1090. kfree(qda);
  1091. return error;
  1092. }
  1093. int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
  1094. {
  1095. struct gfs2_quota_data *qd;
  1096. struct gfs2_holder q_gh;
  1097. int error;
  1098. error = qd_get(sdp, qid, &qd);
  1099. if (error)
  1100. return error;
  1101. error = do_glock(qd, FORCE, &q_gh);
  1102. if (!error)
  1103. gfs2_glock_dq_uninit(&q_gh);
  1104. qd_put(qd);
  1105. return error;
  1106. }
  1107. int gfs2_quota_init(struct gfs2_sbd *sdp)
  1108. {
  1109. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  1110. u64 size = i_size_read(sdp->sd_qc_inode);
  1111. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  1112. unsigned int x, slot = 0;
  1113. unsigned int found = 0;
  1114. unsigned int hash;
  1115. unsigned int bm_size;
  1116. u64 dblock;
  1117. u32 extlen = 0;
  1118. int error;
  1119. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  1120. return -EIO;
  1121. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  1122. bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
  1123. bm_size *= sizeof(unsigned long);
  1124. error = -ENOMEM;
  1125. sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
  1126. if (sdp->sd_quota_bitmap == NULL)
  1127. sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
  1128. __GFP_ZERO, PAGE_KERNEL);
  1129. if (!sdp->sd_quota_bitmap)
  1130. return error;
  1131. for (x = 0; x < blocks; x++) {
  1132. struct buffer_head *bh;
  1133. const struct gfs2_quota_change *qc;
  1134. unsigned int y;
  1135. if (!extlen) {
  1136. int new = 0;
  1137. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1138. if (error)
  1139. goto fail;
  1140. }
  1141. error = -EIO;
  1142. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1143. if (!bh)
  1144. goto fail;
  1145. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1146. brelse(bh);
  1147. goto fail;
  1148. }
  1149. qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
  1150. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1151. y++, slot++) {
  1152. struct gfs2_quota_data *qd;
  1153. s64 qc_change = be64_to_cpu(qc->qc_change);
  1154. u32 qc_flags = be32_to_cpu(qc->qc_flags);
  1155. enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
  1156. USRQUOTA : GRPQUOTA;
  1157. struct kqid qc_id = make_kqid(&init_user_ns, qtype,
  1158. be32_to_cpu(qc->qc_id));
  1159. qc++;
  1160. if (!qc_change)
  1161. continue;
  1162. hash = gfs2_qd_hash(sdp, qc_id);
  1163. qd = qd_alloc(hash, sdp, qc_id);
  1164. if (qd == NULL) {
  1165. brelse(bh);
  1166. goto fail;
  1167. }
  1168. set_bit(QDF_CHANGE, &qd->qd_flags);
  1169. qd->qd_change = qc_change;
  1170. qd->qd_slot = slot;
  1171. qd->qd_slot_count = 1;
  1172. spin_lock(&qd_lock);
  1173. BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
  1174. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1175. atomic_inc(&sdp->sd_quota_count);
  1176. spin_unlock(&qd_lock);
  1177. spin_lock_bucket(hash);
  1178. hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
  1179. spin_unlock_bucket(hash);
  1180. found++;
  1181. }
  1182. brelse(bh);
  1183. dblock++;
  1184. extlen--;
  1185. }
  1186. if (found)
  1187. fs_info(sdp, "found %u quota changes\n", found);
  1188. return 0;
  1189. fail:
  1190. gfs2_quota_cleanup(sdp);
  1191. return error;
  1192. }
  1193. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1194. {
  1195. struct list_head *head = &sdp->sd_quota_list;
  1196. struct gfs2_quota_data *qd;
  1197. spin_lock(&qd_lock);
  1198. while (!list_empty(head)) {
  1199. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1200. list_del(&qd->qd_list);
  1201. /* Also remove if this qd exists in the reclaim list */
  1202. list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
  1203. atomic_dec(&sdp->sd_quota_count);
  1204. spin_unlock(&qd_lock);
  1205. spin_lock_bucket(qd->qd_hash);
  1206. hlist_bl_del_rcu(&qd->qd_hlist);
  1207. spin_unlock_bucket(qd->qd_hash);
  1208. gfs2_assert_warn(sdp, !qd->qd_change);
  1209. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1210. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1211. gfs2_glock_put(qd->qd_gl);
  1212. call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
  1213. spin_lock(&qd_lock);
  1214. }
  1215. spin_unlock(&qd_lock);
  1216. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1217. kvfree(sdp->sd_quota_bitmap);
  1218. sdp->sd_quota_bitmap = NULL;
  1219. }
  1220. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1221. {
  1222. if (error == 0 || error == -EROFS)
  1223. return;
  1224. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  1225. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1226. }
  1227. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1228. int (*fxn)(struct super_block *sb, int type),
  1229. unsigned long t, unsigned long *timeo,
  1230. unsigned int *new_timeo)
  1231. {
  1232. if (t >= *timeo) {
  1233. int error = fxn(sdp->sd_vfs, 0);
  1234. quotad_error(sdp, msg, error);
  1235. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1236. } else {
  1237. *timeo -= t;
  1238. }
  1239. }
  1240. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1241. {
  1242. struct gfs2_inode *ip;
  1243. while(1) {
  1244. ip = NULL;
  1245. spin_lock(&sdp->sd_trunc_lock);
  1246. if (!list_empty(&sdp->sd_trunc_list)) {
  1247. ip = list_entry(sdp->sd_trunc_list.next,
  1248. struct gfs2_inode, i_trunc_list);
  1249. list_del_init(&ip->i_trunc_list);
  1250. }
  1251. spin_unlock(&sdp->sd_trunc_lock);
  1252. if (ip == NULL)
  1253. return;
  1254. gfs2_glock_finish_truncate(ip);
  1255. }
  1256. }
  1257. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1258. if (!sdp->sd_statfs_force_sync) {
  1259. sdp->sd_statfs_force_sync = 1;
  1260. wake_up(&sdp->sd_quota_wait);
  1261. }
  1262. }
  1263. /**
  1264. * gfs2_quotad - Write cached quota changes into the quota file
  1265. * @sdp: Pointer to GFS2 superblock
  1266. *
  1267. */
  1268. int gfs2_quotad(void *data)
  1269. {
  1270. struct gfs2_sbd *sdp = data;
  1271. struct gfs2_tune *tune = &sdp->sd_tune;
  1272. unsigned long statfs_timeo = 0;
  1273. unsigned long quotad_timeo = 0;
  1274. unsigned long t = 0;
  1275. DEFINE_WAIT(wait);
  1276. int empty;
  1277. while (!kthread_should_stop()) {
  1278. /* Update the master statfs file */
  1279. if (sdp->sd_statfs_force_sync) {
  1280. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1281. quotad_error(sdp, "statfs", error);
  1282. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1283. }
  1284. else
  1285. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1286. &statfs_timeo,
  1287. &tune->gt_statfs_quantum);
  1288. /* Update quota file */
  1289. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1290. &quotad_timeo, &tune->gt_quota_quantum);
  1291. /* Check for & recover partially truncated inodes */
  1292. quotad_check_trunc_list(sdp);
  1293. try_to_freeze();
  1294. t = min(quotad_timeo, statfs_timeo);
  1295. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1296. spin_lock(&sdp->sd_trunc_lock);
  1297. empty = list_empty(&sdp->sd_trunc_list);
  1298. spin_unlock(&sdp->sd_trunc_lock);
  1299. if (empty && !sdp->sd_statfs_force_sync)
  1300. t -= schedule_timeout(t);
  1301. else
  1302. t = 0;
  1303. finish_wait(&sdp->sd_quota_wait, &wait);
  1304. }
  1305. return 0;
  1306. }
  1307. static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
  1308. {
  1309. struct gfs2_sbd *sdp = sb->s_fs_info;
  1310. memset(state, 0, sizeof(*state));
  1311. switch (sdp->sd_args.ar_quota) {
  1312. case GFS2_QUOTA_ON:
  1313. state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1314. state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1315. /*FALLTHRU*/
  1316. case GFS2_QUOTA_ACCOUNT:
  1317. state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
  1318. QCI_SYSFILE;
  1319. state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
  1320. QCI_SYSFILE;
  1321. break;
  1322. case GFS2_QUOTA_OFF:
  1323. break;
  1324. }
  1325. if (sdp->sd_quota_inode) {
  1326. state->s_state[USRQUOTA].ino =
  1327. GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1328. state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
  1329. }
  1330. state->s_state[USRQUOTA].nextents = 1; /* unsupported */
  1331. state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
  1332. state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
  1333. return 0;
  1334. }
  1335. static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
  1336. struct qc_dqblk *fdq)
  1337. {
  1338. struct gfs2_sbd *sdp = sb->s_fs_info;
  1339. struct gfs2_quota_lvb *qlvb;
  1340. struct gfs2_quota_data *qd;
  1341. struct gfs2_holder q_gh;
  1342. int error;
  1343. memset(fdq, 0, sizeof(*fdq));
  1344. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1345. return -ESRCH; /* Crazy XFS error code */
  1346. if ((qid.type != USRQUOTA) &&
  1347. (qid.type != GRPQUOTA))
  1348. return -EINVAL;
  1349. error = qd_get(sdp, qid, &qd);
  1350. if (error)
  1351. return error;
  1352. error = do_glock(qd, FORCE, &q_gh);
  1353. if (error)
  1354. goto out;
  1355. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  1356. fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
  1357. fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
  1358. fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
  1359. gfs2_glock_dq_uninit(&q_gh);
  1360. out:
  1361. qd_put(qd);
  1362. return error;
  1363. }
  1364. /* GFS2 only supports a subset of the XFS fields */
  1365. #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
  1366. static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
  1367. struct qc_dqblk *fdq)
  1368. {
  1369. struct gfs2_sbd *sdp = sb->s_fs_info;
  1370. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1371. struct gfs2_quota_data *qd;
  1372. struct gfs2_holder q_gh, i_gh;
  1373. unsigned int data_blocks, ind_blocks;
  1374. unsigned int blocks = 0;
  1375. int alloc_required;
  1376. loff_t offset;
  1377. int error;
  1378. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1379. return -ESRCH; /* Crazy XFS error code */
  1380. if ((qid.type != USRQUOTA) &&
  1381. (qid.type != GRPQUOTA))
  1382. return -EINVAL;
  1383. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1384. return -EINVAL;
  1385. error = qd_get(sdp, qid, &qd);
  1386. if (error)
  1387. return error;
  1388. error = gfs2_rsqa_alloc(ip);
  1389. if (error)
  1390. goto out_put;
  1391. inode_lock(&ip->i_inode);
  1392. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1393. if (error)
  1394. goto out_unlockput;
  1395. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1396. if (error)
  1397. goto out_q;
  1398. /* Check for existing entry, if none then alloc new blocks */
  1399. error = update_qd(sdp, qd);
  1400. if (error)
  1401. goto out_i;
  1402. /* If nothing has changed, this is a no-op */
  1403. if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
  1404. ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1405. fdq->d_fieldmask ^= QC_SPC_SOFT;
  1406. if ((fdq->d_fieldmask & QC_SPC_HARD) &&
  1407. ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1408. fdq->d_fieldmask ^= QC_SPC_HARD;
  1409. if ((fdq->d_fieldmask & QC_SPACE) &&
  1410. ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1411. fdq->d_fieldmask ^= QC_SPACE;
  1412. if (fdq->d_fieldmask == 0)
  1413. goto out_i;
  1414. offset = qd2offset(qd);
  1415. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1416. if (gfs2_is_stuffed(ip))
  1417. alloc_required = 1;
  1418. if (alloc_required) {
  1419. struct gfs2_alloc_parms ap = { .aflags = 0, };
  1420. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1421. &data_blocks, &ind_blocks);
  1422. blocks = 1 + data_blocks + ind_blocks;
  1423. ap.target = blocks;
  1424. error = gfs2_inplace_reserve(ip, &ap);
  1425. if (error)
  1426. goto out_i;
  1427. blocks += gfs2_rg_blocks(ip, blocks);
  1428. }
  1429. /* Some quotas span block boundaries and can update two blocks,
  1430. adding an extra block to the transaction to handle such quotas */
  1431. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1432. if (error)
  1433. goto out_release;
  1434. /* Apply changes */
  1435. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1436. if (!error)
  1437. clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
  1438. gfs2_trans_end(sdp);
  1439. out_release:
  1440. if (alloc_required)
  1441. gfs2_inplace_release(ip);
  1442. out_i:
  1443. gfs2_glock_dq_uninit(&i_gh);
  1444. out_q:
  1445. gfs2_glock_dq_uninit(&q_gh);
  1446. out_unlockput:
  1447. inode_unlock(&ip->i_inode);
  1448. out_put:
  1449. qd_put(qd);
  1450. return error;
  1451. }
  1452. const struct quotactl_ops gfs2_quotactl_ops = {
  1453. .quota_sync = gfs2_quota_sync,
  1454. .get_state = gfs2_quota_get_state,
  1455. .get_dqblk = gfs2_get_dqblk,
  1456. .set_dqblk = gfs2_set_dqblk,
  1457. };
  1458. void __init gfs2_quota_hash_init(void)
  1459. {
  1460. unsigned i;
  1461. for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
  1462. INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
  1463. }