quota_global.c 27 KB

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