quota_local.c 36 KB

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