quota_local.c 34 KB

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