quota_local.c 36 KB

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