quota_local.c 34 KB

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