xattr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * xattr.c
  5. *
  6. * Copyright (C) 2008 Oracle. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 021110-1307, USA.
  22. */
  23. #define MLOG_MASK_PREFIX ML_XATTR
  24. #include <cluster/masklog.h>
  25. #include "ocfs2.h"
  26. #include "alloc.h"
  27. #include "dlmglue.h"
  28. #include "file.h"
  29. #include "inode.h"
  30. #include "journal.h"
  31. #include "ocfs2_fs.h"
  32. #include "suballoc.h"
  33. #include "uptodate.h"
  34. #include "buffer_head_io.h"
  35. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  36. u32 clusters_to_add,
  37. struct buffer_head *xattr_bh,
  38. struct ocfs2_xattr_value_root *xv)
  39. {
  40. int status = 0;
  41. int restart_func = 0;
  42. int credits = 0;
  43. handle_t *handle = NULL;
  44. struct ocfs2_alloc_context *data_ac = NULL;
  45. struct ocfs2_alloc_context *meta_ac = NULL;
  46. enum ocfs2_alloc_restarted why;
  47. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  48. struct ocfs2_extent_list *root_el = &xv->xr_list;
  49. u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
  50. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  51. restart_all:
  52. status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
  53. clusters_to_add, 0, &data_ac,
  54. &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
  55. if (status) {
  56. mlog_errno(status);
  57. goto leave;
  58. }
  59. credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
  60. handle = ocfs2_start_trans(osb, credits);
  61. if (IS_ERR(handle)) {
  62. status = PTR_ERR(handle);
  63. handle = NULL;
  64. mlog_errno(status);
  65. goto leave;
  66. }
  67. restarted_transaction:
  68. status = ocfs2_journal_access(handle, inode, xattr_bh,
  69. OCFS2_JOURNAL_ACCESS_WRITE);
  70. if (status < 0) {
  71. mlog_errno(status);
  72. goto leave;
  73. }
  74. prev_clusters = le32_to_cpu(xv->xr_clusters);
  75. status = ocfs2_add_clusters_in_btree(osb,
  76. inode,
  77. &logical_start,
  78. clusters_to_add,
  79. 0,
  80. xattr_bh,
  81. root_el,
  82. handle,
  83. data_ac,
  84. meta_ac,
  85. &why,
  86. OCFS2_XATTR_VALUE_EXTENT,
  87. xv);
  88. if ((status < 0) && (status != -EAGAIN)) {
  89. if (status != -ENOSPC)
  90. mlog_errno(status);
  91. goto leave;
  92. }
  93. status = ocfs2_journal_dirty(handle, xattr_bh);
  94. if (status < 0) {
  95. mlog_errno(status);
  96. goto leave;
  97. }
  98. clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
  99. if (why != RESTART_NONE && clusters_to_add) {
  100. if (why == RESTART_META) {
  101. mlog(0, "restarting function.\n");
  102. restart_func = 1;
  103. } else {
  104. BUG_ON(why != RESTART_TRANS);
  105. mlog(0, "restarting transaction.\n");
  106. /* TODO: This can be more intelligent. */
  107. credits = ocfs2_calc_extend_credits(osb->sb,
  108. root_el,
  109. clusters_to_add);
  110. status = ocfs2_extend_trans(handle, credits);
  111. if (status < 0) {
  112. /* handle still has to be committed at
  113. * this point. */
  114. status = -ENOMEM;
  115. mlog_errno(status);
  116. goto leave;
  117. }
  118. goto restarted_transaction;
  119. }
  120. }
  121. leave:
  122. if (handle) {
  123. ocfs2_commit_trans(osb, handle);
  124. handle = NULL;
  125. }
  126. if (data_ac) {
  127. ocfs2_free_alloc_context(data_ac);
  128. data_ac = NULL;
  129. }
  130. if (meta_ac) {
  131. ocfs2_free_alloc_context(meta_ac);
  132. meta_ac = NULL;
  133. }
  134. if ((!status) && restart_func) {
  135. restart_func = 0;
  136. goto restart_all;
  137. }
  138. return status;
  139. }
  140. static int __ocfs2_remove_xattr_range(struct inode *inode,
  141. struct buffer_head *root_bh,
  142. struct ocfs2_xattr_value_root *xv,
  143. u32 cpos, u32 phys_cpos, u32 len,
  144. struct ocfs2_cached_dealloc_ctxt *dealloc)
  145. {
  146. int ret;
  147. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  148. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  149. struct inode *tl_inode = osb->osb_tl_inode;
  150. handle_t *handle;
  151. struct ocfs2_alloc_context *meta_ac = NULL;
  152. ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
  153. 0, 1, NULL, &meta_ac,
  154. OCFS2_XATTR_VALUE_EXTENT, xv);
  155. if (ret) {
  156. mlog_errno(ret);
  157. return ret;
  158. }
  159. mutex_lock(&tl_inode->i_mutex);
  160. if (ocfs2_truncate_log_needs_flush(osb)) {
  161. ret = __ocfs2_flush_truncate_log(osb);
  162. if (ret < 0) {
  163. mlog_errno(ret);
  164. goto out;
  165. }
  166. }
  167. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  168. if (IS_ERR(handle)) {
  169. ret = PTR_ERR(handle);
  170. mlog_errno(ret);
  171. goto out;
  172. }
  173. ret = ocfs2_journal_access(handle, inode, root_bh,
  174. OCFS2_JOURNAL_ACCESS_WRITE);
  175. if (ret) {
  176. mlog_errno(ret);
  177. goto out_commit;
  178. }
  179. ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
  180. dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
  181. if (ret) {
  182. mlog_errno(ret);
  183. goto out_commit;
  184. }
  185. le32_add_cpu(&xv->xr_clusters, -len);
  186. ret = ocfs2_journal_dirty(handle, root_bh);
  187. if (ret) {
  188. mlog_errno(ret);
  189. goto out_commit;
  190. }
  191. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  192. if (ret)
  193. mlog_errno(ret);
  194. out_commit:
  195. ocfs2_commit_trans(osb, handle);
  196. out:
  197. mutex_unlock(&tl_inode->i_mutex);
  198. if (meta_ac)
  199. ocfs2_free_alloc_context(meta_ac);
  200. return ret;
  201. }
  202. static int ocfs2_xattr_shrink_size(struct inode *inode,
  203. u32 old_clusters,
  204. u32 new_clusters,
  205. struct buffer_head *root_bh,
  206. struct ocfs2_xattr_value_root *xv)
  207. {
  208. int ret = 0;
  209. u32 trunc_len, cpos, phys_cpos, alloc_size;
  210. u64 block;
  211. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  212. struct ocfs2_cached_dealloc_ctxt dealloc;
  213. ocfs2_init_dealloc_ctxt(&dealloc);
  214. if (old_clusters <= new_clusters)
  215. return 0;
  216. cpos = new_clusters;
  217. trunc_len = old_clusters - new_clusters;
  218. while (trunc_len) {
  219. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  220. &alloc_size, &xv->xr_list);
  221. if (ret) {
  222. mlog_errno(ret);
  223. goto out;
  224. }
  225. if (alloc_size > trunc_len)
  226. alloc_size = trunc_len;
  227. ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
  228. phys_cpos, alloc_size,
  229. &dealloc);
  230. if (ret) {
  231. mlog_errno(ret);
  232. goto out;
  233. }
  234. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  235. ocfs2_remove_xattr_clusters_from_cache(inode, block,
  236. alloc_size);
  237. cpos += alloc_size;
  238. trunc_len -= alloc_size;
  239. }
  240. out:
  241. ocfs2_schedule_truncate_log_flush(osb, 1);
  242. ocfs2_run_deallocs(osb, &dealloc);
  243. return ret;
  244. }
  245. static int ocfs2_xattr_value_truncate(struct inode *inode,
  246. struct buffer_head *root_bh,
  247. struct ocfs2_xattr_value_root *xv,
  248. int len)
  249. {
  250. int ret;
  251. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  252. u32 old_clusters = le32_to_cpu(xv->xr_clusters);
  253. if (new_clusters == old_clusters)
  254. return 0;
  255. if (new_clusters > old_clusters)
  256. ret = ocfs2_xattr_extend_allocation(inode,
  257. new_clusters - old_clusters,
  258. root_bh, xv);
  259. else
  260. ret = ocfs2_xattr_shrink_size(inode,
  261. old_clusters, new_clusters,
  262. root_bh, xv);
  263. return ret;
  264. }