xattr.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852
  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. #include <linux/sort.h>
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/string.h>
  42. #define MLOG_MASK_PREFIX ML_XATTR
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. #include "alloc.h"
  46. #include "dlmglue.h"
  47. #include "file.h"
  48. #include "symlink.h"
  49. #include "sysfile.h"
  50. #include "inode.h"
  51. #include "journal.h"
  52. #include "ocfs2_fs.h"
  53. #include "suballoc.h"
  54. #include "uptodate.h"
  55. #include "buffer_head_io.h"
  56. #include "super.h"
  57. #include "xattr.h"
  58. struct ocfs2_xattr_def_value_root {
  59. struct ocfs2_xattr_value_root xv;
  60. struct ocfs2_extent_rec er;
  61. };
  62. struct ocfs2_xattr_bucket {
  63. struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  64. struct ocfs2_xattr_header *xh;
  65. };
  66. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  67. #define OCFS2_XATTR_INLINE_SIZE 80
  68. static struct ocfs2_xattr_def_value_root def_xv = {
  69. .xv.xr_list.l_count = cpu_to_le16(1),
  70. };
  71. struct xattr_handler *ocfs2_xattr_handlers[] = {
  72. &ocfs2_xattr_user_handler,
  73. &ocfs2_xattr_trusted_handler,
  74. NULL
  75. };
  76. static struct xattr_handler *ocfs2_xattr_handler_map[] = {
  77. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  78. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  79. };
  80. struct ocfs2_xattr_info {
  81. int name_index;
  82. const char *name;
  83. const void *value;
  84. size_t value_len;
  85. };
  86. struct ocfs2_xattr_search {
  87. struct buffer_head *inode_bh;
  88. /*
  89. * xattr_bh point to the block buffer head which has extended attribute
  90. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  91. */
  92. struct buffer_head *xattr_bh;
  93. struct ocfs2_xattr_header *header;
  94. struct ocfs2_xattr_bucket bucket;
  95. void *base;
  96. void *end;
  97. struct ocfs2_xattr_entry *here;
  98. int not_found;
  99. };
  100. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  101. struct ocfs2_xattr_header *xh,
  102. int index,
  103. int *block_off,
  104. int *new_offset);
  105. static int ocfs2_xattr_index_block_find(struct inode *inode,
  106. struct buffer_head *root_bh,
  107. int name_index,
  108. const char *name,
  109. struct ocfs2_xattr_search *xs);
  110. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  111. struct ocfs2_xattr_tree_root *xt,
  112. char *buffer,
  113. size_t buffer_size);
  114. static int ocfs2_xattr_create_index_block(struct inode *inode,
  115. struct ocfs2_xattr_search *xs);
  116. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  117. struct ocfs2_xattr_info *xi,
  118. struct ocfs2_xattr_search *xs);
  119. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  120. struct buffer_head *xb_bh);
  121. static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  125. handler = ocfs2_xattr_handler_map[name_index];
  126. return handler;
  127. }
  128. static u32 ocfs2_xattr_name_hash(struct inode *inode,
  129. char *prefix,
  130. int prefix_len,
  131. char *name,
  132. int name_len)
  133. {
  134. /* Get hash value of uuid from super block */
  135. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  136. int i;
  137. /* hash extended attribute prefix */
  138. for (i = 0; i < prefix_len; i++) {
  139. hash = (hash << OCFS2_HASH_SHIFT) ^
  140. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  141. *prefix++;
  142. }
  143. /* hash extended attribute name */
  144. for (i = 0; i < name_len; i++) {
  145. hash = (hash << OCFS2_HASH_SHIFT) ^
  146. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  147. *name++;
  148. }
  149. return hash;
  150. }
  151. /*
  152. * ocfs2_xattr_hash_entry()
  153. *
  154. * Compute the hash of an extended attribute.
  155. */
  156. static void ocfs2_xattr_hash_entry(struct inode *inode,
  157. struct ocfs2_xattr_header *header,
  158. struct ocfs2_xattr_entry *entry)
  159. {
  160. u32 hash = 0;
  161. struct xattr_handler *handler =
  162. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  163. char *prefix = handler->prefix;
  164. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  165. int prefix_len = strlen(handler->prefix);
  166. hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
  167. entry->xe_name_len);
  168. entry->xe_name_hash = cpu_to_le32(hash);
  169. return;
  170. }
  171. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  172. u32 clusters_to_add,
  173. struct buffer_head *xattr_bh,
  174. struct ocfs2_xattr_value_root *xv)
  175. {
  176. int status = 0;
  177. int restart_func = 0;
  178. int credits = 0;
  179. handle_t *handle = NULL;
  180. struct ocfs2_alloc_context *data_ac = NULL;
  181. struct ocfs2_alloc_context *meta_ac = NULL;
  182. enum ocfs2_alloc_restarted why;
  183. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  184. u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
  185. struct ocfs2_extent_tree et;
  186. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  187. ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
  188. restart_all:
  189. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  190. &data_ac, &meta_ac);
  191. if (status) {
  192. mlog_errno(status);
  193. goto leave;
  194. }
  195. credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
  196. clusters_to_add);
  197. handle = ocfs2_start_trans(osb, credits);
  198. if (IS_ERR(handle)) {
  199. status = PTR_ERR(handle);
  200. handle = NULL;
  201. mlog_errno(status);
  202. goto leave;
  203. }
  204. restarted_transaction:
  205. status = ocfs2_journal_access(handle, inode, xattr_bh,
  206. OCFS2_JOURNAL_ACCESS_WRITE);
  207. if (status < 0) {
  208. mlog_errno(status);
  209. goto leave;
  210. }
  211. prev_clusters = le32_to_cpu(xv->xr_clusters);
  212. status = ocfs2_add_clusters_in_btree(osb,
  213. inode,
  214. &logical_start,
  215. clusters_to_add,
  216. 0,
  217. &et,
  218. handle,
  219. data_ac,
  220. meta_ac,
  221. &why);
  222. if ((status < 0) && (status != -EAGAIN)) {
  223. if (status != -ENOSPC)
  224. mlog_errno(status);
  225. goto leave;
  226. }
  227. status = ocfs2_journal_dirty(handle, xattr_bh);
  228. if (status < 0) {
  229. mlog_errno(status);
  230. goto leave;
  231. }
  232. clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
  233. if (why != RESTART_NONE && clusters_to_add) {
  234. if (why == RESTART_META) {
  235. mlog(0, "restarting function.\n");
  236. restart_func = 1;
  237. } else {
  238. BUG_ON(why != RESTART_TRANS);
  239. mlog(0, "restarting transaction.\n");
  240. /* TODO: This can be more intelligent. */
  241. credits = ocfs2_calc_extend_credits(osb->sb,
  242. et.et_root_el,
  243. clusters_to_add);
  244. status = ocfs2_extend_trans(handle, credits);
  245. if (status < 0) {
  246. /* handle still has to be committed at
  247. * this point. */
  248. status = -ENOMEM;
  249. mlog_errno(status);
  250. goto leave;
  251. }
  252. goto restarted_transaction;
  253. }
  254. }
  255. leave:
  256. if (handle) {
  257. ocfs2_commit_trans(osb, handle);
  258. handle = NULL;
  259. }
  260. if (data_ac) {
  261. ocfs2_free_alloc_context(data_ac);
  262. data_ac = NULL;
  263. }
  264. if (meta_ac) {
  265. ocfs2_free_alloc_context(meta_ac);
  266. meta_ac = NULL;
  267. }
  268. if ((!status) && restart_func) {
  269. restart_func = 0;
  270. goto restart_all;
  271. }
  272. return status;
  273. }
  274. static int __ocfs2_remove_xattr_range(struct inode *inode,
  275. struct buffer_head *root_bh,
  276. struct ocfs2_xattr_value_root *xv,
  277. u32 cpos, u32 phys_cpos, u32 len,
  278. struct ocfs2_cached_dealloc_ctxt *dealloc)
  279. {
  280. int ret;
  281. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  282. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  283. struct inode *tl_inode = osb->osb_tl_inode;
  284. handle_t *handle;
  285. struct ocfs2_alloc_context *meta_ac = NULL;
  286. struct ocfs2_extent_tree et;
  287. ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
  288. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  289. if (ret) {
  290. mlog_errno(ret);
  291. return ret;
  292. }
  293. mutex_lock(&tl_inode->i_mutex);
  294. if (ocfs2_truncate_log_needs_flush(osb)) {
  295. ret = __ocfs2_flush_truncate_log(osb);
  296. if (ret < 0) {
  297. mlog_errno(ret);
  298. goto out;
  299. }
  300. }
  301. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  302. if (IS_ERR(handle)) {
  303. ret = PTR_ERR(handle);
  304. mlog_errno(ret);
  305. goto out;
  306. }
  307. ret = ocfs2_journal_access(handle, inode, root_bh,
  308. OCFS2_JOURNAL_ACCESS_WRITE);
  309. if (ret) {
  310. mlog_errno(ret);
  311. goto out_commit;
  312. }
  313. ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
  314. dealloc);
  315. if (ret) {
  316. mlog_errno(ret);
  317. goto out_commit;
  318. }
  319. le32_add_cpu(&xv->xr_clusters, -len);
  320. ret = ocfs2_journal_dirty(handle, root_bh);
  321. if (ret) {
  322. mlog_errno(ret);
  323. goto out_commit;
  324. }
  325. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  326. if (ret)
  327. mlog_errno(ret);
  328. out_commit:
  329. ocfs2_commit_trans(osb, handle);
  330. out:
  331. mutex_unlock(&tl_inode->i_mutex);
  332. if (meta_ac)
  333. ocfs2_free_alloc_context(meta_ac);
  334. return ret;
  335. }
  336. static int ocfs2_xattr_shrink_size(struct inode *inode,
  337. u32 old_clusters,
  338. u32 new_clusters,
  339. struct buffer_head *root_bh,
  340. struct ocfs2_xattr_value_root *xv)
  341. {
  342. int ret = 0;
  343. u32 trunc_len, cpos, phys_cpos, alloc_size;
  344. u64 block;
  345. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  346. struct ocfs2_cached_dealloc_ctxt dealloc;
  347. ocfs2_init_dealloc_ctxt(&dealloc);
  348. if (old_clusters <= new_clusters)
  349. return 0;
  350. cpos = new_clusters;
  351. trunc_len = old_clusters - new_clusters;
  352. while (trunc_len) {
  353. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  354. &alloc_size, &xv->xr_list);
  355. if (ret) {
  356. mlog_errno(ret);
  357. goto out;
  358. }
  359. if (alloc_size > trunc_len)
  360. alloc_size = trunc_len;
  361. ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
  362. phys_cpos, alloc_size,
  363. &dealloc);
  364. if (ret) {
  365. mlog_errno(ret);
  366. goto out;
  367. }
  368. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  369. ocfs2_remove_xattr_clusters_from_cache(inode, block,
  370. alloc_size);
  371. cpos += alloc_size;
  372. trunc_len -= alloc_size;
  373. }
  374. out:
  375. ocfs2_schedule_truncate_log_flush(osb, 1);
  376. ocfs2_run_deallocs(osb, &dealloc);
  377. return ret;
  378. }
  379. static int ocfs2_xattr_value_truncate(struct inode *inode,
  380. struct buffer_head *root_bh,
  381. struct ocfs2_xattr_value_root *xv,
  382. int len)
  383. {
  384. int ret;
  385. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  386. u32 old_clusters = le32_to_cpu(xv->xr_clusters);
  387. if (new_clusters == old_clusters)
  388. return 0;
  389. if (new_clusters > old_clusters)
  390. ret = ocfs2_xattr_extend_allocation(inode,
  391. new_clusters - old_clusters,
  392. root_bh, xv);
  393. else
  394. ret = ocfs2_xattr_shrink_size(inode,
  395. old_clusters, new_clusters,
  396. root_bh, xv);
  397. return ret;
  398. }
  399. static int ocfs2_xattr_list_entries(struct inode *inode,
  400. struct ocfs2_xattr_header *header,
  401. char *buffer, size_t buffer_size)
  402. {
  403. size_t rest = buffer_size;
  404. int i;
  405. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  406. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  407. struct xattr_handler *handler =
  408. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  409. if (handler) {
  410. size_t size = handler->list(inode, buffer, rest,
  411. ((char *)header +
  412. le16_to_cpu(entry->xe_name_offset)),
  413. entry->xe_name_len);
  414. if (buffer) {
  415. if (size > rest)
  416. return -ERANGE;
  417. buffer += size;
  418. }
  419. rest -= size;
  420. }
  421. }
  422. return buffer_size - rest;
  423. }
  424. static int ocfs2_xattr_ibody_list(struct inode *inode,
  425. struct ocfs2_dinode *di,
  426. char *buffer,
  427. size_t buffer_size)
  428. {
  429. struct ocfs2_xattr_header *header = NULL;
  430. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  431. int ret = 0;
  432. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  433. return ret;
  434. header = (struct ocfs2_xattr_header *)
  435. ((void *)di + inode->i_sb->s_blocksize -
  436. le16_to_cpu(di->i_xattr_inline_size));
  437. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  438. return ret;
  439. }
  440. static int ocfs2_xattr_block_list(struct inode *inode,
  441. struct ocfs2_dinode *di,
  442. char *buffer,
  443. size_t buffer_size)
  444. {
  445. struct buffer_head *blk_bh = NULL;
  446. struct ocfs2_xattr_block *xb;
  447. int ret = 0;
  448. if (!di->i_xattr_loc)
  449. return ret;
  450. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  451. le64_to_cpu(di->i_xattr_loc),
  452. &blk_bh, OCFS2_BH_CACHED, inode);
  453. if (ret < 0) {
  454. mlog_errno(ret);
  455. return ret;
  456. }
  457. /*Verify the signature of xattr block*/
  458. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  459. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  460. ret = -EFAULT;
  461. goto cleanup;
  462. }
  463. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  464. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  465. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  466. ret = ocfs2_xattr_list_entries(inode, header,
  467. buffer, buffer_size);
  468. } else {
  469. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  470. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  471. buffer, buffer_size);
  472. }
  473. cleanup:
  474. brelse(blk_bh);
  475. return ret;
  476. }
  477. ssize_t ocfs2_listxattr(struct dentry *dentry,
  478. char *buffer,
  479. size_t size)
  480. {
  481. int ret = 0, i_ret = 0, b_ret = 0;
  482. struct buffer_head *di_bh = NULL;
  483. struct ocfs2_dinode *di = NULL;
  484. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  485. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  486. return -EOPNOTSUPP;
  487. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  488. return ret;
  489. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  490. if (ret < 0) {
  491. mlog_errno(ret);
  492. return ret;
  493. }
  494. di = (struct ocfs2_dinode *)di_bh->b_data;
  495. down_read(&oi->ip_xattr_sem);
  496. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  497. if (i_ret < 0)
  498. b_ret = 0;
  499. else {
  500. if (buffer) {
  501. buffer += i_ret;
  502. size -= i_ret;
  503. }
  504. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  505. buffer, size);
  506. if (b_ret < 0)
  507. i_ret = 0;
  508. }
  509. up_read(&oi->ip_xattr_sem);
  510. ocfs2_inode_unlock(dentry->d_inode, 0);
  511. brelse(di_bh);
  512. return i_ret + b_ret;
  513. }
  514. static int ocfs2_xattr_find_entry(int name_index,
  515. const char *name,
  516. struct ocfs2_xattr_search *xs)
  517. {
  518. struct ocfs2_xattr_entry *entry;
  519. size_t name_len;
  520. int i, cmp = 1;
  521. if (name == NULL)
  522. return -EINVAL;
  523. name_len = strlen(name);
  524. entry = xs->here;
  525. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  526. cmp = name_index - ocfs2_xattr_get_type(entry);
  527. if (!cmp)
  528. cmp = name_len - entry->xe_name_len;
  529. if (!cmp)
  530. cmp = memcmp(name, (xs->base +
  531. le16_to_cpu(entry->xe_name_offset)),
  532. name_len);
  533. if (cmp == 0)
  534. break;
  535. entry += 1;
  536. }
  537. xs->here = entry;
  538. return cmp ? -ENODATA : 0;
  539. }
  540. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  541. struct ocfs2_xattr_value_root *xv,
  542. void *buffer,
  543. size_t len)
  544. {
  545. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  546. u64 blkno;
  547. int i, ret = 0;
  548. size_t cplen, blocksize;
  549. struct buffer_head *bh = NULL;
  550. struct ocfs2_extent_list *el;
  551. el = &xv->xr_list;
  552. clusters = le32_to_cpu(xv->xr_clusters);
  553. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  554. blocksize = inode->i_sb->s_blocksize;
  555. cpos = 0;
  556. while (cpos < clusters) {
  557. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  558. &num_clusters, el);
  559. if (ret) {
  560. mlog_errno(ret);
  561. goto out;
  562. }
  563. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  564. /* Copy ocfs2_xattr_value */
  565. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  566. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  567. &bh, OCFS2_BH_CACHED, inode);
  568. if (ret) {
  569. mlog_errno(ret);
  570. goto out;
  571. }
  572. cplen = len >= blocksize ? blocksize : len;
  573. memcpy(buffer, bh->b_data, cplen);
  574. len -= cplen;
  575. buffer += cplen;
  576. brelse(bh);
  577. bh = NULL;
  578. if (len == 0)
  579. break;
  580. }
  581. cpos += num_clusters;
  582. }
  583. out:
  584. return ret;
  585. }
  586. static int ocfs2_xattr_ibody_get(struct inode *inode,
  587. int name_index,
  588. const char *name,
  589. void *buffer,
  590. size_t buffer_size,
  591. struct ocfs2_xattr_search *xs)
  592. {
  593. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  594. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  595. struct ocfs2_xattr_value_root *xv;
  596. size_t size;
  597. int ret = 0;
  598. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  599. return -ENODATA;
  600. xs->end = (void *)di + inode->i_sb->s_blocksize;
  601. xs->header = (struct ocfs2_xattr_header *)
  602. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  603. xs->base = (void *)xs->header;
  604. xs->here = xs->header->xh_entries;
  605. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  606. if (ret)
  607. return ret;
  608. size = le64_to_cpu(xs->here->xe_value_size);
  609. if (buffer) {
  610. if (size > buffer_size)
  611. return -ERANGE;
  612. if (ocfs2_xattr_is_local(xs->here)) {
  613. memcpy(buffer, (void *)xs->base +
  614. le16_to_cpu(xs->here->xe_name_offset) +
  615. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  616. } else {
  617. xv = (struct ocfs2_xattr_value_root *)
  618. (xs->base + le16_to_cpu(
  619. xs->here->xe_name_offset) +
  620. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  621. ret = ocfs2_xattr_get_value_outside(inode, xv,
  622. buffer, size);
  623. if (ret < 0) {
  624. mlog_errno(ret);
  625. return ret;
  626. }
  627. }
  628. }
  629. return size;
  630. }
  631. static int ocfs2_xattr_block_get(struct inode *inode,
  632. int name_index,
  633. const char *name,
  634. void *buffer,
  635. size_t buffer_size,
  636. struct ocfs2_xattr_search *xs)
  637. {
  638. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  639. struct buffer_head *blk_bh = NULL;
  640. struct ocfs2_xattr_block *xb;
  641. struct ocfs2_xattr_value_root *xv;
  642. size_t size;
  643. int ret = -ENODATA, name_offset, name_len, block_off, i;
  644. if (!di->i_xattr_loc)
  645. return ret;
  646. memset(&xs->bucket, 0, sizeof(xs->bucket));
  647. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  648. le64_to_cpu(di->i_xattr_loc),
  649. &blk_bh, OCFS2_BH_CACHED, inode);
  650. if (ret < 0) {
  651. mlog_errno(ret);
  652. return ret;
  653. }
  654. /*Verify the signature of xattr block*/
  655. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  656. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  657. ret = -EFAULT;
  658. goto cleanup;
  659. }
  660. xs->xattr_bh = blk_bh;
  661. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  662. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  663. xs->header = &xb->xb_attrs.xb_header;
  664. xs->base = (void *)xs->header;
  665. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  666. xs->here = xs->header->xh_entries;
  667. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  668. } else
  669. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  670. name_index,
  671. name, xs);
  672. if (ret)
  673. goto cleanup;
  674. size = le64_to_cpu(xs->here->xe_value_size);
  675. if (buffer) {
  676. ret = -ERANGE;
  677. if (size > buffer_size)
  678. goto cleanup;
  679. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  680. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  681. i = xs->here - xs->header->xh_entries;
  682. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  683. ret = ocfs2_xattr_bucket_get_name_value(inode,
  684. xs->bucket.xh,
  685. i,
  686. &block_off,
  687. &name_offset);
  688. xs->base = xs->bucket.bhs[block_off]->b_data;
  689. }
  690. if (ocfs2_xattr_is_local(xs->here)) {
  691. memcpy(buffer, (void *)xs->base +
  692. name_offset + name_len, size);
  693. } else {
  694. xv = (struct ocfs2_xattr_value_root *)
  695. (xs->base + name_offset + name_len);
  696. ret = ocfs2_xattr_get_value_outside(inode, xv,
  697. buffer, size);
  698. if (ret < 0) {
  699. mlog_errno(ret);
  700. goto cleanup;
  701. }
  702. }
  703. }
  704. ret = size;
  705. cleanup:
  706. for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
  707. brelse(xs->bucket.bhs[i]);
  708. memset(&xs->bucket, 0, sizeof(xs->bucket));
  709. brelse(blk_bh);
  710. return ret;
  711. }
  712. /* ocfs2_xattr_get()
  713. *
  714. * Copy an extended attribute into the buffer provided.
  715. * Buffer is NULL to compute the size of buffer required.
  716. */
  717. int ocfs2_xattr_get(struct inode *inode,
  718. int name_index,
  719. const char *name,
  720. void *buffer,
  721. size_t buffer_size)
  722. {
  723. int ret;
  724. struct ocfs2_dinode *di = NULL;
  725. struct buffer_head *di_bh = NULL;
  726. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  727. struct ocfs2_xattr_search xis = {
  728. .not_found = -ENODATA,
  729. };
  730. struct ocfs2_xattr_search xbs = {
  731. .not_found = -ENODATA,
  732. };
  733. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  734. return -EOPNOTSUPP;
  735. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  736. ret = -ENODATA;
  737. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  738. if (ret < 0) {
  739. mlog_errno(ret);
  740. return ret;
  741. }
  742. xis.inode_bh = xbs.inode_bh = di_bh;
  743. di = (struct ocfs2_dinode *)di_bh->b_data;
  744. down_read(&oi->ip_xattr_sem);
  745. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  746. buffer_size, &xis);
  747. if (ret == -ENODATA)
  748. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  749. buffer_size, &xbs);
  750. up_read(&oi->ip_xattr_sem);
  751. ocfs2_inode_unlock(inode, 0);
  752. brelse(di_bh);
  753. return ret;
  754. }
  755. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  756. struct ocfs2_xattr_value_root *xv,
  757. const void *value,
  758. int value_len)
  759. {
  760. int ret = 0, i, cp_len, credits;
  761. u16 blocksize = inode->i_sb->s_blocksize;
  762. u32 p_cluster, num_clusters;
  763. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  764. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  765. u64 blkno;
  766. struct buffer_head *bh = NULL;
  767. handle_t *handle;
  768. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  769. credits = clusters * bpc;
  770. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
  771. if (IS_ERR(handle)) {
  772. ret = PTR_ERR(handle);
  773. mlog_errno(ret);
  774. goto out;
  775. }
  776. while (cpos < clusters) {
  777. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  778. &num_clusters, &xv->xr_list);
  779. if (ret) {
  780. mlog_errno(ret);
  781. goto out_commit;
  782. }
  783. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  784. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  785. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  786. &bh, OCFS2_BH_CACHED, inode);
  787. if (ret) {
  788. mlog_errno(ret);
  789. goto out_commit;
  790. }
  791. ret = ocfs2_journal_access(handle,
  792. inode,
  793. bh,
  794. OCFS2_JOURNAL_ACCESS_WRITE);
  795. if (ret < 0) {
  796. mlog_errno(ret);
  797. goto out_commit;
  798. }
  799. cp_len = value_len > blocksize ? blocksize : value_len;
  800. memcpy(bh->b_data, value, cp_len);
  801. value_len -= cp_len;
  802. value += cp_len;
  803. if (cp_len < blocksize)
  804. memset(bh->b_data + cp_len, 0,
  805. blocksize - cp_len);
  806. ret = ocfs2_journal_dirty(handle, bh);
  807. if (ret < 0) {
  808. mlog_errno(ret);
  809. goto out_commit;
  810. }
  811. brelse(bh);
  812. bh = NULL;
  813. /*
  814. * XXX: do we need to empty all the following
  815. * blocks in this cluster?
  816. */
  817. if (!value_len)
  818. break;
  819. }
  820. cpos += num_clusters;
  821. }
  822. out_commit:
  823. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  824. out:
  825. brelse(bh);
  826. return ret;
  827. }
  828. static int ocfs2_xattr_cleanup(struct inode *inode,
  829. struct ocfs2_xattr_info *xi,
  830. struct ocfs2_xattr_search *xs,
  831. size_t offs)
  832. {
  833. handle_t *handle = NULL;
  834. int ret = 0;
  835. size_t name_len = strlen(xi->name);
  836. void *val = xs->base + offs;
  837. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  838. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  839. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  840. if (IS_ERR(handle)) {
  841. ret = PTR_ERR(handle);
  842. mlog_errno(ret);
  843. goto out;
  844. }
  845. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  846. OCFS2_JOURNAL_ACCESS_WRITE);
  847. if (ret) {
  848. mlog_errno(ret);
  849. goto out_commit;
  850. }
  851. /* Decrease xattr count */
  852. le16_add_cpu(&xs->header->xh_count, -1);
  853. /* Remove the xattr entry and tree root which has already be set*/
  854. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  855. memset(val, 0, size);
  856. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  857. if (ret < 0)
  858. mlog_errno(ret);
  859. out_commit:
  860. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  861. out:
  862. return ret;
  863. }
  864. static int ocfs2_xattr_update_entry(struct inode *inode,
  865. struct ocfs2_xattr_info *xi,
  866. struct ocfs2_xattr_search *xs,
  867. size_t offs)
  868. {
  869. handle_t *handle = NULL;
  870. int ret = 0;
  871. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  872. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  873. if (IS_ERR(handle)) {
  874. ret = PTR_ERR(handle);
  875. mlog_errno(ret);
  876. goto out;
  877. }
  878. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  879. OCFS2_JOURNAL_ACCESS_WRITE);
  880. if (ret) {
  881. mlog_errno(ret);
  882. goto out_commit;
  883. }
  884. xs->here->xe_name_offset = cpu_to_le16(offs);
  885. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  886. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  887. ocfs2_xattr_set_local(xs->here, 1);
  888. else
  889. ocfs2_xattr_set_local(xs->here, 0);
  890. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  891. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  892. if (ret < 0)
  893. mlog_errno(ret);
  894. out_commit:
  895. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  896. out:
  897. return ret;
  898. }
  899. /*
  900. * ocfs2_xattr_set_value_outside()
  901. *
  902. * Set large size value in B tree.
  903. */
  904. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  905. struct ocfs2_xattr_info *xi,
  906. struct ocfs2_xattr_search *xs,
  907. size_t offs)
  908. {
  909. size_t name_len = strlen(xi->name);
  910. void *val = xs->base + offs;
  911. struct ocfs2_xattr_value_root *xv = NULL;
  912. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  913. int ret = 0;
  914. memset(val, 0, size);
  915. memcpy(val, xi->name, name_len);
  916. xv = (struct ocfs2_xattr_value_root *)
  917. (val + OCFS2_XATTR_SIZE(name_len));
  918. xv->xr_clusters = 0;
  919. xv->xr_last_eb_blk = 0;
  920. xv->xr_list.l_tree_depth = 0;
  921. xv->xr_list.l_count = cpu_to_le16(1);
  922. xv->xr_list.l_next_free_rec = 0;
  923. ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
  924. xi->value_len);
  925. if (ret < 0) {
  926. mlog_errno(ret);
  927. return ret;
  928. }
  929. ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
  930. xi->value_len);
  931. if (ret < 0) {
  932. mlog_errno(ret);
  933. return ret;
  934. }
  935. ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
  936. if (ret < 0)
  937. mlog_errno(ret);
  938. return ret;
  939. }
  940. /*
  941. * ocfs2_xattr_set_entry_local()
  942. *
  943. * Set, replace or remove extended attribute in local.
  944. */
  945. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  946. struct ocfs2_xattr_info *xi,
  947. struct ocfs2_xattr_search *xs,
  948. struct ocfs2_xattr_entry *last,
  949. size_t min_offs)
  950. {
  951. size_t name_len = strlen(xi->name);
  952. int i;
  953. if (xi->value && xs->not_found) {
  954. /* Insert the new xattr entry. */
  955. le16_add_cpu(&xs->header->xh_count, 1);
  956. ocfs2_xattr_set_type(last, xi->name_index);
  957. ocfs2_xattr_set_local(last, 1);
  958. last->xe_name_len = name_len;
  959. } else {
  960. void *first_val;
  961. void *val;
  962. size_t offs, size;
  963. first_val = xs->base + min_offs;
  964. offs = le16_to_cpu(xs->here->xe_name_offset);
  965. val = xs->base + offs;
  966. if (le64_to_cpu(xs->here->xe_value_size) >
  967. OCFS2_XATTR_INLINE_SIZE)
  968. size = OCFS2_XATTR_SIZE(name_len) +
  969. OCFS2_XATTR_ROOT_SIZE;
  970. else
  971. size = OCFS2_XATTR_SIZE(name_len) +
  972. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  973. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  974. OCFS2_XATTR_SIZE(xi->value_len)) {
  975. /* The old and the new value have the
  976. same size. Just replace the value. */
  977. ocfs2_xattr_set_local(xs->here, 1);
  978. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  979. /* Clear value bytes. */
  980. memset(val + OCFS2_XATTR_SIZE(name_len),
  981. 0,
  982. OCFS2_XATTR_SIZE(xi->value_len));
  983. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  984. xi->value,
  985. xi->value_len);
  986. return;
  987. }
  988. /* Remove the old name+value. */
  989. memmove(first_val + size, first_val, val - first_val);
  990. memset(first_val, 0, size);
  991. xs->here->xe_name_hash = 0;
  992. xs->here->xe_name_offset = 0;
  993. ocfs2_xattr_set_local(xs->here, 1);
  994. xs->here->xe_value_size = 0;
  995. min_offs += size;
  996. /* Adjust all value offsets. */
  997. last = xs->header->xh_entries;
  998. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  999. size_t o = le16_to_cpu(last->xe_name_offset);
  1000. if (o < offs)
  1001. last->xe_name_offset = cpu_to_le16(o + size);
  1002. last += 1;
  1003. }
  1004. if (!xi->value) {
  1005. /* Remove the old entry. */
  1006. last -= 1;
  1007. memmove(xs->here, xs->here + 1,
  1008. (void *)last - (void *)xs->here);
  1009. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1010. le16_add_cpu(&xs->header->xh_count, -1);
  1011. }
  1012. }
  1013. if (xi->value) {
  1014. /* Insert the new name+value. */
  1015. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1016. OCFS2_XATTR_SIZE(xi->value_len);
  1017. void *val = xs->base + min_offs - size;
  1018. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1019. memset(val, 0, size);
  1020. memcpy(val, xi->name, name_len);
  1021. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1022. xi->value,
  1023. xi->value_len);
  1024. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1025. ocfs2_xattr_set_local(xs->here, 1);
  1026. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1027. }
  1028. return;
  1029. }
  1030. /*
  1031. * ocfs2_xattr_set_entry()
  1032. *
  1033. * Set extended attribute entry into inode or block.
  1034. *
  1035. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1036. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1037. * then set value in B tree with set_value_outside().
  1038. */
  1039. static int ocfs2_xattr_set_entry(struct inode *inode,
  1040. struct ocfs2_xattr_info *xi,
  1041. struct ocfs2_xattr_search *xs,
  1042. int flag)
  1043. {
  1044. struct ocfs2_xattr_entry *last;
  1045. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1046. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1047. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1048. size_t size_l = 0;
  1049. handle_t *handle = NULL;
  1050. int free, i, ret;
  1051. struct ocfs2_xattr_info xi_l = {
  1052. .name_index = xi->name_index,
  1053. .name = xi->name,
  1054. .value = xi->value,
  1055. .value_len = xi->value_len,
  1056. };
  1057. /* Compute min_offs, last and free space. */
  1058. last = xs->header->xh_entries;
  1059. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1060. size_t offs = le16_to_cpu(last->xe_name_offset);
  1061. if (offs < min_offs)
  1062. min_offs = offs;
  1063. last += 1;
  1064. }
  1065. free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
  1066. if (free < 0)
  1067. return -EFAULT;
  1068. if (!xs->not_found) {
  1069. size_t size = 0;
  1070. if (ocfs2_xattr_is_local(xs->here))
  1071. size = OCFS2_XATTR_SIZE(name_len) +
  1072. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1073. else
  1074. size = OCFS2_XATTR_SIZE(name_len) +
  1075. OCFS2_XATTR_ROOT_SIZE;
  1076. free += (size + sizeof(struct ocfs2_xattr_entry));
  1077. }
  1078. /* Check free space in inode or block */
  1079. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1080. if (free < sizeof(struct ocfs2_xattr_entry) +
  1081. OCFS2_XATTR_SIZE(name_len) +
  1082. OCFS2_XATTR_ROOT_SIZE) {
  1083. ret = -ENOSPC;
  1084. goto out;
  1085. }
  1086. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1087. xi_l.value = (void *)&def_xv;
  1088. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1089. } else if (xi->value) {
  1090. if (free < sizeof(struct ocfs2_xattr_entry) +
  1091. OCFS2_XATTR_SIZE(name_len) +
  1092. OCFS2_XATTR_SIZE(xi->value_len)) {
  1093. ret = -ENOSPC;
  1094. goto out;
  1095. }
  1096. }
  1097. if (!xs->not_found) {
  1098. /* For existing extended attribute */
  1099. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1100. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1101. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1102. void *val = xs->base + offs;
  1103. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1104. /* Replace existing local xattr with tree root */
  1105. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1106. offs);
  1107. if (ret < 0)
  1108. mlog_errno(ret);
  1109. goto out;
  1110. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1111. /* For existing xattr which has value outside */
  1112. struct ocfs2_xattr_value_root *xv = NULL;
  1113. xv = (struct ocfs2_xattr_value_root *)(val +
  1114. OCFS2_XATTR_SIZE(name_len));
  1115. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1116. /*
  1117. * If new value need set outside also,
  1118. * first truncate old value to new value,
  1119. * then set new value with set_value_outside().
  1120. */
  1121. ret = ocfs2_xattr_value_truncate(inode,
  1122. xs->xattr_bh,
  1123. xv,
  1124. xi->value_len);
  1125. if (ret < 0) {
  1126. mlog_errno(ret);
  1127. goto out;
  1128. }
  1129. ret = __ocfs2_xattr_set_value_outside(inode,
  1130. xv,
  1131. xi->value,
  1132. xi->value_len);
  1133. if (ret < 0) {
  1134. mlog_errno(ret);
  1135. goto out;
  1136. }
  1137. ret = ocfs2_xattr_update_entry(inode,
  1138. xi,
  1139. xs,
  1140. offs);
  1141. if (ret < 0)
  1142. mlog_errno(ret);
  1143. goto out;
  1144. } else {
  1145. /*
  1146. * If new value need set in local,
  1147. * just trucate old value to zero.
  1148. */
  1149. ret = ocfs2_xattr_value_truncate(inode,
  1150. xs->xattr_bh,
  1151. xv,
  1152. 0);
  1153. if (ret < 0)
  1154. mlog_errno(ret);
  1155. }
  1156. }
  1157. }
  1158. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1159. OCFS2_INODE_UPDATE_CREDITS);
  1160. if (IS_ERR(handle)) {
  1161. ret = PTR_ERR(handle);
  1162. mlog_errno(ret);
  1163. goto out;
  1164. }
  1165. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1166. OCFS2_JOURNAL_ACCESS_WRITE);
  1167. if (ret) {
  1168. mlog_errno(ret);
  1169. goto out_commit;
  1170. }
  1171. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1172. /* set extended attribute in external block. */
  1173. ret = ocfs2_extend_trans(handle,
  1174. OCFS2_INODE_UPDATE_CREDITS +
  1175. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1176. if (ret) {
  1177. mlog_errno(ret);
  1178. goto out_commit;
  1179. }
  1180. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1181. OCFS2_JOURNAL_ACCESS_WRITE);
  1182. if (ret) {
  1183. mlog_errno(ret);
  1184. goto out_commit;
  1185. }
  1186. }
  1187. /*
  1188. * Set value in local, include set tree root in local.
  1189. * This is the first step for value size >INLINE_SIZE.
  1190. */
  1191. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1192. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1193. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1194. if (ret < 0) {
  1195. mlog_errno(ret);
  1196. goto out_commit;
  1197. }
  1198. }
  1199. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1200. (flag & OCFS2_INLINE_XATTR_FL)) {
  1201. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1202. unsigned int xattrsize = osb->s_xattr_inline_size;
  1203. /*
  1204. * Adjust extent record count or inline data size
  1205. * to reserve space for extended attribute.
  1206. */
  1207. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1208. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1209. le16_add_cpu(&idata->id_count, -xattrsize);
  1210. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1211. struct ocfs2_extent_list *el = &di->id2.i_list;
  1212. le16_add_cpu(&el->l_count, -(xattrsize /
  1213. sizeof(struct ocfs2_extent_rec)));
  1214. }
  1215. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1216. }
  1217. /* Update xattr flag */
  1218. spin_lock(&oi->ip_lock);
  1219. oi->ip_dyn_features |= flag;
  1220. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1221. spin_unlock(&oi->ip_lock);
  1222. /* Update inode ctime */
  1223. inode->i_ctime = CURRENT_TIME;
  1224. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1225. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1226. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1227. if (ret < 0)
  1228. mlog_errno(ret);
  1229. out_commit:
  1230. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1231. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1232. /*
  1233. * Set value outside in B tree.
  1234. * This is the second step for value size > INLINE_SIZE.
  1235. */
  1236. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1237. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
  1238. if (ret < 0) {
  1239. int ret2;
  1240. mlog_errno(ret);
  1241. /*
  1242. * If set value outside failed, we have to clean
  1243. * the junk tree root we have already set in local.
  1244. */
  1245. ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
  1246. if (ret2 < 0)
  1247. mlog_errno(ret2);
  1248. }
  1249. }
  1250. out:
  1251. return ret;
  1252. }
  1253. static int ocfs2_remove_value_outside(struct inode*inode,
  1254. struct buffer_head *bh,
  1255. struct ocfs2_xattr_header *header)
  1256. {
  1257. int ret = 0, i;
  1258. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1259. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1260. if (!ocfs2_xattr_is_local(entry)) {
  1261. struct ocfs2_xattr_value_root *xv;
  1262. void *val;
  1263. val = (void *)header +
  1264. le16_to_cpu(entry->xe_name_offset);
  1265. xv = (struct ocfs2_xattr_value_root *)
  1266. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1267. ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
  1268. if (ret < 0) {
  1269. mlog_errno(ret);
  1270. return ret;
  1271. }
  1272. }
  1273. }
  1274. return ret;
  1275. }
  1276. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1277. struct buffer_head *di_bh)
  1278. {
  1279. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1280. struct ocfs2_xattr_header *header;
  1281. int ret;
  1282. header = (struct ocfs2_xattr_header *)
  1283. ((void *)di + inode->i_sb->s_blocksize -
  1284. le16_to_cpu(di->i_xattr_inline_size));
  1285. ret = ocfs2_remove_value_outside(inode, di_bh, header);
  1286. return ret;
  1287. }
  1288. static int ocfs2_xattr_block_remove(struct inode *inode,
  1289. struct buffer_head *blk_bh)
  1290. {
  1291. struct ocfs2_xattr_block *xb;
  1292. int ret = 0;
  1293. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1294. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1295. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1296. ret = ocfs2_remove_value_outside(inode, blk_bh, header);
  1297. } else
  1298. ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
  1299. return ret;
  1300. }
  1301. static int ocfs2_xattr_free_block(struct inode *inode,
  1302. u64 block)
  1303. {
  1304. struct inode *xb_alloc_inode;
  1305. struct buffer_head *xb_alloc_bh = NULL;
  1306. struct buffer_head *blk_bh = NULL;
  1307. struct ocfs2_xattr_block *xb;
  1308. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1309. handle_t *handle;
  1310. int ret = 0;
  1311. u64 blk, bg_blkno;
  1312. u16 bit;
  1313. ret = ocfs2_read_block(osb, block, &blk_bh,
  1314. OCFS2_BH_CACHED, inode);
  1315. if (ret < 0) {
  1316. mlog_errno(ret);
  1317. goto out;
  1318. }
  1319. /*Verify the signature of xattr block*/
  1320. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1321. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1322. ret = -EFAULT;
  1323. goto out;
  1324. }
  1325. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1326. if (ret < 0) {
  1327. mlog_errno(ret);
  1328. goto out;
  1329. }
  1330. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1331. blk = le64_to_cpu(xb->xb_blkno);
  1332. bit = le16_to_cpu(xb->xb_suballoc_bit);
  1333. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1334. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1335. EXTENT_ALLOC_SYSTEM_INODE,
  1336. le16_to_cpu(xb->xb_suballoc_slot));
  1337. if (!xb_alloc_inode) {
  1338. ret = -ENOMEM;
  1339. mlog_errno(ret);
  1340. goto out;
  1341. }
  1342. mutex_lock(&xb_alloc_inode->i_mutex);
  1343. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1344. if (ret < 0) {
  1345. mlog_errno(ret);
  1346. goto out_mutex;
  1347. }
  1348. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  1349. if (IS_ERR(handle)) {
  1350. ret = PTR_ERR(handle);
  1351. mlog_errno(ret);
  1352. goto out_unlock;
  1353. }
  1354. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1355. bit, bg_blkno, 1);
  1356. if (ret < 0)
  1357. mlog_errno(ret);
  1358. ocfs2_commit_trans(osb, handle);
  1359. out_unlock:
  1360. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1361. brelse(xb_alloc_bh);
  1362. out_mutex:
  1363. mutex_unlock(&xb_alloc_inode->i_mutex);
  1364. iput(xb_alloc_inode);
  1365. out:
  1366. brelse(blk_bh);
  1367. return ret;
  1368. }
  1369. /*
  1370. * ocfs2_xattr_remove()
  1371. *
  1372. * Free extended attribute resources associated with this inode.
  1373. */
  1374. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1375. {
  1376. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1377. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1378. handle_t *handle;
  1379. int ret;
  1380. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1381. return 0;
  1382. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1383. return 0;
  1384. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1385. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1386. if (ret < 0) {
  1387. mlog_errno(ret);
  1388. goto out;
  1389. }
  1390. }
  1391. if (di->i_xattr_loc) {
  1392. ret = ocfs2_xattr_free_block(inode,
  1393. le64_to_cpu(di->i_xattr_loc));
  1394. if (ret < 0) {
  1395. mlog_errno(ret);
  1396. goto out;
  1397. }
  1398. }
  1399. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1400. OCFS2_INODE_UPDATE_CREDITS);
  1401. if (IS_ERR(handle)) {
  1402. ret = PTR_ERR(handle);
  1403. mlog_errno(ret);
  1404. goto out;
  1405. }
  1406. ret = ocfs2_journal_access(handle, inode, di_bh,
  1407. OCFS2_JOURNAL_ACCESS_WRITE);
  1408. if (ret) {
  1409. mlog_errno(ret);
  1410. goto out_commit;
  1411. }
  1412. di->i_xattr_loc = 0;
  1413. spin_lock(&oi->ip_lock);
  1414. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1415. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1416. spin_unlock(&oi->ip_lock);
  1417. ret = ocfs2_journal_dirty(handle, di_bh);
  1418. if (ret < 0)
  1419. mlog_errno(ret);
  1420. out_commit:
  1421. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1422. out:
  1423. return ret;
  1424. }
  1425. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1426. struct ocfs2_dinode *di)
  1427. {
  1428. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1429. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1430. int free;
  1431. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1432. return 0;
  1433. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1434. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1435. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1436. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1437. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1438. le64_to_cpu(di->i_size);
  1439. } else {
  1440. struct ocfs2_extent_list *el = &di->id2.i_list;
  1441. free = (le16_to_cpu(el->l_count) -
  1442. le16_to_cpu(el->l_next_free_rec)) *
  1443. sizeof(struct ocfs2_extent_rec);
  1444. }
  1445. if (free >= xattrsize)
  1446. return 1;
  1447. return 0;
  1448. }
  1449. /*
  1450. * ocfs2_xattr_ibody_find()
  1451. *
  1452. * Find extended attribute in inode block and
  1453. * fill search info into struct ocfs2_xattr_search.
  1454. */
  1455. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1456. int name_index,
  1457. const char *name,
  1458. struct ocfs2_xattr_search *xs)
  1459. {
  1460. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1461. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1462. int ret;
  1463. int has_space = 0;
  1464. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1465. return 0;
  1466. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1467. down_read(&oi->ip_alloc_sem);
  1468. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1469. up_read(&oi->ip_alloc_sem);
  1470. if (!has_space)
  1471. return 0;
  1472. }
  1473. xs->xattr_bh = xs->inode_bh;
  1474. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1475. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1476. xs->header = (struct ocfs2_xattr_header *)
  1477. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1478. else
  1479. xs->header = (struct ocfs2_xattr_header *)
  1480. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1481. xs->base = (void *)xs->header;
  1482. xs->here = xs->header->xh_entries;
  1483. /* Find the named attribute. */
  1484. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1485. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1486. if (ret && ret != -ENODATA)
  1487. return ret;
  1488. xs->not_found = ret;
  1489. }
  1490. return 0;
  1491. }
  1492. /*
  1493. * ocfs2_xattr_ibody_set()
  1494. *
  1495. * Set, replace or remove an extended attribute into inode block.
  1496. *
  1497. */
  1498. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1499. struct ocfs2_xattr_info *xi,
  1500. struct ocfs2_xattr_search *xs)
  1501. {
  1502. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1503. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1504. int ret;
  1505. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1506. return -ENOSPC;
  1507. down_write(&oi->ip_alloc_sem);
  1508. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1509. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1510. ret = -ENOSPC;
  1511. goto out;
  1512. }
  1513. }
  1514. ret = ocfs2_xattr_set_entry(inode, xi, xs,
  1515. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1516. out:
  1517. up_write(&oi->ip_alloc_sem);
  1518. return ret;
  1519. }
  1520. /*
  1521. * ocfs2_xattr_block_find()
  1522. *
  1523. * Find extended attribute in external block and
  1524. * fill search info into struct ocfs2_xattr_search.
  1525. */
  1526. static int ocfs2_xattr_block_find(struct inode *inode,
  1527. int name_index,
  1528. const char *name,
  1529. struct ocfs2_xattr_search *xs)
  1530. {
  1531. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1532. struct buffer_head *blk_bh = NULL;
  1533. struct ocfs2_xattr_block *xb;
  1534. int ret = 0;
  1535. if (!di->i_xattr_loc)
  1536. return ret;
  1537. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1538. le64_to_cpu(di->i_xattr_loc),
  1539. &blk_bh, OCFS2_BH_CACHED, inode);
  1540. if (ret < 0) {
  1541. mlog_errno(ret);
  1542. return ret;
  1543. }
  1544. /*Verify the signature of xattr block*/
  1545. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1546. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1547. ret = -EFAULT;
  1548. goto cleanup;
  1549. }
  1550. xs->xattr_bh = blk_bh;
  1551. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1552. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1553. xs->header = &xb->xb_attrs.xb_header;
  1554. xs->base = (void *)xs->header;
  1555. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1556. xs->here = xs->header->xh_entries;
  1557. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1558. } else
  1559. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1560. name_index,
  1561. name, xs);
  1562. if (ret && ret != -ENODATA) {
  1563. xs->xattr_bh = NULL;
  1564. goto cleanup;
  1565. }
  1566. xs->not_found = ret;
  1567. return 0;
  1568. cleanup:
  1569. brelse(blk_bh);
  1570. return ret;
  1571. }
  1572. /*
  1573. * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
  1574. * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
  1575. * re-initialized.
  1576. */
  1577. static int ocfs2_restore_xattr_block(struct inode *inode,
  1578. struct ocfs2_xattr_search *xs)
  1579. {
  1580. int ret;
  1581. handle_t *handle;
  1582. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1583. struct ocfs2_xattr_block *xb =
  1584. (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1585. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  1586. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  1587. BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
  1588. le16_to_cpu(el->l_next_free_rec) != 0);
  1589. handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1590. if (IS_ERR(handle)) {
  1591. ret = PTR_ERR(handle);
  1592. handle = NULL;
  1593. goto out;
  1594. }
  1595. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1596. OCFS2_JOURNAL_ACCESS_WRITE);
  1597. if (ret < 0) {
  1598. mlog_errno(ret);
  1599. goto out_commit;
  1600. }
  1601. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  1602. offsetof(struct ocfs2_xattr_block, xb_attrs));
  1603. xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
  1604. ocfs2_journal_dirty(handle, xs->xattr_bh);
  1605. out_commit:
  1606. ocfs2_commit_trans(osb, handle);
  1607. out:
  1608. return ret;
  1609. }
  1610. /*
  1611. * ocfs2_xattr_block_set()
  1612. *
  1613. * Set, replace or remove an extended attribute into external block.
  1614. *
  1615. */
  1616. static int ocfs2_xattr_block_set(struct inode *inode,
  1617. struct ocfs2_xattr_info *xi,
  1618. struct ocfs2_xattr_search *xs)
  1619. {
  1620. struct buffer_head *new_bh = NULL;
  1621. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1622. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1623. struct ocfs2_alloc_context *meta_ac = NULL;
  1624. handle_t *handle = NULL;
  1625. struct ocfs2_xattr_block *xblk = NULL;
  1626. u16 suballoc_bit_start;
  1627. u32 num_got;
  1628. u64 first_blkno;
  1629. int ret;
  1630. if (!xs->xattr_bh) {
  1631. /*
  1632. * Alloc one external block for extended attribute
  1633. * outside of inode.
  1634. */
  1635. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  1636. if (ret < 0) {
  1637. mlog_errno(ret);
  1638. goto out;
  1639. }
  1640. handle = ocfs2_start_trans(osb,
  1641. OCFS2_XATTR_BLOCK_CREATE_CREDITS);
  1642. if (IS_ERR(handle)) {
  1643. ret = PTR_ERR(handle);
  1644. mlog_errno(ret);
  1645. goto out;
  1646. }
  1647. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1648. OCFS2_JOURNAL_ACCESS_CREATE);
  1649. if (ret < 0) {
  1650. mlog_errno(ret);
  1651. goto out_commit;
  1652. }
  1653. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  1654. &suballoc_bit_start, &num_got,
  1655. &first_blkno);
  1656. if (ret < 0) {
  1657. mlog_errno(ret);
  1658. goto out_commit;
  1659. }
  1660. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1661. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  1662. ret = ocfs2_journal_access(handle, inode, new_bh,
  1663. OCFS2_JOURNAL_ACCESS_CREATE);
  1664. if (ret < 0) {
  1665. mlog_errno(ret);
  1666. goto out_commit;
  1667. }
  1668. /* Initialize ocfs2_xattr_block */
  1669. xs->xattr_bh = new_bh;
  1670. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1671. memset(xblk, 0, inode->i_sb->s_blocksize);
  1672. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1673. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1674. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1675. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1676. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1677. xs->header = &xblk->xb_attrs.xb_header;
  1678. xs->base = (void *)xs->header;
  1679. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1680. xs->here = xs->header->xh_entries;
  1681. ret = ocfs2_journal_dirty(handle, new_bh);
  1682. if (ret < 0) {
  1683. mlog_errno(ret);
  1684. goto out_commit;
  1685. }
  1686. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1687. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1688. if (ret < 0)
  1689. mlog_errno(ret);
  1690. out_commit:
  1691. ocfs2_commit_trans(osb, handle);
  1692. out:
  1693. if (meta_ac)
  1694. ocfs2_free_alloc_context(meta_ac);
  1695. if (ret < 0)
  1696. return ret;
  1697. } else
  1698. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1699. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1700. /* Set extended attribute into external block */
  1701. ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
  1702. if (!ret || ret != -ENOSPC)
  1703. goto end;
  1704. ret = ocfs2_xattr_create_index_block(inode, xs);
  1705. if (ret)
  1706. goto end;
  1707. }
  1708. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
  1709. if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
  1710. ret = ocfs2_restore_xattr_block(inode, xs);
  1711. end:
  1712. return ret;
  1713. }
  1714. /*
  1715. * ocfs2_xattr_set()
  1716. *
  1717. * Set, replace or remove an extended attribute for this inode.
  1718. * value is NULL to remove an existing extended attribute, else either
  1719. * create or replace an extended attribute.
  1720. */
  1721. int ocfs2_xattr_set(struct inode *inode,
  1722. int name_index,
  1723. const char *name,
  1724. const void *value,
  1725. size_t value_len,
  1726. int flags)
  1727. {
  1728. struct buffer_head *di_bh = NULL;
  1729. struct ocfs2_dinode *di;
  1730. int ret;
  1731. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1732. struct ocfs2_xattr_info xi = {
  1733. .name_index = name_index,
  1734. .name = name,
  1735. .value = value,
  1736. .value_len = value_len,
  1737. };
  1738. struct ocfs2_xattr_search xis = {
  1739. .not_found = -ENODATA,
  1740. };
  1741. struct ocfs2_xattr_search xbs = {
  1742. .not_found = -ENODATA,
  1743. };
  1744. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1745. return -EOPNOTSUPP;
  1746. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1747. if (ret < 0) {
  1748. mlog_errno(ret);
  1749. return ret;
  1750. }
  1751. xis.inode_bh = xbs.inode_bh = di_bh;
  1752. di = (struct ocfs2_dinode *)di_bh->b_data;
  1753. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  1754. /*
  1755. * Scan inode and external block to find the same name
  1756. * extended attribute and collect search infomation.
  1757. */
  1758. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  1759. if (ret)
  1760. goto cleanup;
  1761. if (xis.not_found) {
  1762. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  1763. if (ret)
  1764. goto cleanup;
  1765. }
  1766. if (xis.not_found && xbs.not_found) {
  1767. ret = -ENODATA;
  1768. if (flags & XATTR_REPLACE)
  1769. goto cleanup;
  1770. ret = 0;
  1771. if (!value)
  1772. goto cleanup;
  1773. } else {
  1774. ret = -EEXIST;
  1775. if (flags & XATTR_CREATE)
  1776. goto cleanup;
  1777. }
  1778. if (!value) {
  1779. /* Remove existing extended attribute */
  1780. if (!xis.not_found)
  1781. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1782. else if (!xbs.not_found)
  1783. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1784. } else {
  1785. /* We always try to set extended attribute into inode first*/
  1786. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1787. if (!ret && !xbs.not_found) {
  1788. /*
  1789. * If succeed and that extended attribute existing in
  1790. * external block, then we will remove it.
  1791. */
  1792. xi.value = NULL;
  1793. xi.value_len = 0;
  1794. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1795. } else if (ret == -ENOSPC) {
  1796. if (di->i_xattr_loc && !xbs.xattr_bh) {
  1797. ret = ocfs2_xattr_block_find(inode, name_index,
  1798. name, &xbs);
  1799. if (ret)
  1800. goto cleanup;
  1801. }
  1802. /*
  1803. * If no space in inode, we will set extended attribute
  1804. * into external block.
  1805. */
  1806. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1807. if (ret)
  1808. goto cleanup;
  1809. if (!xis.not_found) {
  1810. /*
  1811. * If succeed and that extended attribute
  1812. * existing in inode, we will remove it.
  1813. */
  1814. xi.value = NULL;
  1815. xi.value_len = 0;
  1816. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1817. }
  1818. }
  1819. }
  1820. cleanup:
  1821. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  1822. ocfs2_inode_unlock(inode, 1);
  1823. brelse(di_bh);
  1824. brelse(xbs.xattr_bh);
  1825. for (i = 0; i < blk_per_bucket; i++)
  1826. brelse(xbs.bucket.bhs[i]);
  1827. return ret;
  1828. }
  1829. static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
  1830. int name_index,
  1831. const char *suffix_name)
  1832. {
  1833. struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
  1834. char *prefix = handler->prefix;
  1835. int prefix_len = strlen(handler->prefix);
  1836. return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
  1837. (char *)suffix_name, strlen(suffix_name));
  1838. }
  1839. /*
  1840. * Find the xattr extent rec which may contains name_hash.
  1841. * e_cpos will be the first name hash of the xattr rec.
  1842. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  1843. */
  1844. static int ocfs2_xattr_get_rec(struct inode *inode,
  1845. u32 name_hash,
  1846. u64 *p_blkno,
  1847. u32 *e_cpos,
  1848. u32 *num_clusters,
  1849. struct ocfs2_extent_list *el)
  1850. {
  1851. int ret = 0, i;
  1852. struct buffer_head *eb_bh = NULL;
  1853. struct ocfs2_extent_block *eb;
  1854. struct ocfs2_extent_rec *rec = NULL;
  1855. u64 e_blkno = 0;
  1856. if (el->l_tree_depth) {
  1857. ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
  1858. if (ret) {
  1859. mlog_errno(ret);
  1860. goto out;
  1861. }
  1862. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  1863. el = &eb->h_list;
  1864. if (el->l_tree_depth) {
  1865. ocfs2_error(inode->i_sb,
  1866. "Inode %lu has non zero tree depth in "
  1867. "xattr tree block %llu\n", inode->i_ino,
  1868. (unsigned long long)eb_bh->b_blocknr);
  1869. ret = -EROFS;
  1870. goto out;
  1871. }
  1872. }
  1873. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1874. rec = &el->l_recs[i];
  1875. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  1876. e_blkno = le64_to_cpu(rec->e_blkno);
  1877. break;
  1878. }
  1879. }
  1880. if (!e_blkno) {
  1881. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  1882. "record (%u, %u, 0) in xattr", inode->i_ino,
  1883. le32_to_cpu(rec->e_cpos),
  1884. ocfs2_rec_clusters(el, rec));
  1885. ret = -EROFS;
  1886. goto out;
  1887. }
  1888. *p_blkno = le64_to_cpu(rec->e_blkno);
  1889. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  1890. if (e_cpos)
  1891. *e_cpos = le32_to_cpu(rec->e_cpos);
  1892. out:
  1893. brelse(eb_bh);
  1894. return ret;
  1895. }
  1896. typedef int (xattr_bucket_func)(struct inode *inode,
  1897. struct ocfs2_xattr_bucket *bucket,
  1898. void *para);
  1899. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  1900. struct buffer_head *header_bh,
  1901. int name_index,
  1902. const char *name,
  1903. u32 name_hash,
  1904. u16 *xe_index,
  1905. int *found)
  1906. {
  1907. int i, ret = 0, cmp = 1, block_off, new_offset;
  1908. struct ocfs2_xattr_header *xh =
  1909. (struct ocfs2_xattr_header *)header_bh->b_data;
  1910. size_t name_len = strlen(name);
  1911. struct ocfs2_xattr_entry *xe = NULL;
  1912. struct buffer_head *name_bh = NULL;
  1913. char *xe_name;
  1914. /*
  1915. * We don't use binary search in the bucket because there
  1916. * may be multiple entries with the same name hash.
  1917. */
  1918. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  1919. xe = &xh->xh_entries[i];
  1920. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  1921. continue;
  1922. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  1923. break;
  1924. cmp = name_index - ocfs2_xattr_get_type(xe);
  1925. if (!cmp)
  1926. cmp = name_len - xe->xe_name_len;
  1927. if (cmp)
  1928. continue;
  1929. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1930. xh,
  1931. i,
  1932. &block_off,
  1933. &new_offset);
  1934. if (ret) {
  1935. mlog_errno(ret);
  1936. break;
  1937. }
  1938. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1939. header_bh->b_blocknr + block_off,
  1940. &name_bh, OCFS2_BH_CACHED, inode);
  1941. if (ret) {
  1942. mlog_errno(ret);
  1943. break;
  1944. }
  1945. xe_name = name_bh->b_data + new_offset;
  1946. cmp = memcmp(name, xe_name, name_len);
  1947. brelse(name_bh);
  1948. name_bh = NULL;
  1949. if (cmp == 0) {
  1950. *xe_index = i;
  1951. *found = 1;
  1952. ret = 0;
  1953. break;
  1954. }
  1955. }
  1956. return ret;
  1957. }
  1958. /*
  1959. * Find the specified xattr entry in a series of buckets.
  1960. * This series start from p_blkno and last for num_clusters.
  1961. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  1962. * the num of the valid buckets.
  1963. *
  1964. * Return the buffer_head this xattr should reside in. And if the xattr's
  1965. * hash is in the gap of 2 buckets, return the lower bucket.
  1966. */
  1967. static int ocfs2_xattr_bucket_find(struct inode *inode,
  1968. int name_index,
  1969. const char *name,
  1970. u32 name_hash,
  1971. u64 p_blkno,
  1972. u32 first_hash,
  1973. u32 num_clusters,
  1974. struct ocfs2_xattr_search *xs)
  1975. {
  1976. int ret, found = 0;
  1977. struct buffer_head *bh = NULL;
  1978. struct buffer_head *lower_bh = NULL;
  1979. struct ocfs2_xattr_header *xh = NULL;
  1980. struct ocfs2_xattr_entry *xe = NULL;
  1981. u16 index = 0;
  1982. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1983. int low_bucket = 0, bucket, high_bucket;
  1984. u32 last_hash;
  1985. u64 blkno;
  1986. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
  1987. &bh, OCFS2_BH_CACHED, inode);
  1988. if (ret) {
  1989. mlog_errno(ret);
  1990. goto out;
  1991. }
  1992. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1993. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  1994. while (low_bucket <= high_bucket) {
  1995. brelse(bh);
  1996. bh = NULL;
  1997. bucket = (low_bucket + high_bucket) / 2;
  1998. blkno = p_blkno + bucket * blk_per_bucket;
  1999. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  2000. &bh, OCFS2_BH_CACHED, inode);
  2001. if (ret) {
  2002. mlog_errno(ret);
  2003. goto out;
  2004. }
  2005. xh = (struct ocfs2_xattr_header *)bh->b_data;
  2006. xe = &xh->xh_entries[0];
  2007. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  2008. high_bucket = bucket - 1;
  2009. continue;
  2010. }
  2011. /*
  2012. * Check whether the hash of the last entry in our
  2013. * bucket is larger than the search one. for an empty
  2014. * bucket, the last one is also the first one.
  2015. */
  2016. if (xh->xh_count)
  2017. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  2018. last_hash = le32_to_cpu(xe->xe_name_hash);
  2019. /* record lower_bh which may be the insert place. */
  2020. brelse(lower_bh);
  2021. lower_bh = bh;
  2022. bh = NULL;
  2023. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2024. low_bucket = bucket + 1;
  2025. continue;
  2026. }
  2027. /* the searched xattr should reside in this bucket if exists. */
  2028. ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
  2029. name_index, name, name_hash,
  2030. &index, &found);
  2031. if (ret) {
  2032. mlog_errno(ret);
  2033. goto out;
  2034. }
  2035. break;
  2036. }
  2037. /*
  2038. * Record the bucket we have found.
  2039. * When the xattr's hash value is in the gap of 2 buckets, we will
  2040. * always set it to the previous bucket.
  2041. */
  2042. if (!lower_bh) {
  2043. /*
  2044. * We can't find any bucket whose first name_hash is less
  2045. * than the find name_hash.
  2046. */
  2047. BUG_ON(bh->b_blocknr != p_blkno);
  2048. lower_bh = bh;
  2049. bh = NULL;
  2050. }
  2051. xs->bucket.bhs[0] = lower_bh;
  2052. xs->bucket.xh = (struct ocfs2_xattr_header *)
  2053. xs->bucket.bhs[0]->b_data;
  2054. lower_bh = NULL;
  2055. xs->header = xs->bucket.xh;
  2056. xs->base = xs->bucket.bhs[0]->b_data;
  2057. xs->end = xs->base + inode->i_sb->s_blocksize;
  2058. if (found) {
  2059. /*
  2060. * If we have found the xattr enty, read all the blocks in
  2061. * this bucket.
  2062. */
  2063. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2064. xs->bucket.bhs[0]->b_blocknr + 1,
  2065. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2066. OCFS2_BH_CACHED, inode);
  2067. if (ret) {
  2068. mlog_errno(ret);
  2069. goto out;
  2070. }
  2071. xs->here = &xs->header->xh_entries[index];
  2072. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2073. (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
  2074. } else
  2075. ret = -ENODATA;
  2076. out:
  2077. brelse(bh);
  2078. brelse(lower_bh);
  2079. return ret;
  2080. }
  2081. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2082. struct buffer_head *root_bh,
  2083. int name_index,
  2084. const char *name,
  2085. struct ocfs2_xattr_search *xs)
  2086. {
  2087. int ret;
  2088. struct ocfs2_xattr_block *xb =
  2089. (struct ocfs2_xattr_block *)root_bh->b_data;
  2090. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2091. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2092. u64 p_blkno = 0;
  2093. u32 first_hash, num_clusters = 0;
  2094. u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
  2095. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2096. return -ENODATA;
  2097. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2098. name, name_hash, name_index);
  2099. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2100. &num_clusters, el);
  2101. if (ret) {
  2102. mlog_errno(ret);
  2103. goto out;
  2104. }
  2105. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2106. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2107. "in the rec is %u\n", num_clusters, p_blkno, first_hash);
  2108. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2109. p_blkno, first_hash, num_clusters, xs);
  2110. out:
  2111. return ret;
  2112. }
  2113. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2114. u64 blkno,
  2115. u32 clusters,
  2116. xattr_bucket_func *func,
  2117. void *para)
  2118. {
  2119. int i, j, ret = 0;
  2120. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2121. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2122. u32 num_buckets = clusters * bpc;
  2123. struct ocfs2_xattr_bucket bucket;
  2124. memset(&bucket, 0, sizeof(bucket));
  2125. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2126. clusters, blkno);
  2127. for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
  2128. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2129. blkno, blk_per_bucket,
  2130. bucket.bhs, OCFS2_BH_CACHED, inode);
  2131. if (ret) {
  2132. mlog_errno(ret);
  2133. goto out;
  2134. }
  2135. bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
  2136. /*
  2137. * The real bucket num in this series of blocks is stored
  2138. * in the 1st bucket.
  2139. */
  2140. if (i == 0)
  2141. num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
  2142. mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
  2143. le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
  2144. if (func) {
  2145. ret = func(inode, &bucket, para);
  2146. if (ret) {
  2147. mlog_errno(ret);
  2148. break;
  2149. }
  2150. }
  2151. for (j = 0; j < blk_per_bucket; j++)
  2152. brelse(bucket.bhs[j]);
  2153. memset(&bucket, 0, sizeof(bucket));
  2154. }
  2155. out:
  2156. for (j = 0; j < blk_per_bucket; j++)
  2157. brelse(bucket.bhs[j]);
  2158. return ret;
  2159. }
  2160. struct ocfs2_xattr_tree_list {
  2161. char *buffer;
  2162. size_t buffer_size;
  2163. };
  2164. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2165. struct ocfs2_xattr_header *xh,
  2166. int index,
  2167. int *block_off,
  2168. int *new_offset)
  2169. {
  2170. u16 name_offset;
  2171. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2172. return -EINVAL;
  2173. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2174. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2175. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2176. return 0;
  2177. }
  2178. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2179. struct ocfs2_xattr_bucket *bucket,
  2180. void *para)
  2181. {
  2182. int ret = 0;
  2183. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2184. size_t size;
  2185. int i, block_off, new_offset;
  2186. for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
  2187. struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
  2188. struct xattr_handler *handler =
  2189. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  2190. if (handler) {
  2191. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2192. bucket->xh,
  2193. i,
  2194. &block_off,
  2195. &new_offset);
  2196. if (ret)
  2197. break;
  2198. size = handler->list(inode, xl->buffer, xl->buffer_size,
  2199. bucket->bhs[block_off]->b_data +
  2200. new_offset,
  2201. entry->xe_name_len);
  2202. if (xl->buffer) {
  2203. if (size > xl->buffer_size)
  2204. return -ERANGE;
  2205. xl->buffer += size;
  2206. }
  2207. xl->buffer_size -= size;
  2208. }
  2209. }
  2210. return ret;
  2211. }
  2212. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2213. struct ocfs2_xattr_tree_root *xt,
  2214. char *buffer,
  2215. size_t buffer_size)
  2216. {
  2217. struct ocfs2_extent_list *el = &xt->xt_list;
  2218. int ret = 0;
  2219. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2220. u64 p_blkno = 0;
  2221. struct ocfs2_xattr_tree_list xl = {
  2222. .buffer = buffer,
  2223. .buffer_size = buffer_size,
  2224. };
  2225. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2226. return 0;
  2227. while (name_hash > 0) {
  2228. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2229. &e_cpos, &num_clusters, el);
  2230. if (ret) {
  2231. mlog_errno(ret);
  2232. goto out;
  2233. }
  2234. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2235. ocfs2_list_xattr_bucket,
  2236. &xl);
  2237. if (ret) {
  2238. mlog_errno(ret);
  2239. goto out;
  2240. }
  2241. if (e_cpos == 0)
  2242. break;
  2243. name_hash = e_cpos - 1;
  2244. }
  2245. ret = buffer_size - xl.buffer_size;
  2246. out:
  2247. return ret;
  2248. }
  2249. static int cmp_xe(const void *a, const void *b)
  2250. {
  2251. const struct ocfs2_xattr_entry *l = a, *r = b;
  2252. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2253. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2254. if (l_hash > r_hash)
  2255. return 1;
  2256. if (l_hash < r_hash)
  2257. return -1;
  2258. return 0;
  2259. }
  2260. static void swap_xe(void *a, void *b, int size)
  2261. {
  2262. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2263. tmp = *l;
  2264. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2265. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2266. }
  2267. /*
  2268. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2269. * and all the xattr entries will be moved to the new bucket.
  2270. * Note: we need to sort the entries since they are not saved in order
  2271. * in the ocfs2_xattr_block.
  2272. */
  2273. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2274. struct buffer_head *xb_bh,
  2275. struct buffer_head *xh_bh,
  2276. struct buffer_head *data_bh)
  2277. {
  2278. int i, blocksize = inode->i_sb->s_blocksize;
  2279. u16 offset, size, off_change;
  2280. struct ocfs2_xattr_entry *xe;
  2281. struct ocfs2_xattr_block *xb =
  2282. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2283. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  2284. struct ocfs2_xattr_header *xh =
  2285. (struct ocfs2_xattr_header *)xh_bh->b_data;
  2286. u16 count = le16_to_cpu(xb_xh->xh_count);
  2287. char *target = xh_bh->b_data, *src = xb_bh->b_data;
  2288. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  2289. (unsigned long long)xb_bh->b_blocknr,
  2290. (unsigned long long)xh_bh->b_blocknr);
  2291. memset(xh_bh->b_data, 0, blocksize);
  2292. if (data_bh)
  2293. memset(data_bh->b_data, 0, blocksize);
  2294. /*
  2295. * Since the xe_name_offset is based on ocfs2_xattr_header,
  2296. * there is a offset change corresponding to the change of
  2297. * ocfs2_xattr_header's position.
  2298. */
  2299. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2300. xe = &xb_xh->xh_entries[count - 1];
  2301. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  2302. size = blocksize - offset;
  2303. /* copy all the names and values. */
  2304. if (data_bh)
  2305. target = data_bh->b_data;
  2306. memcpy(target + offset, src + offset, size);
  2307. /* Init new header now. */
  2308. xh->xh_count = xb_xh->xh_count;
  2309. xh->xh_num_buckets = cpu_to_le16(1);
  2310. xh->xh_name_value_len = cpu_to_le16(size);
  2311. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  2312. /* copy all the entries. */
  2313. target = xh_bh->b_data;
  2314. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  2315. size = count * sizeof(struct ocfs2_xattr_entry);
  2316. memcpy(target + offset, (char *)xb_xh + offset, size);
  2317. /* Change the xe offset for all the xe because of the move. */
  2318. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  2319. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2320. for (i = 0; i < count; i++)
  2321. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  2322. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  2323. offset, size, off_change);
  2324. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  2325. cmp_xe, swap_xe);
  2326. }
  2327. /*
  2328. * After we move xattr from block to index btree, we have to
  2329. * update ocfs2_xattr_search to the new xe and base.
  2330. *
  2331. * When the entry is in xattr block, xattr_bh indicates the storage place.
  2332. * While if the entry is in index b-tree, "bucket" indicates the
  2333. * real place of the xattr.
  2334. */
  2335. static int ocfs2_xattr_update_xattr_search(struct inode *inode,
  2336. struct ocfs2_xattr_search *xs,
  2337. struct buffer_head *old_bh,
  2338. struct buffer_head *new_bh)
  2339. {
  2340. int ret = 0;
  2341. char *buf = old_bh->b_data;
  2342. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  2343. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  2344. int i, blocksize = inode->i_sb->s_blocksize;
  2345. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2346. xs->bucket.bhs[0] = new_bh;
  2347. get_bh(new_bh);
  2348. xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
  2349. xs->header = xs->bucket.xh;
  2350. xs->base = new_bh->b_data;
  2351. xs->end = xs->base + inode->i_sb->s_blocksize;
  2352. if (!xs->not_found) {
  2353. if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
  2354. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2355. xs->bucket.bhs[0]->b_blocknr + 1,
  2356. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2357. OCFS2_BH_CACHED, inode);
  2358. if (ret) {
  2359. mlog_errno(ret);
  2360. return ret;
  2361. }
  2362. i = xs->here - old_xh->xh_entries;
  2363. xs->here = &xs->header->xh_entries[i];
  2364. }
  2365. }
  2366. return ret;
  2367. }
  2368. static int ocfs2_xattr_create_index_block(struct inode *inode,
  2369. struct ocfs2_xattr_search *xs)
  2370. {
  2371. int ret, credits = OCFS2_SUBALLOC_ALLOC;
  2372. u32 bit_off, len;
  2373. u64 blkno;
  2374. handle_t *handle;
  2375. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2376. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2377. struct ocfs2_alloc_context *data_ac;
  2378. struct buffer_head *xh_bh = NULL, *data_bh = NULL;
  2379. struct buffer_head *xb_bh = xs->xattr_bh;
  2380. struct ocfs2_xattr_block *xb =
  2381. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2382. struct ocfs2_xattr_tree_root *xr;
  2383. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  2384. u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2385. mlog(0, "create xattr index block for %llu\n",
  2386. (unsigned long long)xb_bh->b_blocknr);
  2387. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  2388. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2389. if (ret) {
  2390. mlog_errno(ret);
  2391. goto out;
  2392. }
  2393. /*
  2394. * XXX:
  2395. * We can use this lock for now, and maybe move to a dedicated mutex
  2396. * if performance becomes a problem later.
  2397. */
  2398. down_write(&oi->ip_alloc_sem);
  2399. /*
  2400. * 3 more credits, one for xattr block update, one for the 1st block
  2401. * of the new xattr bucket and one for the value/data.
  2402. */
  2403. credits += 3;
  2404. handle = ocfs2_start_trans(osb, credits);
  2405. if (IS_ERR(handle)) {
  2406. ret = PTR_ERR(handle);
  2407. mlog_errno(ret);
  2408. goto out_sem;
  2409. }
  2410. ret = ocfs2_journal_access(handle, inode, xb_bh,
  2411. OCFS2_JOURNAL_ACCESS_WRITE);
  2412. if (ret) {
  2413. mlog_errno(ret);
  2414. goto out_commit;
  2415. }
  2416. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
  2417. if (ret) {
  2418. mlog_errno(ret);
  2419. goto out_commit;
  2420. }
  2421. /*
  2422. * The bucket may spread in many blocks, and
  2423. * we will only touch the 1st block and the last block
  2424. * in the whole bucket(one for entry and one for data).
  2425. */
  2426. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  2427. mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
  2428. xh_bh = sb_getblk(inode->i_sb, blkno);
  2429. if (!xh_bh) {
  2430. ret = -EIO;
  2431. mlog_errno(ret);
  2432. goto out_commit;
  2433. }
  2434. ocfs2_set_new_buffer_uptodate(inode, xh_bh);
  2435. ret = ocfs2_journal_access(handle, inode, xh_bh,
  2436. OCFS2_JOURNAL_ACCESS_CREATE);
  2437. if (ret) {
  2438. mlog_errno(ret);
  2439. goto out_commit;
  2440. }
  2441. if (bpb > 1) {
  2442. data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
  2443. if (!data_bh) {
  2444. ret = -EIO;
  2445. mlog_errno(ret);
  2446. goto out_commit;
  2447. }
  2448. ocfs2_set_new_buffer_uptodate(inode, data_bh);
  2449. ret = ocfs2_journal_access(handle, inode, data_bh,
  2450. OCFS2_JOURNAL_ACCESS_CREATE);
  2451. if (ret) {
  2452. mlog_errno(ret);
  2453. goto out_commit;
  2454. }
  2455. }
  2456. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
  2457. ocfs2_journal_dirty(handle, xh_bh);
  2458. if (data_bh)
  2459. ocfs2_journal_dirty(handle, data_bh);
  2460. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
  2461. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  2462. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  2463. offsetof(struct ocfs2_xattr_block, xb_attrs));
  2464. xr = &xb->xb_attrs.xb_root;
  2465. xr->xt_clusters = cpu_to_le32(1);
  2466. xr->xt_last_eb_blk = 0;
  2467. xr->xt_list.l_tree_depth = 0;
  2468. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  2469. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  2470. xr->xt_list.l_recs[0].e_cpos = 0;
  2471. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  2472. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  2473. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  2474. ret = ocfs2_journal_dirty(handle, xb_bh);
  2475. if (ret) {
  2476. mlog_errno(ret);
  2477. goto out_commit;
  2478. }
  2479. out_commit:
  2480. ocfs2_commit_trans(osb, handle);
  2481. out_sem:
  2482. up_write(&oi->ip_alloc_sem);
  2483. out:
  2484. if (data_ac)
  2485. ocfs2_free_alloc_context(data_ac);
  2486. brelse(xh_bh);
  2487. brelse(data_bh);
  2488. return ret;
  2489. }
  2490. static int cmp_xe_offset(const void *a, const void *b)
  2491. {
  2492. const struct ocfs2_xattr_entry *l = a, *r = b;
  2493. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  2494. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  2495. if (l_name_offset < r_name_offset)
  2496. return 1;
  2497. if (l_name_offset > r_name_offset)
  2498. return -1;
  2499. return 0;
  2500. }
  2501. /*
  2502. * defrag a xattr bucket if we find that the bucket has some
  2503. * holes beteen name/value pairs.
  2504. * We will move all the name/value pairs to the end of the bucket
  2505. * so that we can spare some space for insertion.
  2506. */
  2507. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  2508. struct ocfs2_xattr_bucket *bucket)
  2509. {
  2510. int ret, i;
  2511. size_t end, offset, len, value_len;
  2512. struct ocfs2_xattr_header *xh;
  2513. char *entries, *buf, *bucket_buf = NULL;
  2514. u64 blkno = bucket->bhs[0]->b_blocknr;
  2515. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2516. u16 xh_free_start;
  2517. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2518. size_t blocksize = inode->i_sb->s_blocksize;
  2519. handle_t *handle;
  2520. struct buffer_head **bhs;
  2521. struct ocfs2_xattr_entry *xe;
  2522. bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2523. GFP_NOFS);
  2524. if (!bhs)
  2525. return -ENOMEM;
  2526. ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
  2527. OCFS2_BH_CACHED, inode);
  2528. if (ret)
  2529. goto out;
  2530. /*
  2531. * In order to make the operation more efficient and generic,
  2532. * we copy all the blocks into a contiguous memory and do the
  2533. * defragment there, so if anything is error, we will not touch
  2534. * the real block.
  2535. */
  2536. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  2537. if (!bucket_buf) {
  2538. ret = -EIO;
  2539. goto out;
  2540. }
  2541. buf = bucket_buf;
  2542. for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
  2543. memcpy(buf, bhs[i]->b_data, blocksize);
  2544. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
  2545. if (IS_ERR(handle)) {
  2546. ret = PTR_ERR(handle);
  2547. handle = NULL;
  2548. mlog_errno(ret);
  2549. goto out;
  2550. }
  2551. for (i = 0; i < blk_per_bucket; i++) {
  2552. ret = ocfs2_journal_access(handle, inode, bhs[i],
  2553. OCFS2_JOURNAL_ACCESS_WRITE);
  2554. if (ret < 0) {
  2555. mlog_errno(ret);
  2556. goto commit;
  2557. }
  2558. }
  2559. xh = (struct ocfs2_xattr_header *)bucket_buf;
  2560. entries = (char *)xh->xh_entries;
  2561. xh_free_start = le16_to_cpu(xh->xh_free_start);
  2562. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  2563. "xh_free_start = %u, xh_name_value_len = %u.\n",
  2564. blkno, le16_to_cpu(xh->xh_count), xh_free_start,
  2565. le16_to_cpu(xh->xh_name_value_len));
  2566. /*
  2567. * sort all the entries by their offset.
  2568. * the largest will be the first, so that we can
  2569. * move them to the end one by one.
  2570. */
  2571. sort(entries, le16_to_cpu(xh->xh_count),
  2572. sizeof(struct ocfs2_xattr_entry),
  2573. cmp_xe_offset, swap_xe);
  2574. /* Move all name/values to the end of the bucket. */
  2575. xe = xh->xh_entries;
  2576. end = OCFS2_XATTR_BUCKET_SIZE;
  2577. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  2578. offset = le16_to_cpu(xe->xe_name_offset);
  2579. if (ocfs2_xattr_is_local(xe))
  2580. value_len = OCFS2_XATTR_SIZE(
  2581. le64_to_cpu(xe->xe_value_size));
  2582. else
  2583. value_len = OCFS2_XATTR_ROOT_SIZE;
  2584. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  2585. /*
  2586. * We must make sure that the name/value pair
  2587. * exist in the same block. So adjust end to
  2588. * the previous block end if needed.
  2589. */
  2590. if (((end - len) / blocksize !=
  2591. (end - 1) / blocksize))
  2592. end = end - end % blocksize;
  2593. if (end > offset + len) {
  2594. memmove(bucket_buf + end - len,
  2595. bucket_buf + offset, len);
  2596. xe->xe_name_offset = cpu_to_le16(end - len);
  2597. }
  2598. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  2599. "bucket %llu\n", (unsigned long long)blkno);
  2600. end -= len;
  2601. }
  2602. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  2603. "bucket %llu\n", (unsigned long long)blkno);
  2604. if (xh_free_start == end)
  2605. goto commit;
  2606. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  2607. xh->xh_free_start = cpu_to_le16(end);
  2608. /* sort the entries by their name_hash. */
  2609. sort(entries, le16_to_cpu(xh->xh_count),
  2610. sizeof(struct ocfs2_xattr_entry),
  2611. cmp_xe, swap_xe);
  2612. buf = bucket_buf;
  2613. for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
  2614. memcpy(bhs[i]->b_data, buf, blocksize);
  2615. ocfs2_journal_dirty(handle, bhs[i]);
  2616. }
  2617. commit:
  2618. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  2619. out:
  2620. if (bhs) {
  2621. for (i = 0; i < blk_per_bucket; i++)
  2622. brelse(bhs[i]);
  2623. }
  2624. kfree(bhs);
  2625. kfree(bucket_buf);
  2626. return ret;
  2627. }
  2628. /*
  2629. * Move half nums of the xattr bucket in the previous cluster to this new
  2630. * cluster. We only touch the last cluster of the previous extend record.
  2631. *
  2632. * first_bh is the first buffer_head of a series of bucket in the same
  2633. * extent rec and header_bh is the header of one bucket in this cluster.
  2634. * They will be updated if we move the data header_bh contains to the new
  2635. * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
  2636. */
  2637. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  2638. handle_t *handle,
  2639. struct buffer_head **first_bh,
  2640. struct buffer_head **header_bh,
  2641. u64 new_blkno,
  2642. u64 prev_blkno,
  2643. u32 num_clusters,
  2644. u32 *first_hash)
  2645. {
  2646. int i, ret, credits;
  2647. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2648. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2649. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2650. int blocksize = inode->i_sb->s_blocksize;
  2651. struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
  2652. struct ocfs2_xattr_header *new_xh;
  2653. struct ocfs2_xattr_header *xh =
  2654. (struct ocfs2_xattr_header *)((*first_bh)->b_data);
  2655. BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
  2656. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
  2657. prev_bh = *first_bh;
  2658. get_bh(prev_bh);
  2659. xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
  2660. prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
  2661. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  2662. prev_blkno, new_blkno);
  2663. /*
  2664. * We need to update the 1st half of the new cluster and
  2665. * 1 more for the update of the 1st bucket of the previous
  2666. * extent record.
  2667. */
  2668. credits = bpc / 2 + 1;
  2669. ret = ocfs2_extend_trans(handle, credits);
  2670. if (ret) {
  2671. mlog_errno(ret);
  2672. goto out;
  2673. }
  2674. ret = ocfs2_journal_access(handle, inode, prev_bh,
  2675. OCFS2_JOURNAL_ACCESS_WRITE);
  2676. if (ret) {
  2677. mlog_errno(ret);
  2678. goto out;
  2679. }
  2680. for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
  2681. old_bh = new_bh = NULL;
  2682. new_bh = sb_getblk(inode->i_sb, new_blkno);
  2683. if (!new_bh) {
  2684. ret = -EIO;
  2685. mlog_errno(ret);
  2686. goto out;
  2687. }
  2688. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  2689. ret = ocfs2_journal_access(handle, inode, new_bh,
  2690. OCFS2_JOURNAL_ACCESS_CREATE);
  2691. if (ret < 0) {
  2692. mlog_errno(ret);
  2693. brelse(new_bh);
  2694. goto out;
  2695. }
  2696. ret = ocfs2_read_block(osb, prev_blkno,
  2697. &old_bh, OCFS2_BH_CACHED, inode);
  2698. if (ret < 0) {
  2699. mlog_errno(ret);
  2700. brelse(new_bh);
  2701. goto out;
  2702. }
  2703. memcpy(new_bh->b_data, old_bh->b_data, blocksize);
  2704. if (i == 0) {
  2705. new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
  2706. new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
  2707. if (first_hash)
  2708. *first_hash = le32_to_cpu(
  2709. new_xh->xh_entries[0].xe_name_hash);
  2710. new_first_bh = new_bh;
  2711. get_bh(new_first_bh);
  2712. }
  2713. ocfs2_journal_dirty(handle, new_bh);
  2714. if (*header_bh == old_bh) {
  2715. brelse(*header_bh);
  2716. *header_bh = new_bh;
  2717. get_bh(*header_bh);
  2718. brelse(*first_bh);
  2719. *first_bh = new_first_bh;
  2720. get_bh(*first_bh);
  2721. }
  2722. brelse(new_bh);
  2723. brelse(old_bh);
  2724. }
  2725. le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
  2726. ocfs2_journal_dirty(handle, prev_bh);
  2727. out:
  2728. brelse(prev_bh);
  2729. brelse(new_first_bh);
  2730. return ret;
  2731. }
  2732. static int ocfs2_read_xattr_bucket(struct inode *inode,
  2733. u64 blkno,
  2734. struct buffer_head **bhs,
  2735. int new)
  2736. {
  2737. int ret = 0;
  2738. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2739. if (!new)
  2740. return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
  2741. blk_per_bucket, bhs,
  2742. OCFS2_BH_CACHED, inode);
  2743. for (i = 0; i < blk_per_bucket; i++) {
  2744. bhs[i] = sb_getblk(inode->i_sb, blkno + i);
  2745. if (bhs[i] == NULL) {
  2746. ret = -EIO;
  2747. mlog_errno(ret);
  2748. break;
  2749. }
  2750. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  2751. }
  2752. return ret;
  2753. }
  2754. /*
  2755. * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
  2756. * first_hash will record the 1st hash of the new bucket.
  2757. */
  2758. static int ocfs2_half_xattr_bucket(struct inode *inode,
  2759. handle_t *handle,
  2760. u64 blk,
  2761. u64 new_blk,
  2762. u32 *first_hash,
  2763. int new_bucket_head)
  2764. {
  2765. int ret, i;
  2766. u16 count, start, len, name_value_len, xe_len, name_offset;
  2767. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2768. struct buffer_head **s_bhs, **t_bhs = NULL;
  2769. struct ocfs2_xattr_header *xh;
  2770. struct ocfs2_xattr_entry *xe;
  2771. int blocksize = inode->i_sb->s_blocksize;
  2772. mlog(0, "move half of xattrs from bucket %llu to %llu\n",
  2773. blk, new_blk);
  2774. s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2775. if (!s_bhs)
  2776. return -ENOMEM;
  2777. ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
  2778. if (ret) {
  2779. mlog_errno(ret);
  2780. goto out;
  2781. }
  2782. ret = ocfs2_journal_access(handle, inode, s_bhs[0],
  2783. OCFS2_JOURNAL_ACCESS_WRITE);
  2784. if (ret) {
  2785. mlog_errno(ret);
  2786. goto out;
  2787. }
  2788. t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2789. if (!t_bhs) {
  2790. ret = -ENOMEM;
  2791. goto out;
  2792. }
  2793. ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
  2794. if (ret) {
  2795. mlog_errno(ret);
  2796. goto out;
  2797. }
  2798. for (i = 0; i < blk_per_bucket; i++) {
  2799. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2800. OCFS2_JOURNAL_ACCESS_CREATE);
  2801. if (ret) {
  2802. mlog_errno(ret);
  2803. goto out;
  2804. }
  2805. }
  2806. /* copy the whole bucket to the new first. */
  2807. for (i = 0; i < blk_per_bucket; i++)
  2808. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2809. /* update the new bucket. */
  2810. xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
  2811. count = le16_to_cpu(xh->xh_count);
  2812. start = count / 2;
  2813. /*
  2814. * Calculate the total name/value len and xh_free_start for
  2815. * the old bucket first.
  2816. */
  2817. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  2818. name_value_len = 0;
  2819. for (i = 0; i < start; i++) {
  2820. xe = &xh->xh_entries[i];
  2821. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2822. if (ocfs2_xattr_is_local(xe))
  2823. xe_len +=
  2824. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2825. else
  2826. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2827. name_value_len += xe_len;
  2828. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  2829. name_offset = le16_to_cpu(xe->xe_name_offset);
  2830. }
  2831. /*
  2832. * Now begin the modification to the new bucket.
  2833. *
  2834. * In the new bucket, We just move the xattr entry to the beginning
  2835. * and don't touch the name/value. So there will be some holes in the
  2836. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  2837. * called.
  2838. */
  2839. xe = &xh->xh_entries[start];
  2840. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  2841. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  2842. (int)((char *)xe - (char *)xh),
  2843. (int)((char *)xh->xh_entries - (char *)xh));
  2844. memmove((char *)xh->xh_entries, (char *)xe, len);
  2845. xe = &xh->xh_entries[count - start];
  2846. len = sizeof(struct ocfs2_xattr_entry) * start;
  2847. memset((char *)xe, 0, len);
  2848. le16_add_cpu(&xh->xh_count, -start);
  2849. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  2850. /* Calculate xh_free_start for the new bucket. */
  2851. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  2852. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2853. xe = &xh->xh_entries[i];
  2854. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2855. if (ocfs2_xattr_is_local(xe))
  2856. xe_len +=
  2857. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2858. else
  2859. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2860. if (le16_to_cpu(xe->xe_name_offset) <
  2861. le16_to_cpu(xh->xh_free_start))
  2862. xh->xh_free_start = xe->xe_name_offset;
  2863. }
  2864. /* set xh->xh_num_buckets for the new xh. */
  2865. if (new_bucket_head)
  2866. xh->xh_num_buckets = cpu_to_le16(1);
  2867. else
  2868. xh->xh_num_buckets = 0;
  2869. for (i = 0; i < blk_per_bucket; i++) {
  2870. ocfs2_journal_dirty(handle, t_bhs[i]);
  2871. if (ret)
  2872. mlog_errno(ret);
  2873. }
  2874. /* store the first_hash of the new bucket. */
  2875. if (first_hash)
  2876. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  2877. /*
  2878. * Now only update the 1st block of the old bucket.
  2879. * Please note that the entry has been sorted already above.
  2880. */
  2881. xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
  2882. memset(&xh->xh_entries[start], 0,
  2883. sizeof(struct ocfs2_xattr_entry) * (count - start));
  2884. xh->xh_count = cpu_to_le16(start);
  2885. xh->xh_free_start = cpu_to_le16(name_offset);
  2886. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  2887. ocfs2_journal_dirty(handle, s_bhs[0]);
  2888. if (ret)
  2889. mlog_errno(ret);
  2890. out:
  2891. if (s_bhs) {
  2892. for (i = 0; i < blk_per_bucket; i++)
  2893. brelse(s_bhs[i]);
  2894. }
  2895. kfree(s_bhs);
  2896. if (t_bhs) {
  2897. for (i = 0; i < blk_per_bucket; i++)
  2898. brelse(t_bhs[i]);
  2899. }
  2900. kfree(t_bhs);
  2901. return ret;
  2902. }
  2903. /*
  2904. * Copy xattr from one bucket to another bucket.
  2905. *
  2906. * The caller must make sure that the journal transaction
  2907. * has enough space for journaling.
  2908. */
  2909. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  2910. handle_t *handle,
  2911. u64 s_blkno,
  2912. u64 t_blkno,
  2913. int t_is_new)
  2914. {
  2915. int ret, i;
  2916. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2917. int blocksize = inode->i_sb->s_blocksize;
  2918. struct buffer_head **s_bhs, **t_bhs = NULL;
  2919. BUG_ON(s_blkno == t_blkno);
  2920. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  2921. s_blkno, t_blkno, t_is_new);
  2922. s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2923. GFP_NOFS);
  2924. if (!s_bhs)
  2925. return -ENOMEM;
  2926. ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
  2927. if (ret)
  2928. goto out;
  2929. t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2930. GFP_NOFS);
  2931. if (!t_bhs) {
  2932. ret = -ENOMEM;
  2933. goto out;
  2934. }
  2935. ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
  2936. if (ret)
  2937. goto out;
  2938. for (i = 0; i < blk_per_bucket; i++) {
  2939. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2940. OCFS2_JOURNAL_ACCESS_WRITE);
  2941. if (ret)
  2942. goto out;
  2943. }
  2944. for (i = 0; i < blk_per_bucket; i++) {
  2945. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2946. ocfs2_journal_dirty(handle, t_bhs[i]);
  2947. }
  2948. out:
  2949. if (s_bhs) {
  2950. for (i = 0; i < blk_per_bucket; i++)
  2951. brelse(s_bhs[i]);
  2952. }
  2953. kfree(s_bhs);
  2954. if (t_bhs) {
  2955. for (i = 0; i < blk_per_bucket; i++)
  2956. brelse(t_bhs[i]);
  2957. }
  2958. kfree(t_bhs);
  2959. return ret;
  2960. }
  2961. /*
  2962. * Copy one xattr cluster from src_blk to to_blk.
  2963. * The to_blk will become the first bucket header of the cluster, so its
  2964. * xh_num_buckets will be initialized as the bucket num in the cluster.
  2965. */
  2966. static int ocfs2_cp_xattr_cluster(struct inode *inode,
  2967. handle_t *handle,
  2968. struct buffer_head *first_bh,
  2969. u64 src_blk,
  2970. u64 to_blk,
  2971. u32 *first_hash)
  2972. {
  2973. int i, ret, credits;
  2974. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2975. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2976. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2977. struct buffer_head *bh = NULL;
  2978. struct ocfs2_xattr_header *xh;
  2979. u64 to_blk_start = to_blk;
  2980. mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
  2981. /*
  2982. * We need to update the new cluster and 1 more for the update of
  2983. * the 1st bucket of the previous extent rec.
  2984. */
  2985. credits = bpc + 1;
  2986. ret = ocfs2_extend_trans(handle, credits);
  2987. if (ret) {
  2988. mlog_errno(ret);
  2989. goto out;
  2990. }
  2991. ret = ocfs2_journal_access(handle, inode, first_bh,
  2992. OCFS2_JOURNAL_ACCESS_WRITE);
  2993. if (ret) {
  2994. mlog_errno(ret);
  2995. goto out;
  2996. }
  2997. for (i = 0; i < num_buckets; i++) {
  2998. ret = ocfs2_cp_xattr_bucket(inode, handle,
  2999. src_blk, to_blk, 1);
  3000. if (ret) {
  3001. mlog_errno(ret);
  3002. goto out;
  3003. }
  3004. src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3005. to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3006. }
  3007. /* update the old bucket header. */
  3008. xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  3009. le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
  3010. ocfs2_journal_dirty(handle, first_bh);
  3011. /* update the new bucket header. */
  3012. ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
  3013. if (ret < 0) {
  3014. mlog_errno(ret);
  3015. goto out;
  3016. }
  3017. ret = ocfs2_journal_access(handle, inode, bh,
  3018. OCFS2_JOURNAL_ACCESS_WRITE);
  3019. if (ret) {
  3020. mlog_errno(ret);
  3021. goto out;
  3022. }
  3023. xh = (struct ocfs2_xattr_header *)bh->b_data;
  3024. xh->xh_num_buckets = cpu_to_le16(num_buckets);
  3025. ocfs2_journal_dirty(handle, bh);
  3026. if (first_hash)
  3027. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3028. out:
  3029. brelse(bh);
  3030. return ret;
  3031. }
  3032. /*
  3033. * Move half of the xattrs in this cluster to the new cluster.
  3034. * This function should only be called when bucket size == cluster size.
  3035. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3036. */
  3037. static int ocfs2_half_xattr_cluster(struct inode *inode,
  3038. handle_t *handle,
  3039. u64 prev_blk,
  3040. u64 new_blk,
  3041. u32 *first_hash)
  3042. {
  3043. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3044. int ret, credits = 2 * blk_per_bucket;
  3045. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3046. ret = ocfs2_extend_trans(handle, credits);
  3047. if (ret) {
  3048. mlog_errno(ret);
  3049. return ret;
  3050. }
  3051. /* Move half of the xattr in start_blk to the next bucket. */
  3052. return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
  3053. new_blk, first_hash, 1);
  3054. }
  3055. /*
  3056. * Move some xattrs from the old cluster to the new one since they are not
  3057. * contiguous in ocfs2 xattr tree.
  3058. *
  3059. * new_blk starts a new separate cluster, and we will move some xattrs from
  3060. * prev_blk to it. v_start will be set as the first name hash value in this
  3061. * new cluster so that it can be used as e_cpos during tree insertion and
  3062. * don't collide with our original b-tree operations. first_bh and header_bh
  3063. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3064. * to extend the insert bucket.
  3065. *
  3066. * The problem is how much xattr should we move to the new one and when should
  3067. * we update first_bh and header_bh?
  3068. * 1. If cluster size > bucket size, that means the previous cluster has more
  3069. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3070. * update the first_bh and header_bh if the insert bucket has been moved
  3071. * to the new cluster.
  3072. * 2. If cluster_size == bucket_size:
  3073. * a) If the previous extent rec has more than one cluster and the insert
  3074. * place isn't in the last cluster, copy the entire last cluster to the
  3075. * new one. This time, we don't need to upate the first_bh and header_bh
  3076. * since they will not be moved into the new cluster.
  3077. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3078. * the new one. And we set the extend flag to zero if the insert place is
  3079. * moved into the new allocated cluster since no extend is needed.
  3080. */
  3081. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3082. handle_t *handle,
  3083. struct buffer_head **first_bh,
  3084. struct buffer_head **header_bh,
  3085. u64 new_blk,
  3086. u64 prev_blk,
  3087. u32 prev_clusters,
  3088. u32 *v_start,
  3089. int *extend)
  3090. {
  3091. int ret = 0;
  3092. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3093. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3094. prev_blk, prev_clusters, new_blk);
  3095. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
  3096. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3097. handle,
  3098. first_bh,
  3099. header_bh,
  3100. new_blk,
  3101. prev_blk,
  3102. prev_clusters,
  3103. v_start);
  3104. else {
  3105. u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
  3106. if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
  3107. ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
  3108. last_blk, new_blk,
  3109. v_start);
  3110. else {
  3111. ret = ocfs2_half_xattr_cluster(inode, handle,
  3112. last_blk, new_blk,
  3113. v_start);
  3114. if ((*header_bh)->b_blocknr == last_blk && extend)
  3115. *extend = 0;
  3116. }
  3117. }
  3118. return ret;
  3119. }
  3120. /*
  3121. * Add a new cluster for xattr storage.
  3122. *
  3123. * If the new cluster is contiguous with the previous one, it will be
  3124. * appended to the same extent record, and num_clusters will be updated.
  3125. * If not, we will insert a new extent for it and move some xattrs in
  3126. * the last cluster into the new allocated one.
  3127. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3128. * lose the benefits of hashing because we'll have to search large leaves.
  3129. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3130. * if it's bigger).
  3131. *
  3132. * first_bh is the first block of the previous extent rec and header_bh
  3133. * indicates the bucket we will insert the new xattrs. They will be updated
  3134. * when the header_bh is moved into the new cluster.
  3135. */
  3136. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3137. struct buffer_head *root_bh,
  3138. struct buffer_head **first_bh,
  3139. struct buffer_head **header_bh,
  3140. u32 *num_clusters,
  3141. u32 prev_cpos,
  3142. u64 prev_blkno,
  3143. int *extend)
  3144. {
  3145. int ret, credits;
  3146. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3147. u32 prev_clusters = *num_clusters;
  3148. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3149. u64 block;
  3150. handle_t *handle = NULL;
  3151. struct ocfs2_alloc_context *data_ac = NULL;
  3152. struct ocfs2_alloc_context *meta_ac = NULL;
  3153. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3154. struct ocfs2_extent_tree et;
  3155. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3156. "previous xattr blkno = %llu\n",
  3157. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3158. prev_cpos, prev_blkno);
  3159. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  3160. ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  3161. &data_ac, &meta_ac);
  3162. if (ret) {
  3163. mlog_errno(ret);
  3164. goto leave;
  3165. }
  3166. credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
  3167. clusters_to_add);
  3168. handle = ocfs2_start_trans(osb, credits);
  3169. if (IS_ERR(handle)) {
  3170. ret = PTR_ERR(handle);
  3171. handle = NULL;
  3172. mlog_errno(ret);
  3173. goto leave;
  3174. }
  3175. ret = ocfs2_journal_access(handle, inode, root_bh,
  3176. OCFS2_JOURNAL_ACCESS_WRITE);
  3177. if (ret < 0) {
  3178. mlog_errno(ret);
  3179. goto leave;
  3180. }
  3181. ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  3182. clusters_to_add, &bit_off, &num_bits);
  3183. if (ret < 0) {
  3184. if (ret != -ENOSPC)
  3185. mlog_errno(ret);
  3186. goto leave;
  3187. }
  3188. BUG_ON(num_bits > clusters_to_add);
  3189. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3190. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3191. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3192. if (prev_blkno + prev_clusters * bpc == block &&
  3193. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3194. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3195. /*
  3196. * If this cluster is contiguous with the old one and
  3197. * adding this new cluster, we don't surpass the limit of
  3198. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3199. * initialized and used like other buckets in the previous
  3200. * cluster.
  3201. * So add it as a contiguous one. The caller will handle
  3202. * its init process.
  3203. */
  3204. v_start = prev_cpos + prev_clusters;
  3205. *num_clusters = prev_clusters + num_bits;
  3206. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3207. num_bits);
  3208. } else {
  3209. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3210. handle,
  3211. first_bh,
  3212. header_bh,
  3213. block,
  3214. prev_blkno,
  3215. prev_clusters,
  3216. &v_start,
  3217. extend);
  3218. if (ret) {
  3219. mlog_errno(ret);
  3220. goto leave;
  3221. }
  3222. }
  3223. if (handle->h_buffer_credits < credits) {
  3224. /*
  3225. * The journal has been restarted before, and don't
  3226. * have enough space for the insertion, so extend it
  3227. * here.
  3228. */
  3229. ret = ocfs2_extend_trans(handle, credits);
  3230. if (ret) {
  3231. mlog_errno(ret);
  3232. goto leave;
  3233. }
  3234. }
  3235. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3236. num_bits, block, v_start);
  3237. ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
  3238. num_bits, 0, meta_ac);
  3239. if (ret < 0) {
  3240. mlog_errno(ret);
  3241. goto leave;
  3242. }
  3243. ret = ocfs2_journal_dirty(handle, root_bh);
  3244. if (ret < 0) {
  3245. mlog_errno(ret);
  3246. goto leave;
  3247. }
  3248. leave:
  3249. if (handle)
  3250. ocfs2_commit_trans(osb, handle);
  3251. if (data_ac)
  3252. ocfs2_free_alloc_context(data_ac);
  3253. if (meta_ac)
  3254. ocfs2_free_alloc_context(meta_ac);
  3255. return ret;
  3256. }
  3257. /*
  3258. * Extend a new xattr bucket and move xattrs to the end one by one until
  3259. * We meet with start_bh. Only move half of the xattrs to the bucket after it.
  3260. */
  3261. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3262. struct buffer_head *first_bh,
  3263. struct buffer_head *start_bh,
  3264. u32 num_clusters)
  3265. {
  3266. int ret, credits;
  3267. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3268. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3269. u64 start_blk = start_bh->b_blocknr, end_blk;
  3270. u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
  3271. handle_t *handle;
  3272. struct ocfs2_xattr_header *first_xh =
  3273. (struct ocfs2_xattr_header *)first_bh->b_data;
  3274. u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
  3275. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3276. "from %llu, len = %u\n", start_blk,
  3277. (unsigned long long)first_bh->b_blocknr, num_clusters);
  3278. BUG_ON(bucket >= num_buckets);
  3279. end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
  3280. /*
  3281. * We will touch all the buckets after the start_bh(include it).
  3282. * Add one more bucket and modify the first_bh.
  3283. */
  3284. credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
  3285. handle = ocfs2_start_trans(osb, credits);
  3286. if (IS_ERR(handle)) {
  3287. ret = PTR_ERR(handle);
  3288. handle = NULL;
  3289. mlog_errno(ret);
  3290. goto out;
  3291. }
  3292. ret = ocfs2_journal_access(handle, inode, first_bh,
  3293. OCFS2_JOURNAL_ACCESS_WRITE);
  3294. if (ret) {
  3295. mlog_errno(ret);
  3296. goto commit;
  3297. }
  3298. while (end_blk != start_blk) {
  3299. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3300. end_blk + blk_per_bucket, 0);
  3301. if (ret)
  3302. goto commit;
  3303. end_blk -= blk_per_bucket;
  3304. }
  3305. /* Move half of the xattr in start_blk to the next bucket. */
  3306. ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
  3307. start_blk + blk_per_bucket, NULL, 0);
  3308. le16_add_cpu(&first_xh->xh_num_buckets, 1);
  3309. ocfs2_journal_dirty(handle, first_bh);
  3310. commit:
  3311. ocfs2_commit_trans(osb, handle);
  3312. out:
  3313. return ret;
  3314. }
  3315. /*
  3316. * Add new xattr bucket in an extent record and adjust the buckets accordingly.
  3317. * xb_bh is the ocfs2_xattr_block.
  3318. * We will move all the buckets starting from header_bh to the next place. As
  3319. * for this one, half num of its xattrs will be moved to the next one.
  3320. *
  3321. * We will allocate a new cluster if current cluster is full and adjust
  3322. * header_bh and first_bh if the insert place is moved to the new cluster.
  3323. */
  3324. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3325. struct buffer_head *xb_bh,
  3326. struct buffer_head *header_bh)
  3327. {
  3328. struct ocfs2_xattr_header *first_xh = NULL;
  3329. struct buffer_head *first_bh = NULL;
  3330. struct ocfs2_xattr_block *xb =
  3331. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3332. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3333. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3334. struct ocfs2_xattr_header *xh =
  3335. (struct ocfs2_xattr_header *)header_bh->b_data;
  3336. u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3337. struct super_block *sb = inode->i_sb;
  3338. struct ocfs2_super *osb = OCFS2_SB(sb);
  3339. int ret, num_buckets, extend = 1;
  3340. u64 p_blkno;
  3341. u32 e_cpos, num_clusters;
  3342. mlog(0, "Add new xattr bucket starting form %llu\n",
  3343. (unsigned long long)header_bh->b_blocknr);
  3344. /*
  3345. * Add refrence for header_bh here because it may be
  3346. * changed in ocfs2_add_new_xattr_cluster and we need
  3347. * to free it in the end.
  3348. */
  3349. get_bh(header_bh);
  3350. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3351. &num_clusters, el);
  3352. if (ret) {
  3353. mlog_errno(ret);
  3354. goto out;
  3355. }
  3356. ret = ocfs2_read_block(osb, p_blkno,
  3357. &first_bh, OCFS2_BH_CACHED, inode);
  3358. if (ret) {
  3359. mlog_errno(ret);
  3360. goto out;
  3361. }
  3362. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3363. first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  3364. if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
  3365. ret = ocfs2_add_new_xattr_cluster(inode,
  3366. xb_bh,
  3367. &first_bh,
  3368. &header_bh,
  3369. &num_clusters,
  3370. e_cpos,
  3371. p_blkno,
  3372. &extend);
  3373. if (ret) {
  3374. mlog_errno(ret);
  3375. goto out;
  3376. }
  3377. }
  3378. if (extend)
  3379. ret = ocfs2_extend_xattr_bucket(inode,
  3380. first_bh,
  3381. header_bh,
  3382. num_clusters);
  3383. if (ret)
  3384. mlog_errno(ret);
  3385. out:
  3386. brelse(first_bh);
  3387. brelse(header_bh);
  3388. return ret;
  3389. }
  3390. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  3391. struct ocfs2_xattr_bucket *bucket,
  3392. int offs)
  3393. {
  3394. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  3395. offs = offs % inode->i_sb->s_blocksize;
  3396. return bucket->bhs[block_off]->b_data + offs;
  3397. }
  3398. /*
  3399. * Handle the normal xattr set, including replace, delete and new.
  3400. *
  3401. * Note: "local" indicates the real data's locality. So we can't
  3402. * just its bucket locality by its length.
  3403. */
  3404. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  3405. struct ocfs2_xattr_info *xi,
  3406. struct ocfs2_xattr_search *xs,
  3407. u32 name_hash,
  3408. int local)
  3409. {
  3410. struct ocfs2_xattr_entry *last, *xe;
  3411. int name_len = strlen(xi->name);
  3412. struct ocfs2_xattr_header *xh = xs->header;
  3413. u16 count = le16_to_cpu(xh->xh_count), start;
  3414. size_t blocksize = inode->i_sb->s_blocksize;
  3415. char *val;
  3416. size_t offs, size, new_size;
  3417. last = &xh->xh_entries[count];
  3418. if (!xs->not_found) {
  3419. xe = xs->here;
  3420. offs = le16_to_cpu(xe->xe_name_offset);
  3421. if (ocfs2_xattr_is_local(xe))
  3422. size = OCFS2_XATTR_SIZE(name_len) +
  3423. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3424. else
  3425. size = OCFS2_XATTR_SIZE(name_len) +
  3426. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3427. /*
  3428. * If the new value will be stored outside, xi->value has been
  3429. * initalized as an empty ocfs2_xattr_value_root, and the same
  3430. * goes with xi->value_len, so we can set new_size safely here.
  3431. * See ocfs2_xattr_set_in_bucket.
  3432. */
  3433. new_size = OCFS2_XATTR_SIZE(name_len) +
  3434. OCFS2_XATTR_SIZE(xi->value_len);
  3435. le16_add_cpu(&xh->xh_name_value_len, -size);
  3436. if (xi->value) {
  3437. if (new_size > size)
  3438. goto set_new_name_value;
  3439. /* Now replace the old value with new one. */
  3440. if (local)
  3441. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3442. else
  3443. xe->xe_value_size = 0;
  3444. val = ocfs2_xattr_bucket_get_val(inode,
  3445. &xs->bucket, offs);
  3446. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  3447. size - OCFS2_XATTR_SIZE(name_len));
  3448. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  3449. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  3450. xi->value, xi->value_len);
  3451. le16_add_cpu(&xh->xh_name_value_len, new_size);
  3452. ocfs2_xattr_set_local(xe, local);
  3453. return;
  3454. } else {
  3455. /*
  3456. * Remove the old entry if there is more than one.
  3457. * We don't remove the last entry so that we can
  3458. * use it to indicate the hash value of the empty
  3459. * bucket.
  3460. */
  3461. last -= 1;
  3462. le16_add_cpu(&xh->xh_count, -1);
  3463. if (xh->xh_count) {
  3464. memmove(xe, xe + 1,
  3465. (void *)last - (void *)xe);
  3466. memset(last, 0,
  3467. sizeof(struct ocfs2_xattr_entry));
  3468. } else
  3469. xh->xh_free_start =
  3470. cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3471. return;
  3472. }
  3473. } else {
  3474. /* find a new entry for insert. */
  3475. int low = 0, high = count - 1, tmp;
  3476. struct ocfs2_xattr_entry *tmp_xe;
  3477. while (low <= high && count) {
  3478. tmp = (low + high) / 2;
  3479. tmp_xe = &xh->xh_entries[tmp];
  3480. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  3481. low = tmp + 1;
  3482. else if (name_hash <
  3483. le32_to_cpu(tmp_xe->xe_name_hash))
  3484. high = tmp - 1;
  3485. else {
  3486. low = tmp;
  3487. break;
  3488. }
  3489. }
  3490. xe = &xh->xh_entries[low];
  3491. if (low != count)
  3492. memmove(xe + 1, xe, (void *)last - (void *)xe);
  3493. le16_add_cpu(&xh->xh_count, 1);
  3494. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  3495. xe->xe_name_hash = cpu_to_le32(name_hash);
  3496. xe->xe_name_len = name_len;
  3497. ocfs2_xattr_set_type(xe, xi->name_index);
  3498. }
  3499. set_new_name_value:
  3500. /* Insert the new name+value. */
  3501. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  3502. /*
  3503. * We must make sure that the name/value pair
  3504. * exists in the same block.
  3505. */
  3506. offs = le16_to_cpu(xh->xh_free_start);
  3507. start = offs - size;
  3508. if (start >> inode->i_sb->s_blocksize_bits !=
  3509. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  3510. offs = offs - offs % blocksize;
  3511. xh->xh_free_start = cpu_to_le16(offs);
  3512. }
  3513. val = ocfs2_xattr_bucket_get_val(inode,
  3514. &xs->bucket, offs - size);
  3515. xe->xe_name_offset = cpu_to_le16(offs - size);
  3516. memset(val, 0, size);
  3517. memcpy(val, xi->name, name_len);
  3518. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  3519. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3520. ocfs2_xattr_set_local(xe, local);
  3521. xs->here = xe;
  3522. le16_add_cpu(&xh->xh_free_start, -size);
  3523. le16_add_cpu(&xh->xh_name_value_len, size);
  3524. return;
  3525. }
  3526. static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
  3527. handle_t *handle,
  3528. struct ocfs2_xattr_search *xs,
  3529. struct buffer_head **bhs,
  3530. u16 bh_num)
  3531. {
  3532. int ret = 0, off, block_off;
  3533. struct ocfs2_xattr_entry *xe = xs->here;
  3534. /*
  3535. * First calculate all the blocks we should journal_access
  3536. * and journal_dirty. The first block should always be touched.
  3537. */
  3538. ret = ocfs2_journal_dirty(handle, bhs[0]);
  3539. if (ret)
  3540. mlog_errno(ret);
  3541. /* calc the data. */
  3542. off = le16_to_cpu(xe->xe_name_offset);
  3543. block_off = off >> inode->i_sb->s_blocksize_bits;
  3544. ret = ocfs2_journal_dirty(handle, bhs[block_off]);
  3545. if (ret)
  3546. mlog_errno(ret);
  3547. return ret;
  3548. }
  3549. /*
  3550. * Set the xattr entry in the specified bucket.
  3551. * The bucket is indicated by xs->bucket and it should have the enough
  3552. * space for the xattr insertion.
  3553. */
  3554. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  3555. struct ocfs2_xattr_info *xi,
  3556. struct ocfs2_xattr_search *xs,
  3557. u32 name_hash,
  3558. int local)
  3559. {
  3560. int i, ret;
  3561. handle_t *handle = NULL;
  3562. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3563. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3564. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  3565. (unsigned long)xi->value_len, xi->name_index,
  3566. (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
  3567. if (!xs->bucket.bhs[1]) {
  3568. ret = ocfs2_read_blocks(osb,
  3569. xs->bucket.bhs[0]->b_blocknr + 1,
  3570. blk_per_bucket - 1, &xs->bucket.bhs[1],
  3571. OCFS2_BH_CACHED, inode);
  3572. if (ret) {
  3573. mlog_errno(ret);
  3574. goto out;
  3575. }
  3576. }
  3577. handle = ocfs2_start_trans(osb, blk_per_bucket);
  3578. if (IS_ERR(handle)) {
  3579. ret = PTR_ERR(handle);
  3580. handle = NULL;
  3581. mlog_errno(ret);
  3582. goto out;
  3583. }
  3584. for (i = 0; i < blk_per_bucket; i++) {
  3585. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
  3586. OCFS2_JOURNAL_ACCESS_WRITE);
  3587. if (ret < 0) {
  3588. mlog_errno(ret);
  3589. goto out;
  3590. }
  3591. }
  3592. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
  3593. /*Only dirty the blocks we have touched in set xattr. */
  3594. ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
  3595. xs->bucket.bhs, blk_per_bucket);
  3596. if (ret)
  3597. mlog_errno(ret);
  3598. out:
  3599. ocfs2_commit_trans(osb, handle);
  3600. return ret;
  3601. }
  3602. static int ocfs2_xattr_value_update_size(struct inode *inode,
  3603. struct buffer_head *xe_bh,
  3604. struct ocfs2_xattr_entry *xe,
  3605. u64 new_size)
  3606. {
  3607. int ret;
  3608. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3609. handle_t *handle = NULL;
  3610. handle = ocfs2_start_trans(osb, 1);
  3611. if (handle == NULL) {
  3612. ret = -ENOMEM;
  3613. mlog_errno(ret);
  3614. goto out;
  3615. }
  3616. ret = ocfs2_journal_access(handle, inode, xe_bh,
  3617. OCFS2_JOURNAL_ACCESS_WRITE);
  3618. if (ret < 0) {
  3619. mlog_errno(ret);
  3620. goto out_commit;
  3621. }
  3622. xe->xe_value_size = cpu_to_le64(new_size);
  3623. ret = ocfs2_journal_dirty(handle, xe_bh);
  3624. if (ret < 0)
  3625. mlog_errno(ret);
  3626. out_commit:
  3627. ocfs2_commit_trans(osb, handle);
  3628. out:
  3629. return ret;
  3630. }
  3631. /*
  3632. * Truncate the specified xe_off entry in xattr bucket.
  3633. * bucket is indicated by header_bh and len is the new length.
  3634. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  3635. *
  3636. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  3637. */
  3638. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  3639. struct buffer_head *header_bh,
  3640. int xe_off,
  3641. int len)
  3642. {
  3643. int ret, offset;
  3644. u64 value_blk;
  3645. struct buffer_head *value_bh = NULL;
  3646. struct ocfs2_xattr_value_root *xv;
  3647. struct ocfs2_xattr_entry *xe;
  3648. struct ocfs2_xattr_header *xh =
  3649. (struct ocfs2_xattr_header *)header_bh->b_data;
  3650. size_t blocksize = inode->i_sb->s_blocksize;
  3651. xe = &xh->xh_entries[xe_off];
  3652. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  3653. offset = le16_to_cpu(xe->xe_name_offset) +
  3654. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3655. value_blk = offset / blocksize;
  3656. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  3657. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  3658. value_blk += header_bh->b_blocknr;
  3659. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
  3660. &value_bh, OCFS2_BH_CACHED, inode);
  3661. if (ret) {
  3662. mlog_errno(ret);
  3663. goto out;
  3664. }
  3665. xv = (struct ocfs2_xattr_value_root *)
  3666. (value_bh->b_data + offset % blocksize);
  3667. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  3668. xe_off, (unsigned long long)header_bh->b_blocknr, len);
  3669. ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
  3670. if (ret) {
  3671. mlog_errno(ret);
  3672. goto out;
  3673. }
  3674. ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
  3675. if (ret) {
  3676. mlog_errno(ret);
  3677. goto out;
  3678. }
  3679. out:
  3680. brelse(value_bh);
  3681. return ret;
  3682. }
  3683. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  3684. struct ocfs2_xattr_search *xs,
  3685. int len)
  3686. {
  3687. int ret, offset;
  3688. struct ocfs2_xattr_entry *xe = xs->here;
  3689. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  3690. BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  3691. offset = xe - xh->xh_entries;
  3692. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
  3693. offset, len);
  3694. if (ret)
  3695. mlog_errno(ret);
  3696. return ret;
  3697. }
  3698. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  3699. struct ocfs2_xattr_search *xs,
  3700. char *val,
  3701. int value_len)
  3702. {
  3703. int offset;
  3704. struct ocfs2_xattr_value_root *xv;
  3705. struct ocfs2_xattr_entry *xe = xs->here;
  3706. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  3707. offset = le16_to_cpu(xe->xe_name_offset) +
  3708. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3709. xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
  3710. return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
  3711. }
  3712. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  3713. struct buffer_head *root_bh,
  3714. u64 blkno,
  3715. u32 cpos,
  3716. u32 len)
  3717. {
  3718. int ret;
  3719. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3720. struct inode *tl_inode = osb->osb_tl_inode;
  3721. handle_t *handle;
  3722. struct ocfs2_xattr_block *xb =
  3723. (struct ocfs2_xattr_block *)root_bh->b_data;
  3724. struct ocfs2_alloc_context *meta_ac = NULL;
  3725. struct ocfs2_cached_dealloc_ctxt dealloc;
  3726. struct ocfs2_extent_tree et;
  3727. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  3728. ocfs2_init_dealloc_ctxt(&dealloc);
  3729. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  3730. cpos, len, (unsigned long long)blkno);
  3731. ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
  3732. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  3733. if (ret) {
  3734. mlog_errno(ret);
  3735. return ret;
  3736. }
  3737. mutex_lock(&tl_inode->i_mutex);
  3738. if (ocfs2_truncate_log_needs_flush(osb)) {
  3739. ret = __ocfs2_flush_truncate_log(osb);
  3740. if (ret < 0) {
  3741. mlog_errno(ret);
  3742. goto out;
  3743. }
  3744. }
  3745. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  3746. if (handle == NULL) {
  3747. ret = -ENOMEM;
  3748. mlog_errno(ret);
  3749. goto out;
  3750. }
  3751. ret = ocfs2_journal_access(handle, inode, root_bh,
  3752. OCFS2_JOURNAL_ACCESS_WRITE);
  3753. if (ret) {
  3754. mlog_errno(ret);
  3755. goto out_commit;
  3756. }
  3757. ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
  3758. &dealloc);
  3759. if (ret) {
  3760. mlog_errno(ret);
  3761. goto out_commit;
  3762. }
  3763. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  3764. ret = ocfs2_journal_dirty(handle, root_bh);
  3765. if (ret) {
  3766. mlog_errno(ret);
  3767. goto out_commit;
  3768. }
  3769. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  3770. if (ret)
  3771. mlog_errno(ret);
  3772. out_commit:
  3773. ocfs2_commit_trans(osb, handle);
  3774. out:
  3775. ocfs2_schedule_truncate_log_flush(osb, 1);
  3776. mutex_unlock(&tl_inode->i_mutex);
  3777. if (meta_ac)
  3778. ocfs2_free_alloc_context(meta_ac);
  3779. ocfs2_run_deallocs(osb, &dealloc);
  3780. return ret;
  3781. }
  3782. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  3783. struct ocfs2_xattr_search *xs)
  3784. {
  3785. handle_t *handle = NULL;
  3786. struct ocfs2_xattr_header *xh = xs->bucket.xh;
  3787. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  3788. le16_to_cpu(xh->xh_count) - 1];
  3789. int ret = 0;
  3790. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
  3791. if (IS_ERR(handle)) {
  3792. ret = PTR_ERR(handle);
  3793. mlog_errno(ret);
  3794. return;
  3795. }
  3796. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
  3797. OCFS2_JOURNAL_ACCESS_WRITE);
  3798. if (ret) {
  3799. mlog_errno(ret);
  3800. goto out_commit;
  3801. }
  3802. /* Remove the old entry. */
  3803. memmove(xs->here, xs->here + 1,
  3804. (void *)last - (void *)xs->here);
  3805. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  3806. le16_add_cpu(&xh->xh_count, -1);
  3807. ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
  3808. if (ret < 0)
  3809. mlog_errno(ret);
  3810. out_commit:
  3811. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  3812. }
  3813. /*
  3814. * Set the xattr name/value in the bucket specified in xs.
  3815. *
  3816. * As the new value in xi may be stored in the bucket or in an outside cluster,
  3817. * we divide the whole process into 3 steps:
  3818. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  3819. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  3820. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  3821. * 4. If the clusters for the new outside value can't be allocated, we need
  3822. * to free the xattr we allocated in set.
  3823. */
  3824. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  3825. struct ocfs2_xattr_info *xi,
  3826. struct ocfs2_xattr_search *xs)
  3827. {
  3828. int ret, local = 1;
  3829. size_t value_len;
  3830. char *val = (char *)xi->value;
  3831. struct ocfs2_xattr_entry *xe = xs->here;
  3832. u32 name_hash = ocfs2_xattr_hash_by_name(inode,
  3833. xi->name_index, xi->name);
  3834. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  3835. /*
  3836. * We need to truncate the xattr storage first.
  3837. *
  3838. * If both the old and new value are stored to
  3839. * outside block, we only need to truncate
  3840. * the storage and then set the value outside.
  3841. *
  3842. * If the new value should be stored within block,
  3843. * we should free all the outside block first and
  3844. * the modification to the xattr block will be done
  3845. * by following steps.
  3846. */
  3847. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  3848. value_len = xi->value_len;
  3849. else
  3850. value_len = 0;
  3851. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3852. value_len);
  3853. if (ret)
  3854. goto out;
  3855. if (value_len)
  3856. goto set_value_outside;
  3857. }
  3858. value_len = xi->value_len;
  3859. /* So we have to handle the inside block change now. */
  3860. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  3861. /*
  3862. * If the new value will be stored outside of block,
  3863. * initalize a new empty value root and insert it first.
  3864. */
  3865. local = 0;
  3866. xi->value = &def_xv;
  3867. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  3868. }
  3869. ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
  3870. if (ret) {
  3871. mlog_errno(ret);
  3872. goto out;
  3873. }
  3874. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  3875. goto out;
  3876. /* allocate the space now for the outside block storage. */
  3877. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3878. value_len);
  3879. if (ret) {
  3880. mlog_errno(ret);
  3881. if (xs->not_found) {
  3882. /*
  3883. * We can't allocate enough clusters for outside
  3884. * storage and we have allocated xattr already,
  3885. * so need to remove it.
  3886. */
  3887. ocfs2_xattr_bucket_remove_xs(inode, xs);
  3888. }
  3889. goto out;
  3890. }
  3891. set_value_outside:
  3892. ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
  3893. out:
  3894. return ret;
  3895. }
  3896. /* check whether the xattr bucket is filled up with the same hash value. */
  3897. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  3898. struct ocfs2_xattr_bucket *bucket)
  3899. {
  3900. struct ocfs2_xattr_header *xh = bucket->xh;
  3901. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  3902. xh->xh_entries[0].xe_name_hash) {
  3903. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  3904. "hash = %u\n",
  3905. (unsigned long long)bucket->bhs[0]->b_blocknr,
  3906. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  3907. return -ENOSPC;
  3908. }
  3909. return 0;
  3910. }
  3911. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  3912. struct ocfs2_xattr_info *xi,
  3913. struct ocfs2_xattr_search *xs)
  3914. {
  3915. struct ocfs2_xattr_header *xh;
  3916. struct ocfs2_xattr_entry *xe;
  3917. u16 count, header_size, xh_free_start;
  3918. int i, free, max_free, need, old;
  3919. size_t value_size = 0, name_len = strlen(xi->name);
  3920. size_t blocksize = inode->i_sb->s_blocksize;
  3921. int ret, allocation = 0;
  3922. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3923. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  3924. try_again:
  3925. xh = xs->header;
  3926. count = le16_to_cpu(xh->xh_count);
  3927. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3928. header_size = sizeof(struct ocfs2_xattr_header) +
  3929. count * sizeof(struct ocfs2_xattr_entry);
  3930. max_free = OCFS2_XATTR_BUCKET_SIZE -
  3931. le16_to_cpu(xh->xh_name_value_len) - header_size;
  3932. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  3933. "of %u which exceed block size\n",
  3934. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  3935. header_size);
  3936. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  3937. value_size = OCFS2_XATTR_ROOT_SIZE;
  3938. else if (xi->value)
  3939. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  3940. if (xs->not_found)
  3941. need = sizeof(struct ocfs2_xattr_entry) +
  3942. OCFS2_XATTR_SIZE(name_len) + value_size;
  3943. else {
  3944. need = value_size + OCFS2_XATTR_SIZE(name_len);
  3945. /*
  3946. * We only replace the old value if the new length is smaller
  3947. * than the old one. Otherwise we will allocate new space in the
  3948. * bucket to store it.
  3949. */
  3950. xe = xs->here;
  3951. if (ocfs2_xattr_is_local(xe))
  3952. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3953. else
  3954. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3955. if (old >= value_size)
  3956. need = 0;
  3957. }
  3958. free = xh_free_start - header_size;
  3959. /*
  3960. * We need to make sure the new name/value pair
  3961. * can exist in the same block.
  3962. */
  3963. if (xh_free_start % blocksize < need)
  3964. free -= xh_free_start % blocksize;
  3965. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  3966. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  3967. " %u\n", xs->not_found,
  3968. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  3969. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  3970. le16_to_cpu(xh->xh_name_value_len));
  3971. if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  3972. if (need <= max_free &&
  3973. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  3974. /*
  3975. * We can create the space by defragment. Since only the
  3976. * name/value will be moved, the xe shouldn't be changed
  3977. * in xs.
  3978. */
  3979. ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
  3980. if (ret) {
  3981. mlog_errno(ret);
  3982. goto out;
  3983. }
  3984. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3985. free = xh_free_start - header_size;
  3986. if (xh_free_start % blocksize < need)
  3987. free -= xh_free_start % blocksize;
  3988. if (free >= need)
  3989. goto xattr_set;
  3990. mlog(0, "Can't get enough space for xattr insert by "
  3991. "defragment. Need %u bytes, but we have %d, so "
  3992. "allocate new bucket for it.\n", need, free);
  3993. }
  3994. /*
  3995. * We have to add new buckets or clusters and one
  3996. * allocation should leave us enough space for insert.
  3997. */
  3998. BUG_ON(allocation);
  3999. /*
  4000. * We do not allow for overlapping ranges between buckets. And
  4001. * the maximum number of collisions we will allow for then is
  4002. * one bucket's worth, so check it here whether we need to
  4003. * add a new bucket for the insert.
  4004. */
  4005. ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
  4006. if (ret) {
  4007. mlog_errno(ret);
  4008. goto out;
  4009. }
  4010. ret = ocfs2_add_new_xattr_bucket(inode,
  4011. xs->xattr_bh,
  4012. xs->bucket.bhs[0]);
  4013. if (ret) {
  4014. mlog_errno(ret);
  4015. goto out;
  4016. }
  4017. for (i = 0; i < blk_per_bucket; i++)
  4018. brelse(xs->bucket.bhs[i]);
  4019. memset(&xs->bucket, 0, sizeof(xs->bucket));
  4020. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4021. xi->name_index,
  4022. xi->name, xs);
  4023. if (ret && ret != -ENODATA)
  4024. goto out;
  4025. xs->not_found = ret;
  4026. allocation = 1;
  4027. goto try_again;
  4028. }
  4029. xattr_set:
  4030. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
  4031. out:
  4032. mlog_exit(ret);
  4033. return ret;
  4034. }
  4035. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4036. struct ocfs2_xattr_bucket *bucket,
  4037. void *para)
  4038. {
  4039. int ret = 0;
  4040. struct ocfs2_xattr_header *xh = bucket->xh;
  4041. u16 i;
  4042. struct ocfs2_xattr_entry *xe;
  4043. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4044. xe = &xh->xh_entries[i];
  4045. if (ocfs2_xattr_is_local(xe))
  4046. continue;
  4047. ret = ocfs2_xattr_bucket_value_truncate(inode,
  4048. bucket->bhs[0],
  4049. i, 0);
  4050. if (ret) {
  4051. mlog_errno(ret);
  4052. break;
  4053. }
  4054. }
  4055. return ret;
  4056. }
  4057. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  4058. struct buffer_head *xb_bh)
  4059. {
  4060. struct ocfs2_xattr_block *xb =
  4061. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4062. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  4063. int ret = 0;
  4064. u32 name_hash = UINT_MAX, e_cpos, num_clusters;
  4065. u64 p_blkno;
  4066. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4067. return 0;
  4068. while (name_hash > 0) {
  4069. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  4070. &e_cpos, &num_clusters, el);
  4071. if (ret) {
  4072. mlog_errno(ret);
  4073. goto out;
  4074. }
  4075. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  4076. ocfs2_delete_xattr_in_bucket,
  4077. NULL);
  4078. if (ret) {
  4079. mlog_errno(ret);
  4080. goto out;
  4081. }
  4082. ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
  4083. p_blkno, e_cpos, num_clusters);
  4084. if (ret) {
  4085. mlog_errno(ret);
  4086. break;
  4087. }
  4088. if (e_cpos == 0)
  4089. break;
  4090. name_hash = e_cpos - 1;
  4091. }
  4092. out:
  4093. return ret;
  4094. }
  4095. /*
  4096. * 'trusted' attributes support
  4097. */
  4098. #define XATTR_TRUSTED_PREFIX "trusted."
  4099. static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
  4100. size_t list_size, const char *name,
  4101. size_t name_len)
  4102. {
  4103. const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
  4104. const size_t total_len = prefix_len + name_len + 1;
  4105. if (list && total_len <= list_size) {
  4106. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  4107. memcpy(list + prefix_len, name, name_len);
  4108. list[prefix_len + name_len] = '\0';
  4109. }
  4110. return total_len;
  4111. }
  4112. static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
  4113. void *buffer, size_t size)
  4114. {
  4115. if (strcmp(name, "") == 0)
  4116. return -EINVAL;
  4117. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
  4118. buffer, size);
  4119. }
  4120. static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
  4121. const void *value, size_t size, int flags)
  4122. {
  4123. if (strcmp(name, "") == 0)
  4124. return -EINVAL;
  4125. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
  4126. size, flags);
  4127. }
  4128. struct xattr_handler ocfs2_xattr_trusted_handler = {
  4129. .prefix = XATTR_TRUSTED_PREFIX,
  4130. .list = ocfs2_xattr_trusted_list,
  4131. .get = ocfs2_xattr_trusted_get,
  4132. .set = ocfs2_xattr_trusted_set,
  4133. };
  4134. /*
  4135. * 'user' attributes support
  4136. */
  4137. #define XATTR_USER_PREFIX "user."
  4138. static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
  4139. size_t list_size, const char *name,
  4140. size_t name_len)
  4141. {
  4142. const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
  4143. const size_t total_len = prefix_len + name_len + 1;
  4144. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4145. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4146. return 0;
  4147. if (list && total_len <= list_size) {
  4148. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  4149. memcpy(list + prefix_len, name, name_len);
  4150. list[prefix_len + name_len] = '\0';
  4151. }
  4152. return total_len;
  4153. }
  4154. static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
  4155. void *buffer, size_t size)
  4156. {
  4157. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4158. if (strcmp(name, "") == 0)
  4159. return -EINVAL;
  4160. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4161. return -EOPNOTSUPP;
  4162. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
  4163. buffer, size);
  4164. }
  4165. static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
  4166. const void *value, size_t size, int flags)
  4167. {
  4168. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4169. if (strcmp(name, "") == 0)
  4170. return -EINVAL;
  4171. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4172. return -EOPNOTSUPP;
  4173. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
  4174. size, flags);
  4175. }
  4176. struct xattr_handler ocfs2_xattr_user_handler = {
  4177. .prefix = XATTR_USER_PREFIX,
  4178. .list = ocfs2_xattr_user_list,
  4179. .get = ocfs2_xattr_user_get,
  4180. .set = ocfs2_xattr_user_set,
  4181. };