quota_local.c 33 KB

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