quota_global.c 30 KB

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