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