quota.c 38 KB

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