quota_local.c 36 KB

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