quota_local.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. iput(oinfo->dqi_gqinode);
  333. ocfs2_simple_drop_lockres(OCFS2_SB(sb), &oinfo->dqi_gqlock);
  334. ocfs2_lock_res_free(&oinfo->dqi_gqlock);
  335. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  336. dchunk = (struct ocfs2_local_disk_chunk *)
  337. (chunk->qc_headerbh->b_data);
  338. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  339. len = ol_chunk_entries(sb);
  340. } else {
  341. len = (oinfo->dqi_blocks -
  342. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  343. * ol_quota_entries_per_block(sb);
  344. }
  345. /* Not all entries free? Bug! */
  346. if (le32_to_cpu(dchunk->dqc_free) != len) {
  347. mlog(ML_ERROR, "releasing quota file with used "
  348. "entries (type=%d)\n", type);
  349. mark_clean = 0;
  350. }
  351. }
  352. ocfs2_release_local_quota_bitmaps(&oinfo->dqi_chunk);
  353. if (!mark_clean)
  354. goto out;
  355. /* Mark local file as clean */
  356. info->dqi_flags |= OLQF_CLEAN;
  357. status = ocfs2_modify_bh(sb_dqopt(sb)->files[type],
  358. oinfo->dqi_ibh,
  359. olq_update_info,
  360. info);
  361. if (status < 0) {
  362. mlog_errno(status);
  363. goto out;
  364. }
  365. out:
  366. ocfs2_inode_unlock(sb_dqopt(sb)->files[type], 1);
  367. brelse(oinfo->dqi_ibh);
  368. brelse(oinfo->dqi_lqi_bh);
  369. kfree(oinfo);
  370. return 0;
  371. }
  372. static void olq_set_dquot(struct buffer_head *bh, void *private)
  373. {
  374. struct ocfs2_dquot *od = private;
  375. struct ocfs2_local_disk_dqblk *dqblk;
  376. struct super_block *sb = od->dq_dquot.dq_sb;
  377. dqblk = (struct ocfs2_local_disk_dqblk *)(bh->b_data
  378. + ol_dqblk_block_offset(sb, od->dq_local_off));
  379. dqblk->dqb_id = cpu_to_le64(od->dq_dquot.dq_id);
  380. spin_lock(&dq_data_lock);
  381. dqblk->dqb_spacemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curspace -
  382. od->dq_origspace);
  383. dqblk->dqb_inodemod = cpu_to_le64(od->dq_dquot.dq_dqb.dqb_curinodes -
  384. od->dq_originodes);
  385. spin_unlock(&dq_data_lock);
  386. mlog(0, "Writing local dquot %u space %lld inodes %lld\n",
  387. od->dq_dquot.dq_id, dqblk->dqb_spacemod, dqblk->dqb_inodemod);
  388. }
  389. /* Write dquot to local quota file */
  390. static int ocfs2_local_write_dquot(struct dquot *dquot)
  391. {
  392. struct super_block *sb = dquot->dq_sb;
  393. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  394. struct buffer_head *bh;
  395. int status;
  396. bh = ocfs2_read_quota_block(sb_dqopt(sb)->files[dquot->dq_type],
  397. ol_dqblk_file_block(sb, od->dq_local_off),
  398. &status);
  399. if (!bh) {
  400. mlog_errno(status);
  401. goto out;
  402. }
  403. status = ocfs2_modify_bh(sb_dqopt(sb)->files[dquot->dq_type], bh,
  404. olq_set_dquot, od);
  405. if (status < 0) {
  406. mlog_errno(status);
  407. goto out;
  408. }
  409. out:
  410. brelse(bh);
  411. return status;
  412. }
  413. /* Find free entry in local quota file */
  414. static struct ocfs2_quota_chunk *ocfs2_find_free_entry(struct super_block *sb,
  415. int type,
  416. int *offset)
  417. {
  418. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  419. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  420. struct ocfs2_quota_chunk *chunk;
  421. struct ocfs2_local_disk_chunk *dchunk;
  422. int found = 0, len;
  423. list_for_each_entry(chunk, &oinfo->dqi_chunk, qc_chunk) {
  424. dchunk = (struct ocfs2_local_disk_chunk *)
  425. chunk->qc_headerbh->b_data;
  426. if (le32_to_cpu(dchunk->dqc_free) > 0) {
  427. found = 1;
  428. break;
  429. }
  430. }
  431. if (!found)
  432. return NULL;
  433. if (chunk->qc_num < oinfo->dqi_chunks - 1) {
  434. len = ol_chunk_entries(sb);
  435. } else {
  436. len = (oinfo->dqi_blocks -
  437. ol_quota_chunk_block(sb, chunk->qc_num) - 1)
  438. * ol_quota_entries_per_block(sb);
  439. }
  440. found = ocfs2_find_next_zero_bit(dchunk->dqc_bitmap, len, 0);
  441. /* We failed? */
  442. if (found == len) {
  443. mlog(ML_ERROR, "Did not find empty entry in chunk %d with %u"
  444. " entries free (type=%d)\n", chunk->qc_num,
  445. le32_to_cpu(dchunk->dqc_free), type);
  446. return ERR_PTR(-EIO);
  447. }
  448. *offset = found;
  449. return chunk;
  450. }
  451. /* Add new chunk to the local quota file */
  452. static struct ocfs2_quota_chunk *ocfs2_local_quota_add_chunk(
  453. struct super_block *sb,
  454. int type,
  455. int *offset)
  456. {
  457. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  458. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  459. struct inode *lqinode = sb_dqopt(sb)->files[type];
  460. struct ocfs2_quota_chunk *chunk = NULL;
  461. struct ocfs2_local_disk_chunk *dchunk;
  462. int status;
  463. handle_t *handle;
  464. struct buffer_head *bh = NULL;
  465. u64 p_blkno;
  466. /* We are protected by dqio_sem so no locking needed */
  467. status = ocfs2_extend_no_holes(lqinode,
  468. lqinode->i_size + 2 * sb->s_blocksize,
  469. lqinode->i_size);
  470. if (status < 0) {
  471. mlog_errno(status);
  472. goto out;
  473. }
  474. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  475. lqinode->i_size + 2 * sb->s_blocksize);
  476. if (status < 0) {
  477. mlog_errno(status);
  478. goto out;
  479. }
  480. chunk = kmem_cache_alloc(ocfs2_qf_chunk_cachep, GFP_NOFS);
  481. if (!chunk) {
  482. status = -ENOMEM;
  483. mlog_errno(status);
  484. goto out;
  485. }
  486. down_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  487. status = ocfs2_extent_map_get_blocks(lqinode, oinfo->dqi_blocks,
  488. &p_blkno, NULL, NULL);
  489. up_read(&OCFS2_I(lqinode)->ip_alloc_sem);
  490. if (status < 0) {
  491. mlog_errno(status);
  492. goto out;
  493. }
  494. bh = sb_getblk(sb, p_blkno);
  495. if (!bh) {
  496. status = -ENOMEM;
  497. mlog_errno(status);
  498. goto out;
  499. }
  500. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  501. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  502. if (IS_ERR(handle)) {
  503. status = PTR_ERR(handle);
  504. mlog_errno(status);
  505. goto out;
  506. }
  507. status = ocfs2_journal_access(handle, lqinode, bh,
  508. OCFS2_JOURNAL_ACCESS_WRITE);
  509. if (status < 0) {
  510. mlog_errno(status);
  511. goto out_trans;
  512. }
  513. lock_buffer(bh);
  514. dchunk->dqc_free = ol_quota_entries_per_block(sb);
  515. memset(dchunk->dqc_bitmap, 0,
  516. sb->s_blocksize - sizeof(struct ocfs2_local_disk_chunk) -
  517. OCFS2_QBLK_RESERVED_SPACE);
  518. set_buffer_uptodate(bh);
  519. unlock_buffer(bh);
  520. status = ocfs2_journal_dirty(handle, bh);
  521. if (status < 0) {
  522. mlog_errno(status);
  523. goto out_trans;
  524. }
  525. oinfo->dqi_blocks += 2;
  526. oinfo->dqi_chunks++;
  527. status = ocfs2_local_write_info(sb, type);
  528. if (status < 0) {
  529. mlog_errno(status);
  530. goto out_trans;
  531. }
  532. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  533. if (status < 0) {
  534. mlog_errno(status);
  535. goto out;
  536. }
  537. list_add_tail(&chunk->qc_chunk, &oinfo->dqi_chunk);
  538. chunk->qc_num = list_entry(chunk->qc_chunk.prev,
  539. struct ocfs2_quota_chunk,
  540. qc_chunk)->qc_num + 1;
  541. chunk->qc_headerbh = bh;
  542. *offset = 0;
  543. return chunk;
  544. out_trans:
  545. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  546. out:
  547. brelse(bh);
  548. kmem_cache_free(ocfs2_qf_chunk_cachep, chunk);
  549. return ERR_PTR(status);
  550. }
  551. /* Find free entry in local quota file */
  552. static struct ocfs2_quota_chunk *ocfs2_extend_local_quota_file(
  553. struct super_block *sb,
  554. int type,
  555. int *offset)
  556. {
  557. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  558. struct ocfs2_mem_dqinfo *oinfo = info->dqi_priv;
  559. struct ocfs2_quota_chunk *chunk;
  560. struct inode *lqinode = sb_dqopt(sb)->files[type];
  561. struct ocfs2_local_disk_chunk *dchunk;
  562. int epb = ol_quota_entries_per_block(sb);
  563. unsigned int chunk_blocks;
  564. int status;
  565. handle_t *handle;
  566. if (list_empty(&oinfo->dqi_chunk))
  567. return ocfs2_local_quota_add_chunk(sb, type, offset);
  568. /* Is the last chunk full? */
  569. chunk = list_entry(oinfo->dqi_chunk.prev,
  570. struct ocfs2_quota_chunk, qc_chunk);
  571. chunk_blocks = oinfo->dqi_blocks -
  572. ol_quota_chunk_block(sb, chunk->qc_num) - 1;
  573. if (ol_chunk_blocks(sb) == chunk_blocks)
  574. return ocfs2_local_quota_add_chunk(sb, type, offset);
  575. /* We are protected by dqio_sem so no locking needed */
  576. status = ocfs2_extend_no_holes(lqinode,
  577. lqinode->i_size + sb->s_blocksize,
  578. lqinode->i_size);
  579. if (status < 0) {
  580. mlog_errno(status);
  581. goto out;
  582. }
  583. status = ocfs2_simple_size_update(lqinode, oinfo->dqi_lqi_bh,
  584. lqinode->i_size + sb->s_blocksize);
  585. if (status < 0) {
  586. mlog_errno(status);
  587. goto out;
  588. }
  589. handle = ocfs2_start_trans(OCFS2_SB(sb), 2);
  590. if (IS_ERR(handle)) {
  591. status = PTR_ERR(handle);
  592. mlog_errno(status);
  593. goto out;
  594. }
  595. status = ocfs2_journal_access(handle, lqinode, chunk->qc_headerbh,
  596. OCFS2_JOURNAL_ACCESS_WRITE);
  597. if (status < 0) {
  598. mlog_errno(status);
  599. goto out_trans;
  600. }
  601. dchunk = (struct ocfs2_local_disk_chunk *)chunk->qc_headerbh->b_data;
  602. lock_buffer(chunk->qc_headerbh);
  603. le32_add_cpu(&dchunk->dqc_free, ol_quota_entries_per_block(sb));
  604. unlock_buffer(chunk->qc_headerbh);
  605. status = ocfs2_journal_dirty(handle, chunk->qc_headerbh);
  606. if (status < 0) {
  607. mlog_errno(status);
  608. goto out_trans;
  609. }
  610. oinfo->dqi_blocks++;
  611. status = ocfs2_local_write_info(sb, type);
  612. if (status < 0) {
  613. mlog_errno(status);
  614. goto out_trans;
  615. }
  616. status = ocfs2_commit_trans(OCFS2_SB(sb), handle);
  617. if (status < 0) {
  618. mlog_errno(status);
  619. goto out;
  620. }
  621. *offset = chunk_blocks * epb;
  622. return chunk;
  623. out_trans:
  624. ocfs2_commit_trans(OCFS2_SB(sb), handle);
  625. out:
  626. return ERR_PTR(status);
  627. }
  628. void olq_alloc_dquot(struct buffer_head *bh, void *private)
  629. {
  630. int *offset = private;
  631. struct ocfs2_local_disk_chunk *dchunk;
  632. dchunk = (struct ocfs2_local_disk_chunk *)bh->b_data;
  633. ocfs2_set_bit(*offset, dchunk->dqc_bitmap);
  634. le32_add_cpu(&dchunk->dqc_free, -1);
  635. }
  636. /* Create dquot in the local file for given id */
  637. static int ocfs2_create_local_dquot(struct dquot *dquot)
  638. {
  639. struct super_block *sb = dquot->dq_sb;
  640. int type = dquot->dq_type;
  641. struct inode *lqinode = sb_dqopt(sb)->files[type];
  642. struct ocfs2_quota_chunk *chunk;
  643. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  644. int offset;
  645. int status;
  646. chunk = ocfs2_find_free_entry(sb, type, &offset);
  647. if (!chunk) {
  648. chunk = ocfs2_extend_local_quota_file(sb, type, &offset);
  649. if (IS_ERR(chunk))
  650. return PTR_ERR(chunk);
  651. } else if (IS_ERR(chunk)) {
  652. return PTR_ERR(chunk);
  653. }
  654. od->dq_local_off = ol_dqblk_off(sb, chunk->qc_num, offset);
  655. od->dq_chunk = chunk;
  656. /* Initialize dquot structure on disk */
  657. status = ocfs2_local_write_dquot(dquot);
  658. if (status < 0) {
  659. mlog_errno(status);
  660. goto out;
  661. }
  662. /* Mark structure as allocated */
  663. status = ocfs2_modify_bh(lqinode, chunk->qc_headerbh, olq_alloc_dquot,
  664. &offset);
  665. if (status < 0) {
  666. mlog_errno(status);
  667. goto out;
  668. }
  669. out:
  670. return status;
  671. }
  672. /* Create entry in local file for dquot, load data from the global file */
  673. static int ocfs2_local_read_dquot(struct dquot *dquot)
  674. {
  675. int status;
  676. mlog_entry("id=%u, type=%d\n", dquot->dq_id, dquot->dq_type);
  677. status = ocfs2_global_read_dquot(dquot);
  678. if (status < 0) {
  679. mlog_errno(status);
  680. goto out_err;
  681. }
  682. /* Now create entry in the local quota file */
  683. status = ocfs2_create_local_dquot(dquot);
  684. if (status < 0) {
  685. mlog_errno(status);
  686. goto out_err;
  687. }
  688. mlog_exit(0);
  689. return 0;
  690. out_err:
  691. mlog_exit(status);
  692. return status;
  693. }
  694. /* Release dquot structure from local quota file. ocfs2_release_dquot() has
  695. * already started a transaction and obtained exclusive lock for global
  696. * quota file. */
  697. static int ocfs2_local_release_dquot(struct dquot *dquot)
  698. {
  699. int status;
  700. int type = dquot->dq_type;
  701. struct ocfs2_dquot *od = OCFS2_DQUOT(dquot);
  702. struct super_block *sb = dquot->dq_sb;
  703. struct ocfs2_local_disk_chunk *dchunk;
  704. int offset;
  705. handle_t *handle = journal_current_handle();
  706. BUG_ON(!handle);
  707. /* First write all local changes to global file */
  708. status = ocfs2_global_release_dquot(dquot);
  709. if (status < 0) {
  710. mlog_errno(status);
  711. goto out;
  712. }
  713. status = ocfs2_journal_access(handle, sb_dqopt(sb)->files[type],
  714. od->dq_chunk->qc_headerbh, OCFS2_JOURNAL_ACCESS_WRITE);
  715. if (status < 0) {
  716. mlog_errno(status);
  717. goto out;
  718. }
  719. offset = ol_dqblk_chunk_off(sb, od->dq_chunk->qc_num,
  720. od->dq_local_off);
  721. dchunk = (struct ocfs2_local_disk_chunk *)
  722. (od->dq_chunk->qc_headerbh->b_data);
  723. /* Mark structure as freed */
  724. lock_buffer(od->dq_chunk->qc_headerbh);
  725. ocfs2_clear_bit(offset, dchunk->dqc_bitmap);
  726. le32_add_cpu(&dchunk->dqc_free, 1);
  727. unlock_buffer(od->dq_chunk->qc_headerbh);
  728. status = ocfs2_journal_dirty(handle, od->dq_chunk->qc_headerbh);
  729. if (status < 0) {
  730. mlog_errno(status);
  731. goto out;
  732. }
  733. status = 0;
  734. out:
  735. /* Clear the read bit so that next time someone uses this
  736. * dquot he reads fresh info from disk and allocates local
  737. * dquot structure */
  738. clear_bit(DQ_READ_B, &dquot->dq_flags);
  739. return status;
  740. }
  741. static struct quota_format_ops ocfs2_format_ops = {
  742. .check_quota_file = ocfs2_local_check_quota_file,
  743. .read_file_info = ocfs2_local_read_info,
  744. .write_file_info = ocfs2_global_write_info,
  745. .free_file_info = ocfs2_local_free_info,
  746. .read_dqblk = ocfs2_local_read_dquot,
  747. .commit_dqblk = ocfs2_local_write_dquot,
  748. .release_dqblk = ocfs2_local_release_dquot,
  749. };
  750. struct quota_format_type ocfs2_quota_format = {
  751. .qf_fmt_id = QFMT_OCFS2,
  752. .qf_ops = &ocfs2_format_ops,
  753. .qf_owner = THIS_MODULE
  754. };