quota_local.c 36 KB

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