PageRenderTime 27ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/gfs2/quota.c

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