quota_local.c 36 KB

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