xattr.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  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. * CREDITS:
  9. * Lots of code in this file is taken from ext3.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/capability.h>
  27. #include <linux/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/uio.h>
  33. #include <linux/sched.h>
  34. #include <linux/splice.h>
  35. #include <linux/mount.h>
  36. #include <linux/writeback.h>
  37. #include <linux/falloc.h>
  38. #define MLOG_MASK_PREFIX ML_XATTR
  39. #include <cluster/masklog.h>
  40. #include "ocfs2.h"
  41. #include "alloc.h"
  42. #include "dlmglue.h"
  43. #include "file.h"
  44. #include "symlink.h"
  45. #include "sysfile.h"
  46. #include "inode.h"
  47. #include "journal.h"
  48. #include "ocfs2_fs.h"
  49. #include "suballoc.h"
  50. #include "uptodate.h"
  51. #include "buffer_head_io.h"
  52. #include "super.h"
  53. #include "xattr.h"
  54. struct ocfs2_xattr_def_value_root {
  55. struct ocfs2_xattr_value_root xv;
  56. struct ocfs2_extent_rec er;
  57. };
  58. struct ocfs2_xattr_bucket {
  59. struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  60. struct ocfs2_xattr_header *xh;
  61. };
  62. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  63. #define OCFS2_XATTR_INLINE_SIZE 80
  64. static struct ocfs2_xattr_def_value_root def_xv = {
  65. .xv.xr_list.l_count = cpu_to_le16(1),
  66. };
  67. struct xattr_handler *ocfs2_xattr_handlers[] = {
  68. &ocfs2_xattr_user_handler,
  69. &ocfs2_xattr_trusted_handler,
  70. NULL
  71. };
  72. static struct xattr_handler *ocfs2_xattr_handler_map[] = {
  73. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  74. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  75. };
  76. struct ocfs2_xattr_info {
  77. int name_index;
  78. const char *name;
  79. const void *value;
  80. size_t value_len;
  81. };
  82. struct ocfs2_xattr_search {
  83. struct buffer_head *inode_bh;
  84. /*
  85. * xattr_bh point to the block buffer head which has extended attribute
  86. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  87. */
  88. struct buffer_head *xattr_bh;
  89. struct ocfs2_xattr_header *header;
  90. struct ocfs2_xattr_bucket bucket;
  91. void *base;
  92. void *end;
  93. struct ocfs2_xattr_entry *here;
  94. int not_found;
  95. };
  96. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  97. struct ocfs2_xattr_header *xh,
  98. int index,
  99. int *block_off,
  100. int *new_offset);
  101. static int ocfs2_xattr_index_block_find(struct inode *inode,
  102. struct buffer_head *root_bh,
  103. int name_index,
  104. const char *name,
  105. struct ocfs2_xattr_search *xs);
  106. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  107. struct ocfs2_xattr_tree_root *xt,
  108. char *buffer,
  109. size_t buffer_size);
  110. static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
  111. {
  112. struct xattr_handler *handler = NULL;
  113. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  114. handler = ocfs2_xattr_handler_map[name_index];
  115. return handler;
  116. }
  117. static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
  118. char *prefix,
  119. int prefix_len,
  120. char *name,
  121. int name_len)
  122. {
  123. /* Get hash value of uuid from super block */
  124. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  125. int i;
  126. /* hash extended attribute prefix */
  127. for (i = 0; i < prefix_len; i++) {
  128. hash = (hash << OCFS2_HASH_SHIFT) ^
  129. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  130. *prefix++;
  131. }
  132. /* hash extended attribute name */
  133. for (i = 0; i < name_len; i++) {
  134. hash = (hash << OCFS2_HASH_SHIFT) ^
  135. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  136. *name++;
  137. }
  138. return hash;
  139. }
  140. /*
  141. * ocfs2_xattr_hash_entry()
  142. *
  143. * Compute the hash of an extended attribute.
  144. */
  145. static void ocfs2_xattr_hash_entry(struct inode *inode,
  146. struct ocfs2_xattr_header *header,
  147. struct ocfs2_xattr_entry *entry)
  148. {
  149. u32 hash = 0;
  150. struct xattr_handler *handler =
  151. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  152. char *prefix = handler->prefix;
  153. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  154. int prefix_len = strlen(handler->prefix);
  155. hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
  156. entry->xe_name_len);
  157. entry->xe_name_hash = cpu_to_le32(hash);
  158. return;
  159. }
  160. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  161. u32 clusters_to_add,
  162. struct buffer_head *xattr_bh,
  163. struct ocfs2_xattr_value_root *xv)
  164. {
  165. int status = 0;
  166. int restart_func = 0;
  167. int credits = 0;
  168. handle_t *handle = NULL;
  169. struct ocfs2_alloc_context *data_ac = NULL;
  170. struct ocfs2_alloc_context *meta_ac = NULL;
  171. enum ocfs2_alloc_restarted why;
  172. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  173. struct ocfs2_extent_list *root_el = &xv->xr_list;
  174. u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
  175. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  176. restart_all:
  177. status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
  178. clusters_to_add, 0, &data_ac,
  179. &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
  180. if (status) {
  181. mlog_errno(status);
  182. goto leave;
  183. }
  184. credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
  185. handle = ocfs2_start_trans(osb, credits);
  186. if (IS_ERR(handle)) {
  187. status = PTR_ERR(handle);
  188. handle = NULL;
  189. mlog_errno(status);
  190. goto leave;
  191. }
  192. restarted_transaction:
  193. status = ocfs2_journal_access(handle, inode, xattr_bh,
  194. OCFS2_JOURNAL_ACCESS_WRITE);
  195. if (status < 0) {
  196. mlog_errno(status);
  197. goto leave;
  198. }
  199. prev_clusters = le32_to_cpu(xv->xr_clusters);
  200. status = ocfs2_add_clusters_in_btree(osb,
  201. inode,
  202. &logical_start,
  203. clusters_to_add,
  204. 0,
  205. xattr_bh,
  206. root_el,
  207. handle,
  208. data_ac,
  209. meta_ac,
  210. &why,
  211. OCFS2_XATTR_VALUE_EXTENT,
  212. xv);
  213. if ((status < 0) && (status != -EAGAIN)) {
  214. if (status != -ENOSPC)
  215. mlog_errno(status);
  216. goto leave;
  217. }
  218. status = ocfs2_journal_dirty(handle, xattr_bh);
  219. if (status < 0) {
  220. mlog_errno(status);
  221. goto leave;
  222. }
  223. clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
  224. if (why != RESTART_NONE && clusters_to_add) {
  225. if (why == RESTART_META) {
  226. mlog(0, "restarting function.\n");
  227. restart_func = 1;
  228. } else {
  229. BUG_ON(why != RESTART_TRANS);
  230. mlog(0, "restarting transaction.\n");
  231. /* TODO: This can be more intelligent. */
  232. credits = ocfs2_calc_extend_credits(osb->sb,
  233. root_el,
  234. clusters_to_add);
  235. status = ocfs2_extend_trans(handle, credits);
  236. if (status < 0) {
  237. /* handle still has to be committed at
  238. * this point. */
  239. status = -ENOMEM;
  240. mlog_errno(status);
  241. goto leave;
  242. }
  243. goto restarted_transaction;
  244. }
  245. }
  246. leave:
  247. if (handle) {
  248. ocfs2_commit_trans(osb, handle);
  249. handle = NULL;
  250. }
  251. if (data_ac) {
  252. ocfs2_free_alloc_context(data_ac);
  253. data_ac = NULL;
  254. }
  255. if (meta_ac) {
  256. ocfs2_free_alloc_context(meta_ac);
  257. meta_ac = NULL;
  258. }
  259. if ((!status) && restart_func) {
  260. restart_func = 0;
  261. goto restart_all;
  262. }
  263. return status;
  264. }
  265. static int __ocfs2_remove_xattr_range(struct inode *inode,
  266. struct buffer_head *root_bh,
  267. struct ocfs2_xattr_value_root *xv,
  268. u32 cpos, u32 phys_cpos, u32 len,
  269. struct ocfs2_cached_dealloc_ctxt *dealloc)
  270. {
  271. int ret;
  272. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  273. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  274. struct inode *tl_inode = osb->osb_tl_inode;
  275. handle_t *handle;
  276. struct ocfs2_alloc_context *meta_ac = NULL;
  277. ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
  278. 0, 1, NULL, &meta_ac,
  279. OCFS2_XATTR_VALUE_EXTENT, xv);
  280. if (ret) {
  281. mlog_errno(ret);
  282. return ret;
  283. }
  284. mutex_lock(&tl_inode->i_mutex);
  285. if (ocfs2_truncate_log_needs_flush(osb)) {
  286. ret = __ocfs2_flush_truncate_log(osb);
  287. if (ret < 0) {
  288. mlog_errno(ret);
  289. goto out;
  290. }
  291. }
  292. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  293. if (IS_ERR(handle)) {
  294. ret = PTR_ERR(handle);
  295. mlog_errno(ret);
  296. goto out;
  297. }
  298. ret = ocfs2_journal_access(handle, inode, root_bh,
  299. OCFS2_JOURNAL_ACCESS_WRITE);
  300. if (ret) {
  301. mlog_errno(ret);
  302. goto out_commit;
  303. }
  304. ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
  305. dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
  306. if (ret) {
  307. mlog_errno(ret);
  308. goto out_commit;
  309. }
  310. le32_add_cpu(&xv->xr_clusters, -len);
  311. ret = ocfs2_journal_dirty(handle, root_bh);
  312. if (ret) {
  313. mlog_errno(ret);
  314. goto out_commit;
  315. }
  316. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  317. if (ret)
  318. mlog_errno(ret);
  319. out_commit:
  320. ocfs2_commit_trans(osb, handle);
  321. out:
  322. mutex_unlock(&tl_inode->i_mutex);
  323. if (meta_ac)
  324. ocfs2_free_alloc_context(meta_ac);
  325. return ret;
  326. }
  327. static int ocfs2_xattr_shrink_size(struct inode *inode,
  328. u32 old_clusters,
  329. u32 new_clusters,
  330. struct buffer_head *root_bh,
  331. struct ocfs2_xattr_value_root *xv)
  332. {
  333. int ret = 0;
  334. u32 trunc_len, cpos, phys_cpos, alloc_size;
  335. u64 block;
  336. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  337. struct ocfs2_cached_dealloc_ctxt dealloc;
  338. ocfs2_init_dealloc_ctxt(&dealloc);
  339. if (old_clusters <= new_clusters)
  340. return 0;
  341. cpos = new_clusters;
  342. trunc_len = old_clusters - new_clusters;
  343. while (trunc_len) {
  344. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  345. &alloc_size, &xv->xr_list);
  346. if (ret) {
  347. mlog_errno(ret);
  348. goto out;
  349. }
  350. if (alloc_size > trunc_len)
  351. alloc_size = trunc_len;
  352. ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
  353. phys_cpos, alloc_size,
  354. &dealloc);
  355. if (ret) {
  356. mlog_errno(ret);
  357. goto out;
  358. }
  359. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  360. ocfs2_remove_xattr_clusters_from_cache(inode, block,
  361. alloc_size);
  362. cpos += alloc_size;
  363. trunc_len -= alloc_size;
  364. }
  365. out:
  366. ocfs2_schedule_truncate_log_flush(osb, 1);
  367. ocfs2_run_deallocs(osb, &dealloc);
  368. return ret;
  369. }
  370. static int ocfs2_xattr_value_truncate(struct inode *inode,
  371. struct buffer_head *root_bh,
  372. struct ocfs2_xattr_value_root *xv,
  373. int len)
  374. {
  375. int ret;
  376. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  377. u32 old_clusters = le32_to_cpu(xv->xr_clusters);
  378. if (new_clusters == old_clusters)
  379. return 0;
  380. if (new_clusters > old_clusters)
  381. ret = ocfs2_xattr_extend_allocation(inode,
  382. new_clusters - old_clusters,
  383. root_bh, xv);
  384. else
  385. ret = ocfs2_xattr_shrink_size(inode,
  386. old_clusters, new_clusters,
  387. root_bh, xv);
  388. return ret;
  389. }
  390. static int ocfs2_xattr_list_entries(struct inode *inode,
  391. struct ocfs2_xattr_header *header,
  392. char *buffer, size_t buffer_size)
  393. {
  394. size_t rest = buffer_size;
  395. int i;
  396. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  397. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  398. struct xattr_handler *handler =
  399. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  400. if (handler) {
  401. size_t size = handler->list(inode, buffer, rest,
  402. ((char *)header +
  403. le16_to_cpu(entry->xe_name_offset)),
  404. entry->xe_name_len);
  405. if (buffer) {
  406. if (size > rest)
  407. return -ERANGE;
  408. buffer += size;
  409. }
  410. rest -= size;
  411. }
  412. }
  413. return buffer_size - rest;
  414. }
  415. static int ocfs2_xattr_ibody_list(struct inode *inode,
  416. struct ocfs2_dinode *di,
  417. char *buffer,
  418. size_t buffer_size)
  419. {
  420. struct ocfs2_xattr_header *header = NULL;
  421. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  422. int ret = 0;
  423. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  424. return ret;
  425. header = (struct ocfs2_xattr_header *)
  426. ((void *)di + inode->i_sb->s_blocksize -
  427. le16_to_cpu(di->i_xattr_inline_size));
  428. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  429. return ret;
  430. }
  431. static int ocfs2_xattr_block_list(struct inode *inode,
  432. struct ocfs2_dinode *di,
  433. char *buffer,
  434. size_t buffer_size)
  435. {
  436. struct buffer_head *blk_bh = NULL;
  437. struct ocfs2_xattr_block *xb;
  438. int ret = 0;
  439. if (!di->i_xattr_loc)
  440. return ret;
  441. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  442. le64_to_cpu(di->i_xattr_loc),
  443. &blk_bh, OCFS2_BH_CACHED, inode);
  444. if (ret < 0) {
  445. mlog_errno(ret);
  446. return ret;
  447. }
  448. /*Verify the signature of xattr block*/
  449. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  450. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  451. ret = -EFAULT;
  452. goto cleanup;
  453. }
  454. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  455. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  456. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  457. ret = ocfs2_xattr_list_entries(inode, header,
  458. buffer, buffer_size);
  459. } else {
  460. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  461. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  462. buffer, buffer_size);
  463. }
  464. cleanup:
  465. brelse(blk_bh);
  466. return ret;
  467. }
  468. ssize_t ocfs2_listxattr(struct dentry *dentry,
  469. char *buffer,
  470. size_t size)
  471. {
  472. int ret = 0, i_ret = 0, b_ret = 0;
  473. struct buffer_head *di_bh = NULL;
  474. struct ocfs2_dinode *di = NULL;
  475. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  476. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  477. return ret;
  478. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  479. if (ret < 0) {
  480. mlog_errno(ret);
  481. return ret;
  482. }
  483. di = (struct ocfs2_dinode *)di_bh->b_data;
  484. down_read(&oi->ip_xattr_sem);
  485. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  486. if (i_ret < 0)
  487. b_ret = 0;
  488. else {
  489. if (buffer) {
  490. buffer += i_ret;
  491. size -= i_ret;
  492. }
  493. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  494. buffer, size);
  495. if (b_ret < 0)
  496. i_ret = 0;
  497. }
  498. up_read(&oi->ip_xattr_sem);
  499. ocfs2_inode_unlock(dentry->d_inode, 0);
  500. brelse(di_bh);
  501. return i_ret + b_ret;
  502. }
  503. static int ocfs2_xattr_find_entry(int name_index,
  504. const char *name,
  505. struct ocfs2_xattr_search *xs)
  506. {
  507. struct ocfs2_xattr_entry *entry;
  508. size_t name_len;
  509. int i, cmp = 1;
  510. if (name == NULL)
  511. return -EINVAL;
  512. name_len = strlen(name);
  513. entry = xs->here;
  514. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  515. cmp = name_index - ocfs2_xattr_get_type(entry);
  516. if (!cmp)
  517. cmp = name_len - entry->xe_name_len;
  518. if (!cmp)
  519. cmp = memcmp(name, (xs->base +
  520. le16_to_cpu(entry->xe_name_offset)),
  521. name_len);
  522. if (cmp == 0)
  523. break;
  524. entry += 1;
  525. }
  526. xs->here = entry;
  527. return cmp ? -ENODATA : 0;
  528. }
  529. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  530. struct ocfs2_xattr_value_root *xv,
  531. void *buffer,
  532. size_t len)
  533. {
  534. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  535. u64 blkno;
  536. int i, ret = 0;
  537. size_t cplen, blocksize;
  538. struct buffer_head *bh = NULL;
  539. struct ocfs2_extent_list *el;
  540. el = &xv->xr_list;
  541. clusters = le32_to_cpu(xv->xr_clusters);
  542. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  543. blocksize = inode->i_sb->s_blocksize;
  544. cpos = 0;
  545. while (cpos < clusters) {
  546. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  547. &num_clusters, el);
  548. if (ret) {
  549. mlog_errno(ret);
  550. goto out;
  551. }
  552. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  553. /* Copy ocfs2_xattr_value */
  554. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  555. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  556. &bh, OCFS2_BH_CACHED, inode);
  557. if (ret) {
  558. mlog_errno(ret);
  559. goto out;
  560. }
  561. cplen = len >= blocksize ? blocksize : len;
  562. memcpy(buffer, bh->b_data, cplen);
  563. len -= cplen;
  564. buffer += cplen;
  565. brelse(bh);
  566. bh = NULL;
  567. if (len == 0)
  568. break;
  569. }
  570. cpos += num_clusters;
  571. }
  572. out:
  573. return ret;
  574. }
  575. static int ocfs2_xattr_ibody_get(struct inode *inode,
  576. int name_index,
  577. const char *name,
  578. void *buffer,
  579. size_t buffer_size,
  580. struct ocfs2_xattr_search *xs)
  581. {
  582. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  583. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  584. struct ocfs2_xattr_value_root *xv;
  585. size_t size;
  586. int ret = 0;
  587. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  588. return -ENODATA;
  589. xs->end = (void *)di + inode->i_sb->s_blocksize;
  590. xs->header = (struct ocfs2_xattr_header *)
  591. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  592. xs->base = (void *)xs->header;
  593. xs->here = xs->header->xh_entries;
  594. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  595. if (ret)
  596. return ret;
  597. size = le64_to_cpu(xs->here->xe_value_size);
  598. if (buffer) {
  599. if (size > buffer_size)
  600. return -ERANGE;
  601. if (ocfs2_xattr_is_local(xs->here)) {
  602. memcpy(buffer, (void *)xs->base +
  603. le16_to_cpu(xs->here->xe_name_offset) +
  604. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  605. } else {
  606. xv = (struct ocfs2_xattr_value_root *)
  607. (xs->base + le16_to_cpu(
  608. xs->here->xe_name_offset) +
  609. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  610. ret = ocfs2_xattr_get_value_outside(inode, xv,
  611. buffer, size);
  612. if (ret < 0) {
  613. mlog_errno(ret);
  614. return ret;
  615. }
  616. }
  617. }
  618. return size;
  619. }
  620. static int ocfs2_xattr_block_get(struct inode *inode,
  621. int name_index,
  622. const char *name,
  623. void *buffer,
  624. size_t buffer_size,
  625. struct ocfs2_xattr_search *xs)
  626. {
  627. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  628. struct buffer_head *blk_bh = NULL;
  629. struct ocfs2_xattr_block *xb;
  630. struct ocfs2_xattr_value_root *xv;
  631. size_t size;
  632. int ret = -ENODATA, name_offset, name_len, block_off, i;
  633. if (!di->i_xattr_loc)
  634. return ret;
  635. memset(&xs->bucket, 0, sizeof(xs->bucket));
  636. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  637. le64_to_cpu(di->i_xattr_loc),
  638. &blk_bh, OCFS2_BH_CACHED, inode);
  639. if (ret < 0) {
  640. mlog_errno(ret);
  641. return ret;
  642. }
  643. /*Verify the signature of xattr block*/
  644. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  645. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  646. ret = -EFAULT;
  647. goto cleanup;
  648. }
  649. xs->xattr_bh = blk_bh;
  650. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  651. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  652. xs->header = &xb->xb_attrs.xb_header;
  653. xs->base = (void *)xs->header;
  654. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  655. xs->here = xs->header->xh_entries;
  656. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  657. } else
  658. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  659. name_index,
  660. name, xs);
  661. if (ret)
  662. goto cleanup;
  663. size = le64_to_cpu(xs->here->xe_value_size);
  664. if (buffer) {
  665. ret = -ERANGE;
  666. if (size > buffer_size)
  667. goto cleanup;
  668. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  669. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  670. i = xs->here - xs->header->xh_entries;
  671. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  672. ret = ocfs2_xattr_bucket_get_name_value(inode,
  673. xs->bucket.xh,
  674. i,
  675. &block_off,
  676. &name_offset);
  677. xs->base = xs->bucket.bhs[block_off]->b_data;
  678. }
  679. if (ocfs2_xattr_is_local(xs->here)) {
  680. memcpy(buffer, (void *)xs->base +
  681. name_offset + name_len, size);
  682. } else {
  683. xv = (struct ocfs2_xattr_value_root *)
  684. (xs->base + name_offset + name_len);
  685. ret = ocfs2_xattr_get_value_outside(inode, xv,
  686. buffer, size);
  687. if (ret < 0) {
  688. mlog_errno(ret);
  689. goto cleanup;
  690. }
  691. }
  692. }
  693. ret = size;
  694. cleanup:
  695. for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
  696. brelse(xs->bucket.bhs[i]);
  697. memset(&xs->bucket, 0, sizeof(xs->bucket));
  698. brelse(blk_bh);
  699. return ret;
  700. }
  701. /* ocfs2_xattr_get()
  702. *
  703. * Copy an extended attribute into the buffer provided.
  704. * Buffer is NULL to compute the size of buffer required.
  705. */
  706. int ocfs2_xattr_get(struct inode *inode,
  707. int name_index,
  708. const char *name,
  709. void *buffer,
  710. size_t buffer_size)
  711. {
  712. int ret;
  713. struct ocfs2_dinode *di = NULL;
  714. struct buffer_head *di_bh = NULL;
  715. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  716. struct ocfs2_xattr_search xis = {
  717. .not_found = -ENODATA,
  718. };
  719. struct ocfs2_xattr_search xbs = {
  720. .not_found = -ENODATA,
  721. };
  722. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  723. ret = -ENODATA;
  724. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  725. if (ret < 0) {
  726. mlog_errno(ret);
  727. return ret;
  728. }
  729. xis.inode_bh = xbs.inode_bh = di_bh;
  730. di = (struct ocfs2_dinode *)di_bh->b_data;
  731. down_read(&oi->ip_xattr_sem);
  732. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  733. buffer_size, &xis);
  734. if (ret == -ENODATA)
  735. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  736. buffer_size, &xbs);
  737. up_read(&oi->ip_xattr_sem);
  738. ocfs2_inode_unlock(inode, 0);
  739. brelse(di_bh);
  740. return ret;
  741. }
  742. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  743. struct ocfs2_xattr_value_root *xv,
  744. const void *value,
  745. int value_len)
  746. {
  747. int ret = 0, i, cp_len, credits;
  748. u16 blocksize = inode->i_sb->s_blocksize;
  749. u32 p_cluster, num_clusters;
  750. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  751. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  752. u64 blkno;
  753. struct buffer_head *bh = NULL;
  754. handle_t *handle;
  755. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  756. credits = clusters * bpc;
  757. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
  758. if (IS_ERR(handle)) {
  759. ret = PTR_ERR(handle);
  760. mlog_errno(ret);
  761. goto out;
  762. }
  763. while (cpos < clusters) {
  764. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  765. &num_clusters, &xv->xr_list);
  766. if (ret) {
  767. mlog_errno(ret);
  768. goto out_commit;
  769. }
  770. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  771. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  772. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  773. &bh, OCFS2_BH_CACHED, inode);
  774. if (ret) {
  775. mlog_errno(ret);
  776. goto out_commit;
  777. }
  778. ret = ocfs2_journal_access(handle,
  779. inode,
  780. bh,
  781. OCFS2_JOURNAL_ACCESS_WRITE);
  782. if (ret < 0) {
  783. mlog_errno(ret);
  784. goto out_commit;
  785. }
  786. cp_len = value_len > blocksize ? blocksize : value_len;
  787. memcpy(bh->b_data, value, cp_len);
  788. value_len -= cp_len;
  789. value += cp_len;
  790. if (cp_len < blocksize)
  791. memset(bh->b_data + cp_len, 0,
  792. blocksize - cp_len);
  793. ret = ocfs2_journal_dirty(handle, bh);
  794. if (ret < 0) {
  795. mlog_errno(ret);
  796. goto out_commit;
  797. }
  798. brelse(bh);
  799. bh = NULL;
  800. /*
  801. * XXX: do we need to empty all the following
  802. * blocks in this cluster?
  803. */
  804. if (!value_len)
  805. break;
  806. }
  807. cpos += num_clusters;
  808. }
  809. out_commit:
  810. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  811. out:
  812. brelse(bh);
  813. return ret;
  814. }
  815. static int ocfs2_xattr_cleanup(struct inode *inode,
  816. struct ocfs2_xattr_info *xi,
  817. struct ocfs2_xattr_search *xs,
  818. size_t offs)
  819. {
  820. handle_t *handle = NULL;
  821. int ret = 0;
  822. size_t name_len = strlen(xi->name);
  823. void *val = xs->base + offs;
  824. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  825. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  826. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  827. if (IS_ERR(handle)) {
  828. ret = PTR_ERR(handle);
  829. mlog_errno(ret);
  830. goto out;
  831. }
  832. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  833. OCFS2_JOURNAL_ACCESS_WRITE);
  834. if (ret) {
  835. mlog_errno(ret);
  836. goto out_commit;
  837. }
  838. /* Decrease xattr count */
  839. le16_add_cpu(&xs->header->xh_count, -1);
  840. /* Remove the xattr entry and tree root which has already be set*/
  841. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  842. memset(val, 0, size);
  843. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  844. if (ret < 0)
  845. mlog_errno(ret);
  846. out_commit:
  847. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  848. out:
  849. return ret;
  850. }
  851. static int ocfs2_xattr_update_entry(struct inode *inode,
  852. struct ocfs2_xattr_info *xi,
  853. struct ocfs2_xattr_search *xs,
  854. size_t offs)
  855. {
  856. handle_t *handle = NULL;
  857. int ret = 0;
  858. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  859. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  860. if (IS_ERR(handle)) {
  861. ret = PTR_ERR(handle);
  862. mlog_errno(ret);
  863. goto out;
  864. }
  865. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  866. OCFS2_JOURNAL_ACCESS_WRITE);
  867. if (ret) {
  868. mlog_errno(ret);
  869. goto out_commit;
  870. }
  871. xs->here->xe_name_offset = cpu_to_le16(offs);
  872. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  873. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  874. ocfs2_xattr_set_local(xs->here, 1);
  875. else
  876. ocfs2_xattr_set_local(xs->here, 0);
  877. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  878. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  879. if (ret < 0)
  880. mlog_errno(ret);
  881. out_commit:
  882. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  883. out:
  884. return ret;
  885. }
  886. /*
  887. * ocfs2_xattr_set_value_outside()
  888. *
  889. * Set large size value in B tree.
  890. */
  891. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  892. struct ocfs2_xattr_info *xi,
  893. struct ocfs2_xattr_search *xs,
  894. size_t offs)
  895. {
  896. size_t name_len = strlen(xi->name);
  897. void *val = xs->base + offs;
  898. struct ocfs2_xattr_value_root *xv = NULL;
  899. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  900. int ret = 0;
  901. memset(val, 0, size);
  902. memcpy(val, xi->name, name_len);
  903. xv = (struct ocfs2_xattr_value_root *)
  904. (val + OCFS2_XATTR_SIZE(name_len));
  905. xv->xr_clusters = 0;
  906. xv->xr_last_eb_blk = 0;
  907. xv->xr_list.l_tree_depth = 0;
  908. xv->xr_list.l_count = cpu_to_le16(1);
  909. xv->xr_list.l_next_free_rec = 0;
  910. ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
  911. xi->value_len);
  912. if (ret < 0) {
  913. mlog_errno(ret);
  914. return ret;
  915. }
  916. ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
  917. xi->value_len);
  918. if (ret < 0) {
  919. mlog_errno(ret);
  920. return ret;
  921. }
  922. ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
  923. if (ret < 0)
  924. mlog_errno(ret);
  925. return ret;
  926. }
  927. /*
  928. * ocfs2_xattr_set_entry_local()
  929. *
  930. * Set, replace or remove extended attribute in local.
  931. */
  932. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  933. struct ocfs2_xattr_info *xi,
  934. struct ocfs2_xattr_search *xs,
  935. struct ocfs2_xattr_entry *last,
  936. size_t min_offs)
  937. {
  938. size_t name_len = strlen(xi->name);
  939. int i;
  940. if (xi->value && xs->not_found) {
  941. /* Insert the new xattr entry. */
  942. le16_add_cpu(&xs->header->xh_count, 1);
  943. ocfs2_xattr_set_type(last, xi->name_index);
  944. ocfs2_xattr_set_local(last, 1);
  945. last->xe_name_len = name_len;
  946. } else {
  947. void *first_val;
  948. void *val;
  949. size_t offs, size;
  950. first_val = xs->base + min_offs;
  951. offs = le16_to_cpu(xs->here->xe_name_offset);
  952. val = xs->base + offs;
  953. if (le64_to_cpu(xs->here->xe_value_size) >
  954. OCFS2_XATTR_INLINE_SIZE)
  955. size = OCFS2_XATTR_SIZE(name_len) +
  956. OCFS2_XATTR_ROOT_SIZE;
  957. else
  958. size = OCFS2_XATTR_SIZE(name_len) +
  959. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  960. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  961. OCFS2_XATTR_SIZE(xi->value_len)) {
  962. /* The old and the new value have the
  963. same size. Just replace the value. */
  964. ocfs2_xattr_set_local(xs->here, 1);
  965. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  966. /* Clear value bytes. */
  967. memset(val + OCFS2_XATTR_SIZE(name_len),
  968. 0,
  969. OCFS2_XATTR_SIZE(xi->value_len));
  970. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  971. xi->value,
  972. xi->value_len);
  973. return;
  974. }
  975. /* Remove the old name+value. */
  976. memmove(first_val + size, first_val, val - first_val);
  977. memset(first_val, 0, size);
  978. xs->here->xe_name_hash = 0;
  979. xs->here->xe_name_offset = 0;
  980. ocfs2_xattr_set_local(xs->here, 1);
  981. xs->here->xe_value_size = 0;
  982. min_offs += size;
  983. /* Adjust all value offsets. */
  984. last = xs->header->xh_entries;
  985. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  986. size_t o = le16_to_cpu(last->xe_name_offset);
  987. if (o < offs)
  988. last->xe_name_offset = cpu_to_le16(o + size);
  989. last += 1;
  990. }
  991. if (!xi->value) {
  992. /* Remove the old entry. */
  993. last -= 1;
  994. memmove(xs->here, xs->here + 1,
  995. (void *)last - (void *)xs->here);
  996. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  997. le16_add_cpu(&xs->header->xh_count, -1);
  998. }
  999. }
  1000. if (xi->value) {
  1001. /* Insert the new name+value. */
  1002. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1003. OCFS2_XATTR_SIZE(xi->value_len);
  1004. void *val = xs->base + min_offs - size;
  1005. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1006. memset(val, 0, size);
  1007. memcpy(val, xi->name, name_len);
  1008. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1009. xi->value,
  1010. xi->value_len);
  1011. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1012. ocfs2_xattr_set_local(xs->here, 1);
  1013. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1014. }
  1015. return;
  1016. }
  1017. /*
  1018. * ocfs2_xattr_set_entry()
  1019. *
  1020. * Set extended attribute entry into inode or block.
  1021. *
  1022. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1023. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1024. * then set value in B tree with set_value_outside().
  1025. */
  1026. static int ocfs2_xattr_set_entry(struct inode *inode,
  1027. struct ocfs2_xattr_info *xi,
  1028. struct ocfs2_xattr_search *xs,
  1029. int flag)
  1030. {
  1031. struct ocfs2_xattr_entry *last;
  1032. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1033. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1034. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1035. size_t size_l = 0;
  1036. handle_t *handle = NULL;
  1037. int free, i, ret;
  1038. struct ocfs2_xattr_info xi_l = {
  1039. .name_index = xi->name_index,
  1040. .name = xi->name,
  1041. .value = xi->value,
  1042. .value_len = xi->value_len,
  1043. };
  1044. /* Compute min_offs, last and free space. */
  1045. last = xs->header->xh_entries;
  1046. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1047. size_t offs = le16_to_cpu(last->xe_name_offset);
  1048. if (offs < min_offs)
  1049. min_offs = offs;
  1050. last += 1;
  1051. }
  1052. free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
  1053. if (free < 0)
  1054. return -EFAULT;
  1055. if (!xs->not_found) {
  1056. size_t size = 0;
  1057. if (ocfs2_xattr_is_local(xs->here))
  1058. size = OCFS2_XATTR_SIZE(name_len) +
  1059. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1060. else
  1061. size = OCFS2_XATTR_SIZE(name_len) +
  1062. OCFS2_XATTR_ROOT_SIZE;
  1063. free += (size + sizeof(struct ocfs2_xattr_entry));
  1064. }
  1065. /* Check free space in inode or block */
  1066. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1067. if (free < sizeof(struct ocfs2_xattr_entry) +
  1068. OCFS2_XATTR_SIZE(name_len) +
  1069. OCFS2_XATTR_ROOT_SIZE) {
  1070. ret = -ENOSPC;
  1071. goto out;
  1072. }
  1073. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1074. xi_l.value = (void *)&def_xv;
  1075. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1076. } else if (xi->value) {
  1077. if (free < sizeof(struct ocfs2_xattr_entry) +
  1078. OCFS2_XATTR_SIZE(name_len) +
  1079. OCFS2_XATTR_SIZE(xi->value_len)) {
  1080. ret = -ENOSPC;
  1081. goto out;
  1082. }
  1083. }
  1084. if (!xs->not_found) {
  1085. /* For existing extended attribute */
  1086. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1087. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1088. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1089. void *val = xs->base + offs;
  1090. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1091. /* Replace existing local xattr with tree root */
  1092. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1093. offs);
  1094. if (ret < 0)
  1095. mlog_errno(ret);
  1096. goto out;
  1097. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1098. /* For existing xattr which has value outside */
  1099. struct ocfs2_xattr_value_root *xv = NULL;
  1100. xv = (struct ocfs2_xattr_value_root *)(val +
  1101. OCFS2_XATTR_SIZE(name_len));
  1102. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1103. /*
  1104. * If new value need set outside also,
  1105. * first truncate old value to new value,
  1106. * then set new value with set_value_outside().
  1107. */
  1108. ret = ocfs2_xattr_value_truncate(inode,
  1109. xs->xattr_bh,
  1110. xv,
  1111. xi->value_len);
  1112. if (ret < 0) {
  1113. mlog_errno(ret);
  1114. goto out;
  1115. }
  1116. ret = __ocfs2_xattr_set_value_outside(inode,
  1117. xv,
  1118. xi->value,
  1119. xi->value_len);
  1120. if (ret < 0) {
  1121. mlog_errno(ret);
  1122. goto out;
  1123. }
  1124. ret = ocfs2_xattr_update_entry(inode,
  1125. xi,
  1126. xs,
  1127. offs);
  1128. if (ret < 0)
  1129. mlog_errno(ret);
  1130. goto out;
  1131. } else {
  1132. /*
  1133. * If new value need set in local,
  1134. * just trucate old value to zero.
  1135. */
  1136. ret = ocfs2_xattr_value_truncate(inode,
  1137. xs->xattr_bh,
  1138. xv,
  1139. 0);
  1140. if (ret < 0)
  1141. mlog_errno(ret);
  1142. }
  1143. }
  1144. }
  1145. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1146. OCFS2_INODE_UPDATE_CREDITS);
  1147. if (IS_ERR(handle)) {
  1148. ret = PTR_ERR(handle);
  1149. mlog_errno(ret);
  1150. goto out;
  1151. }
  1152. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1153. OCFS2_JOURNAL_ACCESS_WRITE);
  1154. if (ret) {
  1155. mlog_errno(ret);
  1156. goto out_commit;
  1157. }
  1158. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1159. /*set extended attribue in external blcok*/
  1160. ret = ocfs2_extend_trans(handle,
  1161. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1162. if (ret) {
  1163. mlog_errno(ret);
  1164. goto out_commit;
  1165. }
  1166. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1167. OCFS2_JOURNAL_ACCESS_WRITE);
  1168. if (ret) {
  1169. mlog_errno(ret);
  1170. goto out_commit;
  1171. }
  1172. }
  1173. /*
  1174. * Set value in local, include set tree root in local.
  1175. * This is the first step for value size >INLINE_SIZE.
  1176. */
  1177. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1178. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1179. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1180. if (ret < 0) {
  1181. mlog_errno(ret);
  1182. goto out_commit;
  1183. }
  1184. }
  1185. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1186. (flag & OCFS2_INLINE_XATTR_FL)) {
  1187. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1188. unsigned int xattrsize = osb->s_xattr_inline_size;
  1189. /*
  1190. * Adjust extent record count or inline data size
  1191. * to reserve space for extended attribute.
  1192. */
  1193. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1194. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1195. le16_add_cpu(&idata->id_count, -xattrsize);
  1196. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1197. struct ocfs2_extent_list *el = &di->id2.i_list;
  1198. le16_add_cpu(&el->l_count, -(xattrsize /
  1199. sizeof(struct ocfs2_extent_rec)));
  1200. }
  1201. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1202. }
  1203. /* Update xattr flag */
  1204. spin_lock(&oi->ip_lock);
  1205. oi->ip_dyn_features |= flag;
  1206. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1207. spin_unlock(&oi->ip_lock);
  1208. /* Update inode ctime */
  1209. inode->i_ctime = CURRENT_TIME;
  1210. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1211. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1212. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1213. if (ret < 0)
  1214. mlog_errno(ret);
  1215. out_commit:
  1216. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1217. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1218. /*
  1219. * Set value outside in B tree.
  1220. * This is the second step for value size > INLINE_SIZE.
  1221. */
  1222. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1223. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
  1224. if (ret < 0) {
  1225. int ret2;
  1226. mlog_errno(ret);
  1227. /*
  1228. * If set value outside failed, we have to clean
  1229. * the junk tree root we have already set in local.
  1230. */
  1231. ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
  1232. if (ret2 < 0)
  1233. mlog_errno(ret2);
  1234. }
  1235. }
  1236. out:
  1237. return ret;
  1238. }
  1239. static int ocfs2_xattr_free_block(handle_t *handle,
  1240. struct ocfs2_super *osb,
  1241. struct ocfs2_xattr_block *xb)
  1242. {
  1243. struct inode *xb_alloc_inode;
  1244. struct buffer_head *xb_alloc_bh = NULL;
  1245. u64 blk = le64_to_cpu(xb->xb_blkno);
  1246. u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
  1247. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1248. int ret = 0;
  1249. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1250. EXTENT_ALLOC_SYSTEM_INODE,
  1251. le16_to_cpu(xb->xb_suballoc_slot));
  1252. if (!xb_alloc_inode) {
  1253. ret = -ENOMEM;
  1254. mlog_errno(ret);
  1255. goto out;
  1256. }
  1257. mutex_lock(&xb_alloc_inode->i_mutex);
  1258. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1259. if (ret < 0) {
  1260. mlog_errno(ret);
  1261. goto out_mutex;
  1262. }
  1263. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  1264. if (ret < 0) {
  1265. mlog_errno(ret);
  1266. goto out_unlock;
  1267. }
  1268. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1269. bit, bg_blkno, 1);
  1270. if (ret < 0)
  1271. mlog_errno(ret);
  1272. out_unlock:
  1273. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1274. brelse(xb_alloc_bh);
  1275. out_mutex:
  1276. mutex_unlock(&xb_alloc_inode->i_mutex);
  1277. iput(xb_alloc_inode);
  1278. out:
  1279. return ret;
  1280. }
  1281. static int ocfs2_remove_value_outside(struct inode*inode,
  1282. struct buffer_head *bh,
  1283. struct ocfs2_xattr_header *header)
  1284. {
  1285. int ret = 0, i;
  1286. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1287. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1288. if (!ocfs2_xattr_is_local(entry)) {
  1289. struct ocfs2_xattr_value_root *xv;
  1290. void *val;
  1291. val = (void *)header +
  1292. le16_to_cpu(entry->xe_name_offset);
  1293. xv = (struct ocfs2_xattr_value_root *)
  1294. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1295. ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
  1296. if (ret < 0) {
  1297. mlog_errno(ret);
  1298. return ret;
  1299. }
  1300. }
  1301. }
  1302. return ret;
  1303. }
  1304. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1305. struct buffer_head *di_bh)
  1306. {
  1307. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1308. struct ocfs2_xattr_header *header;
  1309. int ret;
  1310. header = (struct ocfs2_xattr_header *)
  1311. ((void *)di + inode->i_sb->s_blocksize -
  1312. le16_to_cpu(di->i_xattr_inline_size));
  1313. ret = ocfs2_remove_value_outside(inode, di_bh, header);
  1314. return ret;
  1315. }
  1316. static int ocfs2_xattr_block_remove(struct inode *inode,
  1317. struct buffer_head *blk_bh)
  1318. {
  1319. struct ocfs2_xattr_block *xb;
  1320. struct ocfs2_xattr_header *header;
  1321. int ret = 0;
  1322. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1323. header = &(xb->xb_attrs.xb_header);
  1324. ret = ocfs2_remove_value_outside(inode, blk_bh, header);
  1325. return ret;
  1326. }
  1327. /*
  1328. * ocfs2_xattr_remove()
  1329. *
  1330. * Free extended attribute resources associated with this inode.
  1331. */
  1332. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1333. {
  1334. struct ocfs2_xattr_block *xb;
  1335. struct buffer_head *blk_bh = NULL;
  1336. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1337. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1338. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1339. handle_t *handle;
  1340. int ret;
  1341. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1342. return 0;
  1343. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1344. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1345. if (ret < 0) {
  1346. mlog_errno(ret);
  1347. goto out;
  1348. }
  1349. }
  1350. if (di->i_xattr_loc) {
  1351. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1352. le64_to_cpu(di->i_xattr_loc),
  1353. &blk_bh, OCFS2_BH_CACHED, inode);
  1354. if (ret < 0) {
  1355. mlog_errno(ret);
  1356. return ret;
  1357. }
  1358. /*Verify the signature of xattr block*/
  1359. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1360. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1361. ret = -EFAULT;
  1362. goto out;
  1363. }
  1364. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1365. if (ret < 0) {
  1366. mlog_errno(ret);
  1367. goto out;
  1368. }
  1369. }
  1370. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1371. OCFS2_INODE_UPDATE_CREDITS);
  1372. if (IS_ERR(handle)) {
  1373. ret = PTR_ERR(handle);
  1374. mlog_errno(ret);
  1375. goto out;
  1376. }
  1377. ret = ocfs2_journal_access(handle, inode, di_bh,
  1378. OCFS2_JOURNAL_ACCESS_WRITE);
  1379. if (ret) {
  1380. mlog_errno(ret);
  1381. goto out_commit;
  1382. }
  1383. if (di->i_xattr_loc) {
  1384. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1385. ocfs2_xattr_free_block(handle, osb, xb);
  1386. di->i_xattr_loc = cpu_to_le64(0);
  1387. }
  1388. spin_lock(&oi->ip_lock);
  1389. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1390. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1391. spin_unlock(&oi->ip_lock);
  1392. ret = ocfs2_journal_dirty(handle, di_bh);
  1393. if (ret < 0)
  1394. mlog_errno(ret);
  1395. out_commit:
  1396. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1397. out:
  1398. brelse(blk_bh);
  1399. return ret;
  1400. }
  1401. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1402. struct ocfs2_dinode *di)
  1403. {
  1404. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1405. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1406. int free;
  1407. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1408. return 0;
  1409. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1410. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1411. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1412. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1413. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1414. le64_to_cpu(di->i_size);
  1415. } else {
  1416. struct ocfs2_extent_list *el = &di->id2.i_list;
  1417. free = (le16_to_cpu(el->l_count) -
  1418. le16_to_cpu(el->l_next_free_rec)) *
  1419. sizeof(struct ocfs2_extent_rec);
  1420. }
  1421. if (free >= xattrsize)
  1422. return 1;
  1423. return 0;
  1424. }
  1425. /*
  1426. * ocfs2_xattr_ibody_find()
  1427. *
  1428. * Find extended attribute in inode block and
  1429. * fill search info into struct ocfs2_xattr_search.
  1430. */
  1431. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1432. int name_index,
  1433. const char *name,
  1434. struct ocfs2_xattr_search *xs)
  1435. {
  1436. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1437. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1438. int ret;
  1439. int has_space = 0;
  1440. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1441. return 0;
  1442. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1443. down_read(&oi->ip_alloc_sem);
  1444. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1445. up_read(&oi->ip_alloc_sem);
  1446. if (!has_space)
  1447. return 0;
  1448. }
  1449. xs->xattr_bh = xs->inode_bh;
  1450. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1451. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1452. xs->header = (struct ocfs2_xattr_header *)
  1453. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1454. else
  1455. xs->header = (struct ocfs2_xattr_header *)
  1456. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1457. xs->base = (void *)xs->header;
  1458. xs->here = xs->header->xh_entries;
  1459. /* Find the named attribute. */
  1460. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1461. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1462. if (ret && ret != -ENODATA)
  1463. return ret;
  1464. xs->not_found = ret;
  1465. }
  1466. return 0;
  1467. }
  1468. /*
  1469. * ocfs2_xattr_ibody_set()
  1470. *
  1471. * Set, replace or remove an extended attribute into inode block.
  1472. *
  1473. */
  1474. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1475. struct ocfs2_xattr_info *xi,
  1476. struct ocfs2_xattr_search *xs)
  1477. {
  1478. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1479. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1480. int ret;
  1481. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1482. return -ENOSPC;
  1483. down_write(&oi->ip_alloc_sem);
  1484. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1485. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1486. ret = -ENOSPC;
  1487. goto out;
  1488. }
  1489. }
  1490. ret = ocfs2_xattr_set_entry(inode, xi, xs,
  1491. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1492. out:
  1493. up_write(&oi->ip_alloc_sem);
  1494. return ret;
  1495. }
  1496. /*
  1497. * ocfs2_xattr_block_find()
  1498. *
  1499. * Find extended attribute in external block and
  1500. * fill search info into struct ocfs2_xattr_search.
  1501. */
  1502. static int ocfs2_xattr_block_find(struct inode *inode,
  1503. int name_index,
  1504. const char *name,
  1505. struct ocfs2_xattr_search *xs)
  1506. {
  1507. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1508. struct buffer_head *blk_bh = NULL;
  1509. struct ocfs2_xattr_block *xb;
  1510. int ret = 0;
  1511. if (!di->i_xattr_loc)
  1512. return ret;
  1513. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1514. le64_to_cpu(di->i_xattr_loc),
  1515. &blk_bh, OCFS2_BH_CACHED, inode);
  1516. if (ret < 0) {
  1517. mlog_errno(ret);
  1518. return ret;
  1519. }
  1520. /*Verify the signature of xattr block*/
  1521. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1522. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1523. ret = -EFAULT;
  1524. goto cleanup;
  1525. }
  1526. xs->xattr_bh = blk_bh;
  1527. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1528. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1529. xs->header = &xb->xb_attrs.xb_header;
  1530. xs->base = (void *)xs->header;
  1531. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1532. xs->here = xs->header->xh_entries;
  1533. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1534. } else
  1535. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1536. name_index,
  1537. name, xs);
  1538. if (ret && ret != -ENODATA) {
  1539. xs->xattr_bh = NULL;
  1540. goto cleanup;
  1541. }
  1542. xs->not_found = ret;
  1543. return 0;
  1544. cleanup:
  1545. brelse(blk_bh);
  1546. return ret;
  1547. }
  1548. /*
  1549. * ocfs2_xattr_block_set()
  1550. *
  1551. * Set, replace or remove an extended attribute into external block.
  1552. *
  1553. */
  1554. static int ocfs2_xattr_block_set(struct inode *inode,
  1555. struct ocfs2_xattr_info *xi,
  1556. struct ocfs2_xattr_search *xs)
  1557. {
  1558. struct buffer_head *new_bh = NULL;
  1559. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1560. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1561. struct ocfs2_alloc_context *meta_ac = NULL;
  1562. handle_t *handle = NULL;
  1563. struct ocfs2_xattr_block *xblk = NULL;
  1564. u16 suballoc_bit_start;
  1565. u32 num_got;
  1566. u64 first_blkno;
  1567. int ret;
  1568. if (!xs->xattr_bh) {
  1569. /*
  1570. * Alloc one external block for extended attribute
  1571. * outside of inode.
  1572. */
  1573. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  1574. if (ret < 0) {
  1575. mlog_errno(ret);
  1576. goto out;
  1577. }
  1578. handle = ocfs2_start_trans(osb,
  1579. OCFS2_XATTR_BLOCK_CREATE_CREDITS);
  1580. if (IS_ERR(handle)) {
  1581. ret = PTR_ERR(handle);
  1582. mlog_errno(ret);
  1583. goto out;
  1584. }
  1585. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1586. OCFS2_JOURNAL_ACCESS_CREATE);
  1587. if (ret < 0) {
  1588. mlog_errno(ret);
  1589. goto out_commit;
  1590. }
  1591. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  1592. &suballoc_bit_start, &num_got,
  1593. &first_blkno);
  1594. if (ret < 0) {
  1595. mlog_errno(ret);
  1596. goto out_commit;
  1597. }
  1598. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1599. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  1600. ret = ocfs2_journal_access(handle, inode, new_bh,
  1601. OCFS2_JOURNAL_ACCESS_CREATE);
  1602. if (ret < 0) {
  1603. mlog_errno(ret);
  1604. goto out_commit;
  1605. }
  1606. /* Initialize ocfs2_xattr_block */
  1607. xs->xattr_bh = new_bh;
  1608. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1609. memset(xblk, 0, inode->i_sb->s_blocksize);
  1610. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1611. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1612. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1613. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1614. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1615. xs->header = &xblk->xb_attrs.xb_header;
  1616. xs->base = (void *)xs->header;
  1617. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1618. xs->here = xs->header->xh_entries;
  1619. ret = ocfs2_journal_dirty(handle, new_bh);
  1620. if (ret < 0) {
  1621. mlog_errno(ret);
  1622. goto out_commit;
  1623. }
  1624. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1625. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1626. if (ret < 0)
  1627. mlog_errno(ret);
  1628. out_commit:
  1629. ocfs2_commit_trans(osb, handle);
  1630. out:
  1631. if (meta_ac)
  1632. ocfs2_free_alloc_context(meta_ac);
  1633. if (ret < 0)
  1634. return ret;
  1635. }
  1636. /* Set extended attribute into external block */
  1637. ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
  1638. return ret;
  1639. }
  1640. /*
  1641. * ocfs2_xattr_set()
  1642. *
  1643. * Set, replace or remove an extended attribute for this inode.
  1644. * value is NULL to remove an existing extended attribute, else either
  1645. * create or replace an extended attribute.
  1646. */
  1647. int ocfs2_xattr_set(struct inode *inode,
  1648. int name_index,
  1649. const char *name,
  1650. const void *value,
  1651. size_t value_len,
  1652. int flags)
  1653. {
  1654. struct buffer_head *di_bh = NULL;
  1655. struct ocfs2_dinode *di;
  1656. int ret;
  1657. struct ocfs2_xattr_info xi = {
  1658. .name_index = name_index,
  1659. .name = name,
  1660. .value = value,
  1661. .value_len = value_len,
  1662. };
  1663. struct ocfs2_xattr_search xis = {
  1664. .not_found = -ENODATA,
  1665. };
  1666. struct ocfs2_xattr_search xbs = {
  1667. .not_found = -ENODATA,
  1668. };
  1669. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1670. if (ret < 0) {
  1671. mlog_errno(ret);
  1672. return ret;
  1673. }
  1674. xis.inode_bh = xbs.inode_bh = di_bh;
  1675. di = (struct ocfs2_dinode *)di_bh->b_data;
  1676. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  1677. /*
  1678. * Scan inode and external block to find the same name
  1679. * extended attribute and collect search infomation.
  1680. */
  1681. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  1682. if (ret)
  1683. goto cleanup;
  1684. if (xis.not_found) {
  1685. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  1686. if (ret)
  1687. goto cleanup;
  1688. }
  1689. if (xis.not_found && xbs.not_found) {
  1690. ret = -ENODATA;
  1691. if (flags & XATTR_REPLACE)
  1692. goto cleanup;
  1693. ret = 0;
  1694. if (!value)
  1695. goto cleanup;
  1696. } else {
  1697. ret = -EEXIST;
  1698. if (flags & XATTR_CREATE)
  1699. goto cleanup;
  1700. }
  1701. if (!value) {
  1702. /* Remove existing extended attribute */
  1703. if (!xis.not_found)
  1704. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1705. else if (!xbs.not_found)
  1706. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1707. } else {
  1708. /* We always try to set extended attribute into inode first*/
  1709. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1710. if (!ret && !xbs.not_found) {
  1711. /*
  1712. * If succeed and that extended attribute existing in
  1713. * external block, then we will remove it.
  1714. */
  1715. xi.value = NULL;
  1716. xi.value_len = 0;
  1717. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1718. } else if (ret == -ENOSPC) {
  1719. if (di->i_xattr_loc && !xbs.xattr_bh) {
  1720. ret = ocfs2_xattr_block_find(inode, name_index,
  1721. name, &xbs);
  1722. if (ret)
  1723. goto cleanup;
  1724. }
  1725. /*
  1726. * If no space in inode, we will set extended attribute
  1727. * into external block.
  1728. */
  1729. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1730. if (ret)
  1731. goto cleanup;
  1732. if (!xis.not_found) {
  1733. /*
  1734. * If succeed and that extended attribute
  1735. * existing in inode, we will remove it.
  1736. */
  1737. xi.value = NULL;
  1738. xi.value_len = 0;
  1739. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1740. }
  1741. }
  1742. }
  1743. cleanup:
  1744. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  1745. ocfs2_inode_unlock(inode, 1);
  1746. brelse(di_bh);
  1747. brelse(xbs.xattr_bh);
  1748. return ret;
  1749. }
  1750. static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
  1751. int name_index,
  1752. const char *suffix_name)
  1753. {
  1754. struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
  1755. char *prefix = handler->prefix;
  1756. int prefix_len = strlen(handler->prefix);
  1757. return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
  1758. (char *)suffix_name, strlen(suffix_name));
  1759. }
  1760. /*
  1761. * Find the xattr extent rec which may contains name_hash.
  1762. * e_cpos will be the first name hash of the xattr rec.
  1763. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  1764. */
  1765. static int ocfs2_xattr_get_rec(struct inode *inode,
  1766. u32 name_hash,
  1767. u64 *p_blkno,
  1768. u32 *e_cpos,
  1769. u32 *num_clusters,
  1770. struct ocfs2_extent_list *el)
  1771. {
  1772. int ret = 0, i;
  1773. struct buffer_head *eb_bh = NULL;
  1774. struct ocfs2_extent_block *eb;
  1775. struct ocfs2_extent_rec *rec = NULL;
  1776. u64 e_blkno = 0;
  1777. if (el->l_tree_depth) {
  1778. ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
  1779. if (ret) {
  1780. mlog_errno(ret);
  1781. goto out;
  1782. }
  1783. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  1784. el = &eb->h_list;
  1785. if (el->l_tree_depth) {
  1786. ocfs2_error(inode->i_sb,
  1787. "Inode %lu has non zero tree depth in "
  1788. "xattr tree block %llu\n", inode->i_ino,
  1789. (unsigned long long)eb_bh->b_blocknr);
  1790. ret = -EROFS;
  1791. goto out;
  1792. }
  1793. }
  1794. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1795. rec = &el->l_recs[i];
  1796. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  1797. e_blkno = le64_to_cpu(rec->e_blkno);
  1798. break;
  1799. }
  1800. }
  1801. if (!e_blkno) {
  1802. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  1803. "record (%u, %u, 0) in xattr", inode->i_ino,
  1804. le32_to_cpu(rec->e_cpos),
  1805. ocfs2_rec_clusters(el, rec));
  1806. ret = -EROFS;
  1807. goto out;
  1808. }
  1809. *p_blkno = le64_to_cpu(rec->e_blkno);
  1810. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  1811. if (e_cpos)
  1812. *e_cpos = le32_to_cpu(rec->e_cpos);
  1813. out:
  1814. brelse(eb_bh);
  1815. return ret;
  1816. }
  1817. typedef int (xattr_bucket_func)(struct inode *inode,
  1818. struct ocfs2_xattr_bucket *bucket,
  1819. void *para);
  1820. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  1821. struct buffer_head *header_bh,
  1822. int name_index,
  1823. const char *name,
  1824. u32 name_hash,
  1825. u16 *xe_index,
  1826. int *found)
  1827. {
  1828. int i, ret = 0, cmp = 1, block_off, new_offset;
  1829. struct ocfs2_xattr_header *xh =
  1830. (struct ocfs2_xattr_header *)header_bh->b_data;
  1831. size_t name_len = strlen(name);
  1832. struct ocfs2_xattr_entry *xe = NULL;
  1833. struct buffer_head *name_bh = NULL;
  1834. char *xe_name;
  1835. /*
  1836. * We don't use binary search in the bucket because there
  1837. * may be multiple entries with the same name hash.
  1838. */
  1839. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  1840. xe = &xh->xh_entries[i];
  1841. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  1842. continue;
  1843. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  1844. break;
  1845. cmp = name_index - ocfs2_xattr_get_type(xe);
  1846. if (!cmp)
  1847. cmp = name_len - xe->xe_name_len;
  1848. if (cmp)
  1849. continue;
  1850. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1851. xh,
  1852. i,
  1853. &block_off,
  1854. &new_offset);
  1855. if (ret) {
  1856. mlog_errno(ret);
  1857. break;
  1858. }
  1859. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1860. header_bh->b_blocknr + block_off,
  1861. &name_bh, OCFS2_BH_CACHED, inode);
  1862. if (ret) {
  1863. mlog_errno(ret);
  1864. break;
  1865. }
  1866. xe_name = name_bh->b_data + new_offset;
  1867. cmp = memcmp(name, xe_name, name_len);
  1868. brelse(name_bh);
  1869. name_bh = NULL;
  1870. if (cmp == 0) {
  1871. *xe_index = i;
  1872. *found = 1;
  1873. ret = 0;
  1874. break;
  1875. }
  1876. }
  1877. return ret;
  1878. }
  1879. /*
  1880. * Find the specified xattr entry in a series of buckets.
  1881. * This series start from p_blkno and last for num_clusters.
  1882. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  1883. * the num of the valid buckets.
  1884. *
  1885. * Return the buffer_head this xattr should reside in. And if the xattr's
  1886. * hash is in the gap of 2 buckets, return the lower bucket.
  1887. */
  1888. static int ocfs2_xattr_bucket_find(struct inode *inode,
  1889. int name_index,
  1890. const char *name,
  1891. u32 name_hash,
  1892. u64 p_blkno,
  1893. u32 first_hash,
  1894. u32 num_clusters,
  1895. struct ocfs2_xattr_search *xs)
  1896. {
  1897. int ret, found = 0;
  1898. struct buffer_head *bh = NULL;
  1899. struct buffer_head *lower_bh = NULL;
  1900. struct ocfs2_xattr_header *xh = NULL;
  1901. struct ocfs2_xattr_entry *xe = NULL;
  1902. u16 index = 0;
  1903. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1904. int low_bucket = 0, bucket, high_bucket;
  1905. u32 last_hash;
  1906. u64 blkno;
  1907. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
  1908. &bh, OCFS2_BH_CACHED, inode);
  1909. if (ret) {
  1910. mlog_errno(ret);
  1911. goto out;
  1912. }
  1913. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1914. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  1915. while (low_bucket <= high_bucket) {
  1916. brelse(bh);
  1917. bh = NULL;
  1918. bucket = (low_bucket + high_bucket) / 2;
  1919. blkno = p_blkno + bucket * blk_per_bucket;
  1920. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  1921. &bh, OCFS2_BH_CACHED, inode);
  1922. if (ret) {
  1923. mlog_errno(ret);
  1924. goto out;
  1925. }
  1926. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1927. xe = &xh->xh_entries[0];
  1928. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  1929. high_bucket = bucket - 1;
  1930. continue;
  1931. }
  1932. /*
  1933. * Check whether the hash of the last entry in our
  1934. * bucket is larger than the search one.
  1935. */
  1936. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  1937. last_hash = le32_to_cpu(xe->xe_name_hash);
  1938. /* record lower_bh which may be the insert place. */
  1939. brelse(lower_bh);
  1940. lower_bh = bh;
  1941. bh = NULL;
  1942. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  1943. low_bucket = bucket + 1;
  1944. continue;
  1945. }
  1946. /* the searched xattr should reside in this bucket if exists. */
  1947. ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
  1948. name_index, name, name_hash,
  1949. &index, &found);
  1950. if (ret) {
  1951. mlog_errno(ret);
  1952. goto out;
  1953. }
  1954. break;
  1955. }
  1956. /*
  1957. * Record the bucket we have found.
  1958. * When the xattr's hash value is in the gap of 2 buckets, we will
  1959. * always set it to the previous bucket.
  1960. */
  1961. if (!lower_bh) {
  1962. /*
  1963. * We can't find any bucket whose first name_hash is less
  1964. * than the find name_hash.
  1965. */
  1966. BUG_ON(bh->b_blocknr != p_blkno);
  1967. lower_bh = bh;
  1968. bh = NULL;
  1969. }
  1970. xs->bucket.bhs[0] = lower_bh;
  1971. xs->bucket.xh = (struct ocfs2_xattr_header *)
  1972. xs->bucket.bhs[0]->b_data;
  1973. lower_bh = NULL;
  1974. xs->header = xs->bucket.xh;
  1975. xs->base = xs->bucket.bhs[0]->b_data;
  1976. xs->end = xs->base + inode->i_sb->s_blocksize;
  1977. if (found) {
  1978. /*
  1979. * If we have found the xattr enty, read all the blocks in
  1980. * this bucket.
  1981. */
  1982. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  1983. xs->bucket.bhs[0]->b_blocknr + 1,
  1984. blk_per_bucket - 1, &xs->bucket.bhs[1],
  1985. OCFS2_BH_CACHED, inode);
  1986. if (ret) {
  1987. mlog_errno(ret);
  1988. goto out;
  1989. }
  1990. xs->here = &xs->header->xh_entries[index];
  1991. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  1992. (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
  1993. } else
  1994. ret = -ENODATA;
  1995. out:
  1996. brelse(bh);
  1997. brelse(lower_bh);
  1998. return ret;
  1999. }
  2000. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2001. struct buffer_head *root_bh,
  2002. int name_index,
  2003. const char *name,
  2004. struct ocfs2_xattr_search *xs)
  2005. {
  2006. int ret;
  2007. struct ocfs2_xattr_block *xb =
  2008. (struct ocfs2_xattr_block *)root_bh->b_data;
  2009. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2010. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2011. u64 p_blkno = 0;
  2012. u32 first_hash, num_clusters = 0;
  2013. u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
  2014. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2015. return -ENODATA;
  2016. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2017. name, name_hash, name_index);
  2018. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2019. &num_clusters, el);
  2020. if (ret) {
  2021. mlog_errno(ret);
  2022. goto out;
  2023. }
  2024. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2025. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2026. "in the rec is %u\n", num_clusters, p_blkno, first_hash);
  2027. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2028. p_blkno, first_hash, num_clusters, xs);
  2029. out:
  2030. return ret;
  2031. }
  2032. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2033. u64 blkno,
  2034. u32 clusters,
  2035. xattr_bucket_func *func,
  2036. void *para)
  2037. {
  2038. int i, j, ret = 0;
  2039. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2040. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2041. u32 num_buckets = clusters * bpc;
  2042. struct ocfs2_xattr_bucket bucket;
  2043. memset(&bucket, 0, sizeof(bucket));
  2044. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2045. clusters, blkno);
  2046. for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
  2047. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2048. blkno, blk_per_bucket,
  2049. bucket.bhs, OCFS2_BH_CACHED, inode);
  2050. if (ret) {
  2051. mlog_errno(ret);
  2052. goto out;
  2053. }
  2054. bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
  2055. /*
  2056. * The real bucket num in this series of blocks is stored
  2057. * in the 1st bucket.
  2058. */
  2059. if (i == 0)
  2060. num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
  2061. mlog(0, "iterating xattr bucket %llu\n", blkno);
  2062. if (func) {
  2063. ret = func(inode, &bucket, para);
  2064. if (ret) {
  2065. mlog_errno(ret);
  2066. break;
  2067. }
  2068. }
  2069. for (j = 0; j < blk_per_bucket; j++)
  2070. brelse(bucket.bhs[j]);
  2071. memset(&bucket, 0, sizeof(bucket));
  2072. }
  2073. out:
  2074. for (j = 0; j < blk_per_bucket; j++)
  2075. brelse(bucket.bhs[j]);
  2076. return ret;
  2077. }
  2078. struct ocfs2_xattr_tree_list {
  2079. char *buffer;
  2080. size_t buffer_size;
  2081. };
  2082. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2083. struct ocfs2_xattr_header *xh,
  2084. int index,
  2085. int *block_off,
  2086. int *new_offset)
  2087. {
  2088. u16 name_offset;
  2089. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2090. return -EINVAL;
  2091. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2092. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2093. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2094. return 0;
  2095. }
  2096. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2097. struct ocfs2_xattr_bucket *bucket,
  2098. void *para)
  2099. {
  2100. int ret = 0;
  2101. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2102. size_t size;
  2103. int i, block_off, new_offset;
  2104. for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
  2105. struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
  2106. struct xattr_handler *handler =
  2107. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  2108. if (handler) {
  2109. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2110. bucket->xh,
  2111. i,
  2112. &block_off,
  2113. &new_offset);
  2114. if (ret)
  2115. break;
  2116. size = handler->list(inode, xl->buffer, xl->buffer_size,
  2117. bucket->bhs[block_off]->b_data +
  2118. new_offset,
  2119. entry->xe_name_len);
  2120. if (xl->buffer) {
  2121. if (size > xl->buffer_size)
  2122. return -ERANGE;
  2123. xl->buffer += size;
  2124. }
  2125. xl->buffer_size -= size;
  2126. }
  2127. }
  2128. return ret;
  2129. }
  2130. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2131. struct ocfs2_xattr_tree_root *xt,
  2132. char *buffer,
  2133. size_t buffer_size)
  2134. {
  2135. struct ocfs2_extent_list *el = &xt->xt_list;
  2136. int ret = 0;
  2137. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2138. u64 p_blkno = 0;
  2139. struct ocfs2_xattr_tree_list xl = {
  2140. .buffer = buffer,
  2141. .buffer_size = buffer_size,
  2142. };
  2143. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2144. return 0;
  2145. while (name_hash > 0) {
  2146. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2147. &e_cpos, &num_clusters, el);
  2148. if (ret) {
  2149. mlog_errno(ret);
  2150. goto out;
  2151. }
  2152. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2153. ocfs2_list_xattr_bucket,
  2154. &xl);
  2155. if (ret) {
  2156. mlog_errno(ret);
  2157. goto out;
  2158. }
  2159. if (e_cpos == 0)
  2160. break;
  2161. name_hash = e_cpos - 1;
  2162. }
  2163. ret = buffer_size - xl.buffer_size;
  2164. out:
  2165. return ret;
  2166. }