quota_global.c 25 KB

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