PageRenderTime 68ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/linux-2.6.33/kernel/linux-2.6.33/fs/gfs2/quota.c

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