quota_global.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Implementation of operations over global quota file
  3. */
  4. #include <linux/spinlock.h>
  5. #include <linux/fs.h>
  6. #include <linux/quota.h>
  7. #include <linux/quotaops.h>
  8. #include <linux/dqblk_qtree.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/writeback.h>
  11. #include <linux/workqueue.h>
  12. #define MLOG_MASK_PREFIX ML_QUOTA
  13. #include <cluster/masklog.h>
  14. #include "ocfs2_fs.h"
  15. #include "ocfs2.h"
  16. #include "alloc.h"
  17. #include "blockcheck.h"
  18. #include "inode.h"
  19. #include "journal.h"
  20. #include "file.h"
  21. #include "sysfile.h"
  22. #include "dlmglue.h"
  23. #include "uptodate.h"
  24. #include "quota.h"
  25. static struct workqueue_struct *ocfs2_quota_wq = NULL;
  26. static void qsync_work_fn(struct work_struct *work);
  27. static void ocfs2_global_disk2memdqb(struct dquot *dquot, void *dp)
  28. {
  29. struct ocfs2_global_disk_dqblk *d = dp;
  30. struct mem_dqblk *m = &dquot->dq_dqb;
  31. /* Update from disk only entries not set by the admin */
  32. if (!test_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags)) {
  33. m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  34. m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  35. }
  36. if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
  37. m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  38. if (!test_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags)) {
  39. m->dqb_bhardlimit = le64_to_cpu(d->dqb_bhardlimit);
  40. m->dqb_bsoftlimit = le64_to_cpu(d->dqb_bsoftlimit);
  41. }
  42. if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
  43. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  44. if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags))
  45. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  46. if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags))
  47. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  48. OCFS2_DQUOT(dquot)->dq_use_count = le32_to_cpu(d->dqb_use_count);
  49. }
  50. static void ocfs2_global_mem2diskdqb(void *dp, struct dquot *dquot)
  51. {
  52. struct ocfs2_global_disk_dqblk *d = dp;
  53. struct mem_dqblk *m = &dquot->dq_dqb;
  54. d->dqb_id = cpu_to_le32(dquot->dq_id);
  55. d->dqb_use_count = cpu_to_le32(OCFS2_DQUOT(dquot)->dq_use_count);
  56. d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  57. d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  58. d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  59. d->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
  60. d->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
  61. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  62. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  63. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  64. }
  65. static int ocfs2_global_is_id(void *dp, struct dquot *dquot)
  66. {
  67. struct ocfs2_global_disk_dqblk *d = dp;
  68. struct ocfs2_mem_dqinfo *oinfo =
  69. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  70. if (qtree_entry_unused(&oinfo->dqi_gi, dp))
  71. return 0;
  72. return le32_to_cpu(d->dqb_id) == dquot->dq_id;
  73. }
  74. struct qtree_fmt_operations ocfs2_global_ops = {
  75. .mem2disk_dqblk = ocfs2_global_mem2diskdqb,
  76. .disk2mem_dqblk = ocfs2_global_disk2memdqb,
  77. .is_id = ocfs2_global_is_id,
  78. };
  79. static int ocfs2_validate_quota_block(struct super_block *sb,
  80. struct buffer_head *bh)
  81. {
  82. struct ocfs2_disk_dqtrailer *dqt =
  83. ocfs2_block_dqtrailer(sb->s_blocksize, bh->b_data);
  84. mlog(0, "Validating quota block %llu\n",
  85. (unsigned long long)bh->b_blocknr);
  86. BUG_ON(!buffer_uptodate(bh));
  87. /*
  88. * If the ecc fails, we return the error but otherwise
  89. * leave the filesystem running. We know any error is
  90. * local to this block.
  91. */
  92. return ocfs2_validate_meta_ecc(sb, bh->b_data, &dqt->dq_check);
  93. }
  94. int ocfs2_read_quota_block(struct inode *inode, u64 v_block,
  95. struct buffer_head **bh)
  96. {
  97. int rc = 0;
  98. struct buffer_head *tmp = *bh;
  99. rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, 0,
  100. ocfs2_validate_quota_block);
  101. if (rc)
  102. mlog_errno(rc);
  103. /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
  104. if (!rc && !*bh)
  105. *bh = tmp;
  106. return rc;
  107. }
  108. static int ocfs2_get_quota_block(struct inode *inode, int block,
  109. struct buffer_head **bh)
  110. {
  111. u64 pblock, pcount;
  112. int err;
  113. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  114. err = ocfs2_extent_map_get_blocks(inode, block, &pblock, &pcount, NULL);
  115. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  116. if (err) {
  117. mlog_errno(err);
  118. return err;
  119. }
  120. *bh = sb_getblk(inode->i_sb, pblock);
  121. if (!*bh) {
  122. err = -EIO;
  123. mlog_errno(err);
  124. }
  125. return err;;
  126. }
  127. /* Read data from global quotafile - avoid pagecache and such because we cannot
  128. * afford acquiring the locks... We use quota cluster lock to serialize
  129. * operations. Caller is responsible for acquiring it. */
  130. ssize_t ocfs2_quota_read(struct super_block *sb, int type, char *data,
  131. size_t len, loff_t off)
  132. {
  133. struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  134. struct inode *gqinode = oinfo->dqi_gqinode;
  135. loff_t i_size = i_size_read(gqinode);
  136. int offset = off & (sb->s_blocksize - 1);
  137. sector_t blk = off >> sb->s_blocksize_bits;
  138. int err = 0;
  139. struct buffer_head *bh;
  140. size_t toread, tocopy;
  141. if (off > i_size)
  142. return 0;
  143. if (off + len > i_size)
  144. len = i_size - off;
  145. toread = len;
  146. while (toread > 0) {
  147. tocopy = min_t(size_t, (sb->s_blocksize - offset), toread);
  148. bh = NULL;
  149. err = ocfs2_read_quota_block(gqinode, blk, &bh);
  150. if (err) {
  151. mlog_errno(err);
  152. return err;
  153. }
  154. memcpy(data, bh->b_data + offset, tocopy);
  155. brelse(bh);
  156. offset = 0;
  157. toread -= tocopy;
  158. data += tocopy;
  159. blk++;
  160. }
  161. return len;
  162. }
  163. /* Write to quotafile (we know the transaction is already started and has
  164. * enough credits) */
  165. ssize_t ocfs2_quota_write(struct super_block *sb, int type,
  166. const char *data, size_t len, loff_t off)
  167. {
  168. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  169. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  170. struct inode *gqinode = oinfo->dqi_gqinode;
  171. int offset = off & (sb->s_blocksize - 1);
  172. sector_t blk = off >> sb->s_blocksize_bits;
  173. int err = 0, new = 0, ja_type;
  174. struct buffer_head *bh = NULL;
  175. handle_t *handle = journal_current_handle();
  176. if (!handle) {
  177. mlog(ML_ERROR, "Quota write (off=%llu, len=%llu) cancelled "
  178. "because transaction was not started.\n",
  179. (unsigned long long)off, (unsigned long long)len);
  180. return -EIO;
  181. }
  182. if (len > sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset) {
  183. WARN_ON(1);
  184. len = sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE - offset;
  185. }
  186. mutex_lock_nested(&gqinode->i_mutex, I_MUTEX_QUOTA);
  187. if (gqinode->i_size < off + len) {
  188. down_write(&OCFS2_I(gqinode)->ip_alloc_sem);
  189. err = ocfs2_extend_no_holes(gqinode, off + len, off);
  190. up_write(&OCFS2_I(gqinode)->ip_alloc_sem);
  191. if (err < 0)
  192. goto out;
  193. err = ocfs2_simple_size_update(gqinode,
  194. oinfo->dqi_gqi_bh,
  195. off + len);
  196. if (err < 0)
  197. goto out;
  198. new = 1;
  199. }
  200. /* Not rewriting whole block? */
  201. if ((offset || len < sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) &&
  202. !new) {
  203. err = ocfs2_read_quota_block(gqinode, blk, &bh);
  204. ja_type = OCFS2_JOURNAL_ACCESS_WRITE;
  205. } else {
  206. err = ocfs2_get_quota_block(gqinode, blk, &bh);
  207. ja_type = OCFS2_JOURNAL_ACCESS_CREATE;
  208. }
  209. if (err) {
  210. mlog_errno(err);
  211. return err;
  212. }
  213. lock_buffer(bh);
  214. if (new)
  215. memset(bh->b_data, 0, sb->s_blocksize);
  216. memcpy(bh->b_data + offset, data, len);
  217. flush_dcache_page(bh->b_page);
  218. set_buffer_uptodate(bh);
  219. unlock_buffer(bh);
  220. ocfs2_set_buffer_uptodate(gqinode, bh);
  221. err = ocfs2_journal_access_dq(handle, gqinode, bh, ja_type);
  222. if (err < 0) {
  223. brelse(bh);
  224. goto out;
  225. }
  226. err = ocfs2_journal_dirty(handle, bh);
  227. brelse(bh);
  228. if (err < 0)
  229. goto out;
  230. out:
  231. if (err) {
  232. mutex_unlock(&gqinode->i_mutex);
  233. mlog_errno(err);
  234. return err;
  235. }
  236. gqinode->i_version++;
  237. ocfs2_mark_inode_dirty(handle, gqinode, oinfo->dqi_gqi_bh);
  238. mutex_unlock(&gqinode->i_mutex);
  239. return len;
  240. }
  241. int ocfs2_lock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
  242. {
  243. int status;
  244. struct buffer_head *bh = NULL;
  245. status = ocfs2_inode_lock(oinfo->dqi_gqinode, &bh, ex);
  246. if (status < 0)
  247. return status;
  248. spin_lock(&dq_data_lock);
  249. if (!oinfo->dqi_gqi_count++)
  250. oinfo->dqi_gqi_bh = bh;
  251. else
  252. WARN_ON(bh != oinfo->dqi_gqi_bh);
  253. spin_unlock(&dq_data_lock);
  254. return 0;
  255. }
  256. void ocfs2_unlock_global_qf(struct ocfs2_mem_dqinfo *oinfo, int ex)
  257. {
  258. ocfs2_inode_unlock(oinfo->dqi_gqinode, ex);
  259. brelse(oinfo->dqi_gqi_bh);
  260. spin_lock(&dq_data_lock);
  261. if (!--oinfo->dqi_gqi_count)
  262. oinfo->dqi_gqi_bh = NULL;
  263. spin_unlock(&dq_data_lock);
  264. }
  265. /* Read information header from global quota file */
  266. int ocfs2_global_read_info(struct super_block *sb, int type)
  267. {
  268. struct inode *gqinode = NULL;
  269. unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
  270. GROUP_QUOTA_SYSTEM_INODE };
  271. struct ocfs2_global_disk_dqinfo dinfo;
  272. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  273. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  274. int status;
  275. mlog_entry_void();
  276. /* Read global header */
  277. gqinode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
  278. OCFS2_INVALID_SLOT);
  279. if (!gqinode) {
  280. mlog(ML_ERROR, "failed to get global quota inode (type=%d)\n",
  281. type);
  282. status = -EINVAL;
  283. goto out_err;
  284. }
  285. oinfo->dqi_gi.dqi_sb = sb;
  286. oinfo->dqi_gi.dqi_type = type;
  287. ocfs2_qinfo_lock_res_init(&oinfo->dqi_gqlock, oinfo);
  288. oinfo->dqi_gi.dqi_entry_size = sizeof(struct ocfs2_global_disk_dqblk);
  289. oinfo->dqi_gi.dqi_ops = &ocfs2_global_ops;
  290. oinfo->dqi_gqi_bh = NULL;
  291. oinfo->dqi_gqi_count = 0;
  292. oinfo->dqi_gqinode = gqinode;
  293. status = ocfs2_lock_global_qf(oinfo, 0);
  294. if (status < 0) {
  295. mlog_errno(status);
  296. goto out_err;
  297. }
  298. status = sb->s_op->quota_read(sb, type, (char *)&dinfo,
  299. sizeof(struct ocfs2_global_disk_dqinfo),
  300. OCFS2_GLOBAL_INFO_OFF);
  301. ocfs2_unlock_global_qf(oinfo, 0);
  302. if (status != sizeof(struct ocfs2_global_disk_dqinfo)) {
  303. mlog(ML_ERROR, "Cannot read global quota info (%d).\n",
  304. status);
  305. if (status >= 0)
  306. status = -EIO;
  307. mlog_errno(status);
  308. goto out_err;
  309. }
  310. info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  311. info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  312. oinfo->dqi_syncms = le32_to_cpu(dinfo.dqi_syncms);
  313. oinfo->dqi_syncjiff = msecs_to_jiffies(oinfo->dqi_syncms);
  314. oinfo->dqi_gi.dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  315. oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  316. oinfo->dqi_gi.dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  317. oinfo->dqi_gi.dqi_blocksize_bits = sb->s_blocksize_bits;
  318. oinfo->dqi_gi.dqi_usable_bs = sb->s_blocksize -
  319. OCFS2_QBLK_RESERVED_SPACE;
  320. oinfo->dqi_gi.dqi_qtree_depth = qtree_depth(&oinfo->dqi_gi);
  321. INIT_DELAYED_WORK(&oinfo->dqi_sync_work, qsync_work_fn);
  322. queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
  323. oinfo->dqi_syncjiff);
  324. out_err:
  325. mlog_exit(status);
  326. return status;
  327. }
  328. /* Write information to global quota file. Expects exlusive lock on quota
  329. * file inode and quota info */
  330. static int __ocfs2_global_write_info(struct super_block *sb, int type)
  331. {
  332. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  333. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  334. struct ocfs2_global_disk_dqinfo dinfo;
  335. ssize_t size;
  336. spin_lock(&dq_data_lock);
  337. info->dqi_flags &= ~DQF_INFO_DIRTY;
  338. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  339. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  340. spin_unlock(&dq_data_lock);
  341. dinfo.dqi_syncms = cpu_to_le32(oinfo->dqi_syncms);
  342. dinfo.dqi_blocks = cpu_to_le32(oinfo->dqi_gi.dqi_blocks);
  343. dinfo.dqi_free_blk = cpu_to_le32(oinfo->dqi_gi.dqi_free_blk);
  344. dinfo.dqi_free_entry = cpu_to_le32(oinfo->dqi_gi.dqi_free_entry);
  345. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  346. sizeof(struct ocfs2_global_disk_dqinfo),
  347. OCFS2_GLOBAL_INFO_OFF);
  348. if (size != sizeof(struct ocfs2_global_disk_dqinfo)) {
  349. mlog(ML_ERROR, "Cannot write global quota info structure\n");
  350. if (size >= 0)
  351. size = -EIO;
  352. return size;
  353. }
  354. return 0;
  355. }
  356. int ocfs2_global_write_info(struct super_block *sb, int type)
  357. {
  358. int err;
  359. struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
  360. err = ocfs2_qinfo_lock(info, 1);
  361. if (err < 0)
  362. return err;
  363. err = __ocfs2_global_write_info(sb, type);
  364. ocfs2_qinfo_unlock(info, 1);
  365. return err;
  366. }
  367. /* Read in information from global quota file and acquire a reference to it.
  368. * dquot_acquire() has already started the transaction and locked quota file */
  369. int ocfs2_global_read_dquot(struct dquot *dquot)
  370. {
  371. int err, err2, ex = 0;
  372. struct ocfs2_mem_dqinfo *info =
  373. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  374. err = ocfs2_qinfo_lock(info, 0);
  375. if (err < 0)
  376. goto out;
  377. err = qtree_read_dquot(&info->dqi_gi, dquot);
  378. if (err < 0)
  379. goto out_qlock;
  380. OCFS2_DQUOT(dquot)->dq_use_count++;
  381. OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
  382. OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
  383. if (!dquot->dq_off) { /* No real quota entry? */
  384. /* Upgrade to exclusive lock for allocation */
  385. err = ocfs2_qinfo_lock(info, 1);
  386. if (err < 0)
  387. goto out_qlock;
  388. ex = 1;
  389. }
  390. err = qtree_write_dquot(&info->dqi_gi, dquot);
  391. if (ex && info_dirty(sb_dqinfo(dquot->dq_sb, dquot->dq_type))) {
  392. err2 = __ocfs2_global_write_info(dquot->dq_sb, dquot->dq_type);
  393. if (!err)
  394. err = err2;
  395. }
  396. out_qlock:
  397. if (ex)
  398. ocfs2_qinfo_unlock(info, 1);
  399. ocfs2_qinfo_unlock(info, 0);
  400. out:
  401. if (err < 0)
  402. mlog_errno(err);
  403. return err;
  404. }
  405. /* Sync local information about quota modifications with global quota file.
  406. * Caller must have started the transaction and obtained exclusive lock for
  407. * global quota file inode */
  408. int __ocfs2_sync_dquot(struct dquot *dquot, int freeing)
  409. {
  410. int err, err2;
  411. struct super_block *sb = dquot->dq_sb;
  412. int type = dquot->dq_type;
  413. struct ocfs2_mem_dqinfo *info = sb_dqinfo(sb, type)->dqi_priv;
  414. struct ocfs2_global_disk_dqblk dqblk;
  415. s64 spacechange, inodechange;
  416. time_t olditime, oldbtime;
  417. err = sb->s_op->quota_read(sb, type, (char *)&dqblk,
  418. sizeof(struct ocfs2_global_disk_dqblk),
  419. dquot->dq_off);
  420. if (err != sizeof(struct ocfs2_global_disk_dqblk)) {
  421. if (err >= 0) {
  422. mlog(ML_ERROR, "Short read from global quota file "
  423. "(%u read)\n", err);
  424. err = -EIO;
  425. }
  426. goto out;
  427. }
  428. /* Update space and inode usage. Get also other information from
  429. * global quota file so that we don't overwrite any changes there.
  430. * We are */
  431. spin_lock(&dq_data_lock);
  432. spacechange = dquot->dq_dqb.dqb_curspace -
  433. OCFS2_DQUOT(dquot)->dq_origspace;
  434. inodechange = dquot->dq_dqb.dqb_curinodes -
  435. OCFS2_DQUOT(dquot)->dq_originodes;
  436. olditime = dquot->dq_dqb.dqb_itime;
  437. oldbtime = dquot->dq_dqb.dqb_btime;
  438. ocfs2_global_disk2memdqb(dquot, &dqblk);
  439. mlog(0, "Syncing global dquot %u space %lld+%lld, inodes %lld+%lld\n",
  440. dquot->dq_id, dquot->dq_dqb.dqb_curspace, (long long)spacechange,
  441. dquot->dq_dqb.dqb_curinodes, (long long)inodechange);
  442. if (!test_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags))
  443. dquot->dq_dqb.dqb_curspace += spacechange;
  444. if (!test_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags))
  445. dquot->dq_dqb.dqb_curinodes += inodechange;
  446. /* Set properly space grace time... */
  447. if (dquot->dq_dqb.dqb_bsoftlimit &&
  448. dquot->dq_dqb.dqb_curspace > dquot->dq_dqb.dqb_bsoftlimit) {
  449. if (!test_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags) &&
  450. oldbtime > 0) {
  451. if (dquot->dq_dqb.dqb_btime > 0)
  452. dquot->dq_dqb.dqb_btime =
  453. min(dquot->dq_dqb.dqb_btime, oldbtime);
  454. else
  455. dquot->dq_dqb.dqb_btime = oldbtime;
  456. }
  457. } else {
  458. dquot->dq_dqb.dqb_btime = 0;
  459. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  460. }
  461. /* Set properly inode grace time... */
  462. if (dquot->dq_dqb.dqb_isoftlimit &&
  463. dquot->dq_dqb.dqb_curinodes > dquot->dq_dqb.dqb_isoftlimit) {
  464. if (!test_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags) &&
  465. olditime > 0) {
  466. if (dquot->dq_dqb.dqb_itime > 0)
  467. dquot->dq_dqb.dqb_itime =
  468. min(dquot->dq_dqb.dqb_itime, olditime);
  469. else
  470. dquot->dq_dqb.dqb_itime = olditime;
  471. }
  472. } else {
  473. dquot->dq_dqb.dqb_itime = 0;
  474. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  475. }
  476. /* All information is properly updated, clear the flags */
  477. __clear_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
  478. __clear_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
  479. __clear_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
  480. __clear_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
  481. __clear_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
  482. __clear_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
  483. OCFS2_DQUOT(dquot)->dq_origspace = dquot->dq_dqb.dqb_curspace;
  484. OCFS2_DQUOT(dquot)->dq_originodes = dquot->dq_dqb.dqb_curinodes;
  485. spin_unlock(&dq_data_lock);
  486. err = ocfs2_qinfo_lock(info, freeing);
  487. if (err < 0) {
  488. mlog(ML_ERROR, "Failed to lock quota info, loosing quota write"
  489. " (type=%d, id=%u)\n", dquot->dq_type,
  490. (unsigned)dquot->dq_id);
  491. goto out;
  492. }
  493. if (freeing)
  494. OCFS2_DQUOT(dquot)->dq_use_count--;
  495. err = qtree_write_dquot(&info->dqi_gi, dquot);
  496. if (err < 0)
  497. goto out_qlock;
  498. if (freeing && !OCFS2_DQUOT(dquot)->dq_use_count) {
  499. err = qtree_release_dquot(&info->dqi_gi, dquot);
  500. if (info_dirty(sb_dqinfo(sb, type))) {
  501. err2 = __ocfs2_global_write_info(sb, type);
  502. if (!err)
  503. err = err2;
  504. }
  505. }
  506. out_qlock:
  507. ocfs2_qinfo_unlock(info, freeing);
  508. out:
  509. if (err < 0)
  510. mlog_errno(err);
  511. return err;
  512. }
  513. /*
  514. * Functions for periodic syncing of dquots with global file
  515. */
  516. static int ocfs2_sync_dquot_helper(struct dquot *dquot, unsigned long type)
  517. {
  518. handle_t *handle;
  519. struct super_block *sb = dquot->dq_sb;
  520. struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  521. struct ocfs2_super *osb = OCFS2_SB(sb);
  522. int status = 0;
  523. mlog_entry("id=%u qtype=%u type=%lu device=%s\n", dquot->dq_id,
  524. dquot->dq_type, type, sb->s_id);
  525. if (type != dquot->dq_type)
  526. goto out;
  527. status = ocfs2_lock_global_qf(oinfo, 1);
  528. if (status < 0)
  529. goto out;
  530. handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
  531. if (IS_ERR(handle)) {
  532. status = PTR_ERR(handle);
  533. mlog_errno(status);
  534. goto out_ilock;
  535. }
  536. mutex_lock(&sb_dqopt(sb)->dqio_mutex);
  537. status = ocfs2_sync_dquot(dquot);
  538. mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
  539. if (status < 0)
  540. mlog_errno(status);
  541. /* We have to write local structure as well... */
  542. dquot_mark_dquot_dirty(dquot);
  543. status = dquot_commit(dquot);
  544. if (status < 0)
  545. mlog_errno(status);
  546. ocfs2_commit_trans(osb, handle);
  547. out_ilock:
  548. ocfs2_unlock_global_qf(oinfo, 1);
  549. out:
  550. mlog_exit(status);
  551. return status;
  552. }
  553. static void qsync_work_fn(struct work_struct *work)
  554. {
  555. struct ocfs2_mem_dqinfo *oinfo = container_of(work,
  556. struct ocfs2_mem_dqinfo,
  557. dqi_sync_work.work);
  558. struct super_block *sb = oinfo->dqi_gqinode->i_sb;
  559. dquot_scan_active(sb, ocfs2_sync_dquot_helper, oinfo->dqi_type);
  560. queue_delayed_work(ocfs2_quota_wq, &oinfo->dqi_sync_work,
  561. oinfo->dqi_syncjiff);
  562. }
  563. /*
  564. * Wrappers for generic quota functions
  565. */
  566. static int ocfs2_write_dquot(struct dquot *dquot)
  567. {
  568. handle_t *handle;
  569. struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
  570. int status = 0;
  571. mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
  572. handle = ocfs2_start_trans(osb, OCFS2_QWRITE_CREDITS);
  573. if (IS_ERR(handle)) {
  574. status = PTR_ERR(handle);
  575. mlog_errno(status);
  576. goto out;
  577. }
  578. status = dquot_commit(dquot);
  579. ocfs2_commit_trans(osb, handle);
  580. out:
  581. mlog_exit(status);
  582. return status;
  583. }
  584. int ocfs2_calc_qdel_credits(struct super_block *sb, int type)
  585. {
  586. struct ocfs2_mem_dqinfo *oinfo;
  587. int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  588. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
  589. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
  590. return 0;
  591. oinfo = sb_dqinfo(sb, type)->dqi_priv;
  592. /* We modify tree, leaf block, global info, local chunk header,
  593. * global and local inode */
  594. return oinfo->dqi_gi.dqi_qtree_depth + 2 + 1 +
  595. 2 * OCFS2_INODE_UPDATE_CREDITS;
  596. }
  597. static int ocfs2_release_dquot(struct dquot *dquot)
  598. {
  599. handle_t *handle;
  600. struct ocfs2_mem_dqinfo *oinfo =
  601. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  602. struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
  603. int status = 0;
  604. mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
  605. status = ocfs2_lock_global_qf(oinfo, 1);
  606. if (status < 0)
  607. goto out;
  608. handle = ocfs2_start_trans(osb,
  609. ocfs2_calc_qdel_credits(dquot->dq_sb, dquot->dq_type));
  610. if (IS_ERR(handle)) {
  611. status = PTR_ERR(handle);
  612. mlog_errno(status);
  613. goto out_ilock;
  614. }
  615. status = dquot_release(dquot);
  616. ocfs2_commit_trans(osb, handle);
  617. out_ilock:
  618. ocfs2_unlock_global_qf(oinfo, 1);
  619. out:
  620. mlog_exit(status);
  621. return status;
  622. }
  623. int ocfs2_calc_qinit_credits(struct super_block *sb, int type)
  624. {
  625. struct ocfs2_mem_dqinfo *oinfo;
  626. int features[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  627. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA };
  628. struct ocfs2_dinode *lfe, *gfe;
  629. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, features[type]))
  630. return 0;
  631. oinfo = sb_dqinfo(sb, type)->dqi_priv;
  632. gfe = (struct ocfs2_dinode *)oinfo->dqi_gqi_bh->b_data;
  633. lfe = (struct ocfs2_dinode *)oinfo->dqi_lqi_bh->b_data;
  634. /* We can extend local file + global file. In local file we
  635. * can modify info, chunk header block and dquot block. In
  636. * global file we can modify info, tree and leaf block */
  637. return ocfs2_calc_extend_credits(sb, &lfe->id2.i_list, 0) +
  638. ocfs2_calc_extend_credits(sb, &gfe->id2.i_list, 0) +
  639. 3 + oinfo->dqi_gi.dqi_qtree_depth + 2;
  640. }
  641. static int ocfs2_acquire_dquot(struct dquot *dquot)
  642. {
  643. handle_t *handle;
  644. struct ocfs2_mem_dqinfo *oinfo =
  645. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  646. struct ocfs2_super *osb = OCFS2_SB(dquot->dq_sb);
  647. int status = 0;
  648. mlog_entry("id=%u, type=%d", dquot->dq_id, dquot->dq_type);
  649. /* We need an exclusive lock, because we're going to update use count
  650. * and instantiate possibly new dquot structure */
  651. status = ocfs2_lock_global_qf(oinfo, 1);
  652. if (status < 0)
  653. goto out;
  654. handle = ocfs2_start_trans(osb,
  655. ocfs2_calc_qinit_credits(dquot->dq_sb, dquot->dq_type));
  656. if (IS_ERR(handle)) {
  657. status = PTR_ERR(handle);
  658. mlog_errno(status);
  659. goto out_ilock;
  660. }
  661. status = dquot_acquire(dquot);
  662. ocfs2_commit_trans(osb, handle);
  663. out_ilock:
  664. ocfs2_unlock_global_qf(oinfo, 1);
  665. out:
  666. mlog_exit(status);
  667. return status;
  668. }
  669. static int ocfs2_mark_dquot_dirty(struct dquot *dquot)
  670. {
  671. unsigned long mask = (1 << (DQ_LASTSET_B + QIF_ILIMITS_B)) |
  672. (1 << (DQ_LASTSET_B + QIF_BLIMITS_B)) |
  673. (1 << (DQ_LASTSET_B + QIF_INODES_B)) |
  674. (1 << (DQ_LASTSET_B + QIF_SPACE_B)) |
  675. (1 << (DQ_LASTSET_B + QIF_BTIME_B)) |
  676. (1 << (DQ_LASTSET_B + QIF_ITIME_B));
  677. int sync = 0;
  678. int status;
  679. struct super_block *sb = dquot->dq_sb;
  680. int type = dquot->dq_type;
  681. struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  682. handle_t *handle;
  683. struct ocfs2_super *osb = OCFS2_SB(sb);
  684. mlog_entry("id=%u, type=%d", dquot->dq_id, type);
  685. dquot_mark_dquot_dirty(dquot);
  686. /* In case user set some limits, sync dquot immediately to global
  687. * quota file so that information propagates quicker */
  688. spin_lock(&dq_data_lock);
  689. if (dquot->dq_flags & mask)
  690. sync = 1;
  691. spin_unlock(&dq_data_lock);
  692. /* This is a slight hack but we can't afford getting global quota
  693. * lock if we already have a transaction started. */
  694. if (!sync || journal_current_handle()) {
  695. status = ocfs2_write_dquot(dquot);
  696. goto out;
  697. }
  698. status = ocfs2_lock_global_qf(oinfo, 1);
  699. if (status < 0)
  700. goto out;
  701. handle = ocfs2_start_trans(osb, OCFS2_QSYNC_CREDITS);
  702. if (IS_ERR(handle)) {
  703. status = PTR_ERR(handle);
  704. mlog_errno(status);
  705. goto out_ilock;
  706. }
  707. status = ocfs2_sync_dquot(dquot);
  708. if (status < 0) {
  709. mlog_errno(status);
  710. goto out_trans;
  711. }
  712. /* Now write updated local dquot structure */
  713. status = dquot_commit(dquot);
  714. out_trans:
  715. ocfs2_commit_trans(osb, handle);
  716. out_ilock:
  717. ocfs2_unlock_global_qf(oinfo, 1);
  718. out:
  719. mlog_exit(status);
  720. return status;
  721. }
  722. /* This should happen only after set_dqinfo(). */
  723. static int ocfs2_write_info(struct super_block *sb, int type)
  724. {
  725. handle_t *handle;
  726. int status = 0;
  727. struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  728. mlog_entry_void();
  729. status = ocfs2_lock_global_qf(oinfo, 1);
  730. if (status < 0)
  731. goto out;
  732. handle = ocfs2_start_trans(OCFS2_SB(sb), OCFS2_QINFO_WRITE_CREDITS);
  733. if (IS_ERR(handle)) {
  734. status = PTR_ERR(handle);
  735. mlog_errno(status);
  736. goto out_ilock;
  737. }
  738. status = dquot_commit_info(sb, type);
  739. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  740. out_ilock:
  741. ocfs2_unlock_global_qf(oinfo, 1);
  742. out:
  743. mlog_exit(status);
  744. return status;
  745. }
  746. static struct dquot *ocfs2_alloc_dquot(struct super_block *sb, int type)
  747. {
  748. struct ocfs2_dquot *dquot =
  749. kmem_cache_zalloc(ocfs2_dquot_cachep, GFP_NOFS);
  750. if (!dquot)
  751. return NULL;
  752. return &dquot->dq_dquot;
  753. }
  754. static void ocfs2_destroy_dquot(struct dquot *dquot)
  755. {
  756. kmem_cache_free(ocfs2_dquot_cachep, dquot);
  757. }
  758. struct dquot_operations ocfs2_quota_operations = {
  759. .initialize = dquot_initialize,
  760. .drop = dquot_drop,
  761. .alloc_space = dquot_alloc_space,
  762. .alloc_inode = dquot_alloc_inode,
  763. .free_space = dquot_free_space,
  764. .free_inode = dquot_free_inode,
  765. .transfer = dquot_transfer,
  766. .write_dquot = ocfs2_write_dquot,
  767. .acquire_dquot = ocfs2_acquire_dquot,
  768. .release_dquot = ocfs2_release_dquot,
  769. .mark_dirty = ocfs2_mark_dquot_dirty,
  770. .write_info = ocfs2_write_info,
  771. .alloc_dquot = ocfs2_alloc_dquot,
  772. .destroy_dquot = ocfs2_destroy_dquot,
  773. };
  774. int ocfs2_quota_setup(void)
  775. {
  776. ocfs2_quota_wq = create_workqueue("o2quot");
  777. if (!ocfs2_quota_wq)
  778. return -ENOMEM;
  779. return 0;
  780. }
  781. void ocfs2_quota_shutdown(void)
  782. {
  783. if (ocfs2_quota_wq) {
  784. flush_workqueue(ocfs2_quota_wq);
  785. destroy_workqueue(ocfs2_quota_wq);
  786. ocfs2_quota_wq = NULL;
  787. }
  788. }