quota_local.c 36 KB

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