quota_local.c 36 KB

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