PageRenderTime 48ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/quota.c

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