quota_local.c 36 KB

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