quota_global.c 27 KB

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