quota_global.c 29 KB

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