quota_global.c 25 KB

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