quota_local.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * Implementation of operations over local quota file
  3. */
  4. #include <linux/fs.h>
  5. #include <linux/quota.h>
  6. #include <linux/quotaops.h>
  7. #include <linux/module.h>
  8. #define MLOG_MASK_PREFIX ML_QUOTA
  9. #include <cluster/masklog.h>
  10. #include "ocfs2_fs.h"
  11. #include "ocfs2.h"
  12. #include "inode.h"
  13. #include "alloc.h"
  14. #include "file.h"
  15. #include "buffer_head_io.h"
  16. #include "journal.h"
  17. #include "sysfile.h"
  18. #include "dlmglue.h"
  19. #include "quota.h"
  20. /* Number of local quota structures per block */
  21. static inline unsigned int ol_quota_entries_per_block(struct super_block *sb)
  22. {
  23. return ((sb->s_blocksize - OCFS2_QBLK_RESERVED_SPACE) /
  24. sizeof(struct ocfs2_local_disk_dqblk));
  25. }
  26. /* Number of blocks with entries in one chunk */
  27. static inline unsigned int ol_chunk_blocks(struct super_block *sb)
  28. {
  29. return ((sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  30. OCFS2_QBLK_RESERVED_SPACE) << 3) /
  31. ol_quota_entries_per_block(sb);
  32. }
  33. /* Number of entries in a chunk bitmap */
  34. static unsigned int ol_chunk_entries(struct super_block *sb)
  35. {
  36. return ol_chunk_blocks(sb) * ol_quota_entries_per_block(sb);
  37. }
  38. /* Offset of the chunk in quota file */
  39. static unsigned int ol_quota_chunk_block(struct super_block *sb, int c)
  40. {
  41. /* 1 block for local quota file info, 1 block per chunk for chunk info */
  42. return 1 + (ol_chunk_blocks(sb) + 1) * c;
  43. }
  44. static unsigned int ol_dqblk_block(struct super_block *sb, int c, int off)
  45. {
  46. int epb = ol_quota_entries_per_block(sb);
  47. return ol_quota_chunk_block(sb, c) + 1 + off / epb;
  48. }
  49. static unsigned int ol_dqblk_block_off(struct super_block *sb, int c, int off)
  50. {
  51. int epb = ol_quota_entries_per_block(sb);
  52. return (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
  53. }
  54. /* Offset of the dquot structure in the quota file */
  55. static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
  56. {
  57. return (ol_dqblk_block(sb, c, off) << sb->s_blocksize_bits) +
  58. ol_dqblk_block_off(sb, c, off);
  59. }
  60. /* Compute block number from given offset */
  61. static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
  62. {
  63. return off >> sb->s_blocksize_bits;
  64. }
  65. static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
  66. {
  67. return off & ((1 << sb->s_blocksize_bits) - 1);
  68. }
  69. /* Compute offset in the chunk of a structure with the given offset */
  70. static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
  71. {
  72. int epb = ol_quota_entries_per_block(sb);
  73. return ((off >> sb->s_blocksize_bits) -
  74. ol_quota_chunk_block(sb, c) - 1) * epb
  75. + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
  76. sizeof(struct ocfs2_local_disk_dqblk);
  77. }
  78. /* Write bufferhead into the fs */
  79. static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
  80. void (*modify)(struct buffer_head *, void *), void *private)
  81. {
  82. struct super_block *sb = inode->i_sb;
  83. handle_t *handle;
  84. int status;
  85. handle = ocfs2_start_trans(OCFS2_SB(sb), 1);
  86. if (IS_ERR(handle)) {
  87. status = PTR_ERR(handle);
  88. mlog_errno(status);
  89. return status;
  90. }
  91. status = ocfs2_journal_access_dq(handle, inode, bh,
  92. OCFS2_JOURNAL_ACCESS_WRITE);
  93. if (status < 0) {
  94. mlog_errno(status);
  95. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  96. return status;
  97. }
  98. lock_buffer(bh);
  99. modify(bh, private);
  100. unlock_buffer(bh);
  101. status = ocfs2_journal_dirty(handle, bh);
  102. if (status < 0) {
  103. mlog_errno(status);
  104. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  105. return status;
  106. }
  107. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  108. if (status < 0) {
  109. mlog_errno(status);
  110. return status;
  111. }
  112. return 0;
  113. }
  114. /* Check whether we understand format of quota files */
  115. static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
  116. {
  117. unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
  118. unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
  119. unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
  120. unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
  121. unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
  122. GROUP_QUOTA_SYSTEM_INODE };
  123. struct buffer_head *bh = NULL;
  124. struct inode *linode = sb_dqopt(sb)->files[type];
  125. struct inode *ginode = NULL;
  126. struct ocfs2_disk_dqheader *dqhead;
  127. int status, ret = 0;
  128. /* First check whether we understand local quota file */
  129. status = ocfs2_read_quota_block(linode, 0, &bh);
  130. if (status) {
  131. mlog_errno(status);
  132. mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
  133. type);
  134. goto out_err;
  135. }
  136. dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  137. if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
  138. mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
  139. " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
  140. lmagics[type], type);
  141. goto out_err;
  142. }
  143. if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
  144. mlog(ML_ERROR, "quota file version does not match (%u != %u),"
  145. " type=%d\n", le32_to_cpu(dqhead->dqh_version),
  146. lversions[type], type);
  147. goto out_err;
  148. }
  149. brelse(bh);
  150. bh = NULL;
  151. /* Next check whether we understand global quota file */
  152. ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
  153. OCFS2_INVALID_SLOT);
  154. if (!ginode) {
  155. mlog(ML_ERROR, "cannot get global quota file inode "
  156. "(type=%d)\n", type);
  157. goto out_err;
  158. }
  159. /* Since the header is read only, we don't care about locking */
  160. status = ocfs2_read_quota_block(ginode, 0, &bh);
  161. if (status) {
  162. mlog_errno(status);
  163. mlog(ML_ERROR, "failed to read global quota file header "
  164. "(type=%d)\n", type);
  165. goto out_err;
  166. }
  167. dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  168. if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
  169. mlog(ML_ERROR, "global quota file magic does not match "
  170. "(%u != %u), type=%d\n",
  171. le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
  172. goto out_err;
  173. }
  174. if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
  175. mlog(ML_ERROR, "global quota file version does not match "
  176. "(%u != %u), type=%d\n",
  177. le32_to_cpu(dqhead->dqh_version), gversions[type],
  178. type);
  179. goto out_err;
  180. }
  181. ret = 1;
  182. out_err:
  183. brelse(bh);
  184. iput(ginode);
  185. return ret;
  186. }
  187. /* Release given list of quota file chunks */
  188. static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
  189. {
  190. struct ocfs2_quota_chunk *pos, *next;
  191. list_for_each_entry_safe(pos, next, head, qc_chunk) {
  192. list_del(&pos->qc_chunk);
  193. brelse(pos->qc_headerbh);
  194. kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
  195. }
  196. }
  197. /* Load quota bitmaps into memory */
  198. static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
  199. struct ocfs2_local_disk_dqinfo *ldinfo,
  200. struct list_head *head)
  201. {
  202. struct ocfs2_quota_chunk *newchunk;
  203. int i, status;
  204. INIT_LIST_HEAD(head);
  205. for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
  206. newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  207. if (!newchunk) {
  208. ocfs2_release_local_quota_bitmaps(head);
  209. return -ENOMEM;
  210. }
  211. newchunk->qc_num = i;
  212. newchunk->qc_headerbh = NULL;
  213. status = ocfs2_read_quota_block(inode,
  214. ol_quota_chunk_block(inode->i_sb, i),
  215. &newchunk->qc_headerbh);
  216. if (status) {
  217. mlog_errno(status);
  218. kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
  219. ocfs2_release_local_quota_bitmaps(head);
  220. return status;
  221. }
  222. list_add_tail(&newchunk->qc_chunk, head);
  223. }
  224. return 0;
  225. }
  226. static void olq_update_info(struct buffer_head *bh, void *private)
  227. {
  228. struct mem_dqinfo *info = private;
  229. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  230. struct ocfs2_local_disk_dqinfo *ldinfo;
  231. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  232. OCFS2_LOCAL_INFO_OFF);
  233. spin_lock(&dq_data_lock);
  234. ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
  235. ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
  236. ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
  237. spin_unlock(&dq_data_lock);
  238. }
  239. static int ocfs2_add_recovery_chunk(struct super_block *sb,
  240. struct ocfs2_local_disk_chunk *dchunk,
  241. int chunk,
  242. struct list_head *head)
  243. {
  244. struct ocfs2_recovery_chunk *rc;
  245. rc = kmalloc(sizeof(struct ocfs2_recovery_chunk), GFP_NOFS);
  246. if (!rc)
  247. return -ENOMEM;
  248. rc->rc_chunk = chunk;
  249. rc->rc_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
  250. if (!rc->rc_bitmap) {
  251. kfree(rc);
  252. return -ENOMEM;
  253. }
  254. memcpy(rc->rc_bitmap, dchunk->dqc_bitmap,
  255. (ol_chunk_entries(sb) + 7) >> 3);
  256. list_add_tail(&rc->rc_list, head);
  257. return 0;
  258. }
  259. static void free_recovery_list(struct list_head *head)
  260. {
  261. struct ocfs2_recovery_chunk *next;
  262. struct ocfs2_recovery_chunk *rchunk;
  263. list_for_each_entry_safe(rchunk, next, head, rc_list) {
  264. list_del(&rchunk->rc_list);
  265. kfree(rchunk->rc_bitmap);
  266. kfree(rchunk);
  267. }
  268. }
  269. void ocfs2_free_quota_recovery(struct ocfs2_quota_recovery *rec)
  270. {
  271. int type;
  272. for (type = 0; type < MAXQUOTAS; type++)
  273. free_recovery_list(&(rec->r_list[type]));
  274. kfree(rec);
  275. }
  276. /* Load entries in our quota file we have to recover*/
  277. static int ocfs2_recovery_load_quota(struct inode *lqinode,
  278. struct ocfs2_local_disk_dqinfo *ldinfo,
  279. int type,
  280. struct list_head *head)
  281. {
  282. struct super_block *sb = lqinode->i_sb;
  283. struct buffer_head *hbh;
  284. struct ocfs2_local_disk_chunk *dchunk;
  285. int i, chunks = le32_to_cpu(ldinfo->dqi_chunks);
  286. int status = 0;
  287. for (i = 0; i < chunks; i++) {
  288. hbh = NULL;
  289. status = ocfs2_read_quota_block(lqinode,
  290. ol_quota_chunk_block(sb, i),
  291. &hbh);
  292. if (status) {
  293. mlog_errno(status);
  294. break;
  295. }
  296. dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
  297. if (le32_to_cpu(dchunk->dqc_free) < ol_chunk_entries(sb))
  298. status = ocfs2_add_recovery_chunk(sb, dchunk, i, head);
  299. brelse(hbh);
  300. if (status < 0)
  301. break;
  302. }
  303. if (status < 0)
  304. free_recovery_list(head);
  305. return status;
  306. }
  307. static struct ocfs2_quota_recovery *ocfs2_alloc_quota_recovery(void)
  308. {
  309. int type;
  310. struct ocfs2_quota_recovery *rec;
  311. rec = kmalloc(sizeof(struct ocfs2_quota_recovery), GFP_NOFS);
  312. if (!rec)
  313. return NULL;
  314. for (type = 0; type < MAXQUOTAS; type++)
  315. INIT_LIST_HEAD(&(rec->r_list[type]));
  316. return rec;
  317. }
  318. /* Load information we need for quota recovery into memory */
  319. struct ocfs2_quota_recovery *ocfs2_begin_quota_recovery(
  320. struct ocfs2_super *osb,
  321. int slot_num)
  322. {
  323. unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  324. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  325. unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
  326. LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  327. struct super_block *sb = osb->sb;
  328. struct ocfs2_local_disk_dqinfo *ldinfo;
  329. struct inode *lqinode;
  330. struct buffer_head *bh;
  331. int type;
  332. int status = 0;
  333. struct ocfs2_quota_recovery *rec;
  334. mlog(ML_NOTICE, "Beginning quota recovery in slot %u\n", slot_num);
  335. rec = ocfs2_alloc_quota_recovery();
  336. if (!rec)
  337. return ERR_PTR(-ENOMEM);
  338. /* First init... */
  339. for (type = 0; type < MAXQUOTAS; type++) {
  340. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  341. continue;
  342. /* At this point, journal of the slot is already replayed so
  343. * we can trust metadata and data of the quota file */
  344. lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
  345. if (!lqinode) {
  346. status = -ENOENT;
  347. goto out;
  348. }
  349. status = ocfs2_inode_lock_full(lqinode, NULL, 1,
  350. OCFS2_META_LOCK_RECOVERY);
  351. if (status < 0) {
  352. mlog_errno(status);
  353. goto out_put;
  354. }
  355. /* Now read local header */
  356. bh = NULL;
  357. status = ocfs2_read_quota_block(lqinode, 0, &bh);
  358. if (status) {
  359. mlog_errno(status);
  360. mlog(ML_ERROR, "failed to read quota file info header "
  361. "(slot=%d type=%d)\n", slot_num, type);
  362. goto out_lock;
  363. }
  364. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  365. OCFS2_LOCAL_INFO_OFF);
  366. status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
  367. &rec->r_list[type]);
  368. brelse(bh);
  369. out_lock:
  370. ocfs2_inode_unlock(lqinode, 1);
  371. out_put:
  372. iput(lqinode);
  373. if (status < 0)
  374. break;
  375. }
  376. out:
  377. if (status < 0) {
  378. ocfs2_free_quota_recovery(rec);
  379. rec = ERR_PTR(status);
  380. }
  381. return rec;
  382. }
  383. /* Sync changes in local quota file into global quota file and
  384. * reinitialize local quota file.
  385. * The function expects local quota file to be already locked and
  386. * dqonoff_mutex locked. */
  387. static int ocfs2_recover_local_quota_file(struct inode *lqinode,
  388. int type,
  389. struct ocfs2_quota_recovery *rec)
  390. {
  391. struct super_block *sb = lqinode->i_sb;
  392. struct ocfs2_mem_dqinfo *oinfo = sb_dqinfo(sb, type)->dqi_priv;
  393. struct ocfs2_local_disk_chunk *dchunk;
  394. struct ocfs2_local_disk_dqblk *dqblk;
  395. struct dquot *dquot;
  396. handle_t *handle;
  397. struct buffer_head *hbh = NULL, *qbh = NULL;
  398. int status = 0;
  399. int bit, chunk;
  400. struct ocfs2_recovery_chunk *rchunk, *next;
  401. qsize_t spacechange, inodechange;
  402. mlog_entry("ino=%lu type=%u", (unsigned long)lqinode->i_ino, type);
  403. status = ocfs2_lock_global_qf(oinfo, 1);
  404. if (status < 0)
  405. goto out;
  406. list_for_each_entry_safe(rchunk, next, &(rec->r_list[type]), rc_list) {
  407. chunk = rchunk->rc_chunk;
  408. hbh = NULL;
  409. status = ocfs2_read_quota_block(lqinode,
  410. ol_quota_chunk_block(sb, chunk),
  411. &hbh);
  412. if (status) {
  413. mlog_errno(status);
  414. break;
  415. }
  416. dchunk = (struct ocfs2_local_disk_chunk *)hbh->b_data;
  417. for_each_bit(bit, rchunk->rc_bitmap, ol_chunk_entries(sb)) {
  418. qbh = NULL;
  419. status = ocfs2_read_quota_block(lqinode,
  420. ol_dqblk_block(sb, chunk, bit),
  421. &qbh);
  422. if (status) {
  423. mlog_errno(status);
  424. break;
  425. }
  426. dqblk = (struct ocfs2_local_disk_dqblk *)(qbh->b_data +
  427. ol_dqblk_block_off(sb, chunk, bit));
  428. dquot = dqget(sb, le64_to_cpu(dqblk->dqb_id), type);
  429. if (!dquot) {
  430. status = -EIO;
  431. mlog(ML_ERROR, "Failed to get quota structure "
  432. "for id %u, type %d. Cannot finish quota "
  433. "file recovery.\n",
  434. (unsigned)le64_to_cpu(dqblk->dqb_id),
  435. type);
  436. goto out_put_bh;
  437. }
  438. handle = ocfs2_start_trans(OCFS2_SB(sb),
  439. OCFS2_QSYNC_CREDITS);
  440. if (IS_ERR(handle)) {
  441. status = PTR_ERR(handle);
  442. mlog_errno(status);
  443. goto out_put_dquot;
  444. }
  445. mutex_lock(&sb_dqopt(sb)->dqio_mutex);
  446. spin_lock(&dq_data_lock);
  447. /* Add usage from quota entry into quota changes
  448. * of our node. Auxiliary variables are important
  449. * due to signedness */
  450. spacechange = le64_to_cpu(dqblk->dqb_spacemod);
  451. inodechange = le64_to_cpu(dqblk->dqb_inodemod);
  452. dquot->dq_dqb.dqb_curspace += spacechange;
  453. dquot->dq_dqb.dqb_curinodes += inodechange;
  454. spin_unlock(&dq_data_lock);
  455. /* We want to drop reference held by the crashed
  456. * node. Since we have our own reference we know
  457. * global structure actually won't be freed. */
  458. status = ocfs2_global_release_dquot(dquot);
  459. if (status < 0) {
  460. mlog_errno(status);
  461. goto out_commit;
  462. }
  463. /* Release local quota file entry */
  464. status = ocfs2_journal_access_dq(handle, lqinode,
  465. qbh, OCFS2_JOURNAL_ACCESS_WRITE);
  466. if (status < 0) {
  467. mlog_errno(status);
  468. goto out_commit;
  469. }
  470. lock_buffer(qbh);
  471. WARN_ON(!ocfs2_test_bit(bit, dchunk->dqc_bitmap));
  472. ocfs2_clear_bit(bit, dchunk->dqc_bitmap);
  473. le32_add_cpu(&dchunk->dqc_free, 1);
  474. unlock_buffer(qbh);
  475. status = ocfs2_journal_dirty(handle, qbh);
  476. if (status < 0)
  477. mlog_errno(status);
  478. out_commit:
  479. mutex_unlock(&sb_dqopt(sb)->dqio_mutex);
  480. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  481. out_put_dquot:
  482. dqput(dquot);
  483. out_put_bh:
  484. brelse(qbh);
  485. if (status < 0)
  486. break;
  487. }
  488. brelse(hbh);
  489. list_del(&rchunk->rc_list);
  490. kfree(rchunk->rc_bitmap);
  491. kfree(rchunk);
  492. if (status < 0)
  493. break;
  494. }
  495. ocfs2_unlock_global_qf(oinfo, 1);
  496. out:
  497. if (status < 0)
  498. free_recovery_list(&(rec->r_list[type]));
  499. mlog_exit(status);
  500. return status;
  501. }
  502. /* Recover local quota files for given node different from us */
  503. int ocfs2_finish_quota_recovery(struct ocfs2_super *osb,
  504. struct ocfs2_quota_recovery *rec,
  505. int slot_num)
  506. {
  507. unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
  508. LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  509. struct super_block *sb = osb->sb;
  510. struct ocfs2_local_disk_dqinfo *ldinfo;
  511. struct buffer_head *bh;
  512. handle_t *handle;
  513. int type;
  514. int status = 0;
  515. struct inode *lqinode;
  516. unsigned int flags;
  517. mlog(ML_NOTICE, "Finishing quota recovery in slot %u\n", slot_num);
  518. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  519. for (type = 0; type < MAXQUOTAS; type++) {
  520. if (list_empty(&(rec->r_list[type])))
  521. continue;
  522. mlog(0, "Recovering quota in slot %d\n", slot_num);
  523. lqinode = ocfs2_get_system_file_inode(osb, ino[type], slot_num);
  524. if (!lqinode) {
  525. status = -ENOENT;
  526. goto out;
  527. }
  528. status = ocfs2_inode_lock_full(lqinode, NULL, 1,
  529. OCFS2_META_LOCK_NOQUEUE);
  530. /* Someone else is holding the lock? Then he must be
  531. * doing the recovery. Just skip the file... */
  532. if (status == -EAGAIN) {
  533. mlog(ML_NOTICE, "skipping quota recovery for slot %d "
  534. "because quota file is locked.\n", slot_num);
  535. status = 0;
  536. goto out_put;
  537. } else if (status < 0) {
  538. mlog_errno(status);
  539. goto out_put;
  540. }
  541. /* Now read local header */
  542. bh = NULL;
  543. status = ocfs2_read_quota_block(lqinode, 0, &bh);
  544. if (status) {
  545. mlog_errno(status);
  546. mlog(ML_ERROR, "failed to read quota file info header "
  547. "(slot=%d type=%d)\n", slot_num, type);
  548. goto out_lock;
  549. }
  550. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  551. OCFS2_LOCAL_INFO_OFF);
  552. /* Is recovery still needed? */
  553. flags = le32_to_cpu(ldinfo->dqi_flags);
  554. if (!(flags & OLQF_CLEAN))
  555. status = ocfs2_recover_local_quota_file(lqinode,
  556. type,
  557. rec);
  558. /* We don't want to mark file as clean when it is actually
  559. * active */
  560. if (slot_num == osb->slot_num)
  561. goto out_bh;
  562. /* Mark quota file as clean if we are recovering quota file of
  563. * some other node. */
  564. handle = ocfs2_start_trans(osb, 1);
  565. if (IS_ERR(handle)) {
  566. status = PTR_ERR(handle);
  567. mlog_errno(status);
  568. goto out_bh;
  569. }
  570. status = ocfs2_journal_access_dq(handle, lqinode, bh,
  571. OCFS2_JOURNAL_ACCESS_WRITE);
  572. if (status < 0) {
  573. mlog_errno(status);
  574. goto out_trans;
  575. }
  576. lock_buffer(bh);
  577. ldinfo->dqi_flags = cpu_to_le32(flags | OLQF_CLEAN);
  578. unlock_buffer(bh);
  579. status = ocfs2_journal_dirty(handle, bh);
  580. if (status < 0)
  581. mlog_errno(status);
  582. out_trans:
  583. ocfs2_commit_trans(osb, handle);
  584. out_bh:
  585. brelse(bh);
  586. out_lock:
  587. ocfs2_inode_unlock(lqinode, 1);
  588. out_put:
  589. iput(lqinode);
  590. if (status < 0)
  591. break;
  592. }
  593. out:
  594. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  595. kfree(rec);
  596. return status;
  597. }
  598. /* Read information header from quota file */
  599. static int ocfs2_local_read_info(struct super_block *sb, int type)
  600. {
  601. struct ocfs2_local_disk_dqinfo *ldinfo;
  602. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  603. struct ocfs2_mem_dqinfo *oinfo;
  604. struct inode *lqinode = sb_dqopt(sb)->files[type];
  605. int status;
  606. struct buffer_head *bh = NULL;
  607. struct ocfs2_quota_recovery *rec;
  608. int locked = 0;
  609. info->dqi_maxblimit = 0x7fffffffffffffffLL;
  610. info->dqi_maxilimit = 0x7fffffffffffffffLL;
  611. oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
  612. if (!oinfo) {
  613. mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
  614. " info.");
  615. goto out_err;
  616. }
  617. info->dqi_priv = oinfo;
  618. oinfo->dqi_type = type;
  619. INIT_LIST_HEAD(&oinfo->dqi_chunk);
  620. oinfo->dqi_rec = NULL;
  621. oinfo->dqi_lqi_bh = NULL;
  622. oinfo->dqi_ibh = NULL;
  623. status = ocfs2_global_read_info(sb, type);
  624. if (status < 0)
  625. goto out_err;
  626. status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto out_err;
  630. }
  631. locked = 1;
  632. /* Now read local header */
  633. status = ocfs2_read_quota_block(lqinode, 0, &bh);
  634. if (status) {
  635. mlog_errno(status);
  636. mlog(ML_ERROR, "failed to read quota file info header "
  637. "(type=%d)\n", type);
  638. goto out_err;
  639. }
  640. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  641. OCFS2_LOCAL_INFO_OFF);
  642. info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
  643. oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
  644. oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
  645. oinfo->dqi_ibh = bh;
  646. /* We crashed when using local quota file? */
  647. if (!(info->dqi_flags & OLQF_CLEAN)) {
  648. rec = OCFS2_SB(sb)->quota_rec;
  649. if (!rec) {
  650. rec = ocfs2_alloc_quota_recovery();
  651. if (!rec) {
  652. status = -ENOMEM;
  653. mlog_errno(status);
  654. goto out_err;
  655. }
  656. OCFS2_SB(sb)->quota_rec = rec;
  657. }
  658. status = ocfs2_recovery_load_quota(lqinode, ldinfo, type,
  659. &rec->r_list[type]);
  660. if (status < 0) {
  661. mlog_errno(status);
  662. goto out_err;
  663. }
  664. }
  665. status = ocfs2_load_local_quota_bitmaps(lqinode,
  666. ldinfo,
  667. &oinfo->dqi_chunk);
  668. if (status < 0) {
  669. mlog_errno(status);
  670. goto out_err;
  671. }
  672. /* Now mark quota file as used */
  673. info->dqi_flags &= ~OLQF_CLEAN;
  674. status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
  675. if (status < 0) {
  676. mlog_errno(status);
  677. goto out_err;
  678. }
  679. return 0;
  680. out_err:
  681. if (oinfo) {
  682. iput(oinfo->dqi_gqinode);
  683. ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  684. ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  685. brelse(oinfo->dqi_lqi_bh);
  686. if (locked)
  687. ocfs2_inode_unlock(lqinode, 1);
  688. ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  689. kfree(oinfo);
  690. }
  691. brelse(bh);
  692. return -1;
  693. }
  694. /* Write local info to quota file */
  695. static int ocfs2_local_write_info(struct super_block *sb, int type)
  696. {
  697. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  698. struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
  699. ->dqi_ibh;
  700. int status;
  701. status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
  702. info);
  703. if (status < 0) {
  704. mlog_errno(status);
  705. return -1;
  706. }
  707. return 0;
  708. }
  709. /* Release info from memory */
  710. static int ocfs2_local_free_info(struct super_block *sb, int type)
  711. {
  712. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  713. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  714. struct ocfs2_quota_chunk *chunk;
  715. struct ocfs2_local_disk_chunk *dchunk;
  716. int mark_clean = 1, len;
  717. int status;
  718. /* At this point we know there are no more dquots and thus
  719. * even if there's some sync in the pdflush queue, it won't
  720. * find any dquots and return without doing anything */
  721. cancel_delayed_work_sync(&oinfo->dqi_sync_work);
  722. iput(oinfo->dqi_gqinode);
  723. ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  724. ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  725. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  726. dchunk = (struct ocfs2_local_disk_chunk *)
  727. (chunk->qc_headerbh->b_data);
  728. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  729. len = ol_chunk_entries(sb);
  730. } else {
  731. len = (oinfo->dqi_blocks -
  732. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  733. * ol_quota_entries_per_block(sb);
  734. }
  735. /* Not all entries free? Bug! */
  736. if (le32_to_cpu(dchunk->dqc_free) != len) {
  737. mlog(ML_ERROR, "releasing quota file with used "
  738. "entries (type=%d)\n", type);
  739. mark_clean = 0;
  740. }
  741. }
  742. ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  743. /* dqonoff_mutex protects us against racing with recovery thread... */
  744. if (oinfo->dqi_rec) {
  745. ocfs2_free_quota_recovery(oinfo->dqi_rec);
  746. mark_clean = 0;
  747. }
  748. if (!mark_clean)
  749. goto out;
  750. /* Mark local file as clean */
  751. info->dqi_flags |= OLQF_CLEAN;
  752. status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
  753. oinfo->dqi_ibh,
  754. olq_update_info,
  755. info);
  756. if (status < 0) {
  757. mlog_errno(status);
  758. goto out;
  759. }
  760. out:
  761. ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
  762. brelse(oinfo->dqi_ibh);
  763. brelse(oinfo->dqi_lqi_bh);
  764. kfree(oinfo);
  765. return 0;
  766. }
  767. static void olq_set_dquot(struct buffer_head *bh, void *private)
  768. {
  769. struct ocfs2_dquot *od = private;
  770. struct ocfs2_local_disk_dqblk *dqblk;
  771. struct super_block *sb = od->dq_dquot.dq_sb;
  772. dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
  773. + ol_dqblk_block_offset(sb, od->dq_local_off));
  774. dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
  775. spin_lock(&dq_data_lock);
  776. dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
  777. od->dq_origspace);
  778. dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
  779. od->dq_originodes);
  780. spin_unlock(&dq_data_lock);
  781. mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
  782. od->dq_dquot.dq_id, (long long)le64_to_cpu(dqblk->dqb_spacemod),
  783. (long long)le64_to_cpu(dqblk->dqb_inodemod));
  784. }
  785. /* Write dquot to local quota file */
  786. static int ocfs2_local_write_dquot(struct dquot *dquot)
  787. {
  788. struct super_block *sb = dquot->dq_sb;
  789. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  790. struct buffer_head *bh = NULL;
  791. int status;
  792. status = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
  793. ol_dqblk_file_block(sb, od->dq_local_off),
  794. &bh);
  795. if (status) {
  796. mlog_errno(status);
  797. goto out;
  798. }
  799. status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
  800. olq_set_dquot, od);
  801. if (status < 0) {
  802. mlog_errno(status);
  803. goto out;
  804. }
  805. out:
  806. brelse(bh);
  807. return status;
  808. }
  809. /* Find free entry in local quota file */
  810. static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
  811. int type,
  812. int *offset)
  813. {
  814. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  815. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  816. struct ocfs2_quota_chunk *chunk;
  817. struct ocfs2_local_disk_chunk *dchunk;
  818. int found = 0, len;
  819. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  820. dchunk = (struct ocfs2_local_disk_chunk *)
  821. chunk->qc_headerbh->b_data;
  822. if (le32_to_cpu(dchunk->dqc_free) > 0) {
  823. found = 1;
  824. break;
  825. }
  826. }
  827. if (!found)
  828. return NULL;
  829. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  830. len = ol_chunk_entries(sb);
  831. } else {
  832. len = (oinfo->dqi_blocks -
  833. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  834. * ol_quota_entries_per_block(sb);
  835. }
  836. found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
  837. /* We failed? */
  838. if (found == len) {
  839. mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
  840. " entries free (type=%d)\n", chunk->qc_num,
  841. le32_to_cpu(dchunk->dqc_free), type);
  842. return ERR_PTR(-EIO);
  843. }
  844. *offset = found;
  845. return chunk;
  846. }
  847. /* Add new chunk to the local quota file */
  848. static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
  849. struct super_block *sb,
  850. int type,
  851. int *offset)
  852. {
  853. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  854. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  855. struct inode *lqinode = sb_dqopt(sb)->files[type];
  856. struct ocfs2_quota_chunk *chunk = NULL;
  857. struct ocfs2_local_disk_chunk *dchunk;
  858. int status;
  859. handle_t *handle;
  860. struct buffer_head *bh = NULL;
  861. u64 p_blkno;
  862. /* We are protected by dqio_sem so no locking needed */
  863. status = ocfs2_extend_no_holes(lqinode,
  864. lqinode->i_size + 2 * sb->s_blocksize,
  865. lqinode->i_size);
  866. if (status < 0) {
  867. mlog_errno(status);
  868. goto out;
  869. }
  870. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  871. lqinode->i_size + 2 * sb->s_blocksize);
  872. if (status < 0) {
  873. mlog_errno(status);
  874. goto out;
  875. }
  876. chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  877. if (!chunk) {
  878. status = -ENOMEM;
  879. mlog_errno(status);
  880. goto out;
  881. }
  882. down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  883. status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
  884. &p_blkno, NULL, NULL);
  885. up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  886. if (status < 0) {
  887. mlog_errno(status);
  888. goto out;
  889. }
  890. bh = sb_getblk(sb, p_blkno);
  891. if (!bh) {
  892. status = -ENOMEM;
  893. mlog_errno(status);
  894. goto out;
  895. }
  896. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  897. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  898. if (IS_ERR(handle)) {
  899. status = PTR_ERR(handle);
  900. mlog_errno(status);
  901. goto out;
  902. }
  903. status = ocfs2_journal_access_dq(handle, lqinode, bh,
  904. OCFS2_JOURNAL_ACCESS_WRITE);
  905. if (status < 0) {
  906. mlog_errno(status);
  907. goto out_trans;
  908. }
  909. lock_buffer(bh);
  910. dchunk->dqc_free = cpu_to_le32(ol_quota_entries_per_block(sb));
  911. memset(dchunk->dqc_bitmap, 0,
  912. sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  913. OCFS2_QBLK_RESERVED_SPACE);
  914. set_buffer_uptodate(bh);
  915. unlock_buffer(bh);
  916. status = ocfs2_journal_dirty(handle, bh);
  917. if (status < 0) {
  918. mlog_errno(status);
  919. goto out_trans;
  920. }
  921. oinfo->dqi_blocks += 2;
  922. oinfo->dqi_chunks++;
  923. status = ocfs2_local_write_info(sb, type);
  924. if (status < 0) {
  925. mlog_errno(status);
  926. goto out_trans;
  927. }
  928. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  929. if (status < 0) {
  930. mlog_errno(status);
  931. goto out;
  932. }
  933. list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
  934. chunk->qc_num = list_entry(chunk->qc_chunk.prev,
  935. struct ocfs2_quota_chunk,
  936. qc_chunk)->qc_num + 1;
  937. chunk->qc_headerbh = bh;
  938. *offset = 0;
  939. return chunk;
  940. out_trans:
  941. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  942. out:
  943. brelse(bh);
  944. kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
  945. return ERR_PTR(status);
  946. }
  947. /* Find free entry in local quota file */
  948. static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
  949. struct super_block *sb,
  950. int type,
  951. int *offset)
  952. {
  953. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  954. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  955. struct ocfs2_quota_chunk *chunk;
  956. struct inode *lqinode = sb_dqopt(sb)->files[type];
  957. struct ocfs2_local_disk_chunk *dchunk;
  958. int epb = ol_quota_entries_per_block(sb);
  959. unsigned int chunk_blocks;
  960. int status;
  961. handle_t *handle;
  962. if (list_empty(&oinfo->dqi_chunk))
  963. return ocfs2_local_quota_add_chunk(sb, type, offset);
  964. /* Is the last chunk full? */
  965. chunk = list_entry(oinfo->dqi_chunk.prev,
  966. struct ocfs2_quota_chunk, qc_chunk);
  967. chunk_blocks = oinfo->dqi_blocks -
  968. ol_quota_chunk_block(sb, chunk->qc_num) - 1;
  969. if (ol_chunk_blocks(sb) == chunk_blocks)
  970. return ocfs2_local_quota_add_chunk(sb, type, offset);
  971. /* We are protected by dqio_sem so no locking needed */
  972. status = ocfs2_extend_no_holes(lqinode,
  973. lqinode->i_size + sb->s_blocksize,
  974. lqinode->i_size);
  975. if (status < 0) {
  976. mlog_errno(status);
  977. goto out;
  978. }
  979. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  980. lqinode->i_size + sb->s_blocksize);
  981. if (status < 0) {
  982. mlog_errno(status);
  983. goto out;
  984. }
  985. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  986. if (IS_ERR(handle)) {
  987. status = PTR_ERR(handle);
  988. mlog_errno(status);
  989. goto out;
  990. }
  991. status = ocfs2_journal_access_dq(handle, lqinode, chunk->qc_headerbh,
  992. OCFS2_JOURNAL_ACCESS_WRITE);
  993. if (status < 0) {
  994. mlog_errno(status);
  995. goto out_trans;
  996. }
  997. dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
  998. lock_buffer(chunk->qc_headerbh);
  999. le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
  1000. unlock_buffer(chunk->qc_headerbh);
  1001. status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
  1002. if (status < 0) {
  1003. mlog_errno(status);
  1004. goto out_trans;
  1005. }
  1006. oinfo->dqi_blocks++;
  1007. status = ocfs2_local_write_info(sb, type);
  1008. if (status < 0) {
  1009. mlog_errno(status);
  1010. goto out_trans;
  1011. }
  1012. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  1013. if (status < 0) {
  1014. mlog_errno(status);
  1015. goto out;
  1016. }
  1017. *offset = chunk_blocks * epb;
  1018. return chunk;
  1019. out_trans:
  1020. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  1021. out:
  1022. return ERR_PTR(status);
  1023. }
  1024. static void olq_alloc_dquot(struct buffer_head *bh, void *private)
  1025. {
  1026. int *offset = private;
  1027. struct ocfs2_local_disk_chunk *dchunk;
  1028. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  1029. ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
  1030. le32_add_cpu(&dchunk->dqc_free, -1);
  1031. }
  1032. /* Create dquot in the local file for given id */
  1033. static int ocfs2_create_local_dquot(struct dquot *dquot)
  1034. {
  1035. struct super_block *sb = dquot->dq_sb;
  1036. int type = dquot->dq_type;
  1037. struct inode *lqinode = sb_dqopt(sb)->files[type];
  1038. struct ocfs2_quota_chunk *chunk;
  1039. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  1040. int offset;
  1041. int status;
  1042. chunk = ocfs2_find_free_entry(sb, type, &offset);
  1043. if (!chunk) {
  1044. chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
  1045. if (IS_ERR(chunk))
  1046. return PTR_ERR(chunk);
  1047. } else if (IS_ERR(chunk)) {
  1048. return PTR_ERR(chunk);
  1049. }
  1050. od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
  1051. od->dq_chunk = chunk;
  1052. /* Initialize dquot structure on disk */
  1053. status = ocfs2_local_write_dquot(dquot);
  1054. if (status < 0) {
  1055. mlog_errno(status);
  1056. goto out;
  1057. }
  1058. /* Mark structure as allocated */
  1059. status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
  1060. &offset);
  1061. if (status < 0) {
  1062. mlog_errno(status);
  1063. goto out;
  1064. }
  1065. out:
  1066. return status;
  1067. }
  1068. /* Create entry in local file for dquot, load data from the global file */
  1069. static int ocfs2_local_read_dquot(struct dquot *dquot)
  1070. {
  1071. int status;
  1072. mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
  1073. status = ocfs2_global_read_dquot(dquot);
  1074. if (status < 0) {
  1075. mlog_errno(status);
  1076. goto out_err;
  1077. }
  1078. /* Now create entry in the local quota file */
  1079. status = ocfs2_create_local_dquot(dquot);
  1080. if (status < 0) {
  1081. mlog_errno(status);
  1082. goto out_err;
  1083. }
  1084. mlog_exit(0);
  1085. return 0;
  1086. out_err:
  1087. mlog_exit(status);
  1088. return status;
  1089. }
  1090. /* Release dquot structure from local quota file. ocfs2_release_dquot() has
  1091. * already started a transaction and obtained exclusive lock for global
  1092. * quota file. */
  1093. static int ocfs2_local_release_dquot(struct dquot *dquot)
  1094. {
  1095. int status;
  1096. int type = dquot->dq_type;
  1097. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  1098. struct super_block *sb = dquot->dq_sb;
  1099. struct ocfs2_local_disk_chunk *dchunk;
  1100. int offset;
  1101. handle_t *handle = journal_current_handle();
  1102. BUG_ON(!handle);
  1103. /* First write all local changes to global file */
  1104. status = ocfs2_global_release_dquot(dquot);
  1105. if (status < 0) {
  1106. mlog_errno(status);
  1107. goto out;
  1108. }
  1109. status = ocfs2_journal_access_dq(handle, sb_dqopt(sb)->files[type],
  1110. od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
  1111. if (status < 0) {
  1112. mlog_errno(status);
  1113. goto out;
  1114. }
  1115. offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
  1116. od->dq_local_off);
  1117. dchunk = (struct ocfs2_local_disk_chunk *)
  1118. (od->dq_chunk->qc_headerbh->b_data);
  1119. /* Mark structure as freed */
  1120. lock_buffer(od->dq_chunk->qc_headerbh);
  1121. ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
  1122. le32_add_cpu(&dchunk->dqc_free, 1);
  1123. unlock_buffer(od->dq_chunk->qc_headerbh);
  1124. status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
  1125. if (status < 0) {
  1126. mlog_errno(status);
  1127. goto out;
  1128. }
  1129. status = 0;
  1130. out:
  1131. /* Clear the read bit so that next time someone uses this
  1132. * dquot he reads fresh info from disk and allocates local
  1133. * dquot structure */
  1134. clear_bit(DQ_READ_B, &dquot->dq_flags);
  1135. return status;
  1136. }
  1137. static struct quota_format_ops ocfs2_format_ops = {
  1138. .check_quota_file = ocfs2_local_check_quota_file,
  1139. .read_file_info = ocfs2_local_read_info,
  1140. .write_file_info = ocfs2_global_write_info,
  1141. .free_file_info = ocfs2_local_free_info,
  1142. .read_dqblk = ocfs2_local_read_dquot,
  1143. .commit_dqblk = ocfs2_local_write_dquot,
  1144. .release_dqblk = ocfs2_local_release_dquot,
  1145. };
  1146. struct quota_format_type ocfs2_quota_format = {
  1147. .qf_fmt_id = QFMT_OCFS2,
  1148. .qf_ops = &ocfs2_format_ops,
  1149. .qf_owner = THIS_MODULE
  1150. };