quota.c 38 KB

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