quota.c 39 KB

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