quota.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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 (sb_rdonly(sdp->sd_vfs))
  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 | REQ_PRIO, 1, &bh);
  604. wait_on_buffer(bh);
  605. if (!buffer_uptodate(bh))
  606. goto unlock_out;
  607. }
  608. if (gfs2_is_jdata(ip))
  609. gfs2_trans_add_data(ip->i_gl, bh);
  610. else
  611. gfs2_ordered_add_inode(ip);
  612. /* If we need to write to the next block as well */
  613. if (to_write > (bsize - boff)) {
  614. pg_off += (bsize - boff);
  615. to_write -= (bsize - boff);
  616. boff = pg_off % bsize;
  617. continue;
  618. }
  619. done = 1;
  620. }
  621. /* Write to the page, now that we have setup the buffer(s) */
  622. kaddr = kmap_atomic(page);
  623. memcpy(kaddr + off, buf, bytes);
  624. flush_dcache_page(page);
  625. kunmap_atomic(kaddr);
  626. unlock_page(page);
  627. put_page(page);
  628. return 0;
  629. unlock_out:
  630. unlock_page(page);
  631. put_page(page);
  632. return -EIO;
  633. }
  634. static int gfs2_write_disk_quota(struct gfs2_inode *ip, struct gfs2_quota *qp,
  635. loff_t loc)
  636. {
  637. unsigned long pg_beg;
  638. unsigned pg_off, nbytes, overflow = 0;
  639. int pg_oflow = 0, error;
  640. void *ptr;
  641. nbytes = sizeof(struct gfs2_quota);
  642. pg_beg = loc >> PAGE_SHIFT;
  643. pg_off = loc % PAGE_SIZE;
  644. /* If the quota straddles a page boundary, split the write in two */
  645. if ((pg_off + nbytes) > PAGE_SIZE) {
  646. pg_oflow = 1;
  647. overflow = (pg_off + nbytes) - PAGE_SIZE;
  648. }
  649. ptr = qp;
  650. error = gfs2_write_buf_to_page(ip, pg_beg, pg_off, ptr,
  651. nbytes - overflow);
  652. /* If there's an overflow, write the remaining bytes to the next page */
  653. if (!error && pg_oflow)
  654. error = gfs2_write_buf_to_page(ip, pg_beg + 1, 0,
  655. ptr + nbytes - overflow,
  656. overflow);
  657. return error;
  658. }
  659. /**
  660. * gfs2_adjust_quota - adjust record of current block usage
  661. * @ip: The quota inode
  662. * @loc: Offset of the entry in the quota file
  663. * @change: The amount of usage change to record
  664. * @qd: The quota data
  665. * @fdq: The updated limits to record
  666. *
  667. * This function was mostly borrowed from gfs2_block_truncate_page which was
  668. * in turn mostly borrowed from ext3
  669. *
  670. * Returns: 0 or -ve on error
  671. */
  672. static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
  673. s64 change, struct gfs2_quota_data *qd,
  674. struct qc_dqblk *fdq)
  675. {
  676. struct inode *inode = &ip->i_inode;
  677. struct gfs2_sbd *sdp = GFS2_SB(inode);
  678. struct gfs2_quota q;
  679. int err;
  680. u64 size;
  681. if (gfs2_is_stuffed(ip)) {
  682. err = gfs2_unstuff_dinode(ip, NULL);
  683. if (err)
  684. return err;
  685. }
  686. memset(&q, 0, sizeof(struct gfs2_quota));
  687. err = gfs2_internal_read(ip, (char *)&q, &loc, sizeof(q));
  688. if (err < 0)
  689. return err;
  690. loc -= sizeof(q); /* gfs2_internal_read would've advanced the loc ptr */
  691. err = -EIO;
  692. be64_add_cpu(&q.qu_value, change);
  693. if (((s64)be64_to_cpu(q.qu_value)) < 0)
  694. q.qu_value = 0; /* Never go negative on quota usage */
  695. qd->qd_qb.qb_value = q.qu_value;
  696. if (fdq) {
  697. if (fdq->d_fieldmask & QC_SPC_SOFT) {
  698. q.qu_warn = cpu_to_be64(fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift);
  699. qd->qd_qb.qb_warn = q.qu_warn;
  700. }
  701. if (fdq->d_fieldmask & QC_SPC_HARD) {
  702. q.qu_limit = cpu_to_be64(fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift);
  703. qd->qd_qb.qb_limit = q.qu_limit;
  704. }
  705. if (fdq->d_fieldmask & QC_SPACE) {
  706. q.qu_value = cpu_to_be64(fdq->d_space >> sdp->sd_sb.sb_bsize_shift);
  707. qd->qd_qb.qb_value = q.qu_value;
  708. }
  709. }
  710. err = gfs2_write_disk_quota(ip, &q, loc);
  711. if (!err) {
  712. size = loc + sizeof(struct gfs2_quota);
  713. if (size > inode->i_size)
  714. i_size_write(inode, size);
  715. inode->i_mtime = inode->i_atime = current_time(inode);
  716. mark_inode_dirty(inode);
  717. set_bit(QDF_REFRESH, &qd->qd_flags);
  718. }
  719. return err;
  720. }
  721. static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
  722. {
  723. struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_name.ln_sbd;
  724. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  725. struct gfs2_alloc_parms ap = { .aflags = 0, };
  726. unsigned int data_blocks, ind_blocks;
  727. struct gfs2_holder *ghs, i_gh;
  728. unsigned int qx, x;
  729. struct gfs2_quota_data *qd;
  730. unsigned reserved;
  731. loff_t offset;
  732. unsigned int nalloc = 0, blocks;
  733. int error;
  734. error = gfs2_rsqa_alloc(ip);
  735. if (error)
  736. return error;
  737. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  738. &data_blocks, &ind_blocks);
  739. ghs = kmalloc_array(num_qd, sizeof(struct gfs2_holder), GFP_NOFS);
  740. if (!ghs)
  741. return -ENOMEM;
  742. sort(qda, num_qd, sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  743. inode_lock(&ip->i_inode);
  744. for (qx = 0; qx < num_qd; qx++) {
  745. error = gfs2_glock_nq_init(qda[qx]->qd_gl, LM_ST_EXCLUSIVE,
  746. GL_NOCACHE, &ghs[qx]);
  747. if (error)
  748. goto out;
  749. }
  750. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  751. if (error)
  752. goto out;
  753. for (x = 0; x < num_qd; x++) {
  754. offset = qd2offset(qda[x]);
  755. if (gfs2_write_alloc_required(ip, offset,
  756. sizeof(struct gfs2_quota)))
  757. nalloc++;
  758. }
  759. /*
  760. * 1 blk for unstuffing inode if stuffed. We add this extra
  761. * block to the reservation unconditionally. If the inode
  762. * doesn't need unstuffing, the block will be released to the
  763. * rgrp since it won't be allocated during the transaction
  764. */
  765. /* +3 in the end for unstuffing block, inode size update block
  766. * and another block in case quota straddles page boundary and
  767. * two blocks need to be updated instead of 1 */
  768. blocks = num_qd * data_blocks + RES_DINODE + num_qd + 3;
  769. reserved = 1 + (nalloc * (data_blocks + ind_blocks));
  770. ap.target = reserved;
  771. error = gfs2_inplace_reserve(ip, &ap);
  772. if (error)
  773. goto out_alloc;
  774. if (nalloc)
  775. blocks += gfs2_rg_blocks(ip, reserved) + nalloc * ind_blocks + RES_STATFS;
  776. error = gfs2_trans_begin(sdp, blocks, 0);
  777. if (error)
  778. goto out_ipres;
  779. for (x = 0; x < num_qd; x++) {
  780. qd = qda[x];
  781. offset = qd2offset(qd);
  782. error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync, qd, NULL);
  783. if (error)
  784. goto out_end_trans;
  785. do_qc(qd, -qd->qd_change_sync);
  786. set_bit(QDF_REFRESH, &qd->qd_flags);
  787. }
  788. error = 0;
  789. out_end_trans:
  790. gfs2_trans_end(sdp);
  791. out_ipres:
  792. gfs2_inplace_release(ip);
  793. out_alloc:
  794. gfs2_glock_dq_uninit(&i_gh);
  795. out:
  796. while (qx--)
  797. gfs2_glock_dq_uninit(&ghs[qx]);
  798. inode_unlock(&ip->i_inode);
  799. kfree(ghs);
  800. gfs2_log_flush(ip->i_gl->gl_name.ln_sbd, ip->i_gl,
  801. GFS2_LOG_HEAD_FLUSH_NORMAL | GFS2_LFC_DO_SYNC);
  802. return error;
  803. }
  804. static int update_qd(struct gfs2_sbd *sdp, struct gfs2_quota_data *qd)
  805. {
  806. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  807. struct gfs2_quota q;
  808. struct gfs2_quota_lvb *qlvb;
  809. loff_t pos;
  810. int error;
  811. memset(&q, 0, sizeof(struct gfs2_quota));
  812. pos = qd2offset(qd);
  813. error = gfs2_internal_read(ip, (char *)&q, &pos, sizeof(q));
  814. if (error < 0)
  815. return error;
  816. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  817. qlvb->qb_magic = cpu_to_be32(GFS2_MAGIC);
  818. qlvb->__pad = 0;
  819. qlvb->qb_limit = q.qu_limit;
  820. qlvb->qb_warn = q.qu_warn;
  821. qlvb->qb_value = q.qu_value;
  822. qd->qd_qb = *qlvb;
  823. return 0;
  824. }
  825. static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
  826. struct gfs2_holder *q_gh)
  827. {
  828. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  829. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  830. struct gfs2_holder i_gh;
  831. int error;
  832. restart:
  833. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
  834. if (error)
  835. return error;
  836. if (test_and_clear_bit(QDF_REFRESH, &qd->qd_flags))
  837. force_refresh = FORCE;
  838. qd->qd_qb = *(struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  839. if (force_refresh || qd->qd_qb.qb_magic != cpu_to_be32(GFS2_MAGIC)) {
  840. gfs2_glock_dq_uninit(q_gh);
  841. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE,
  842. GL_NOCACHE, q_gh);
  843. if (error)
  844. return error;
  845. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  846. if (error)
  847. goto fail;
  848. error = update_qd(sdp, qd);
  849. if (error)
  850. goto fail_gunlock;
  851. gfs2_glock_dq_uninit(&i_gh);
  852. gfs2_glock_dq_uninit(q_gh);
  853. force_refresh = 0;
  854. goto restart;
  855. }
  856. return 0;
  857. fail_gunlock:
  858. gfs2_glock_dq_uninit(&i_gh);
  859. fail:
  860. gfs2_glock_dq_uninit(q_gh);
  861. return error;
  862. }
  863. int gfs2_quota_lock(struct gfs2_inode *ip, kuid_t uid, kgid_t gid)
  864. {
  865. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  866. struct gfs2_quota_data *qd;
  867. u32 x;
  868. int error = 0;
  869. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  870. return 0;
  871. error = gfs2_quota_hold(ip, uid, gid);
  872. if (error)
  873. return error;
  874. sort(ip->i_qadata->qa_qd, ip->i_qadata->qa_qd_num,
  875. sizeof(struct gfs2_quota_data *), sort_qd, NULL);
  876. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  877. qd = ip->i_qadata->qa_qd[x];
  878. error = do_glock(qd, NO_FORCE, &ip->i_qadata->qa_qd_ghs[x]);
  879. if (error)
  880. break;
  881. }
  882. if (!error)
  883. set_bit(GIF_QD_LOCKED, &ip->i_flags);
  884. else {
  885. while (x--)
  886. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  887. gfs2_quota_unhold(ip);
  888. }
  889. return error;
  890. }
  891. static int need_sync(struct gfs2_quota_data *qd)
  892. {
  893. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  894. struct gfs2_tune *gt = &sdp->sd_tune;
  895. s64 value;
  896. unsigned int num, den;
  897. int do_sync = 1;
  898. if (!qd->qd_qb.qb_limit)
  899. return 0;
  900. spin_lock(&qd_lock);
  901. value = qd->qd_change;
  902. spin_unlock(&qd_lock);
  903. spin_lock(&gt->gt_spin);
  904. num = gt->gt_quota_scale_num;
  905. den = gt->gt_quota_scale_den;
  906. spin_unlock(&gt->gt_spin);
  907. if (value < 0)
  908. do_sync = 0;
  909. else if ((s64)be64_to_cpu(qd->qd_qb.qb_value) >=
  910. (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  911. do_sync = 0;
  912. else {
  913. value *= gfs2_jindex_size(sdp) * num;
  914. value = div_s64(value, den);
  915. value += (s64)be64_to_cpu(qd->qd_qb.qb_value);
  916. if (value < (s64)be64_to_cpu(qd->qd_qb.qb_limit))
  917. do_sync = 0;
  918. }
  919. return do_sync;
  920. }
  921. void gfs2_quota_unlock(struct gfs2_inode *ip)
  922. {
  923. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  924. struct gfs2_quota_data *qda[4];
  925. unsigned int count = 0;
  926. u32 x;
  927. int found;
  928. if (!test_and_clear_bit(GIF_QD_LOCKED, &ip->i_flags))
  929. goto out;
  930. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  931. struct gfs2_quota_data *qd;
  932. int sync;
  933. qd = ip->i_qadata->qa_qd[x];
  934. sync = need_sync(qd);
  935. gfs2_glock_dq_uninit(&ip->i_qadata->qa_qd_ghs[x]);
  936. if (!sync)
  937. continue;
  938. spin_lock(&qd_lock);
  939. found = qd_check_sync(sdp, qd, NULL);
  940. spin_unlock(&qd_lock);
  941. if (!found)
  942. continue;
  943. gfs2_assert_warn(sdp, qd->qd_change_sync);
  944. if (bh_get(qd)) {
  945. clear_bit(QDF_LOCKED, &qd->qd_flags);
  946. slot_put(qd);
  947. qd_put(qd);
  948. continue;
  949. }
  950. qda[count++] = qd;
  951. }
  952. if (count) {
  953. do_sync(count, qda);
  954. for (x = 0; x < count; x++)
  955. qd_unlock(qda[x]);
  956. }
  957. out:
  958. gfs2_quota_unhold(ip);
  959. }
  960. #define MAX_LINE 256
  961. static int print_message(struct gfs2_quota_data *qd, char *type)
  962. {
  963. struct gfs2_sbd *sdp = qd->qd_gl->gl_name.ln_sbd;
  964. fs_info(sdp, "quota %s for %s %u\n",
  965. type,
  966. (qd->qd_id.type == USRQUOTA) ? "user" : "group",
  967. from_kqid(&init_user_ns, qd->qd_id));
  968. return 0;
  969. }
  970. /**
  971. * gfs2_quota_check - check if allocating new blocks will exceed quota
  972. * @ip: The inode for which this check is being performed
  973. * @uid: The uid to check against
  974. * @gid: The gid to check against
  975. * @ap: The allocation parameters. ap->target contains the requested
  976. * blocks. ap->min_target, if set, contains the minimum blks
  977. * requested.
  978. *
  979. * Returns: 0 on success.
  980. * min_req = ap->min_target ? ap->min_target : ap->target;
  981. * quota must allow atleast min_req blks for success and
  982. * ap->allowed is set to the number of blocks allowed
  983. *
  984. * -EDQUOT otherwise, quota violation. ap->allowed is set to number
  985. * of blocks available.
  986. */
  987. int gfs2_quota_check(struct gfs2_inode *ip, kuid_t uid, kgid_t gid,
  988. struct gfs2_alloc_parms *ap)
  989. {
  990. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  991. struct gfs2_quota_data *qd;
  992. s64 value, warn, limit;
  993. u32 x;
  994. int error = 0;
  995. ap->allowed = UINT_MAX; /* Assume we are permitted a whole lot */
  996. if (!test_bit(GIF_QD_LOCKED, &ip->i_flags))
  997. return 0;
  998. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON)
  999. return 0;
  1000. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  1001. qd = ip->i_qadata->qa_qd[x];
  1002. if (!(qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1003. qid_eq(qd->qd_id, make_kqid_gid(gid))))
  1004. continue;
  1005. warn = (s64)be64_to_cpu(qd->qd_qb.qb_warn);
  1006. limit = (s64)be64_to_cpu(qd->qd_qb.qb_limit);
  1007. value = (s64)be64_to_cpu(qd->qd_qb.qb_value);
  1008. spin_lock(&qd_lock);
  1009. value += qd->qd_change;
  1010. spin_unlock(&qd_lock);
  1011. if (limit > 0 && (limit - value) < ap->allowed)
  1012. ap->allowed = limit - value;
  1013. /* If we can't meet the target */
  1014. if (limit && limit < (value + (s64)ap->target)) {
  1015. /* If no min_target specified or we don't meet
  1016. * min_target, return -EDQUOT */
  1017. if (!ap->min_target || ap->min_target > ap->allowed) {
  1018. if (!test_and_set_bit(QDF_QMSG_QUIET,
  1019. &qd->qd_flags)) {
  1020. print_message(qd, "exceeded");
  1021. quota_send_warning(qd->qd_id,
  1022. sdp->sd_vfs->s_dev,
  1023. QUOTA_NL_BHARDWARN);
  1024. }
  1025. error = -EDQUOT;
  1026. break;
  1027. }
  1028. } else if (warn && warn < value &&
  1029. time_after_eq(jiffies, qd->qd_last_warn +
  1030. gfs2_tune_get(sdp, gt_quota_warn_period)
  1031. * HZ)) {
  1032. quota_send_warning(qd->qd_id,
  1033. sdp->sd_vfs->s_dev, QUOTA_NL_BSOFTWARN);
  1034. error = print_message(qd, "warning");
  1035. qd->qd_last_warn = jiffies;
  1036. }
  1037. }
  1038. return error;
  1039. }
  1040. void gfs2_quota_change(struct gfs2_inode *ip, s64 change,
  1041. kuid_t uid, kgid_t gid)
  1042. {
  1043. struct gfs2_quota_data *qd;
  1044. u32 x;
  1045. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1046. if (sdp->sd_args.ar_quota != GFS2_QUOTA_ON ||
  1047. gfs2_assert_warn(sdp, change))
  1048. return;
  1049. if (ip->i_diskflags & GFS2_DIF_SYSTEM)
  1050. return;
  1051. for (x = 0; x < ip->i_qadata->qa_qd_num; x++) {
  1052. qd = ip->i_qadata->qa_qd[x];
  1053. if (qid_eq(qd->qd_id, make_kqid_uid(uid)) ||
  1054. qid_eq(qd->qd_id, make_kqid_gid(gid))) {
  1055. do_qc(qd, change);
  1056. }
  1057. }
  1058. }
  1059. int gfs2_quota_sync(struct super_block *sb, int type)
  1060. {
  1061. struct gfs2_sbd *sdp = sb->s_fs_info;
  1062. struct gfs2_quota_data **qda;
  1063. unsigned int max_qd = PAGE_SIZE/sizeof(struct gfs2_holder);
  1064. unsigned int num_qd;
  1065. unsigned int x;
  1066. int error = 0;
  1067. qda = kcalloc(max_qd, sizeof(struct gfs2_quota_data *), GFP_KERNEL);
  1068. if (!qda)
  1069. return -ENOMEM;
  1070. mutex_lock(&sdp->sd_quota_sync_mutex);
  1071. sdp->sd_quota_sync_gen++;
  1072. do {
  1073. num_qd = 0;
  1074. for (;;) {
  1075. error = qd_fish(sdp, qda + num_qd);
  1076. if (error || !qda[num_qd])
  1077. break;
  1078. if (++num_qd == max_qd)
  1079. break;
  1080. }
  1081. if (num_qd) {
  1082. if (!error)
  1083. error = do_sync(num_qd, qda);
  1084. if (!error)
  1085. for (x = 0; x < num_qd; x++)
  1086. qda[x]->qd_sync_gen =
  1087. sdp->sd_quota_sync_gen;
  1088. for (x = 0; x < num_qd; x++)
  1089. qd_unlock(qda[x]);
  1090. }
  1091. } while (!error && num_qd == max_qd);
  1092. mutex_unlock(&sdp->sd_quota_sync_mutex);
  1093. kfree(qda);
  1094. return error;
  1095. }
  1096. int gfs2_quota_refresh(struct gfs2_sbd *sdp, struct kqid qid)
  1097. {
  1098. struct gfs2_quota_data *qd;
  1099. struct gfs2_holder q_gh;
  1100. int error;
  1101. error = qd_get(sdp, qid, &qd);
  1102. if (error)
  1103. return error;
  1104. error = do_glock(qd, FORCE, &q_gh);
  1105. if (!error)
  1106. gfs2_glock_dq_uninit(&q_gh);
  1107. qd_put(qd);
  1108. return error;
  1109. }
  1110. int gfs2_quota_init(struct gfs2_sbd *sdp)
  1111. {
  1112. struct gfs2_inode *ip = GFS2_I(sdp->sd_qc_inode);
  1113. u64 size = i_size_read(sdp->sd_qc_inode);
  1114. unsigned int blocks = size >> sdp->sd_sb.sb_bsize_shift;
  1115. unsigned int x, slot = 0;
  1116. unsigned int found = 0;
  1117. unsigned int hash;
  1118. unsigned int bm_size;
  1119. u64 dblock;
  1120. u32 extlen = 0;
  1121. int error;
  1122. if (gfs2_check_internal_file_size(sdp->sd_qc_inode, 1, 64 << 20))
  1123. return -EIO;
  1124. sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
  1125. bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
  1126. bm_size *= sizeof(unsigned long);
  1127. error = -ENOMEM;
  1128. sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
  1129. if (sdp->sd_quota_bitmap == NULL)
  1130. sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
  1131. __GFP_ZERO, PAGE_KERNEL);
  1132. if (!sdp->sd_quota_bitmap)
  1133. return error;
  1134. for (x = 0; x < blocks; x++) {
  1135. struct buffer_head *bh;
  1136. const struct gfs2_quota_change *qc;
  1137. unsigned int y;
  1138. if (!extlen) {
  1139. int new = 0;
  1140. error = gfs2_extent_map(&ip->i_inode, x, &new, &dblock, &extlen);
  1141. if (error)
  1142. goto fail;
  1143. }
  1144. error = -EIO;
  1145. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  1146. if (!bh)
  1147. goto fail;
  1148. if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_QC)) {
  1149. brelse(bh);
  1150. goto fail;
  1151. }
  1152. qc = (const struct gfs2_quota_change *)(bh->b_data + sizeof(struct gfs2_meta_header));
  1153. for (y = 0; y < sdp->sd_qc_per_block && slot < sdp->sd_quota_slots;
  1154. y++, slot++) {
  1155. struct gfs2_quota_data *qd;
  1156. s64 qc_change = be64_to_cpu(qc->qc_change);
  1157. u32 qc_flags = be32_to_cpu(qc->qc_flags);
  1158. enum quota_type qtype = (qc_flags & GFS2_QCF_USER) ?
  1159. USRQUOTA : GRPQUOTA;
  1160. struct kqid qc_id = make_kqid(&init_user_ns, qtype,
  1161. be32_to_cpu(qc->qc_id));
  1162. qc++;
  1163. if (!qc_change)
  1164. continue;
  1165. hash = gfs2_qd_hash(sdp, qc_id);
  1166. qd = qd_alloc(hash, sdp, qc_id);
  1167. if (qd == NULL) {
  1168. brelse(bh);
  1169. goto fail;
  1170. }
  1171. set_bit(QDF_CHANGE, &qd->qd_flags);
  1172. qd->qd_change = qc_change;
  1173. qd->qd_slot = slot;
  1174. qd->qd_slot_count = 1;
  1175. spin_lock(&qd_lock);
  1176. BUG_ON(test_and_set_bit(slot, sdp->sd_quota_bitmap));
  1177. list_add(&qd->qd_list, &sdp->sd_quota_list);
  1178. atomic_inc(&sdp->sd_quota_count);
  1179. spin_unlock(&qd_lock);
  1180. spin_lock_bucket(hash);
  1181. hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
  1182. spin_unlock_bucket(hash);
  1183. found++;
  1184. }
  1185. brelse(bh);
  1186. dblock++;
  1187. extlen--;
  1188. }
  1189. if (found)
  1190. fs_info(sdp, "found %u quota changes\n", found);
  1191. return 0;
  1192. fail:
  1193. gfs2_quota_cleanup(sdp);
  1194. return error;
  1195. }
  1196. void gfs2_quota_cleanup(struct gfs2_sbd *sdp)
  1197. {
  1198. struct list_head *head = &sdp->sd_quota_list;
  1199. struct gfs2_quota_data *qd;
  1200. spin_lock(&qd_lock);
  1201. while (!list_empty(head)) {
  1202. qd = list_entry(head->prev, struct gfs2_quota_data, qd_list);
  1203. list_del(&qd->qd_list);
  1204. /* Also remove if this qd exists in the reclaim list */
  1205. list_lru_del(&gfs2_qd_lru, &qd->qd_lru);
  1206. atomic_dec(&sdp->sd_quota_count);
  1207. spin_unlock(&qd_lock);
  1208. spin_lock_bucket(qd->qd_hash);
  1209. hlist_bl_del_rcu(&qd->qd_hlist);
  1210. spin_unlock_bucket(qd->qd_hash);
  1211. gfs2_assert_warn(sdp, !qd->qd_change);
  1212. gfs2_assert_warn(sdp, !qd->qd_slot_count);
  1213. gfs2_assert_warn(sdp, !qd->qd_bh_count);
  1214. gfs2_glock_put(qd->qd_gl);
  1215. call_rcu(&qd->qd_rcu, gfs2_qd_dealloc);
  1216. spin_lock(&qd_lock);
  1217. }
  1218. spin_unlock(&qd_lock);
  1219. gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_quota_count));
  1220. kvfree(sdp->sd_quota_bitmap);
  1221. sdp->sd_quota_bitmap = NULL;
  1222. }
  1223. static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
  1224. {
  1225. if (error == 0 || error == -EROFS)
  1226. return;
  1227. if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  1228. fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
  1229. sdp->sd_log_error = error;
  1230. wake_up(&sdp->sd_logd_waitq);
  1231. }
  1232. }
  1233. static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
  1234. int (*fxn)(struct super_block *sb, int type),
  1235. unsigned long t, unsigned long *timeo,
  1236. unsigned int *new_timeo)
  1237. {
  1238. if (t >= *timeo) {
  1239. int error = fxn(sdp->sd_vfs, 0);
  1240. quotad_error(sdp, msg, error);
  1241. *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
  1242. } else {
  1243. *timeo -= t;
  1244. }
  1245. }
  1246. static void quotad_check_trunc_list(struct gfs2_sbd *sdp)
  1247. {
  1248. struct gfs2_inode *ip;
  1249. while(1) {
  1250. ip = NULL;
  1251. spin_lock(&sdp->sd_trunc_lock);
  1252. if (!list_empty(&sdp->sd_trunc_list)) {
  1253. ip = list_entry(sdp->sd_trunc_list.next,
  1254. struct gfs2_inode, i_trunc_list);
  1255. list_del_init(&ip->i_trunc_list);
  1256. }
  1257. spin_unlock(&sdp->sd_trunc_lock);
  1258. if (ip == NULL)
  1259. return;
  1260. gfs2_glock_finish_truncate(ip);
  1261. }
  1262. }
  1263. void gfs2_wake_up_statfs(struct gfs2_sbd *sdp) {
  1264. if (!sdp->sd_statfs_force_sync) {
  1265. sdp->sd_statfs_force_sync = 1;
  1266. wake_up(&sdp->sd_quota_wait);
  1267. }
  1268. }
  1269. /**
  1270. * gfs2_quotad - Write cached quota changes into the quota file
  1271. * @sdp: Pointer to GFS2 superblock
  1272. *
  1273. */
  1274. int gfs2_quotad(void *data)
  1275. {
  1276. struct gfs2_sbd *sdp = data;
  1277. struct gfs2_tune *tune = &sdp->sd_tune;
  1278. unsigned long statfs_timeo = 0;
  1279. unsigned long quotad_timeo = 0;
  1280. unsigned long t = 0;
  1281. DEFINE_WAIT(wait);
  1282. int empty;
  1283. while (!kthread_should_stop()) {
  1284. /* Update the master statfs file */
  1285. if (sdp->sd_statfs_force_sync) {
  1286. int error = gfs2_statfs_sync(sdp->sd_vfs, 0);
  1287. quotad_error(sdp, "statfs", error);
  1288. statfs_timeo = gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
  1289. }
  1290. else
  1291. quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
  1292. &statfs_timeo,
  1293. &tune->gt_statfs_quantum);
  1294. /* Update quota file */
  1295. quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
  1296. &quotad_timeo, &tune->gt_quota_quantum);
  1297. /* Check for & recover partially truncated inodes */
  1298. quotad_check_trunc_list(sdp);
  1299. try_to_freeze();
  1300. t = min(quotad_timeo, statfs_timeo);
  1301. prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_INTERRUPTIBLE);
  1302. spin_lock(&sdp->sd_trunc_lock);
  1303. empty = list_empty(&sdp->sd_trunc_list);
  1304. spin_unlock(&sdp->sd_trunc_lock);
  1305. if (empty && !sdp->sd_statfs_force_sync)
  1306. t -= schedule_timeout(t);
  1307. else
  1308. t = 0;
  1309. finish_wait(&sdp->sd_quota_wait, &wait);
  1310. }
  1311. return 0;
  1312. }
  1313. static int gfs2_quota_get_state(struct super_block *sb, struct qc_state *state)
  1314. {
  1315. struct gfs2_sbd *sdp = sb->s_fs_info;
  1316. memset(state, 0, sizeof(*state));
  1317. switch (sdp->sd_args.ar_quota) {
  1318. case GFS2_QUOTA_ON:
  1319. state->s_state[USRQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1320. state->s_state[GRPQUOTA].flags |= QCI_LIMITS_ENFORCED;
  1321. /*FALLTHRU*/
  1322. case GFS2_QUOTA_ACCOUNT:
  1323. state->s_state[USRQUOTA].flags |= QCI_ACCT_ENABLED |
  1324. QCI_SYSFILE;
  1325. state->s_state[GRPQUOTA].flags |= QCI_ACCT_ENABLED |
  1326. QCI_SYSFILE;
  1327. break;
  1328. case GFS2_QUOTA_OFF:
  1329. break;
  1330. }
  1331. if (sdp->sd_quota_inode) {
  1332. state->s_state[USRQUOTA].ino =
  1333. GFS2_I(sdp->sd_quota_inode)->i_no_addr;
  1334. state->s_state[USRQUOTA].blocks = sdp->sd_quota_inode->i_blocks;
  1335. }
  1336. state->s_state[USRQUOTA].nextents = 1; /* unsupported */
  1337. state->s_state[GRPQUOTA] = state->s_state[USRQUOTA];
  1338. state->s_incoredqs = list_lru_count(&gfs2_qd_lru);
  1339. return 0;
  1340. }
  1341. static int gfs2_get_dqblk(struct super_block *sb, struct kqid qid,
  1342. struct qc_dqblk *fdq)
  1343. {
  1344. struct gfs2_sbd *sdp = sb->s_fs_info;
  1345. struct gfs2_quota_lvb *qlvb;
  1346. struct gfs2_quota_data *qd;
  1347. struct gfs2_holder q_gh;
  1348. int error;
  1349. memset(fdq, 0, sizeof(*fdq));
  1350. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1351. return -ESRCH; /* Crazy XFS error code */
  1352. if ((qid.type != USRQUOTA) &&
  1353. (qid.type != GRPQUOTA))
  1354. return -EINVAL;
  1355. error = qd_get(sdp, qid, &qd);
  1356. if (error)
  1357. return error;
  1358. error = do_glock(qd, FORCE, &q_gh);
  1359. if (error)
  1360. goto out;
  1361. qlvb = (struct gfs2_quota_lvb *)qd->qd_gl->gl_lksb.sb_lvbptr;
  1362. fdq->d_spc_hardlimit = be64_to_cpu(qlvb->qb_limit) << sdp->sd_sb.sb_bsize_shift;
  1363. fdq->d_spc_softlimit = be64_to_cpu(qlvb->qb_warn) << sdp->sd_sb.sb_bsize_shift;
  1364. fdq->d_space = be64_to_cpu(qlvb->qb_value) << sdp->sd_sb.sb_bsize_shift;
  1365. gfs2_glock_dq_uninit(&q_gh);
  1366. out:
  1367. qd_put(qd);
  1368. return error;
  1369. }
  1370. /* GFS2 only supports a subset of the XFS fields */
  1371. #define GFS2_FIELDMASK (QC_SPC_SOFT|QC_SPC_HARD|QC_SPACE)
  1372. static int gfs2_set_dqblk(struct super_block *sb, struct kqid qid,
  1373. struct qc_dqblk *fdq)
  1374. {
  1375. struct gfs2_sbd *sdp = sb->s_fs_info;
  1376. struct gfs2_inode *ip = GFS2_I(sdp->sd_quota_inode);
  1377. struct gfs2_quota_data *qd;
  1378. struct gfs2_holder q_gh, i_gh;
  1379. unsigned int data_blocks, ind_blocks;
  1380. unsigned int blocks = 0;
  1381. int alloc_required;
  1382. loff_t offset;
  1383. int error;
  1384. if (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF)
  1385. return -ESRCH; /* Crazy XFS error code */
  1386. if ((qid.type != USRQUOTA) &&
  1387. (qid.type != GRPQUOTA))
  1388. return -EINVAL;
  1389. if (fdq->d_fieldmask & ~GFS2_FIELDMASK)
  1390. return -EINVAL;
  1391. error = qd_get(sdp, qid, &qd);
  1392. if (error)
  1393. return error;
  1394. error = gfs2_rsqa_alloc(ip);
  1395. if (error)
  1396. goto out_put;
  1397. inode_lock(&ip->i_inode);
  1398. error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_EXCLUSIVE, 0, &q_gh);
  1399. if (error)
  1400. goto out_unlockput;
  1401. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1402. if (error)
  1403. goto out_q;
  1404. /* Check for existing entry, if none then alloc new blocks */
  1405. error = update_qd(sdp, qd);
  1406. if (error)
  1407. goto out_i;
  1408. /* If nothing has changed, this is a no-op */
  1409. if ((fdq->d_fieldmask & QC_SPC_SOFT) &&
  1410. ((fdq->d_spc_softlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_warn)))
  1411. fdq->d_fieldmask ^= QC_SPC_SOFT;
  1412. if ((fdq->d_fieldmask & QC_SPC_HARD) &&
  1413. ((fdq->d_spc_hardlimit >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_limit)))
  1414. fdq->d_fieldmask ^= QC_SPC_HARD;
  1415. if ((fdq->d_fieldmask & QC_SPACE) &&
  1416. ((fdq->d_space >> sdp->sd_sb.sb_bsize_shift) == be64_to_cpu(qd->qd_qb.qb_value)))
  1417. fdq->d_fieldmask ^= QC_SPACE;
  1418. if (fdq->d_fieldmask == 0)
  1419. goto out_i;
  1420. offset = qd2offset(qd);
  1421. alloc_required = gfs2_write_alloc_required(ip, offset, sizeof(struct gfs2_quota));
  1422. if (gfs2_is_stuffed(ip))
  1423. alloc_required = 1;
  1424. if (alloc_required) {
  1425. struct gfs2_alloc_parms ap = { .aflags = 0, };
  1426. gfs2_write_calc_reserv(ip, sizeof(struct gfs2_quota),
  1427. &data_blocks, &ind_blocks);
  1428. blocks = 1 + data_blocks + ind_blocks;
  1429. ap.target = blocks;
  1430. error = gfs2_inplace_reserve(ip, &ap);
  1431. if (error)
  1432. goto out_i;
  1433. blocks += gfs2_rg_blocks(ip, blocks);
  1434. }
  1435. /* Some quotas span block boundaries and can update two blocks,
  1436. adding an extra block to the transaction to handle such quotas */
  1437. error = gfs2_trans_begin(sdp, blocks + RES_DINODE + 2, 0);
  1438. if (error)
  1439. goto out_release;
  1440. /* Apply changes */
  1441. error = gfs2_adjust_quota(ip, offset, 0, qd, fdq);
  1442. if (!error)
  1443. clear_bit(QDF_QMSG_QUIET, &qd->qd_flags);
  1444. gfs2_trans_end(sdp);
  1445. out_release:
  1446. if (alloc_required)
  1447. gfs2_inplace_release(ip);
  1448. out_i:
  1449. gfs2_glock_dq_uninit(&i_gh);
  1450. out_q:
  1451. gfs2_glock_dq_uninit(&q_gh);
  1452. out_unlockput:
  1453. inode_unlock(&ip->i_inode);
  1454. out_put:
  1455. qd_put(qd);
  1456. return error;
  1457. }
  1458. const struct quotactl_ops gfs2_quotactl_ops = {
  1459. .quota_sync = gfs2_quota_sync,
  1460. .get_state = gfs2_quota_get_state,
  1461. .get_dqblk = gfs2_get_dqblk,
  1462. .set_dqblk = gfs2_set_dqblk,
  1463. };
  1464. void __init gfs2_quota_hash_init(void)
  1465. {
  1466. unsigned i;
  1467. for(i = 0; i < GFS2_QD_HASH_SIZE; i++)
  1468. INIT_HLIST_BL_HEAD(&qd_hash_table[i]);
  1469. }