quota_local.c 34 KB

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