quota_local.c 36 KB

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