quota_local.c 35 KB

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