quota.c 42 KB

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