xattr.c 122 KB

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