quota_local.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. /* Offset of the dquot structure in the quota file */
  45. static loff_t ol_dqblk_off(struct super_block *sb, int c, int off)
  46. {
  47. int epb = ol_quota_entries_per_block(sb);
  48. return ((ol_quota_chunk_block(sb, c) + 1 + off / epb)
  49. << sb->s_blocksize_bits) +
  50. (off % epb) * sizeof(struct ocfs2_local_disk_dqblk);
  51. }
  52. /* Compute block number from given offset */
  53. static inline unsigned int ol_dqblk_file_block(struct super_block *sb, loff_t off)
  54. {
  55. return off >> sb->s_blocksize_bits;
  56. }
  57. static inline unsigned int ol_dqblk_block_offset(struct super_block *sb, loff_t off)
  58. {
  59. return off & ((1 << sb->s_blocksize_bits) - 1);
  60. }
  61. /* Compute offset in the chunk of a structure with the given offset */
  62. static int ol_dqblk_chunk_off(struct super_block *sb, int c, loff_t off)
  63. {
  64. int epb = ol_quota_entries_per_block(sb);
  65. return ((off >> sb->s_blocksize_bits) -
  66. ol_quota_chunk_block(sb, c) - 1) * epb
  67. + ((unsigned int)(off & ((1 << sb->s_blocksize_bits) - 1))) /
  68. sizeof(struct ocfs2_local_disk_dqblk);
  69. }
  70. /* Write bufferhead into the fs */
  71. static int ocfs2_modify_bh(struct inode *inode, struct buffer_head *bh,
  72. void (*modify)(struct buffer_head *, void *), void *private)
  73. {
  74. struct super_block *sb = inode->i_sb;
  75. handle_t *handle;
  76. int status;
  77. handle = ocfs2_start_trans(OCFS2_SB(sb), 1);
  78. if (IS_ERR(handle)) {
  79. status = PTR_ERR(handle);
  80. mlog_errno(status);
  81. return status;
  82. }
  83. status = ocfs2_journal_access(handle, inode, bh,
  84. OCFS2_JOURNAL_ACCESS_WRITE);
  85. if (status < 0) {
  86. mlog_errno(status);
  87. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  88. return status;
  89. }
  90. lock_buffer(bh);
  91. modify(bh, private);
  92. unlock_buffer(bh);
  93. status = ocfs2_journal_dirty(handle, bh);
  94. if (status < 0) {
  95. mlog_errno(status);
  96. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  97. return status;
  98. }
  99. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  100. if (status < 0) {
  101. mlog_errno(status);
  102. return status;
  103. }
  104. return 0;
  105. }
  106. /* Check whether we understand format of quota files */
  107. static int ocfs2_local_check_quota_file(struct super_block *sb, int type)
  108. {
  109. unsigned int lmagics[MAXQUOTAS] = OCFS2_LOCAL_QMAGICS;
  110. unsigned int lversions[MAXQUOTAS] = OCFS2_LOCAL_QVERSIONS;
  111. unsigned int gmagics[MAXQUOTAS] = OCFS2_GLOBAL_QMAGICS;
  112. unsigned int gversions[MAXQUOTAS] = OCFS2_GLOBAL_QVERSIONS;
  113. unsigned int ino[MAXQUOTAS] = { USER_QUOTA_SYSTEM_INODE,
  114. GROUP_QUOTA_SYSTEM_INODE };
  115. struct buffer_head *bh;
  116. struct inode *linode = sb_dqopt(sb)->files[type];
  117. struct inode *ginode = NULL;
  118. struct ocfs2_disk_dqheader *dqhead;
  119. int status, ret = 0;
  120. /* First check whether we understand local quota file */
  121. bh = ocfs2_read_quota_block(linode, 0, &status);
  122. if (!bh) {
  123. mlog_errno(status);
  124. mlog(ML_ERROR, "failed to read quota file header (type=%d)\n",
  125. type);
  126. goto out_err;
  127. }
  128. dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  129. if (le32_to_cpu(dqhead->dqh_magic) != lmagics[type]) {
  130. mlog(ML_ERROR, "quota file magic does not match (%u != %u),"
  131. " type=%d\n", le32_to_cpu(dqhead->dqh_magic),
  132. lmagics[type], type);
  133. goto out_err;
  134. }
  135. if (le32_to_cpu(dqhead->dqh_version) != lversions[type]) {
  136. mlog(ML_ERROR, "quota file version does not match (%u != %u),"
  137. " type=%d\n", le32_to_cpu(dqhead->dqh_version),
  138. lversions[type], type);
  139. goto out_err;
  140. }
  141. brelse(bh);
  142. bh = NULL;
  143. /* Next check whether we understand global quota file */
  144. ginode = ocfs2_get_system_file_inode(OCFS2_SB(sb), ino[type],
  145. OCFS2_INVALID_SLOT);
  146. if (!ginode) {
  147. mlog(ML_ERROR, "cannot get global quota file inode "
  148. "(type=%d)\n", type);
  149. goto out_err;
  150. }
  151. /* Since the header is read only, we don't care about locking */
  152. bh = ocfs2_read_quota_block(ginode, 0, &status);
  153. if (!bh) {
  154. mlog_errno(status);
  155. mlog(ML_ERROR, "failed to read global quota file header "
  156. "(type=%d)\n", type);
  157. goto out_err;
  158. }
  159. dqhead = (struct ocfs2_disk_dqheader *)(bh->b_data);
  160. if (le32_to_cpu(dqhead->dqh_magic) != gmagics[type]) {
  161. mlog(ML_ERROR, "global quota file magic does not match "
  162. "(%u != %u), type=%d\n",
  163. le32_to_cpu(dqhead->dqh_magic), gmagics[type], type);
  164. goto out_err;
  165. }
  166. if (le32_to_cpu(dqhead->dqh_version) != gversions[type]) {
  167. mlog(ML_ERROR, "global quota file version does not match "
  168. "(%u != %u), type=%d\n",
  169. le32_to_cpu(dqhead->dqh_version), gversions[type],
  170. type);
  171. goto out_err;
  172. }
  173. ret = 1;
  174. out_err:
  175. brelse(bh);
  176. iput(ginode);
  177. return ret;
  178. }
  179. /* Release given list of quota file chunks */
  180. static void ocfs2_release_local_quota_bitmaps(struct list_head *head)
  181. {
  182. struct ocfs2_quota_chunk *pos, *next;
  183. list_for_each_entry_safe(pos, next, head, qc_chunk) {
  184. list_del(&pos->qc_chunk);
  185. brelse(pos->qc_headerbh);
  186. kmem_cache_free(ocfs2_qf_chunk_cachep, pos);
  187. }
  188. }
  189. /* Load quota bitmaps into memory */
  190. static int ocfs2_load_local_quota_bitmaps(struct inode *inode,
  191. struct ocfs2_local_disk_dqinfo *ldinfo,
  192. struct list_head *head)
  193. {
  194. struct ocfs2_quota_chunk *newchunk;
  195. int i, status;
  196. INIT_LIST_HEAD(head);
  197. for (i = 0; i < le32_to_cpu(ldinfo->dqi_chunks); i++) {
  198. newchunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  199. if (!newchunk) {
  200. ocfs2_release_local_quota_bitmaps(head);
  201. return -ENOMEM;
  202. }
  203. newchunk->qc_num = i;
  204. newchunk->qc_headerbh = ocfs2_read_quota_block(inode,
  205. ol_quota_chunk_block(inode->i_sb, i),
  206. &status);
  207. if (!newchunk->qc_headerbh) {
  208. mlog_errno(status);
  209. kmem_cache_free(ocfs2_qf_chunk_cachep, newchunk);
  210. ocfs2_release_local_quota_bitmaps(head);
  211. return status;
  212. }
  213. list_add_tail(&newchunk->qc_chunk, head);
  214. }
  215. return 0;
  216. }
  217. static void olq_update_info(struct buffer_head *bh, void *private)
  218. {
  219. struct mem_dqinfo *info = private;
  220. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  221. struct ocfs2_local_disk_dqinfo *ldinfo;
  222. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  223. OCFS2_LOCAL_INFO_OFF);
  224. spin_lock(&dq_data_lock);
  225. ldinfo->dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
  226. ldinfo->dqi_chunks = cpu_to_le32(oinfo->dqi_chunks);
  227. ldinfo->dqi_blocks = cpu_to_le32(oinfo->dqi_blocks);
  228. spin_unlock(&dq_data_lock);
  229. }
  230. /* Read information header from quota file */
  231. static int ocfs2_local_read_info(struct super_block *sb, int type)
  232. {
  233. struct ocfs2_local_disk_dqinfo *ldinfo;
  234. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  235. struct ocfs2_mem_dqinfo *oinfo;
  236. struct inode *lqinode = sb_dqopt(sb)->files[type];
  237. int status;
  238. struct buffer_head *bh = NULL;
  239. int locked = 0;
  240. info->dqi_maxblimit = 0x7fffffffffffffffLL;
  241. info->dqi_maxilimit = 0x7fffffffffffffffLL;
  242. oinfo = kmalloc(sizeof(struct ocfs2_mem_dqinfo), GFP_NOFS);
  243. if (!oinfo) {
  244. mlog(ML_ERROR, "failed to allocate memory for ocfs2 quota"
  245. " info.");
  246. goto out_err;
  247. }
  248. info->dqi_priv = oinfo;
  249. oinfo->dqi_type = type;
  250. INIT_LIST_HEAD(&oinfo->dqi_chunk);
  251. oinfo->dqi_lqi_bh = NULL;
  252. oinfo->dqi_ibh = NULL;
  253. status = ocfs2_global_read_info(sb, type);
  254. if (status < 0)
  255. goto out_err;
  256. status = ocfs2_inode_lock(lqinode, &oinfo->dqi_lqi_bh, 1);
  257. if (status < 0) {
  258. mlog_errno(status);
  259. goto out_err;
  260. }
  261. locked = 1;
  262. /* Now read local header */
  263. bh = ocfs2_read_quota_block(lqinode, 0, &status);
  264. if (!bh) {
  265. mlog_errno(status);
  266. mlog(ML_ERROR, "failed to read quota file info header "
  267. "(type=%d)\n", type);
  268. goto out_err;
  269. }
  270. ldinfo = (struct ocfs2_local_disk_dqinfo *)(bh->b_data +
  271. OCFS2_LOCAL_INFO_OFF);
  272. info->dqi_flags = le32_to_cpu(ldinfo->dqi_flags);
  273. oinfo->dqi_chunks = le32_to_cpu(ldinfo->dqi_chunks);
  274. oinfo->dqi_blocks = le32_to_cpu(ldinfo->dqi_blocks);
  275. oinfo->dqi_ibh = bh;
  276. /* We crashed when using local quota file? */
  277. if (!(info->dqi_flags & OLQF_CLEAN))
  278. goto out_err; /* So far we just bail out. Later we should resync here */
  279. status = ocfs2_load_local_quota_bitmaps(sb_dqopt(sb)->files[type],
  280. ldinfo,
  281. &oinfo->dqi_chunk);
  282. if (status < 0) {
  283. mlog_errno(status);
  284. goto out_err;
  285. }
  286. /* Now mark quota file as used */
  287. info->dqi_flags &= ~OLQF_CLEAN;
  288. status = ocfs2_modify_bh(lqinode, bh, olq_update_info, info);
  289. if (status < 0) {
  290. mlog_errno(status);
  291. goto out_err;
  292. }
  293. return 0;
  294. out_err:
  295. if (oinfo) {
  296. iput(oinfo->dqi_gqinode);
  297. ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  298. ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  299. brelse(oinfo->dqi_lqi_bh);
  300. if (locked)
  301. ocfs2_inode_unlock(lqinode, 1);
  302. ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  303. kfree(oinfo);
  304. }
  305. brelse(bh);
  306. return -1;
  307. }
  308. /* Write local info to quota file */
  309. static int ocfs2_local_write_info(struct super_block *sb, int type)
  310. {
  311. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  312. struct buffer_head *bh = ((struct ocfs2_mem_dqinfo *)info->dqi_priv)
  313. ->dqi_ibh;
  314. int status;
  315. status = ocfs2_modify_bh(sb_dqopt(sb)->files[type], bh, olq_update_info,
  316. info);
  317. if (status < 0) {
  318. mlog_errno(status);
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. /* Release info from memory */
  324. static int ocfs2_local_free_info(struct super_block *sb, int type)
  325. {
  326. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  327. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  328. struct ocfs2_quota_chunk *chunk;
  329. struct ocfs2_local_disk_chunk *dchunk;
  330. int mark_clean = 1, len;
  331. int status;
  332. /* At this point we know there are no more dquots and thus
  333. * even if there's some sync in the pdflush queue, it won't
  334. * find any dquots and return without doing anything */
  335. cancel_delayed_work_sync(&oinfo->dqi_sync_work);
  336. iput(oinfo->dqi_gqinode);
  337. ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  338. ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  339. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  340. dchunk = (struct ocfs2_local_disk_chunk *)
  341. (chunk->qc_headerbh->b_data);
  342. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  343. len = ol_chunk_entries(sb);
  344. } else {
  345. len = (oinfo->dqi_blocks -
  346. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  347. * ol_quota_entries_per_block(sb);
  348. }
  349. /* Not all entries free? Bug! */
  350. if (le32_to_cpu(dchunk->dqc_free) != len) {
  351. mlog(ML_ERROR, "releasing quota file with used "
  352. "entries (type=%d)\n", type);
  353. mark_clean = 0;
  354. }
  355. }
  356. ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  357. if (!mark_clean)
  358. goto out;
  359. /* Mark local file as clean */
  360. info->dqi_flags |= OLQF_CLEAN;
  361. status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
  362. oinfo->dqi_ibh,
  363. olq_update_info,
  364. info);
  365. if (status < 0) {
  366. mlog_errno(status);
  367. goto out;
  368. }
  369. out:
  370. ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
  371. brelse(oinfo->dqi_ibh);
  372. brelse(oinfo->dqi_lqi_bh);
  373. kfree(oinfo);
  374. return 0;
  375. }
  376. static void olq_set_dquot(struct buffer_head *bh, void *private)
  377. {
  378. struct ocfs2_dquot *od = private;
  379. struct ocfs2_local_disk_dqblk *dqblk;
  380. struct super_block *sb = od->dq_dquot.dq_sb;
  381. dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
  382. + ol_dqblk_block_offset(sb, od->dq_local_off));
  383. dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
  384. spin_lock(&dq_data_lock);
  385. dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
  386. od->dq_origspace);
  387. dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
  388. od->dq_originodes);
  389. spin_unlock(&dq_data_lock);
  390. mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
  391. od->dq_dquot.dq_id, dqblk->dqb_spacemod, dqblk->dqb_inodemod);
  392. }
  393. /* Write dquot to local quota file */
  394. static int ocfs2_local_write_dquot(struct dquot *dquot)
  395. {
  396. struct super_block *sb = dquot->dq_sb;
  397. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  398. struct buffer_head *bh;
  399. int status;
  400. bh = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
  401. ol_dqblk_file_block(sb, od->dq_local_off),
  402. &status);
  403. if (!bh) {
  404. mlog_errno(status);
  405. goto out;
  406. }
  407. status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
  408. olq_set_dquot, od);
  409. if (status < 0) {
  410. mlog_errno(status);
  411. goto out;
  412. }
  413. out:
  414. brelse(bh);
  415. return status;
  416. }
  417. /* Find free entry in local quota file */
  418. static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
  419. int type,
  420. int *offset)
  421. {
  422. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  423. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  424. struct ocfs2_quota_chunk *chunk;
  425. struct ocfs2_local_disk_chunk *dchunk;
  426. int found = 0, len;
  427. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  428. dchunk = (struct ocfs2_local_disk_chunk *)
  429. chunk->qc_headerbh->b_data;
  430. if (le32_to_cpu(dchunk->dqc_free) > 0) {
  431. found = 1;
  432. break;
  433. }
  434. }
  435. if (!found)
  436. return NULL;
  437. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  438. len = ol_chunk_entries(sb);
  439. } else {
  440. len = (oinfo->dqi_blocks -
  441. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  442. * ol_quota_entries_per_block(sb);
  443. }
  444. found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
  445. /* We failed? */
  446. if (found == len) {
  447. mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
  448. " entries free (type=%d)\n", chunk->qc_num,
  449. le32_to_cpu(dchunk->dqc_free), type);
  450. return ERR_PTR(-EIO);
  451. }
  452. *offset = found;
  453. return chunk;
  454. }
  455. /* Add new chunk to the local quota file */
  456. static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
  457. struct super_block *sb,
  458. int type,
  459. int *offset)
  460. {
  461. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  462. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  463. struct inode *lqinode = sb_dqopt(sb)->files[type];
  464. struct ocfs2_quota_chunk *chunk = NULL;
  465. struct ocfs2_local_disk_chunk *dchunk;
  466. int status;
  467. handle_t *handle;
  468. struct buffer_head *bh = NULL;
  469. u64 p_blkno;
  470. /* We are protected by dqio_sem so no locking needed */
  471. status = ocfs2_extend_no_holes(lqinode,
  472. lqinode->i_size + 2 * sb->s_blocksize,
  473. lqinode->i_size);
  474. if (status < 0) {
  475. mlog_errno(status);
  476. goto out;
  477. }
  478. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  479. lqinode->i_size + 2 * sb->s_blocksize);
  480. if (status < 0) {
  481. mlog_errno(status);
  482. goto out;
  483. }
  484. chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  485. if (!chunk) {
  486. status = -ENOMEM;
  487. mlog_errno(status);
  488. goto out;
  489. }
  490. down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  491. status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
  492. &p_blkno, NULL, NULL);
  493. up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  494. if (status < 0) {
  495. mlog_errno(status);
  496. goto out;
  497. }
  498. bh = sb_getblk(sb, p_blkno);
  499. if (!bh) {
  500. status = -ENOMEM;
  501. mlog_errno(status);
  502. goto out;
  503. }
  504. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  505. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  506. if (IS_ERR(handle)) {
  507. status = PTR_ERR(handle);
  508. mlog_errno(status);
  509. goto out;
  510. }
  511. status = ocfs2_journal_access(handle, lqinode, bh,
  512. OCFS2_JOURNAL_ACCESS_WRITE);
  513. if (status < 0) {
  514. mlog_errno(status);
  515. goto out_trans;
  516. }
  517. lock_buffer(bh);
  518. dchunk->dqc_free = ol_quota_entries_per_block(sb);
  519. memset(dchunk->dqc_bitmap, 0,
  520. sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  521. OCFS2_QBLK_RESERVED_SPACE);
  522. set_buffer_uptodate(bh);
  523. unlock_buffer(bh);
  524. status = ocfs2_journal_dirty(handle, bh);
  525. if (status < 0) {
  526. mlog_errno(status);
  527. goto out_trans;
  528. }
  529. oinfo->dqi_blocks += 2;
  530. oinfo->dqi_chunks++;
  531. status = ocfs2_local_write_info(sb, type);
  532. if (status < 0) {
  533. mlog_errno(status);
  534. goto out_trans;
  535. }
  536. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  537. if (status < 0) {
  538. mlog_errno(status);
  539. goto out;
  540. }
  541. list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
  542. chunk->qc_num = list_entry(chunk->qc_chunk.prev,
  543. struct ocfs2_quota_chunk,
  544. qc_chunk)->qc_num + 1;
  545. chunk->qc_headerbh = bh;
  546. *offset = 0;
  547. return chunk;
  548. out_trans:
  549. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  550. out:
  551. brelse(bh);
  552. kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
  553. return ERR_PTR(status);
  554. }
  555. /* Find free entry in local quota file */
  556. static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
  557. struct super_block *sb,
  558. int type,
  559. int *offset)
  560. {
  561. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  562. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  563. struct ocfs2_quota_chunk *chunk;
  564. struct inode *lqinode = sb_dqopt(sb)->files[type];
  565. struct ocfs2_local_disk_chunk *dchunk;
  566. int epb = ol_quota_entries_per_block(sb);
  567. unsigned int chunk_blocks;
  568. int status;
  569. handle_t *handle;
  570. if (list_empty(&oinfo->dqi_chunk))
  571. return ocfs2_local_quota_add_chunk(sb, type, offset);
  572. /* Is the last chunk full? */
  573. chunk = list_entry(oinfo->dqi_chunk.prev,
  574. struct ocfs2_quota_chunk, qc_chunk);
  575. chunk_blocks = oinfo->dqi_blocks -
  576. ol_quota_chunk_block(sb, chunk->qc_num) - 1;
  577. if (ol_chunk_blocks(sb) == chunk_blocks)
  578. return ocfs2_local_quota_add_chunk(sb, type, offset);
  579. /* We are protected by dqio_sem so no locking needed */
  580. status = ocfs2_extend_no_holes(lqinode,
  581. lqinode->i_size + sb->s_blocksize,
  582. lqinode->i_size);
  583. if (status < 0) {
  584. mlog_errno(status);
  585. goto out;
  586. }
  587. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  588. lqinode->i_size + sb->s_blocksize);
  589. if (status < 0) {
  590. mlog_errno(status);
  591. goto out;
  592. }
  593. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  594. if (IS_ERR(handle)) {
  595. status = PTR_ERR(handle);
  596. mlog_errno(status);
  597. goto out;
  598. }
  599. status = ocfs2_journal_access(handle, lqinode, chunk->qc_headerbh,
  600. OCFS2_JOURNAL_ACCESS_WRITE);
  601. if (status < 0) {
  602. mlog_errno(status);
  603. goto out_trans;
  604. }
  605. dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
  606. lock_buffer(chunk->qc_headerbh);
  607. le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
  608. unlock_buffer(chunk->qc_headerbh);
  609. status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
  610. if (status < 0) {
  611. mlog_errno(status);
  612. goto out_trans;
  613. }
  614. oinfo->dqi_blocks++;
  615. status = ocfs2_local_write_info(sb, type);
  616. if (status < 0) {
  617. mlog_errno(status);
  618. goto out_trans;
  619. }
  620. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  621. if (status < 0) {
  622. mlog_errno(status);
  623. goto out;
  624. }
  625. *offset = chunk_blocks * epb;
  626. return chunk;
  627. out_trans:
  628. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  629. out:
  630. return ERR_PTR(status);
  631. }
  632. void olq_alloc_dquot(struct buffer_head *bh, void *private)
  633. {
  634. int *offset = private;
  635. struct ocfs2_local_disk_chunk *dchunk;
  636. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  637. ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
  638. le32_add_cpu(&dchunk->dqc_free, -1);
  639. }
  640. /* Create dquot in the local file for given id */
  641. static int ocfs2_create_local_dquot(struct dquot *dquot)
  642. {
  643. struct super_block *sb = dquot->dq_sb;
  644. int type = dquot->dq_type;
  645. struct inode *lqinode = sb_dqopt(sb)->files[type];
  646. struct ocfs2_quota_chunk *chunk;
  647. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  648. int offset;
  649. int status;
  650. chunk = ocfs2_find_free_entry(sb, type, &offset);
  651. if (!chunk) {
  652. chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
  653. if (IS_ERR(chunk))
  654. return PTR_ERR(chunk);
  655. } else if (IS_ERR(chunk)) {
  656. return PTR_ERR(chunk);
  657. }
  658. od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
  659. od->dq_chunk = chunk;
  660. /* Initialize dquot structure on disk */
  661. status = ocfs2_local_write_dquot(dquot);
  662. if (status < 0) {
  663. mlog_errno(status);
  664. goto out;
  665. }
  666. /* Mark structure as allocated */
  667. status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
  668. &offset);
  669. if (status < 0) {
  670. mlog_errno(status);
  671. goto out;
  672. }
  673. out:
  674. return status;
  675. }
  676. /* Create entry in local file for dquot, load data from the global file */
  677. static int ocfs2_local_read_dquot(struct dquot *dquot)
  678. {
  679. int status;
  680. mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
  681. status = ocfs2_global_read_dquot(dquot);
  682. if (status < 0) {
  683. mlog_errno(status);
  684. goto out_err;
  685. }
  686. /* Now create entry in the local quota file */
  687. status = ocfs2_create_local_dquot(dquot);
  688. if (status < 0) {
  689. mlog_errno(status);
  690. goto out_err;
  691. }
  692. mlog_exit(0);
  693. return 0;
  694. out_err:
  695. mlog_exit(status);
  696. return status;
  697. }
  698. /* Release dquot structure from local quota file. ocfs2_release_dquot() has
  699. * already started a transaction and obtained exclusive lock for global
  700. * quota file. */
  701. static int ocfs2_local_release_dquot(struct dquot *dquot)
  702. {
  703. int status;
  704. int type = dquot->dq_type;
  705. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  706. struct super_block *sb = dquot->dq_sb;
  707. struct ocfs2_local_disk_chunk *dchunk;
  708. int offset;
  709. handle_t *handle = journal_current_handle();
  710. BUG_ON(!handle);
  711. /* First write all local changes to global file */
  712. status = ocfs2_global_release_dquot(dquot);
  713. if (status < 0) {
  714. mlog_errno(status);
  715. goto out;
  716. }
  717. status = ocfs2_journal_access(handle, sb_dqopt(sb)->files[type],
  718. od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
  719. if (status < 0) {
  720. mlog_errno(status);
  721. goto out;
  722. }
  723. offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
  724. od->dq_local_off);
  725. dchunk = (struct ocfs2_local_disk_chunk *)
  726. (od->dq_chunk->qc_headerbh->b_data);
  727. /* Mark structure as freed */
  728. lock_buffer(od->dq_chunk->qc_headerbh);
  729. ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
  730. le32_add_cpu(&dchunk->dqc_free, 1);
  731. unlock_buffer(od->dq_chunk->qc_headerbh);
  732. status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
  733. if (status < 0) {
  734. mlog_errno(status);
  735. goto out;
  736. }
  737. status = 0;
  738. out:
  739. /* Clear the read bit so that next time someone uses this
  740. * dquot he reads fresh info from disk and allocates local
  741. * dquot structure */
  742. clear_bit(DQ_READ_B, &dquot->dq_flags);
  743. return status;
  744. }
  745. static struct quota_format_ops ocfs2_format_ops = {
  746. .check_quota_file = ocfs2_local_check_quota_file,
  747. .read_file_info = ocfs2_local_read_info,
  748. .write_file_info = ocfs2_global_write_info,
  749. .free_file_info = ocfs2_local_free_info,
  750. .read_dqblk = ocfs2_local_read_dquot,
  751. .commit_dqblk = ocfs2_local_write_dquot,
  752. .release_dqblk = ocfs2_local_release_dquot,
  753. };
  754. struct quota_format_type ocfs2_quota_format = {
  755. .qf_fmt_id = QFMT_OCFS2,
  756. .qf_ops = &ocfs2_format_ops,
  757. .qf_owner = THIS_MODULE
  758. };