quota_local.c 36 KB

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