dir.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dir.c
  4. *
  5. * Creates, reads, walks and deletes directory-nodes
  6. *
  7. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  8. *
  9. * Portions of this code from linux/fs/ext3/dir.c
  10. *
  11. * Copyright (C) 1992, 1993, 1994, 1995
  12. * Remy Card ([email protected])
  13. * Laboratoire MASI - Institut Blaise pascal
  14. * Universite Pierre et Marie Curie (Paris VI)
  15. *
  16. * from
  17. *
  18. * linux/fs/minix/dir.c
  19. *
  20. * Copyright (C) 1991, 1992 Linus Torvalds
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/types.h>
  24. #include <linux/slab.h>
  25. #include <linux/highmem.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/sort.h>
  28. #include <linux/iversion.h>
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "blockcheck.h"
  33. #include "dir.h"
  34. #include "dlmglue.h"
  35. #include "extent_map.h"
  36. #include "file.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "namei.h"
  40. #include "suballoc.h"
  41. #include "super.h"
  42. #include "sysfile.h"
  43. #include "uptodate.h"
  44. #include "ocfs2_trace.h"
  45. #include "buffer_head_io.h"
  46. #define NAMEI_RA_CHUNKS 2
  47. #define NAMEI_RA_BLOCKS 4
  48. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  49. static int ocfs2_do_extend_dir(struct super_block *sb,
  50. handle_t *handle,
  51. struct inode *dir,
  52. struct buffer_head *parent_fe_bh,
  53. struct ocfs2_alloc_context *data_ac,
  54. struct ocfs2_alloc_context *meta_ac,
  55. struct buffer_head **new_bh);
  56. static int ocfs2_dir_indexed(struct inode *inode);
  57. /*
  58. * These are distinct checks because future versions of the file system will
  59. * want to have a trailing dirent structure independent of indexing.
  60. */
  61. static int ocfs2_supports_dir_trailer(struct inode *dir)
  62. {
  63. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  64. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  65. return 0;
  66. return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
  67. }
  68. /*
  69. * "new' here refers to the point at which we're creating a new
  70. * directory via "mkdir()", but also when we're expanding an inline
  71. * directory. In either case, we don't yet have the indexing bit set
  72. * on the directory, so the standard checks will fail in when metaecc
  73. * is turned off. Only directory-initialization type functions should
  74. * use this then. Everything else wants ocfs2_supports_dir_trailer()
  75. */
  76. static int ocfs2_new_dir_wants_trailer(struct inode *dir)
  77. {
  78. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  79. return ocfs2_meta_ecc(osb) ||
  80. ocfs2_supports_indexed_dirs(osb);
  81. }
  82. static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
  83. {
  84. return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
  85. }
  86. #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
  87. /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
  88. * them more consistent? */
  89. struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
  90. void *data)
  91. {
  92. char *p = data;
  93. p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
  94. return (struct ocfs2_dir_block_trailer *)p;
  95. }
  96. /*
  97. * XXX: This is executed once on every dirent. We should consider optimizing
  98. * it.
  99. */
  100. static int ocfs2_skip_dir_trailer(struct inode *dir,
  101. struct ocfs2_dir_entry *de,
  102. unsigned long offset,
  103. unsigned long blklen)
  104. {
  105. unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
  106. if (!ocfs2_supports_dir_trailer(dir))
  107. return 0;
  108. if (offset != toff)
  109. return 0;
  110. return 1;
  111. }
  112. static void ocfs2_init_dir_trailer(struct inode *inode,
  113. struct buffer_head *bh, u16 rec_len)
  114. {
  115. struct ocfs2_dir_block_trailer *trailer;
  116. trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
  117. strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
  118. trailer->db_compat_rec_len =
  119. cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
  120. trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  121. trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
  122. trailer->db_free_rec_len = cpu_to_le16(rec_len);
  123. }
  124. /*
  125. * Link an unindexed block with a dir trailer structure into the index free
  126. * list. This function will modify dirdata_bh, but assumes you've already
  127. * passed it to the journal.
  128. */
  129. static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
  130. struct buffer_head *dx_root_bh,
  131. struct buffer_head *dirdata_bh)
  132. {
  133. int ret;
  134. struct ocfs2_dx_root_block *dx_root;
  135. struct ocfs2_dir_block_trailer *trailer;
  136. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  137. OCFS2_JOURNAL_ACCESS_WRITE);
  138. if (ret) {
  139. mlog_errno(ret);
  140. goto out;
  141. }
  142. trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  143. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  144. trailer->db_free_next = dx_root->dr_free_blk;
  145. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  146. ocfs2_journal_dirty(handle, dx_root_bh);
  147. out:
  148. return ret;
  149. }
  150. static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
  151. {
  152. return res->dl_prev_leaf_bh == NULL;
  153. }
  154. void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
  155. {
  156. brelse(res->dl_dx_root_bh);
  157. brelse(res->dl_leaf_bh);
  158. brelse(res->dl_dx_leaf_bh);
  159. brelse(res->dl_prev_leaf_bh);
  160. }
  161. static int ocfs2_dir_indexed(struct inode *inode)
  162. {
  163. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
  164. return 1;
  165. return 0;
  166. }
  167. static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
  168. {
  169. return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
  170. }
  171. /*
  172. * Hashing code adapted from ext3
  173. */
  174. #define DELTA 0x9E3779B9
  175. static void TEA_transform(__u32 buf[4], __u32 const in[])
  176. {
  177. __u32 sum = 0;
  178. __u32 b0 = buf[0], b1 = buf[1];
  179. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  180. int n = 16;
  181. do {
  182. sum += DELTA;
  183. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  184. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  185. } while (--n);
  186. buf[0] += b0;
  187. buf[1] += b1;
  188. }
  189. static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
  190. {
  191. __u32 pad, val;
  192. int i;
  193. pad = (__u32)len | ((__u32)len << 8);
  194. pad |= pad << 16;
  195. val = pad;
  196. if (len > num*4)
  197. len = num * 4;
  198. for (i = 0; i < len; i++) {
  199. if ((i % 4) == 0)
  200. val = pad;
  201. val = msg[i] + (val << 8);
  202. if ((i % 4) == 3) {
  203. *buf++ = val;
  204. val = pad;
  205. num--;
  206. }
  207. }
  208. if (--num >= 0)
  209. *buf++ = val;
  210. while (--num >= 0)
  211. *buf++ = pad;
  212. }
  213. static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  214. struct ocfs2_dx_hinfo *hinfo)
  215. {
  216. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  217. const char *p;
  218. __u32 in[8], buf[4];
  219. /*
  220. * XXX: Is this really necessary, if the index is never looked
  221. * at by readdir? Is a hash value of '0' a bad idea?
  222. */
  223. if ((len == 1 && !strncmp(".", name, 1)) ||
  224. (len == 2 && !strncmp("..", name, 2))) {
  225. buf[0] = buf[1] = 0;
  226. goto out;
  227. }
  228. #ifdef OCFS2_DEBUG_DX_DIRS
  229. /*
  230. * This makes it very easy to debug indexing problems. We
  231. * should never allow this to be selected without hand editing
  232. * this file though.
  233. */
  234. buf[0] = buf[1] = len;
  235. goto out;
  236. #endif
  237. memcpy(buf, osb->osb_dx_seed, sizeof(buf));
  238. p = name;
  239. while (len > 0) {
  240. str2hashbuf(p, len, in, 4);
  241. TEA_transform(buf, in);
  242. len -= 16;
  243. p += 16;
  244. }
  245. out:
  246. hinfo->major_hash = buf[0];
  247. hinfo->minor_hash = buf[1];
  248. }
  249. /*
  250. * bh passed here can be an inode block or a dir data block, depending
  251. * on the inode inline data flag.
  252. */
  253. static int ocfs2_check_dir_entry(struct inode * dir,
  254. struct ocfs2_dir_entry * de,
  255. struct buffer_head * bh,
  256. unsigned long offset)
  257. {
  258. const char *error_msg = NULL;
  259. const int rlen = le16_to_cpu(de->rec_len);
  260. if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
  261. error_msg = "rec_len is smaller than minimal";
  262. else if (unlikely(rlen % 4 != 0))
  263. error_msg = "rec_len % 4 != 0";
  264. else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
  265. error_msg = "rec_len is too small for name_len";
  266. else if (unlikely(
  267. ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
  268. error_msg = "directory entry across blocks";
  269. if (unlikely(error_msg != NULL))
  270. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  271. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  272. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  273. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  274. de->name_len);
  275. return error_msg == NULL ? 1 : 0;
  276. }
  277. static inline int ocfs2_match(int len,
  278. const char * const name,
  279. struct ocfs2_dir_entry *de)
  280. {
  281. if (len != de->name_len)
  282. return 0;
  283. if (!de->inode)
  284. return 0;
  285. return !memcmp(name, de->name, len);
  286. }
  287. /*
  288. * Returns 0 if not found, -1 on failure, and 1 on success
  289. */
  290. static inline int ocfs2_search_dirblock(struct buffer_head *bh,
  291. struct inode *dir,
  292. const char *name, int namelen,
  293. unsigned long offset,
  294. char *first_de,
  295. unsigned int bytes,
  296. struct ocfs2_dir_entry **res_dir)
  297. {
  298. struct ocfs2_dir_entry *de;
  299. char *dlimit, *de_buf;
  300. int de_len;
  301. int ret = 0;
  302. de_buf = first_de;
  303. dlimit = de_buf + bytes;
  304. while (de_buf < dlimit) {
  305. /* this code is executed quadratically often */
  306. /* do minimal checking `by hand' */
  307. de = (struct ocfs2_dir_entry *) de_buf;
  308. if (de_buf + namelen <= dlimit &&
  309. ocfs2_match(namelen, name, de)) {
  310. /* found a match - just to be sure, do a full check */
  311. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  312. ret = -1;
  313. goto bail;
  314. }
  315. *res_dir = de;
  316. ret = 1;
  317. goto bail;
  318. }
  319. /* prevent looping on a bad block */
  320. de_len = le16_to_cpu(de->rec_len);
  321. if (de_len <= 0) {
  322. ret = -1;
  323. goto bail;
  324. }
  325. de_buf += de_len;
  326. offset += de_len;
  327. }
  328. bail:
  329. trace_ocfs2_search_dirblock(ret);
  330. return ret;
  331. }
  332. static struct buffer_head *ocfs2_find_entry_id(const char *name,
  333. int namelen,
  334. struct inode *dir,
  335. struct ocfs2_dir_entry **res_dir)
  336. {
  337. int ret, found;
  338. struct buffer_head *di_bh = NULL;
  339. struct ocfs2_dinode *di;
  340. struct ocfs2_inline_data *data;
  341. ret = ocfs2_read_inode_block(dir, &di_bh);
  342. if (ret) {
  343. mlog_errno(ret);
  344. goto out;
  345. }
  346. di = (struct ocfs2_dinode *)di_bh->b_data;
  347. data = &di->id2.i_data;
  348. found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  349. data->id_data, i_size_read(dir), res_dir);
  350. if (found == 1)
  351. return di_bh;
  352. brelse(di_bh);
  353. out:
  354. return NULL;
  355. }
  356. static int ocfs2_validate_dir_block(struct super_block *sb,
  357. struct buffer_head *bh)
  358. {
  359. int rc;
  360. struct ocfs2_dir_block_trailer *trailer =
  361. ocfs2_trailer_from_bh(bh, sb);
  362. /*
  363. * We don't validate dirents here, that's handled
  364. * in-place when the code walks them.
  365. */
  366. trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
  367. BUG_ON(!buffer_uptodate(bh));
  368. /*
  369. * If the ecc fails, we return the error but otherwise
  370. * leave the filesystem running. We know any error is
  371. * local to this block.
  372. *
  373. * Note that we are safe to call this even if the directory
  374. * doesn't have a trailer. Filesystems without metaecc will do
  375. * nothing, and filesystems with it will have one.
  376. */
  377. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
  378. if (rc)
  379. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  380. (unsigned long long)bh->b_blocknr);
  381. return rc;
  382. }
  383. /*
  384. * Validate a directory trailer.
  385. *
  386. * We check the trailer here rather than in ocfs2_validate_dir_block()
  387. * because that function doesn't have the inode to test.
  388. */
  389. static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
  390. {
  391. int rc = 0;
  392. struct ocfs2_dir_block_trailer *trailer;
  393. trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
  394. if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
  395. rc = ocfs2_error(dir->i_sb,
  396. "Invalid dirblock #%llu: signature = %.*s\n",
  397. (unsigned long long)bh->b_blocknr, 7,
  398. trailer->db_signature);
  399. goto out;
  400. }
  401. if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
  402. rc = ocfs2_error(dir->i_sb,
  403. "Directory block #%llu has an invalid db_blkno of %llu\n",
  404. (unsigned long long)bh->b_blocknr,
  405. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  406. goto out;
  407. }
  408. if (le64_to_cpu(trailer->db_parent_dinode) !=
  409. OCFS2_I(dir)->ip_blkno) {
  410. rc = ocfs2_error(dir->i_sb,
  411. "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu\n",
  412. (unsigned long long)bh->b_blocknr,
  413. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  414. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  415. goto out;
  416. }
  417. out:
  418. return rc;
  419. }
  420. /*
  421. * This function forces all errors to -EIO for consistency with its
  422. * predecessor, ocfs2_bread(). We haven't audited what returning the
  423. * real error codes would do to callers. We log the real codes with
  424. * mlog_errno() before we squash them.
  425. */
  426. static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
  427. struct buffer_head **bh, int flags)
  428. {
  429. int rc = 0;
  430. struct buffer_head *tmp = *bh;
  431. rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
  432. ocfs2_validate_dir_block);
  433. if (rc) {
  434. mlog_errno(rc);
  435. goto out;
  436. }
  437. if (!(flags & OCFS2_BH_READAHEAD) &&
  438. ocfs2_supports_dir_trailer(inode)) {
  439. rc = ocfs2_check_dir_trailer(inode, tmp);
  440. if (rc) {
  441. if (!*bh)
  442. brelse(tmp);
  443. mlog_errno(rc);
  444. goto out;
  445. }
  446. }
  447. /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
  448. if (!*bh)
  449. *bh = tmp;
  450. out:
  451. return rc ? -EIO : 0;
  452. }
  453. /*
  454. * Read the block at 'phys' which belongs to this directory
  455. * inode. This function does no virtual->physical block translation -
  456. * what's passed in is assumed to be a valid directory block.
  457. */
  458. static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
  459. struct buffer_head **bh)
  460. {
  461. int ret;
  462. struct buffer_head *tmp = *bh;
  463. ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
  464. ocfs2_validate_dir_block);
  465. if (ret) {
  466. mlog_errno(ret);
  467. goto out;
  468. }
  469. if (ocfs2_supports_dir_trailer(dir)) {
  470. ret = ocfs2_check_dir_trailer(dir, tmp);
  471. if (ret) {
  472. if (!*bh)
  473. brelse(tmp);
  474. mlog_errno(ret);
  475. goto out;
  476. }
  477. }
  478. if (!ret && !*bh)
  479. *bh = tmp;
  480. out:
  481. return ret;
  482. }
  483. static int ocfs2_validate_dx_root(struct super_block *sb,
  484. struct buffer_head *bh)
  485. {
  486. int ret;
  487. struct ocfs2_dx_root_block *dx_root;
  488. BUG_ON(!buffer_uptodate(bh));
  489. dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
  490. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
  491. if (ret) {
  492. mlog(ML_ERROR,
  493. "Checksum failed for dir index root block %llu\n",
  494. (unsigned long long)bh->b_blocknr);
  495. return ret;
  496. }
  497. if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
  498. ret = ocfs2_error(sb,
  499. "Dir Index Root # %llu has bad signature %.*s\n",
  500. (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
  501. 7, dx_root->dr_signature);
  502. }
  503. return ret;
  504. }
  505. static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
  506. struct buffer_head **dx_root_bh)
  507. {
  508. int ret;
  509. u64 blkno = le64_to_cpu(di->i_dx_root);
  510. struct buffer_head *tmp = *dx_root_bh;
  511. ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  512. ocfs2_validate_dx_root);
  513. /* If ocfs2_read_block() got us a new bh, pass it up. */
  514. if (!ret && !*dx_root_bh)
  515. *dx_root_bh = tmp;
  516. return ret;
  517. }
  518. static int ocfs2_validate_dx_leaf(struct super_block *sb,
  519. struct buffer_head *bh)
  520. {
  521. int ret;
  522. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
  523. BUG_ON(!buffer_uptodate(bh));
  524. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
  525. if (ret) {
  526. mlog(ML_ERROR,
  527. "Checksum failed for dir index leaf block %llu\n",
  528. (unsigned long long)bh->b_blocknr);
  529. return ret;
  530. }
  531. if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
  532. ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s\n",
  533. 7, dx_leaf->dl_signature);
  534. }
  535. return ret;
  536. }
  537. static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
  538. struct buffer_head **dx_leaf_bh)
  539. {
  540. int ret;
  541. struct buffer_head *tmp = *dx_leaf_bh;
  542. ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  543. ocfs2_validate_dx_leaf);
  544. /* If ocfs2_read_block() got us a new bh, pass it up. */
  545. if (!ret && !*dx_leaf_bh)
  546. *dx_leaf_bh = tmp;
  547. return ret;
  548. }
  549. /*
  550. * Read a series of dx_leaf blocks. This expects all buffer_head
  551. * pointers to be NULL on function entry.
  552. */
  553. static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
  554. struct buffer_head **dx_leaf_bhs)
  555. {
  556. int ret;
  557. ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
  558. ocfs2_validate_dx_leaf);
  559. if (ret)
  560. mlog_errno(ret);
  561. return ret;
  562. }
  563. static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  564. struct inode *dir,
  565. struct ocfs2_dir_entry **res_dir)
  566. {
  567. struct super_block *sb;
  568. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  569. struct buffer_head *bh, *ret = NULL;
  570. unsigned long start, block, b;
  571. int ra_max = 0; /* Number of bh's in the readahead
  572. buffer, bh_use[] */
  573. int ra_ptr = 0; /* Current index into readahead
  574. buffer */
  575. int num = 0;
  576. int nblocks, i;
  577. sb = dir->i_sb;
  578. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  579. start = OCFS2_I(dir)->ip_dir_start_lookup;
  580. if (start >= nblocks)
  581. start = 0;
  582. block = start;
  583. restart:
  584. do {
  585. /*
  586. * We deal with the read-ahead logic here.
  587. */
  588. if (ra_ptr >= ra_max) {
  589. /* Refill the readahead buffer */
  590. ra_ptr = 0;
  591. b = block;
  592. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  593. /*
  594. * Terminate if we reach the end of the
  595. * directory and must wrap, or if our
  596. * search has finished at this block.
  597. */
  598. if (b >= nblocks || (num && block == start)) {
  599. bh_use[ra_max] = NULL;
  600. break;
  601. }
  602. num++;
  603. bh = NULL;
  604. ocfs2_read_dir_block(dir, b++, &bh,
  605. OCFS2_BH_READAHEAD);
  606. bh_use[ra_max] = bh;
  607. }
  608. }
  609. if ((bh = bh_use[ra_ptr++]) == NULL)
  610. goto next;
  611. if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
  612. /* read error, skip block & hope for the best.
  613. * ocfs2_read_dir_block() has released the bh. */
  614. mlog(ML_ERROR, "reading directory %llu, "
  615. "offset %lu\n",
  616. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  617. block);
  618. goto next;
  619. }
  620. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  621. block << sb->s_blocksize_bits,
  622. bh->b_data, sb->s_blocksize,
  623. res_dir);
  624. if (i == 1) {
  625. OCFS2_I(dir)->ip_dir_start_lookup = block;
  626. ret = bh;
  627. goto cleanup_and_exit;
  628. } else {
  629. brelse(bh);
  630. if (i < 0)
  631. goto cleanup_and_exit;
  632. }
  633. next:
  634. if (++block >= nblocks)
  635. block = 0;
  636. } while (block != start);
  637. /*
  638. * If the directory has grown while we were searching, then
  639. * search the last part of the directory before giving up.
  640. */
  641. block = nblocks;
  642. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  643. if (block < nblocks) {
  644. start = 0;
  645. goto restart;
  646. }
  647. cleanup_and_exit:
  648. /* Clean up the read-ahead blocks */
  649. for (; ra_ptr < ra_max; ra_ptr++)
  650. brelse(bh_use[ra_ptr]);
  651. trace_ocfs2_find_entry_el(ret);
  652. return ret;
  653. }
  654. static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
  655. struct ocfs2_extent_list *el,
  656. u32 major_hash,
  657. u32 *ret_cpos,
  658. u64 *ret_phys_blkno,
  659. unsigned int *ret_clen)
  660. {
  661. int ret = 0, i, found;
  662. struct buffer_head *eb_bh = NULL;
  663. struct ocfs2_extent_block *eb;
  664. struct ocfs2_extent_rec *rec = NULL;
  665. if (el->l_tree_depth) {
  666. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
  667. &eb_bh);
  668. if (ret) {
  669. mlog_errno(ret);
  670. goto out;
  671. }
  672. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  673. el = &eb->h_list;
  674. if (el->l_tree_depth) {
  675. ret = ocfs2_error(inode->i_sb,
  676. "Inode %lu has non zero tree depth in btree tree block %llu\n",
  677. inode->i_ino,
  678. (unsigned long long)eb_bh->b_blocknr);
  679. goto out;
  680. }
  681. }
  682. found = 0;
  683. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  684. rec = &el->l_recs[i];
  685. if (le32_to_cpu(rec->e_cpos) <= major_hash) {
  686. found = 1;
  687. break;
  688. }
  689. }
  690. if (!found) {
  691. ret = ocfs2_error(inode->i_sb,
  692. "Inode %lu has bad extent record (%u, %u, 0) in btree\n",
  693. inode->i_ino,
  694. le32_to_cpu(rec->e_cpos),
  695. ocfs2_rec_clusters(el, rec));
  696. goto out;
  697. }
  698. if (ret_phys_blkno)
  699. *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
  700. if (ret_cpos)
  701. *ret_cpos = le32_to_cpu(rec->e_cpos);
  702. if (ret_clen)
  703. *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
  704. out:
  705. brelse(eb_bh);
  706. return ret;
  707. }
  708. /*
  709. * Returns the block index, from the start of the cluster which this
  710. * hash belongs too.
  711. */
  712. static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  713. u32 minor_hash)
  714. {
  715. return minor_hash & osb->osb_dx_mask;
  716. }
  717. static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  718. struct ocfs2_dx_hinfo *hinfo)
  719. {
  720. return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
  721. }
  722. static int ocfs2_dx_dir_lookup(struct inode *inode,
  723. struct ocfs2_extent_list *el,
  724. struct ocfs2_dx_hinfo *hinfo,
  725. u32 *ret_cpos,
  726. u64 *ret_phys_blkno)
  727. {
  728. int ret = 0;
  729. unsigned int cend, clen;
  730. u32 cpos;
  731. u64 blkno;
  732. u32 name_hash = hinfo->major_hash;
  733. ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
  734. &clen);
  735. if (ret) {
  736. mlog_errno(ret);
  737. goto out;
  738. }
  739. cend = cpos + clen;
  740. if (name_hash >= cend) {
  741. /* We want the last cluster */
  742. blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
  743. cpos += clen - 1;
  744. } else {
  745. blkno += ocfs2_clusters_to_blocks(inode->i_sb,
  746. name_hash - cpos);
  747. cpos = name_hash;
  748. }
  749. /*
  750. * We now have the cluster which should hold our entry. To
  751. * find the exact block from the start of the cluster to
  752. * search, we take the lower bits of the hash.
  753. */
  754. blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
  755. if (ret_phys_blkno)
  756. *ret_phys_blkno = blkno;
  757. if (ret_cpos)
  758. *ret_cpos = cpos;
  759. out:
  760. return ret;
  761. }
  762. static int ocfs2_dx_dir_search(const char *name, int namelen,
  763. struct inode *dir,
  764. struct ocfs2_dx_root_block *dx_root,
  765. struct ocfs2_dir_lookup_result *res)
  766. {
  767. int ret, i, found;
  768. u64 phys;
  769. struct buffer_head *dx_leaf_bh = NULL;
  770. struct ocfs2_dx_leaf *dx_leaf;
  771. struct ocfs2_dx_entry *dx_entry = NULL;
  772. struct buffer_head *dir_ent_bh = NULL;
  773. struct ocfs2_dir_entry *dir_ent = NULL;
  774. struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
  775. struct ocfs2_extent_list *dr_el;
  776. struct ocfs2_dx_entry_list *entry_list;
  777. ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
  778. if (ocfs2_dx_root_inline(dx_root)) {
  779. entry_list = &dx_root->dr_entries;
  780. goto search;
  781. }
  782. dr_el = &dx_root->dr_list;
  783. ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
  784. if (ret) {
  785. mlog_errno(ret);
  786. goto out;
  787. }
  788. trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
  789. namelen, name, hinfo->major_hash,
  790. hinfo->minor_hash, (unsigned long long)phys);
  791. ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
  792. if (ret) {
  793. mlog_errno(ret);
  794. goto out;
  795. }
  796. dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
  797. trace_ocfs2_dx_dir_search_leaf_info(
  798. le16_to_cpu(dx_leaf->dl_list.de_num_used),
  799. le16_to_cpu(dx_leaf->dl_list.de_count));
  800. entry_list = &dx_leaf->dl_list;
  801. search:
  802. /*
  803. * Empty leaf is legal, so no need to check for that.
  804. */
  805. found = 0;
  806. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  807. dx_entry = &entry_list->de_entries[i];
  808. if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
  809. || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
  810. continue;
  811. /*
  812. * Search unindexed leaf block now. We're not
  813. * guaranteed to find anything.
  814. */
  815. ret = ocfs2_read_dir_block_direct(dir,
  816. le64_to_cpu(dx_entry->dx_dirent_blk),
  817. &dir_ent_bh);
  818. if (ret) {
  819. mlog_errno(ret);
  820. goto out;
  821. }
  822. /*
  823. * XXX: We should check the unindexed block here,
  824. * before using it.
  825. */
  826. found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
  827. 0, dir_ent_bh->b_data,
  828. dir->i_sb->s_blocksize, &dir_ent);
  829. if (found == 1)
  830. break;
  831. if (found == -1) {
  832. /* This means we found a bad directory entry. */
  833. ret = -EIO;
  834. mlog_errno(ret);
  835. goto out;
  836. }
  837. brelse(dir_ent_bh);
  838. dir_ent_bh = NULL;
  839. }
  840. if (found <= 0) {
  841. ret = -ENOENT;
  842. goto out;
  843. }
  844. res->dl_leaf_bh = dir_ent_bh;
  845. res->dl_entry = dir_ent;
  846. res->dl_dx_leaf_bh = dx_leaf_bh;
  847. res->dl_dx_entry = dx_entry;
  848. ret = 0;
  849. out:
  850. if (ret) {
  851. brelse(dx_leaf_bh);
  852. brelse(dir_ent_bh);
  853. }
  854. return ret;
  855. }
  856. static int ocfs2_find_entry_dx(const char *name, int namelen,
  857. struct inode *dir,
  858. struct ocfs2_dir_lookup_result *lookup)
  859. {
  860. int ret;
  861. struct buffer_head *di_bh = NULL;
  862. struct ocfs2_dinode *di;
  863. struct buffer_head *dx_root_bh = NULL;
  864. struct ocfs2_dx_root_block *dx_root;
  865. ret = ocfs2_read_inode_block(dir, &di_bh);
  866. if (ret) {
  867. mlog_errno(ret);
  868. goto out;
  869. }
  870. di = (struct ocfs2_dinode *)di_bh->b_data;
  871. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  872. if (ret) {
  873. mlog_errno(ret);
  874. goto out;
  875. }
  876. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  877. ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
  878. if (ret) {
  879. if (ret != -ENOENT)
  880. mlog_errno(ret);
  881. goto out;
  882. }
  883. lookup->dl_dx_root_bh = dx_root_bh;
  884. dx_root_bh = NULL;
  885. out:
  886. brelse(di_bh);
  887. brelse(dx_root_bh);
  888. return ret;
  889. }
  890. /*
  891. * Try to find an entry of the provided name within 'dir'.
  892. *
  893. * If nothing was found, -ENOENT is returned. Otherwise, zero is
  894. * returned and the struct 'res' will contain information useful to
  895. * other directory manipulation functions.
  896. *
  897. * Caller can NOT assume anything about the contents of the
  898. * buffer_heads - they are passed back only so that it can be passed
  899. * into any one of the manipulation functions (add entry, delete
  900. * entry, etc). As an example, bh in the extent directory case is a
  901. * data block, in the inline-data case it actually points to an inode,
  902. * in the indexed directory case, multiple buffers are involved.
  903. */
  904. int ocfs2_find_entry(const char *name, int namelen,
  905. struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
  906. {
  907. struct buffer_head *bh;
  908. struct ocfs2_dir_entry *res_dir = NULL;
  909. if (ocfs2_dir_indexed(dir))
  910. return ocfs2_find_entry_dx(name, namelen, dir, lookup);
  911. /*
  912. * The unindexed dir code only uses part of the lookup
  913. * structure, so there's no reason to push it down further
  914. * than this.
  915. */
  916. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  917. bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
  918. else
  919. bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
  920. if (bh == NULL)
  921. return -ENOENT;
  922. lookup->dl_leaf_bh = bh;
  923. lookup->dl_entry = res_dir;
  924. return 0;
  925. }
  926. /*
  927. * Update inode number and type of a previously found directory entry.
  928. */
  929. int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  930. struct ocfs2_dir_lookup_result *res,
  931. struct inode *new_entry_inode)
  932. {
  933. int ret;
  934. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  935. struct ocfs2_dir_entry *de = res->dl_entry;
  936. struct buffer_head *de_bh = res->dl_leaf_bh;
  937. /*
  938. * The same code works fine for both inline-data and extent
  939. * based directories, so no need to split this up. The only
  940. * difference is the journal_access function.
  941. */
  942. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  943. access = ocfs2_journal_access_di;
  944. ret = access(handle, INODE_CACHE(dir), de_bh,
  945. OCFS2_JOURNAL_ACCESS_WRITE);
  946. if (ret) {
  947. mlog_errno(ret);
  948. goto out;
  949. }
  950. de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  951. ocfs2_set_de_type(de, new_entry_inode->i_mode);
  952. ocfs2_journal_dirty(handle, de_bh);
  953. out:
  954. return ret;
  955. }
  956. /*
  957. * __ocfs2_delete_entry deletes a directory entry by merging it with the
  958. * previous entry
  959. */
  960. static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
  961. struct ocfs2_dir_entry *de_del,
  962. struct buffer_head *bh, char *first_de,
  963. unsigned int bytes)
  964. {
  965. struct ocfs2_dir_entry *de, *pde;
  966. int i, status = -ENOENT;
  967. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  968. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  969. access = ocfs2_journal_access_di;
  970. i = 0;
  971. pde = NULL;
  972. de = (struct ocfs2_dir_entry *) first_de;
  973. while (i < bytes) {
  974. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  975. status = -EIO;
  976. mlog_errno(status);
  977. goto bail;
  978. }
  979. if (de == de_del) {
  980. status = access(handle, INODE_CACHE(dir), bh,
  981. OCFS2_JOURNAL_ACCESS_WRITE);
  982. if (status < 0) {
  983. status = -EIO;
  984. mlog_errno(status);
  985. goto bail;
  986. }
  987. if (pde)
  988. le16_add_cpu(&pde->rec_len,
  989. le16_to_cpu(de->rec_len));
  990. de->inode = 0;
  991. inode_inc_iversion(dir);
  992. ocfs2_journal_dirty(handle, bh);
  993. goto bail;
  994. }
  995. i += le16_to_cpu(de->rec_len);
  996. pde = de;
  997. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  998. }
  999. bail:
  1000. return status;
  1001. }
  1002. static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
  1003. {
  1004. unsigned int hole;
  1005. if (le64_to_cpu(de->inode) == 0)
  1006. hole = le16_to_cpu(de->rec_len);
  1007. else
  1008. hole = le16_to_cpu(de->rec_len) -
  1009. OCFS2_DIR_REC_LEN(de->name_len);
  1010. return hole;
  1011. }
  1012. static int ocfs2_find_max_rec_len(struct super_block *sb,
  1013. struct buffer_head *dirblock_bh)
  1014. {
  1015. int size, this_hole, largest_hole = 0;
  1016. char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
  1017. struct ocfs2_dir_entry *de;
  1018. trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
  1019. size = ocfs2_dir_trailer_blk_off(sb);
  1020. limit = start + size;
  1021. de_buf = start;
  1022. de = (struct ocfs2_dir_entry *)de_buf;
  1023. do {
  1024. if (de_buf != trailer) {
  1025. this_hole = ocfs2_figure_dirent_hole(de);
  1026. if (this_hole > largest_hole)
  1027. largest_hole = this_hole;
  1028. }
  1029. de_buf += le16_to_cpu(de->rec_len);
  1030. de = (struct ocfs2_dir_entry *)de_buf;
  1031. } while (de_buf < limit);
  1032. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  1033. return largest_hole;
  1034. return 0;
  1035. }
  1036. static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
  1037. int index)
  1038. {
  1039. int num_used = le16_to_cpu(entry_list->de_num_used);
  1040. if (num_used == 1 || index == (num_used - 1))
  1041. goto clear;
  1042. memmove(&entry_list->de_entries[index],
  1043. &entry_list->de_entries[index + 1],
  1044. (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
  1045. clear:
  1046. num_used--;
  1047. memset(&entry_list->de_entries[num_used], 0,
  1048. sizeof(struct ocfs2_dx_entry));
  1049. entry_list->de_num_used = cpu_to_le16(num_used);
  1050. }
  1051. static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
  1052. struct ocfs2_dir_lookup_result *lookup)
  1053. {
  1054. int ret, index, max_rec_len, add_to_free_list = 0;
  1055. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1056. struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
  1057. struct ocfs2_dx_leaf *dx_leaf;
  1058. struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
  1059. struct ocfs2_dir_block_trailer *trailer;
  1060. struct ocfs2_dx_root_block *dx_root;
  1061. struct ocfs2_dx_entry_list *entry_list;
  1062. /*
  1063. * This function gets a bit messy because we might have to
  1064. * modify the root block, regardless of whether the indexed
  1065. * entries are stored inline.
  1066. */
  1067. /*
  1068. * *Only* set 'entry_list' here, based on where we're looking
  1069. * for the indexed entries. Later, we might still want to
  1070. * journal both blocks, based on free list state.
  1071. */
  1072. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1073. if (ocfs2_dx_root_inline(dx_root)) {
  1074. entry_list = &dx_root->dr_entries;
  1075. } else {
  1076. dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
  1077. entry_list = &dx_leaf->dl_list;
  1078. }
  1079. /* Neither of these are a disk corruption - that should have
  1080. * been caught by lookup, before we got here. */
  1081. BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
  1082. BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
  1083. index = (char *)dx_entry - (char *)entry_list->de_entries;
  1084. index /= sizeof(*dx_entry);
  1085. if (index >= le16_to_cpu(entry_list->de_num_used)) {
  1086. mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
  1087. (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
  1088. entry_list, dx_entry);
  1089. return -EIO;
  1090. }
  1091. /*
  1092. * We know that removal of this dirent will leave enough room
  1093. * for a new one, so add this block to the free list if it
  1094. * isn't already there.
  1095. */
  1096. trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  1097. if (trailer->db_free_rec_len == 0)
  1098. add_to_free_list = 1;
  1099. /*
  1100. * Add the block holding our index into the journal before
  1101. * removing the unindexed entry. If we get an error return
  1102. * from __ocfs2_delete_entry(), then it hasn't removed the
  1103. * entry yet. Likewise, successful return means we *must*
  1104. * remove the indexed entry.
  1105. *
  1106. * We're also careful to journal the root tree block here as
  1107. * the entry count needs to be updated. Also, we might be
  1108. * adding to the start of the free list.
  1109. */
  1110. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  1111. OCFS2_JOURNAL_ACCESS_WRITE);
  1112. if (ret) {
  1113. mlog_errno(ret);
  1114. goto out;
  1115. }
  1116. if (!ocfs2_dx_root_inline(dx_root)) {
  1117. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  1118. lookup->dl_dx_leaf_bh,
  1119. OCFS2_JOURNAL_ACCESS_WRITE);
  1120. if (ret) {
  1121. mlog_errno(ret);
  1122. goto out;
  1123. }
  1124. }
  1125. trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
  1126. index);
  1127. ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
  1128. leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
  1129. if (ret) {
  1130. mlog_errno(ret);
  1131. goto out;
  1132. }
  1133. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
  1134. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1135. if (add_to_free_list) {
  1136. trailer->db_free_next = dx_root->dr_free_blk;
  1137. dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
  1138. ocfs2_journal_dirty(handle, dx_root_bh);
  1139. }
  1140. /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
  1141. ocfs2_journal_dirty(handle, leaf_bh);
  1142. le32_add_cpu(&dx_root->dr_num_entries, -1);
  1143. ocfs2_journal_dirty(handle, dx_root_bh);
  1144. ocfs2_dx_list_remove_entry(entry_list, index);
  1145. if (!ocfs2_dx_root_inline(dx_root))
  1146. ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
  1147. out:
  1148. return ret;
  1149. }
  1150. static inline int ocfs2_delete_entry_id(handle_t *handle,
  1151. struct inode *dir,
  1152. struct ocfs2_dir_entry *de_del,
  1153. struct buffer_head *bh)
  1154. {
  1155. int ret;
  1156. struct buffer_head *di_bh = NULL;
  1157. struct ocfs2_dinode *di;
  1158. struct ocfs2_inline_data *data;
  1159. ret = ocfs2_read_inode_block(dir, &di_bh);
  1160. if (ret) {
  1161. mlog_errno(ret);
  1162. goto out;
  1163. }
  1164. di = (struct ocfs2_dinode *)di_bh->b_data;
  1165. data = &di->id2.i_data;
  1166. ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
  1167. i_size_read(dir));
  1168. brelse(di_bh);
  1169. out:
  1170. return ret;
  1171. }
  1172. static inline int ocfs2_delete_entry_el(handle_t *handle,
  1173. struct inode *dir,
  1174. struct ocfs2_dir_entry *de_del,
  1175. struct buffer_head *bh)
  1176. {
  1177. return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
  1178. bh->b_size);
  1179. }
  1180. /*
  1181. * Delete a directory entry. Hide the details of directory
  1182. * implementation from the caller.
  1183. */
  1184. int ocfs2_delete_entry(handle_t *handle,
  1185. struct inode *dir,
  1186. struct ocfs2_dir_lookup_result *res)
  1187. {
  1188. if (ocfs2_dir_indexed(dir))
  1189. return ocfs2_delete_entry_dx(handle, dir, res);
  1190. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1191. return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
  1192. res->dl_leaf_bh);
  1193. return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
  1194. res->dl_leaf_bh);
  1195. }
  1196. /*
  1197. * Check whether 'de' has enough room to hold an entry of
  1198. * 'new_rec_len' bytes.
  1199. */
  1200. static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  1201. unsigned int new_rec_len)
  1202. {
  1203. unsigned int de_really_used;
  1204. /* Check whether this is an empty record with enough space */
  1205. if (le64_to_cpu(de->inode) == 0 &&
  1206. le16_to_cpu(de->rec_len) >= new_rec_len)
  1207. return 1;
  1208. /*
  1209. * Record might have free space at the end which we can
  1210. * use.
  1211. */
  1212. de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  1213. if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  1214. return 1;
  1215. return 0;
  1216. }
  1217. static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
  1218. struct ocfs2_dx_entry *dx_new_entry)
  1219. {
  1220. int i;
  1221. i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  1222. dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
  1223. le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
  1224. }
  1225. static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
  1226. struct ocfs2_dx_hinfo *hinfo,
  1227. u64 dirent_blk)
  1228. {
  1229. int i;
  1230. struct ocfs2_dx_entry *dx_entry;
  1231. i = le16_to_cpu(entry_list->de_num_used);
  1232. dx_entry = &entry_list->de_entries[i];
  1233. memset(dx_entry, 0, sizeof(*dx_entry));
  1234. dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
  1235. dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
  1236. dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
  1237. le16_add_cpu(&entry_list->de_num_used, 1);
  1238. }
  1239. static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
  1240. struct ocfs2_dx_hinfo *hinfo,
  1241. u64 dirent_blk,
  1242. struct buffer_head *dx_leaf_bh)
  1243. {
  1244. int ret;
  1245. struct ocfs2_dx_leaf *dx_leaf;
  1246. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
  1247. OCFS2_JOURNAL_ACCESS_WRITE);
  1248. if (ret) {
  1249. mlog_errno(ret);
  1250. goto out;
  1251. }
  1252. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  1253. ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
  1254. ocfs2_journal_dirty(handle, dx_leaf_bh);
  1255. out:
  1256. return ret;
  1257. }
  1258. static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
  1259. struct ocfs2_dx_hinfo *hinfo,
  1260. u64 dirent_blk,
  1261. struct ocfs2_dx_root_block *dx_root)
  1262. {
  1263. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
  1264. }
  1265. static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
  1266. struct ocfs2_dir_lookup_result *lookup)
  1267. {
  1268. int ret = 0;
  1269. struct ocfs2_dx_root_block *dx_root;
  1270. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1271. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  1272. OCFS2_JOURNAL_ACCESS_WRITE);
  1273. if (ret) {
  1274. mlog_errno(ret);
  1275. goto out;
  1276. }
  1277. dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
  1278. if (ocfs2_dx_root_inline(dx_root)) {
  1279. ocfs2_dx_inline_root_insert(dir, handle,
  1280. &lookup->dl_hinfo,
  1281. lookup->dl_leaf_bh->b_blocknr,
  1282. dx_root);
  1283. } else {
  1284. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
  1285. lookup->dl_leaf_bh->b_blocknr,
  1286. lookup->dl_dx_leaf_bh);
  1287. if (ret)
  1288. goto out;
  1289. }
  1290. le32_add_cpu(&dx_root->dr_num_entries, 1);
  1291. ocfs2_journal_dirty(handle, dx_root_bh);
  1292. out:
  1293. return ret;
  1294. }
  1295. static void ocfs2_remove_block_from_free_list(struct inode *dir,
  1296. handle_t *handle,
  1297. struct ocfs2_dir_lookup_result *lookup)
  1298. {
  1299. struct ocfs2_dir_block_trailer *trailer, *prev;
  1300. struct ocfs2_dx_root_block *dx_root;
  1301. struct buffer_head *bh;
  1302. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1303. if (ocfs2_free_list_at_root(lookup)) {
  1304. bh = lookup->dl_dx_root_bh;
  1305. dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
  1306. dx_root->dr_free_blk = trailer->db_free_next;
  1307. } else {
  1308. bh = lookup->dl_prev_leaf_bh;
  1309. prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
  1310. prev->db_free_next = trailer->db_free_next;
  1311. }
  1312. trailer->db_free_rec_len = cpu_to_le16(0);
  1313. trailer->db_free_next = cpu_to_le64(0);
  1314. ocfs2_journal_dirty(handle, bh);
  1315. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1316. }
  1317. /*
  1318. * This expects that a journal write has been reserved on
  1319. * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
  1320. */
  1321. static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
  1322. struct ocfs2_dir_lookup_result *lookup)
  1323. {
  1324. int max_rec_len;
  1325. struct ocfs2_dir_block_trailer *trailer;
  1326. /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
  1327. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
  1328. if (max_rec_len) {
  1329. /*
  1330. * There's still room in this block, so no need to remove it
  1331. * from the free list. In this case, we just want to update
  1332. * the rec len accounting.
  1333. */
  1334. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1335. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1336. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1337. } else {
  1338. ocfs2_remove_block_from_free_list(dir, handle, lookup);
  1339. }
  1340. }
  1341. /* we don't always have a dentry for what we want to add, so people
  1342. * like orphan dir can call this instead.
  1343. *
  1344. * The lookup context must have been filled from
  1345. * ocfs2_prepare_dir_for_insert.
  1346. */
  1347. int __ocfs2_add_entry(handle_t *handle,
  1348. struct inode *dir,
  1349. const char *name, int namelen,
  1350. struct inode *inode, u64 blkno,
  1351. struct buffer_head *parent_fe_bh,
  1352. struct ocfs2_dir_lookup_result *lookup)
  1353. {
  1354. unsigned long offset;
  1355. unsigned short rec_len;
  1356. struct ocfs2_dir_entry *de, *de1;
  1357. struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  1358. struct super_block *sb = dir->i_sb;
  1359. int retval;
  1360. unsigned int size = sb->s_blocksize;
  1361. struct buffer_head *insert_bh = lookup->dl_leaf_bh;
  1362. char *data_start = insert_bh->b_data;
  1363. if (!namelen)
  1364. return -EINVAL;
  1365. if (ocfs2_dir_indexed(dir)) {
  1366. struct buffer_head *bh;
  1367. /*
  1368. * An indexed dir may require that we update the free space
  1369. * list. Reserve a write to the previous node in the list so
  1370. * that we don't fail later.
  1371. *
  1372. * XXX: This can be either a dx_root_block, or an unindexed
  1373. * directory tree leaf block.
  1374. */
  1375. if (ocfs2_free_list_at_root(lookup)) {
  1376. bh = lookup->dl_dx_root_bh;
  1377. retval = ocfs2_journal_access_dr(handle,
  1378. INODE_CACHE(dir), bh,
  1379. OCFS2_JOURNAL_ACCESS_WRITE);
  1380. } else {
  1381. bh = lookup->dl_prev_leaf_bh;
  1382. retval = ocfs2_journal_access_db(handle,
  1383. INODE_CACHE(dir), bh,
  1384. OCFS2_JOURNAL_ACCESS_WRITE);
  1385. }
  1386. if (retval) {
  1387. mlog_errno(retval);
  1388. return retval;
  1389. }
  1390. } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1391. data_start = di->id2.i_data.id_data;
  1392. size = i_size_read(dir);
  1393. BUG_ON(insert_bh != parent_fe_bh);
  1394. }
  1395. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1396. offset = 0;
  1397. de = (struct ocfs2_dir_entry *) data_start;
  1398. while (1) {
  1399. BUG_ON((char *)de >= (size + data_start));
  1400. /* These checks should've already been passed by the
  1401. * prepare function, but I guess we can leave them
  1402. * here anyway. */
  1403. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  1404. retval = -ENOENT;
  1405. goto bail;
  1406. }
  1407. if (ocfs2_match(namelen, name, de)) {
  1408. retval = -EEXIST;
  1409. goto bail;
  1410. }
  1411. /* We're guaranteed that we should have space, so we
  1412. * can't possibly have hit the trailer...right? */
  1413. mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
  1414. "Hit dir trailer trying to insert %.*s "
  1415. "(namelen %d) into directory %llu. "
  1416. "offset is %lu, trailer offset is %d\n",
  1417. namelen, name, namelen,
  1418. (unsigned long long)parent_fe_bh->b_blocknr,
  1419. offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
  1420. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1421. dir->i_mtime = dir->i_ctime = current_time(dir);
  1422. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  1423. if (retval < 0) {
  1424. mlog_errno(retval);
  1425. goto bail;
  1426. }
  1427. if (insert_bh == parent_fe_bh)
  1428. retval = ocfs2_journal_access_di(handle,
  1429. INODE_CACHE(dir),
  1430. insert_bh,
  1431. OCFS2_JOURNAL_ACCESS_WRITE);
  1432. else {
  1433. retval = ocfs2_journal_access_db(handle,
  1434. INODE_CACHE(dir),
  1435. insert_bh,
  1436. OCFS2_JOURNAL_ACCESS_WRITE);
  1437. if (!retval && ocfs2_dir_indexed(dir))
  1438. retval = ocfs2_dx_dir_insert(dir,
  1439. handle,
  1440. lookup);
  1441. }
  1442. if (retval) {
  1443. mlog_errno(retval);
  1444. goto bail;
  1445. }
  1446. /* By now the buffer is marked for journaling */
  1447. offset += le16_to_cpu(de->rec_len);
  1448. if (le64_to_cpu(de->inode)) {
  1449. de1 = (struct ocfs2_dir_entry *)((char *) de +
  1450. OCFS2_DIR_REC_LEN(de->name_len));
  1451. de1->rec_len =
  1452. cpu_to_le16(le16_to_cpu(de->rec_len) -
  1453. OCFS2_DIR_REC_LEN(de->name_len));
  1454. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1455. de = de1;
  1456. }
  1457. de->file_type = FT_UNKNOWN;
  1458. if (blkno) {
  1459. de->inode = cpu_to_le64(blkno);
  1460. ocfs2_set_de_type(de, inode->i_mode);
  1461. } else
  1462. de->inode = 0;
  1463. de->name_len = namelen;
  1464. memcpy(de->name, name, namelen);
  1465. if (ocfs2_dir_indexed(dir))
  1466. ocfs2_recalc_free_list(dir, handle, lookup);
  1467. inode_inc_iversion(dir);
  1468. ocfs2_journal_dirty(handle, insert_bh);
  1469. retval = 0;
  1470. goto bail;
  1471. }
  1472. offset += le16_to_cpu(de->rec_len);
  1473. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  1474. }
  1475. /* when you think about it, the assert above should prevent us
  1476. * from ever getting here. */
  1477. retval = -ENOSPC;
  1478. bail:
  1479. if (retval)
  1480. mlog_errno(retval);
  1481. return retval;
  1482. }
  1483. static int ocfs2_dir_foreach_blk_id(struct inode *inode,
  1484. u64 *f_version,
  1485. struct dir_context *ctx)
  1486. {
  1487. int ret, i;
  1488. unsigned long offset = ctx->pos;
  1489. struct buffer_head *di_bh = NULL;
  1490. struct ocfs2_dinode *di;
  1491. struct ocfs2_inline_data *data;
  1492. struct ocfs2_dir_entry *de;
  1493. ret = ocfs2_read_inode_block(inode, &di_bh);
  1494. if (ret) {
  1495. mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
  1496. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1497. goto out;
  1498. }
  1499. di = (struct ocfs2_dinode *)di_bh->b_data;
  1500. data = &di->id2.i_data;
  1501. while (ctx->pos < i_size_read(inode)) {
  1502. /* If the dir block has changed since the last call to
  1503. * readdir(2), then we might be pointing to an invalid
  1504. * dirent right now. Scan from the start of the block
  1505. * to make sure. */
  1506. if (!inode_eq_iversion(inode, *f_version)) {
  1507. for (i = 0; i < i_size_read(inode) && i < offset; ) {
  1508. de = (struct ocfs2_dir_entry *)
  1509. (data->id_data + i);
  1510. /* It's too expensive to do a full
  1511. * dirent test each time round this
  1512. * loop, but we do have to test at
  1513. * least that it is non-zero. A
  1514. * failure will be detected in the
  1515. * dirent test below. */
  1516. if (le16_to_cpu(de->rec_len) <
  1517. OCFS2_DIR_REC_LEN(1))
  1518. break;
  1519. i += le16_to_cpu(de->rec_len);
  1520. }
  1521. ctx->pos = offset = i;
  1522. *f_version = inode_query_iversion(inode);
  1523. }
  1524. de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
  1525. if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
  1526. /* On error, skip the f_pos to the end. */
  1527. ctx->pos = i_size_read(inode);
  1528. break;
  1529. }
  1530. offset += le16_to_cpu(de->rec_len);
  1531. if (le64_to_cpu(de->inode)) {
  1532. if (!dir_emit(ctx, de->name, de->name_len,
  1533. le64_to_cpu(de->inode),
  1534. fs_ftype_to_dtype(de->file_type)))
  1535. goto out;
  1536. }
  1537. ctx->pos += le16_to_cpu(de->rec_len);
  1538. }
  1539. out:
  1540. brelse(di_bh);
  1541. return 0;
  1542. }
  1543. /*
  1544. * NOTE: This function can be called against unindexed directories,
  1545. * and indexed ones.
  1546. */
  1547. static int ocfs2_dir_foreach_blk_el(struct inode *inode,
  1548. u64 *f_version,
  1549. struct dir_context *ctx,
  1550. bool persist)
  1551. {
  1552. unsigned long offset, blk, last_ra_blk = 0;
  1553. int i;
  1554. struct buffer_head * bh, * tmp;
  1555. struct ocfs2_dir_entry * de;
  1556. struct super_block * sb = inode->i_sb;
  1557. unsigned int ra_sectors = 16;
  1558. int stored = 0;
  1559. bh = NULL;
  1560. offset = ctx->pos & (sb->s_blocksize - 1);
  1561. while (ctx->pos < i_size_read(inode)) {
  1562. blk = ctx->pos >> sb->s_blocksize_bits;
  1563. if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
  1564. /* Skip the corrupt dirblock and keep trying */
  1565. ctx->pos += sb->s_blocksize - offset;
  1566. continue;
  1567. }
  1568. /* The idea here is to begin with 8k read-ahead and to stay
  1569. * 4k ahead of our current position.
  1570. *
  1571. * TODO: Use the pagecache for this. We just need to
  1572. * make sure it's cluster-safe... */
  1573. if (!last_ra_blk
  1574. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  1575. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  1576. i > 0; i--) {
  1577. tmp = NULL;
  1578. if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
  1579. OCFS2_BH_READAHEAD))
  1580. brelse(tmp);
  1581. }
  1582. last_ra_blk = blk;
  1583. ra_sectors = 8;
  1584. }
  1585. /* If the dir block has changed since the last call to
  1586. * readdir(2), then we might be pointing to an invalid
  1587. * dirent right now. Scan from the start of the block
  1588. * to make sure. */
  1589. if (!inode_eq_iversion(inode, *f_version)) {
  1590. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  1591. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  1592. /* It's too expensive to do a full
  1593. * dirent test each time round this
  1594. * loop, but we do have to test at
  1595. * least that it is non-zero. A
  1596. * failure will be detected in the
  1597. * dirent test below. */
  1598. if (le16_to_cpu(de->rec_len) <
  1599. OCFS2_DIR_REC_LEN(1))
  1600. break;
  1601. i += le16_to_cpu(de->rec_len);
  1602. }
  1603. offset = i;
  1604. ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
  1605. | offset;
  1606. *f_version = inode_query_iversion(inode);
  1607. }
  1608. while (ctx->pos < i_size_read(inode)
  1609. && offset < sb->s_blocksize) {
  1610. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  1611. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  1612. /* On error, skip the f_pos to the
  1613. next block. */
  1614. ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
  1615. break;
  1616. }
  1617. if (le64_to_cpu(de->inode)) {
  1618. if (!dir_emit(ctx, de->name,
  1619. de->name_len,
  1620. le64_to_cpu(de->inode),
  1621. fs_ftype_to_dtype(de->file_type))) {
  1622. brelse(bh);
  1623. return 0;
  1624. }
  1625. stored++;
  1626. }
  1627. offset += le16_to_cpu(de->rec_len);
  1628. ctx->pos += le16_to_cpu(de->rec_len);
  1629. }
  1630. offset = 0;
  1631. brelse(bh);
  1632. bh = NULL;
  1633. if (!persist && stored)
  1634. break;
  1635. }
  1636. return 0;
  1637. }
  1638. static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
  1639. struct dir_context *ctx,
  1640. bool persist)
  1641. {
  1642. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1643. return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
  1644. return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
  1645. }
  1646. /*
  1647. * This is intended to be called from inside other kernel functions,
  1648. * so we fake some arguments.
  1649. */
  1650. int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
  1651. {
  1652. u64 version = inode_query_iversion(inode);
  1653. ocfs2_dir_foreach_blk(inode, &version, ctx, true);
  1654. return 0;
  1655. }
  1656. /*
  1657. * ocfs2_readdir()
  1658. *
  1659. */
  1660. int ocfs2_readdir(struct file *file, struct dir_context *ctx)
  1661. {
  1662. int error = 0;
  1663. struct inode *inode = file_inode(file);
  1664. int lock_level = 0;
  1665. trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
  1666. error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
  1667. if (lock_level && error >= 0) {
  1668. /* We release EX lock which used to update atime
  1669. * and get PR lock again to reduce contention
  1670. * on commonly accessed directories. */
  1671. ocfs2_inode_unlock(inode, 1);
  1672. lock_level = 0;
  1673. error = ocfs2_inode_lock(inode, NULL, 0);
  1674. }
  1675. if (error < 0) {
  1676. if (error != -ENOENT)
  1677. mlog_errno(error);
  1678. /* we haven't got any yet, so propagate the error. */
  1679. goto bail_nolock;
  1680. }
  1681. error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
  1682. ocfs2_inode_unlock(inode, lock_level);
  1683. if (error)
  1684. mlog_errno(error);
  1685. bail_nolock:
  1686. return error;
  1687. }
  1688. /*
  1689. * NOTE: this should always be called with parent dir i_rwsem taken.
  1690. */
  1691. int ocfs2_find_files_on_disk(const char *name,
  1692. int namelen,
  1693. u64 *blkno,
  1694. struct inode *inode,
  1695. struct ocfs2_dir_lookup_result *lookup)
  1696. {
  1697. int status = -ENOENT;
  1698. trace_ocfs2_find_files_on_disk(namelen, name, blkno,
  1699. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1700. status = ocfs2_find_entry(name, namelen, inode, lookup);
  1701. if (status)
  1702. goto leave;
  1703. *blkno = le64_to_cpu(lookup->dl_entry->inode);
  1704. status = 0;
  1705. leave:
  1706. return status;
  1707. }
  1708. /*
  1709. * Convenience function for callers which just want the block number
  1710. * mapped to a name and don't require the full dirent info, etc.
  1711. */
  1712. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  1713. int namelen, u64 *blkno)
  1714. {
  1715. int ret;
  1716. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1717. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
  1718. ocfs2_free_dir_lookup_result(&lookup);
  1719. return ret;
  1720. }
  1721. /* Check for a name within a directory.
  1722. *
  1723. * Return 0 if the name does not exist
  1724. * Return -EEXIST if the directory contains the name
  1725. *
  1726. * Callers should have i_rwsem + a cluster lock on dir
  1727. */
  1728. int ocfs2_check_dir_for_entry(struct inode *dir,
  1729. const char *name,
  1730. int namelen)
  1731. {
  1732. int ret = 0;
  1733. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1734. trace_ocfs2_check_dir_for_entry(
  1735. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  1736. if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
  1737. ret = -EEXIST;
  1738. mlog_errno(ret);
  1739. }
  1740. ocfs2_free_dir_lookup_result(&lookup);
  1741. return ret;
  1742. }
  1743. struct ocfs2_empty_dir_priv {
  1744. struct dir_context ctx;
  1745. unsigned seen_dot;
  1746. unsigned seen_dot_dot;
  1747. unsigned seen_other;
  1748. unsigned dx_dir;
  1749. };
  1750. static bool ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
  1751. int name_len, loff_t pos, u64 ino,
  1752. unsigned type)
  1753. {
  1754. struct ocfs2_empty_dir_priv *p =
  1755. container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
  1756. /*
  1757. * Check the positions of "." and ".." records to be sure
  1758. * they're in the correct place.
  1759. *
  1760. * Indexed directories don't need to proceed past the first
  1761. * two entries, so we end the scan after seeing '..'. Despite
  1762. * that, we allow the scan to proceed In the event that we
  1763. * have a corrupted indexed directory (no dot or dot dot
  1764. * entries). This allows us to double check for existing
  1765. * entries which might not have been found in the index.
  1766. */
  1767. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  1768. p->seen_dot = 1;
  1769. return true;
  1770. }
  1771. if (name_len == 2 && !strncmp("..", name, 2) &&
  1772. pos == OCFS2_DIR_REC_LEN(1)) {
  1773. p->seen_dot_dot = 1;
  1774. if (p->dx_dir && p->seen_dot)
  1775. return false;
  1776. return true;
  1777. }
  1778. p->seen_other = 1;
  1779. return false;
  1780. }
  1781. static int ocfs2_empty_dir_dx(struct inode *inode,
  1782. struct ocfs2_empty_dir_priv *priv)
  1783. {
  1784. int ret;
  1785. struct buffer_head *di_bh = NULL;
  1786. struct buffer_head *dx_root_bh = NULL;
  1787. struct ocfs2_dinode *di;
  1788. struct ocfs2_dx_root_block *dx_root;
  1789. priv->dx_dir = 1;
  1790. ret = ocfs2_read_inode_block(inode, &di_bh);
  1791. if (ret) {
  1792. mlog_errno(ret);
  1793. goto out;
  1794. }
  1795. di = (struct ocfs2_dinode *)di_bh->b_data;
  1796. ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
  1797. if (ret) {
  1798. mlog_errno(ret);
  1799. goto out;
  1800. }
  1801. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1802. if (le32_to_cpu(dx_root->dr_num_entries) != 2)
  1803. priv->seen_other = 1;
  1804. out:
  1805. brelse(di_bh);
  1806. brelse(dx_root_bh);
  1807. return ret;
  1808. }
  1809. /*
  1810. * routine to check that the specified directory is empty (for rmdir)
  1811. *
  1812. * Returns 1 if dir is empty, zero otherwise.
  1813. *
  1814. * XXX: This is a performance problem for unindexed directories.
  1815. */
  1816. int ocfs2_empty_dir(struct inode *inode)
  1817. {
  1818. int ret;
  1819. struct ocfs2_empty_dir_priv priv = {
  1820. .ctx.actor = ocfs2_empty_dir_filldir,
  1821. };
  1822. if (ocfs2_dir_indexed(inode)) {
  1823. ret = ocfs2_empty_dir_dx(inode, &priv);
  1824. if (ret)
  1825. mlog_errno(ret);
  1826. /*
  1827. * We still run ocfs2_dir_foreach to get the checks
  1828. * for "." and "..".
  1829. */
  1830. }
  1831. ret = ocfs2_dir_foreach(inode, &priv.ctx);
  1832. if (ret)
  1833. mlog_errno(ret);
  1834. if (!priv.seen_dot || !priv.seen_dot_dot) {
  1835. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  1836. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1837. /*
  1838. * XXX: Is it really safe to allow an unlink to continue?
  1839. */
  1840. return 1;
  1841. }
  1842. return !priv.seen_other;
  1843. }
  1844. /*
  1845. * Fills "." and ".." dirents in a new directory block. Returns dirent for
  1846. * "..", which might be used during creation of a directory with a trailing
  1847. * header. It is otherwise safe to ignore the return code.
  1848. */
  1849. static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
  1850. struct inode *parent,
  1851. char *start,
  1852. unsigned int size)
  1853. {
  1854. struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  1855. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  1856. de->name_len = 1;
  1857. de->rec_len =
  1858. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1859. strcpy(de->name, ".");
  1860. ocfs2_set_de_type(de, S_IFDIR);
  1861. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  1862. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  1863. de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  1864. de->name_len = 2;
  1865. strcpy(de->name, "..");
  1866. ocfs2_set_de_type(de, S_IFDIR);
  1867. return de;
  1868. }
  1869. /*
  1870. * This works together with code in ocfs2_mknod_locked() which sets
  1871. * the inline-data flag and initializes the inline-data section.
  1872. */
  1873. static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  1874. handle_t *handle,
  1875. struct inode *parent,
  1876. struct inode *inode,
  1877. struct buffer_head *di_bh)
  1878. {
  1879. int ret;
  1880. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1881. struct ocfs2_inline_data *data = &di->id2.i_data;
  1882. unsigned int size = le16_to_cpu(data->id_count);
  1883. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1884. OCFS2_JOURNAL_ACCESS_WRITE);
  1885. if (ret) {
  1886. mlog_errno(ret);
  1887. goto out;
  1888. }
  1889. ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
  1890. ocfs2_journal_dirty(handle, di_bh);
  1891. i_size_write(inode, size);
  1892. set_nlink(inode, 2);
  1893. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1894. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1895. if (ret < 0)
  1896. mlog_errno(ret);
  1897. out:
  1898. return ret;
  1899. }
  1900. static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  1901. handle_t *handle,
  1902. struct inode *parent,
  1903. struct inode *inode,
  1904. struct buffer_head *fe_bh,
  1905. struct ocfs2_alloc_context *data_ac,
  1906. struct buffer_head **ret_new_bh)
  1907. {
  1908. int status;
  1909. unsigned int size = osb->sb->s_blocksize;
  1910. struct buffer_head *new_bh = NULL;
  1911. struct ocfs2_dir_entry *de;
  1912. if (ocfs2_new_dir_wants_trailer(inode))
  1913. size = ocfs2_dir_trailer_blk_off(parent->i_sb);
  1914. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  1915. data_ac, NULL, &new_bh);
  1916. if (status < 0) {
  1917. mlog_errno(status);
  1918. goto bail;
  1919. }
  1920. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  1921. status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
  1922. OCFS2_JOURNAL_ACCESS_CREATE);
  1923. if (status < 0) {
  1924. mlog_errno(status);
  1925. goto bail;
  1926. }
  1927. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  1928. de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
  1929. if (ocfs2_new_dir_wants_trailer(inode)) {
  1930. int size = le16_to_cpu(de->rec_len);
  1931. /*
  1932. * Figure out the size of the hole left over after
  1933. * insertion of '.' and '..'. The trailer wants this
  1934. * information.
  1935. */
  1936. size -= OCFS2_DIR_REC_LEN(2);
  1937. size -= sizeof(struct ocfs2_dir_block_trailer);
  1938. ocfs2_init_dir_trailer(inode, new_bh, size);
  1939. }
  1940. ocfs2_journal_dirty(handle, new_bh);
  1941. i_size_write(inode, inode->i_sb->s_blocksize);
  1942. set_nlink(inode, 2);
  1943. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1944. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  1945. if (status < 0) {
  1946. mlog_errno(status);
  1947. goto bail;
  1948. }
  1949. status = 0;
  1950. if (ret_new_bh) {
  1951. *ret_new_bh = new_bh;
  1952. new_bh = NULL;
  1953. }
  1954. bail:
  1955. brelse(new_bh);
  1956. return status;
  1957. }
  1958. static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
  1959. handle_t *handle, struct inode *dir,
  1960. struct buffer_head *di_bh,
  1961. struct buffer_head *dirdata_bh,
  1962. struct ocfs2_alloc_context *meta_ac,
  1963. int dx_inline, u32 num_entries,
  1964. struct buffer_head **ret_dx_root_bh)
  1965. {
  1966. int ret;
  1967. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  1968. u16 dr_suballoc_bit;
  1969. u64 suballoc_loc, dr_blkno;
  1970. unsigned int num_bits;
  1971. struct buffer_head *dx_root_bh = NULL;
  1972. struct ocfs2_dx_root_block *dx_root;
  1973. struct ocfs2_dir_block_trailer *trailer =
  1974. ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  1975. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  1976. &dr_suballoc_bit, &num_bits, &dr_blkno);
  1977. if (ret) {
  1978. mlog_errno(ret);
  1979. goto out;
  1980. }
  1981. trace_ocfs2_dx_dir_attach_index(
  1982. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  1983. (unsigned long long)dr_blkno);
  1984. dx_root_bh = sb_getblk(osb->sb, dr_blkno);
  1985. if (dx_root_bh == NULL) {
  1986. ret = -ENOMEM;
  1987. goto out;
  1988. }
  1989. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
  1990. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  1991. OCFS2_JOURNAL_ACCESS_CREATE);
  1992. if (ret < 0) {
  1993. mlog_errno(ret);
  1994. goto out;
  1995. }
  1996. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1997. memset(dx_root, 0, osb->sb->s_blocksize);
  1998. strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
  1999. dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  2000. dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
  2001. dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
  2002. dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
  2003. dx_root->dr_blkno = cpu_to_le64(dr_blkno);
  2004. dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
  2005. dx_root->dr_num_entries = cpu_to_le32(num_entries);
  2006. if (le16_to_cpu(trailer->db_free_rec_len))
  2007. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  2008. else
  2009. dx_root->dr_free_blk = cpu_to_le64(0);
  2010. if (dx_inline) {
  2011. dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
  2012. dx_root->dr_entries.de_count =
  2013. cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
  2014. } else {
  2015. dx_root->dr_list.l_count =
  2016. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  2017. }
  2018. ocfs2_journal_dirty(handle, dx_root_bh);
  2019. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2020. OCFS2_JOURNAL_ACCESS_CREATE);
  2021. if (ret) {
  2022. mlog_errno(ret);
  2023. goto out;
  2024. }
  2025. di->i_dx_root = cpu_to_le64(dr_blkno);
  2026. spin_lock(&OCFS2_I(dir)->ip_lock);
  2027. OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
  2028. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  2029. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2030. ocfs2_journal_dirty(handle, di_bh);
  2031. *ret_dx_root_bh = dx_root_bh;
  2032. dx_root_bh = NULL;
  2033. out:
  2034. brelse(dx_root_bh);
  2035. return ret;
  2036. }
  2037. static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
  2038. handle_t *handle, struct inode *dir,
  2039. struct buffer_head **dx_leaves,
  2040. int num_dx_leaves, u64 start_blk)
  2041. {
  2042. int ret, i;
  2043. struct ocfs2_dx_leaf *dx_leaf;
  2044. struct buffer_head *bh;
  2045. for (i = 0; i < num_dx_leaves; i++) {
  2046. bh = sb_getblk(osb->sb, start_blk + i);
  2047. if (bh == NULL) {
  2048. ret = -ENOMEM;
  2049. goto out;
  2050. }
  2051. dx_leaves[i] = bh;
  2052. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
  2053. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
  2054. OCFS2_JOURNAL_ACCESS_CREATE);
  2055. if (ret < 0) {
  2056. mlog_errno(ret);
  2057. goto out;
  2058. }
  2059. dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
  2060. memset(dx_leaf, 0, osb->sb->s_blocksize);
  2061. strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
  2062. dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
  2063. dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
  2064. dx_leaf->dl_list.de_count =
  2065. cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
  2066. trace_ocfs2_dx_dir_format_cluster(
  2067. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2068. (unsigned long long)bh->b_blocknr,
  2069. le16_to_cpu(dx_leaf->dl_list.de_count));
  2070. ocfs2_journal_dirty(handle, bh);
  2071. }
  2072. ret = 0;
  2073. out:
  2074. return ret;
  2075. }
  2076. /*
  2077. * Allocates and formats a new cluster for use in an indexed dir
  2078. * leaf. This version will not do the extent insert, so that it can be
  2079. * used by operations which need careful ordering.
  2080. */
  2081. static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
  2082. u32 cpos, handle_t *handle,
  2083. struct ocfs2_alloc_context *data_ac,
  2084. struct buffer_head **dx_leaves,
  2085. int num_dx_leaves, u64 *ret_phys_blkno)
  2086. {
  2087. int ret;
  2088. u32 phys, num;
  2089. u64 phys_blkno;
  2090. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2091. /*
  2092. * XXX: For create, this should claim cluster for the index
  2093. * *before* the unindexed insert so that we have a better
  2094. * chance of contiguousness as the directory grows in number
  2095. * of entries.
  2096. */
  2097. ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
  2098. if (ret) {
  2099. mlog_errno(ret);
  2100. goto out;
  2101. }
  2102. /*
  2103. * Format the new cluster first. That way, we're inserting
  2104. * valid data.
  2105. */
  2106. phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
  2107. ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
  2108. num_dx_leaves, phys_blkno);
  2109. if (ret) {
  2110. mlog_errno(ret);
  2111. goto out;
  2112. }
  2113. *ret_phys_blkno = phys_blkno;
  2114. out:
  2115. return ret;
  2116. }
  2117. static int ocfs2_dx_dir_new_cluster(struct inode *dir,
  2118. struct ocfs2_extent_tree *et,
  2119. u32 cpos, handle_t *handle,
  2120. struct ocfs2_alloc_context *data_ac,
  2121. struct ocfs2_alloc_context *meta_ac,
  2122. struct buffer_head **dx_leaves,
  2123. int num_dx_leaves)
  2124. {
  2125. int ret;
  2126. u64 phys_blkno;
  2127. ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
  2128. num_dx_leaves, &phys_blkno);
  2129. if (ret) {
  2130. mlog_errno(ret);
  2131. goto out;
  2132. }
  2133. ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
  2134. meta_ac);
  2135. if (ret)
  2136. mlog_errno(ret);
  2137. out:
  2138. return ret;
  2139. }
  2140. static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
  2141. int *ret_num_leaves)
  2142. {
  2143. int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
  2144. struct buffer_head **dx_leaves;
  2145. dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
  2146. GFP_NOFS);
  2147. if (dx_leaves && ret_num_leaves)
  2148. *ret_num_leaves = num_dx_leaves;
  2149. return dx_leaves;
  2150. }
  2151. static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
  2152. handle_t *handle,
  2153. struct inode *parent,
  2154. struct inode *inode,
  2155. struct buffer_head *di_bh,
  2156. struct ocfs2_alloc_context *data_ac,
  2157. struct ocfs2_alloc_context *meta_ac)
  2158. {
  2159. int ret;
  2160. struct buffer_head *leaf_bh = NULL;
  2161. struct buffer_head *dx_root_bh = NULL;
  2162. struct ocfs2_dx_hinfo hinfo;
  2163. struct ocfs2_dx_root_block *dx_root;
  2164. struct ocfs2_dx_entry_list *entry_list;
  2165. /*
  2166. * Our strategy is to create the directory as though it were
  2167. * unindexed, then add the index block. This works with very
  2168. * little complication since the state of a new directory is a
  2169. * very well known quantity.
  2170. *
  2171. * Essentially, we have two dirents ("." and ".."), in the 1st
  2172. * block which need indexing. These are easily inserted into
  2173. * the index block.
  2174. */
  2175. ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
  2176. data_ac, &leaf_bh);
  2177. if (ret) {
  2178. mlog_errno(ret);
  2179. goto out;
  2180. }
  2181. ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
  2182. meta_ac, 1, 2, &dx_root_bh);
  2183. if (ret) {
  2184. mlog_errno(ret);
  2185. goto out;
  2186. }
  2187. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2188. entry_list = &dx_root->dr_entries;
  2189. /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
  2190. ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
  2191. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2192. ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
  2193. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2194. out:
  2195. brelse(dx_root_bh);
  2196. brelse(leaf_bh);
  2197. return ret;
  2198. }
  2199. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  2200. handle_t *handle,
  2201. struct inode *parent,
  2202. struct inode *inode,
  2203. struct buffer_head *fe_bh,
  2204. struct ocfs2_alloc_context *data_ac,
  2205. struct ocfs2_alloc_context *meta_ac)
  2206. {
  2207. BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  2208. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  2209. return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  2210. if (ocfs2_supports_indexed_dirs(osb))
  2211. return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
  2212. data_ac, meta_ac);
  2213. return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  2214. data_ac, NULL);
  2215. }
  2216. static int ocfs2_dx_dir_index_block(struct inode *dir,
  2217. handle_t *handle,
  2218. struct buffer_head **dx_leaves,
  2219. int num_dx_leaves,
  2220. u32 *num_dx_entries,
  2221. struct buffer_head *dirent_bh)
  2222. {
  2223. int ret = 0, namelen, i;
  2224. char *de_buf, *limit;
  2225. struct ocfs2_dir_entry *de;
  2226. struct buffer_head *dx_leaf_bh;
  2227. struct ocfs2_dx_hinfo hinfo;
  2228. u64 dirent_blk = dirent_bh->b_blocknr;
  2229. de_buf = dirent_bh->b_data;
  2230. limit = de_buf + dir->i_sb->s_blocksize;
  2231. while (de_buf < limit) {
  2232. de = (struct ocfs2_dir_entry *)de_buf;
  2233. namelen = de->name_len;
  2234. if (!namelen || !de->inode)
  2235. goto inc;
  2236. ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
  2237. i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
  2238. dx_leaf_bh = dx_leaves[i];
  2239. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
  2240. dirent_blk, dx_leaf_bh);
  2241. if (ret) {
  2242. mlog_errno(ret);
  2243. goto out;
  2244. }
  2245. *num_dx_entries = *num_dx_entries + 1;
  2246. inc:
  2247. de_buf += le16_to_cpu(de->rec_len);
  2248. }
  2249. out:
  2250. return ret;
  2251. }
  2252. /*
  2253. * XXX: This expects dx_root_bh to already be part of the transaction.
  2254. */
  2255. static void ocfs2_dx_dir_index_root_block(struct inode *dir,
  2256. struct buffer_head *dx_root_bh,
  2257. struct buffer_head *dirent_bh)
  2258. {
  2259. char *de_buf, *limit;
  2260. struct ocfs2_dx_root_block *dx_root;
  2261. struct ocfs2_dir_entry *de;
  2262. struct ocfs2_dx_hinfo hinfo;
  2263. u64 dirent_blk = dirent_bh->b_blocknr;
  2264. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2265. de_buf = dirent_bh->b_data;
  2266. limit = de_buf + dir->i_sb->s_blocksize;
  2267. while (de_buf < limit) {
  2268. de = (struct ocfs2_dir_entry *)de_buf;
  2269. if (!de->name_len || !de->inode)
  2270. goto inc;
  2271. ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
  2272. trace_ocfs2_dx_dir_index_root_block(
  2273. (unsigned long long)dir->i_ino,
  2274. hinfo.major_hash, hinfo.minor_hash,
  2275. de->name_len, de->name,
  2276. le16_to_cpu(dx_root->dr_entries.de_num_used));
  2277. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
  2278. dirent_blk);
  2279. le32_add_cpu(&dx_root->dr_num_entries, 1);
  2280. inc:
  2281. de_buf += le16_to_cpu(de->rec_len);
  2282. }
  2283. }
  2284. /*
  2285. * Count the number of inline directory entries in di_bh and compare
  2286. * them against the number of entries we can hold in an inline dx root
  2287. * block.
  2288. */
  2289. static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  2290. struct buffer_head *di_bh)
  2291. {
  2292. int dirent_count = 0;
  2293. char *de_buf, *limit;
  2294. struct ocfs2_dir_entry *de;
  2295. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2296. de_buf = di->id2.i_data.id_data;
  2297. limit = de_buf + i_size_read(dir);
  2298. while (de_buf < limit) {
  2299. de = (struct ocfs2_dir_entry *)de_buf;
  2300. if (de->name_len && de->inode)
  2301. dirent_count++;
  2302. de_buf += le16_to_cpu(de->rec_len);
  2303. }
  2304. /* We are careful to leave room for one extra record. */
  2305. return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
  2306. }
  2307. /*
  2308. * Expand rec_len of the rightmost dirent in a directory block so that it
  2309. * contains the end of our valid space for dirents. We do this during
  2310. * expansion from an inline directory to one with extents. The first dir block
  2311. * in that case is taken from the inline data portion of the inode block.
  2312. *
  2313. * This will also return the largest amount of contiguous space for a dirent
  2314. * in the block. That value is *not* necessarily the last dirent, even after
  2315. * expansion. The directory indexing code wants this value for free space
  2316. * accounting. We do this here since we're already walking the entire dir
  2317. * block.
  2318. *
  2319. * We add the dir trailer if this filesystem wants it.
  2320. */
  2321. static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  2322. struct inode *dir)
  2323. {
  2324. struct super_block *sb = dir->i_sb;
  2325. struct ocfs2_dir_entry *de;
  2326. struct ocfs2_dir_entry *prev_de;
  2327. char *de_buf, *limit;
  2328. unsigned int new_size = sb->s_blocksize;
  2329. unsigned int bytes, this_hole;
  2330. unsigned int largest_hole = 0;
  2331. if (ocfs2_new_dir_wants_trailer(dir))
  2332. new_size = ocfs2_dir_trailer_blk_off(sb);
  2333. bytes = new_size - old_size;
  2334. limit = start + old_size;
  2335. de_buf = start;
  2336. de = (struct ocfs2_dir_entry *)de_buf;
  2337. do {
  2338. this_hole = ocfs2_figure_dirent_hole(de);
  2339. if (this_hole > largest_hole)
  2340. largest_hole = this_hole;
  2341. prev_de = de;
  2342. de_buf += le16_to_cpu(de->rec_len);
  2343. de = (struct ocfs2_dir_entry *)de_buf;
  2344. } while (de_buf < limit);
  2345. le16_add_cpu(&prev_de->rec_len, bytes);
  2346. /* We need to double check this after modification of the final
  2347. * dirent. */
  2348. this_hole = ocfs2_figure_dirent_hole(prev_de);
  2349. if (this_hole > largest_hole)
  2350. largest_hole = this_hole;
  2351. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  2352. return largest_hole;
  2353. return 0;
  2354. }
  2355. /*
  2356. * We allocate enough clusters to fulfill "blocks_wanted", but set
  2357. * i_size to exactly one block. Ocfs2_extend_dir() will handle the
  2358. * rest automatically for us.
  2359. *
  2360. * *first_block_bh is a pointer to the 1st data block allocated to the
  2361. * directory.
  2362. */
  2363. static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  2364. unsigned int blocks_wanted,
  2365. struct ocfs2_dir_lookup_result *lookup,
  2366. struct buffer_head **first_block_bh)
  2367. {
  2368. u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
  2369. struct super_block *sb = dir->i_sb;
  2370. int ret, i, num_dx_leaves = 0, dx_inline = 0,
  2371. credits = ocfs2_inline_to_extents_credits(sb);
  2372. u64 dx_insert_blkno, blkno,
  2373. bytes = blocks_wanted << sb->s_blocksize_bits;
  2374. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2375. struct ocfs2_inode_info *oi = OCFS2_I(dir);
  2376. struct ocfs2_alloc_context *data_ac = NULL;
  2377. struct ocfs2_alloc_context *meta_ac = NULL;
  2378. struct buffer_head *dirdata_bh = NULL;
  2379. struct buffer_head *dx_root_bh = NULL;
  2380. struct buffer_head **dx_leaves = NULL;
  2381. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2382. handle_t *handle;
  2383. struct ocfs2_extent_tree et;
  2384. struct ocfs2_extent_tree dx_et;
  2385. int did_quota = 0, bytes_allocated = 0;
  2386. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
  2387. alloc = ocfs2_clusters_for_bytes(sb, bytes);
  2388. dx_alloc = 0;
  2389. down_write(&oi->ip_alloc_sem);
  2390. if (ocfs2_supports_indexed_dirs(osb)) {
  2391. credits += ocfs2_add_dir_index_credits(sb);
  2392. dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
  2393. if (!dx_inline) {
  2394. /* Add one more cluster for an index leaf */
  2395. dx_alloc++;
  2396. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
  2397. &num_dx_leaves);
  2398. if (!dx_leaves) {
  2399. ret = -ENOMEM;
  2400. mlog_errno(ret);
  2401. goto out;
  2402. }
  2403. }
  2404. /* This gets us the dx_root */
  2405. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  2406. if (ret) {
  2407. mlog_errno(ret);
  2408. goto out;
  2409. }
  2410. }
  2411. /*
  2412. * We should never need more than 2 clusters for the unindexed
  2413. * tree - maximum dirent size is far less than one block. In
  2414. * fact, the only time we'd need more than one cluster is if
  2415. * blocksize == clustersize and the dirent won't fit in the
  2416. * extra space that the expansion to a single block gives. As
  2417. * of today, that only happens on 4k/4k file systems.
  2418. */
  2419. BUG_ON(alloc > 2);
  2420. ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
  2421. if (ret) {
  2422. mlog_errno(ret);
  2423. goto out;
  2424. }
  2425. /*
  2426. * Prepare for worst case allocation scenario of two separate
  2427. * extents in the unindexed tree.
  2428. */
  2429. if (alloc == 2)
  2430. credits += OCFS2_SUBALLOC_ALLOC;
  2431. handle = ocfs2_start_trans(osb, credits);
  2432. if (IS_ERR(handle)) {
  2433. ret = PTR_ERR(handle);
  2434. mlog_errno(ret);
  2435. goto out;
  2436. }
  2437. ret = dquot_alloc_space_nodirty(dir,
  2438. ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
  2439. if (ret)
  2440. goto out_commit;
  2441. did_quota = 1;
  2442. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2443. /*
  2444. * Allocate our index cluster first, to maximize the
  2445. * possibility that unindexed leaves grow
  2446. * contiguously.
  2447. */
  2448. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
  2449. dx_leaves, num_dx_leaves,
  2450. &dx_insert_blkno);
  2451. if (ret) {
  2452. mlog_errno(ret);
  2453. goto out_commit;
  2454. }
  2455. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2456. }
  2457. /*
  2458. * Try to claim as many clusters as the bitmap can give though
  2459. * if we only get one now, that's enough to continue. The rest
  2460. * will be claimed after the conversion to extents.
  2461. */
  2462. if (ocfs2_dir_resv_allowed(osb))
  2463. data_ac->ac_resv = &oi->ip_la_data_resv;
  2464. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
  2465. if (ret) {
  2466. mlog_errno(ret);
  2467. goto out_commit;
  2468. }
  2469. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2470. /*
  2471. * Operations are carefully ordered so that we set up the new
  2472. * data block first. The conversion from inline data to
  2473. * extents follows.
  2474. */
  2475. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2476. dirdata_bh = sb_getblk(sb, blkno);
  2477. if (!dirdata_bh) {
  2478. ret = -ENOMEM;
  2479. mlog_errno(ret);
  2480. goto out_commit;
  2481. }
  2482. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
  2483. ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
  2484. OCFS2_JOURNAL_ACCESS_CREATE);
  2485. if (ret) {
  2486. mlog_errno(ret);
  2487. goto out_commit;
  2488. }
  2489. memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  2490. memset(dirdata_bh->b_data + i_size_read(dir), 0,
  2491. sb->s_blocksize - i_size_read(dir));
  2492. i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
  2493. if (ocfs2_new_dir_wants_trailer(dir)) {
  2494. /*
  2495. * Prepare the dir trailer up front. It will otherwise look
  2496. * like a valid dirent. Even if inserting the index fails
  2497. * (unlikely), then all we'll have done is given first dir
  2498. * block a small amount of fragmentation.
  2499. */
  2500. ocfs2_init_dir_trailer(dir, dirdata_bh, i);
  2501. }
  2502. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2503. ocfs2_journal_dirty(handle, dirdata_bh);
  2504. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2505. /*
  2506. * Dx dirs with an external cluster need to do this up
  2507. * front. Inline dx root's get handled later, after
  2508. * we've allocated our root block. We get passed back
  2509. * a total number of items so that dr_num_entries can
  2510. * be correctly set once the dx_root has been
  2511. * allocated.
  2512. */
  2513. ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
  2514. num_dx_leaves, &num_dx_entries,
  2515. dirdata_bh);
  2516. if (ret) {
  2517. mlog_errno(ret);
  2518. goto out_commit;
  2519. }
  2520. }
  2521. /*
  2522. * Set extent, i_size, etc on the directory. After this, the
  2523. * inode should contain the same exact dirents as before and
  2524. * be fully accessible from system calls.
  2525. *
  2526. * We let the later dirent insert modify c/mtime - to the user
  2527. * the data hasn't changed.
  2528. */
  2529. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2530. OCFS2_JOURNAL_ACCESS_CREATE);
  2531. if (ret) {
  2532. mlog_errno(ret);
  2533. goto out_commit;
  2534. }
  2535. spin_lock(&oi->ip_lock);
  2536. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  2537. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2538. spin_unlock(&oi->ip_lock);
  2539. ocfs2_dinode_new_extent_list(dir, di);
  2540. i_size_write(dir, sb->s_blocksize);
  2541. dir->i_mtime = dir->i_ctime = current_time(dir);
  2542. di->i_size = cpu_to_le64(sb->s_blocksize);
  2543. di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  2544. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
  2545. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2546. /*
  2547. * This should never fail as our extent list is empty and all
  2548. * related blocks have been journaled already.
  2549. */
  2550. ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
  2551. 0, NULL);
  2552. if (ret) {
  2553. mlog_errno(ret);
  2554. goto out_commit;
  2555. }
  2556. /*
  2557. * Set i_blocks after the extent insert for the most up to
  2558. * date ip_clusters value.
  2559. */
  2560. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2561. ocfs2_journal_dirty(handle, di_bh);
  2562. if (ocfs2_supports_indexed_dirs(osb)) {
  2563. ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
  2564. dirdata_bh, meta_ac, dx_inline,
  2565. num_dx_entries, &dx_root_bh);
  2566. if (ret) {
  2567. mlog_errno(ret);
  2568. goto out_commit;
  2569. }
  2570. if (dx_inline) {
  2571. ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
  2572. dirdata_bh);
  2573. } else {
  2574. ocfs2_init_dx_root_extent_tree(&dx_et,
  2575. INODE_CACHE(dir),
  2576. dx_root_bh);
  2577. ret = ocfs2_insert_extent(handle, &dx_et, 0,
  2578. dx_insert_blkno, 1, 0, NULL);
  2579. if (ret)
  2580. mlog_errno(ret);
  2581. }
  2582. }
  2583. /*
  2584. * We asked for two clusters, but only got one in the 1st
  2585. * pass. Claim the 2nd cluster as a separate extent.
  2586. */
  2587. if (alloc > len) {
  2588. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
  2589. &len);
  2590. if (ret) {
  2591. mlog_errno(ret);
  2592. goto out_commit;
  2593. }
  2594. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2595. ret = ocfs2_insert_extent(handle, &et, 1,
  2596. blkno, len, 0, NULL);
  2597. if (ret) {
  2598. mlog_errno(ret);
  2599. goto out_commit;
  2600. }
  2601. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2602. }
  2603. *first_block_bh = dirdata_bh;
  2604. dirdata_bh = NULL;
  2605. if (ocfs2_supports_indexed_dirs(osb)) {
  2606. unsigned int off;
  2607. if (!dx_inline) {
  2608. /*
  2609. * We need to return the correct block within the
  2610. * cluster which should hold our entry.
  2611. */
  2612. off = ocfs2_dx_dir_hash_idx(osb,
  2613. &lookup->dl_hinfo);
  2614. get_bh(dx_leaves[off]);
  2615. lookup->dl_dx_leaf_bh = dx_leaves[off];
  2616. }
  2617. lookup->dl_dx_root_bh = dx_root_bh;
  2618. dx_root_bh = NULL;
  2619. }
  2620. out_commit:
  2621. if (ret < 0 && did_quota)
  2622. dquot_free_space_nodirty(dir, bytes_allocated);
  2623. ocfs2_commit_trans(osb, handle);
  2624. out:
  2625. up_write(&oi->ip_alloc_sem);
  2626. if (data_ac)
  2627. ocfs2_free_alloc_context(data_ac);
  2628. if (meta_ac)
  2629. ocfs2_free_alloc_context(meta_ac);
  2630. if (dx_leaves) {
  2631. for (i = 0; i < num_dx_leaves; i++)
  2632. brelse(dx_leaves[i]);
  2633. kfree(dx_leaves);
  2634. }
  2635. brelse(dirdata_bh);
  2636. brelse(dx_root_bh);
  2637. return ret;
  2638. }
  2639. /* returns a bh of the 1st new block in the allocation. */
  2640. static int ocfs2_do_extend_dir(struct super_block *sb,
  2641. handle_t *handle,
  2642. struct inode *dir,
  2643. struct buffer_head *parent_fe_bh,
  2644. struct ocfs2_alloc_context *data_ac,
  2645. struct ocfs2_alloc_context *meta_ac,
  2646. struct buffer_head **new_bh)
  2647. {
  2648. int status;
  2649. int extend, did_quota = 0;
  2650. u64 p_blkno, v_blkno;
  2651. spin_lock(&OCFS2_I(dir)->ip_lock);
  2652. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  2653. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2654. if (extend) {
  2655. u32 offset = OCFS2_I(dir)->ip_clusters;
  2656. status = dquot_alloc_space_nodirty(dir,
  2657. ocfs2_clusters_to_bytes(sb, 1));
  2658. if (status)
  2659. goto bail;
  2660. did_quota = 1;
  2661. status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
  2662. 1, 0, parent_fe_bh, handle,
  2663. data_ac, meta_ac, NULL);
  2664. BUG_ON(status == -EAGAIN);
  2665. if (status < 0) {
  2666. mlog_errno(status);
  2667. goto bail;
  2668. }
  2669. }
  2670. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  2671. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  2672. if (status < 0) {
  2673. mlog_errno(status);
  2674. goto bail;
  2675. }
  2676. *new_bh = sb_getblk(sb, p_blkno);
  2677. if (!*new_bh) {
  2678. status = -ENOMEM;
  2679. mlog_errno(status);
  2680. goto bail;
  2681. }
  2682. status = 0;
  2683. bail:
  2684. if (did_quota && status < 0)
  2685. dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
  2686. return status;
  2687. }
  2688. /*
  2689. * Assumes you already have a cluster lock on the directory.
  2690. *
  2691. * 'blocks_wanted' is only used if we have an inline directory which
  2692. * is to be turned into an extent based one. The size of the dirent to
  2693. * insert might be larger than the space gained by growing to just one
  2694. * block, so we may have to grow the inode by two blocks in that case.
  2695. *
  2696. * If the directory is already indexed, dx_root_bh must be provided.
  2697. */
  2698. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  2699. struct inode *dir,
  2700. struct buffer_head *parent_fe_bh,
  2701. unsigned int blocks_wanted,
  2702. struct ocfs2_dir_lookup_result *lookup,
  2703. struct buffer_head **new_de_bh)
  2704. {
  2705. int status = 0;
  2706. int credits, num_free_extents, drop_alloc_sem = 0;
  2707. loff_t dir_i_size;
  2708. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  2709. struct ocfs2_extent_list *el = &fe->id2.i_list;
  2710. struct ocfs2_alloc_context *data_ac = NULL;
  2711. struct ocfs2_alloc_context *meta_ac = NULL;
  2712. handle_t *handle = NULL;
  2713. struct buffer_head *new_bh = NULL;
  2714. struct ocfs2_dir_entry * de;
  2715. struct super_block *sb = osb->sb;
  2716. struct ocfs2_extent_tree et;
  2717. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  2718. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2719. /*
  2720. * This would be a code error as an inline directory should
  2721. * never have an index root.
  2722. */
  2723. BUG_ON(dx_root_bh);
  2724. status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
  2725. blocks_wanted, lookup,
  2726. &new_bh);
  2727. if (status) {
  2728. mlog_errno(status);
  2729. goto bail;
  2730. }
  2731. /* Expansion from inline to an indexed directory will
  2732. * have given us this. */
  2733. dx_root_bh = lookup->dl_dx_root_bh;
  2734. if (blocks_wanted == 1) {
  2735. /*
  2736. * If the new dirent will fit inside the space
  2737. * created by pushing out to one block, then
  2738. * we can complete the operation
  2739. * here. Otherwise we have to expand i_size
  2740. * and format the 2nd block below.
  2741. */
  2742. BUG_ON(new_bh == NULL);
  2743. goto bail_bh;
  2744. }
  2745. /*
  2746. * Get rid of 'new_bh' - we want to format the 2nd
  2747. * data block and return that instead.
  2748. */
  2749. brelse(new_bh);
  2750. new_bh = NULL;
  2751. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2752. drop_alloc_sem = 1;
  2753. dir_i_size = i_size_read(dir);
  2754. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2755. goto do_extend;
  2756. }
  2757. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2758. drop_alloc_sem = 1;
  2759. dir_i_size = i_size_read(dir);
  2760. trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
  2761. dir_i_size);
  2762. /* dir->i_size is always block aligned. */
  2763. spin_lock(&OCFS2_I(dir)->ip_lock);
  2764. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  2765. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2766. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
  2767. parent_fe_bh);
  2768. num_free_extents = ocfs2_num_free_extents(&et);
  2769. if (num_free_extents < 0) {
  2770. status = num_free_extents;
  2771. mlog_errno(status);
  2772. goto bail;
  2773. }
  2774. if (!num_free_extents) {
  2775. status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
  2776. if (status < 0) {
  2777. if (status != -ENOSPC)
  2778. mlog_errno(status);
  2779. goto bail;
  2780. }
  2781. }
  2782. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2783. if (status < 0) {
  2784. if (status != -ENOSPC)
  2785. mlog_errno(status);
  2786. goto bail;
  2787. }
  2788. if (ocfs2_dir_resv_allowed(osb))
  2789. data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
  2790. credits = ocfs2_calc_extend_credits(sb, el);
  2791. } else {
  2792. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2793. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2794. }
  2795. do_extend:
  2796. if (ocfs2_dir_indexed(dir))
  2797. credits++; /* For attaching the new dirent block to the
  2798. * dx_root */
  2799. handle = ocfs2_start_trans(osb, credits);
  2800. if (IS_ERR(handle)) {
  2801. status = PTR_ERR(handle);
  2802. handle = NULL;
  2803. mlog_errno(status);
  2804. goto bail;
  2805. }
  2806. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  2807. data_ac, meta_ac, &new_bh);
  2808. if (status < 0) {
  2809. mlog_errno(status);
  2810. goto bail;
  2811. }
  2812. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
  2813. status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
  2814. OCFS2_JOURNAL_ACCESS_CREATE);
  2815. if (status < 0) {
  2816. mlog_errno(status);
  2817. goto bail;
  2818. }
  2819. memset(new_bh->b_data, 0, sb->s_blocksize);
  2820. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  2821. de->inode = 0;
  2822. if (ocfs2_supports_dir_trailer(dir)) {
  2823. de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
  2824. ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
  2825. if (ocfs2_dir_indexed(dir)) {
  2826. status = ocfs2_dx_dir_link_trailer(dir, handle,
  2827. dx_root_bh, new_bh);
  2828. if (status) {
  2829. mlog_errno(status);
  2830. goto bail;
  2831. }
  2832. }
  2833. } else {
  2834. de->rec_len = cpu_to_le16(sb->s_blocksize);
  2835. }
  2836. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  2837. ocfs2_journal_dirty(handle, new_bh);
  2838. dir_i_size += dir->i_sb->s_blocksize;
  2839. i_size_write(dir, dir_i_size);
  2840. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2841. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  2842. if (status < 0) {
  2843. mlog_errno(status);
  2844. goto bail;
  2845. }
  2846. bail_bh:
  2847. *new_de_bh = new_bh;
  2848. get_bh(*new_de_bh);
  2849. bail:
  2850. if (handle)
  2851. ocfs2_commit_trans(osb, handle);
  2852. if (drop_alloc_sem)
  2853. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  2854. if (data_ac)
  2855. ocfs2_free_alloc_context(data_ac);
  2856. if (meta_ac)
  2857. ocfs2_free_alloc_context(meta_ac);
  2858. brelse(new_bh);
  2859. return status;
  2860. }
  2861. static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  2862. const char *name, int namelen,
  2863. struct buffer_head **ret_de_bh,
  2864. unsigned int *blocks_wanted)
  2865. {
  2866. int ret;
  2867. struct super_block *sb = dir->i_sb;
  2868. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2869. struct ocfs2_dir_entry *de, *last_de = NULL;
  2870. char *de_buf, *limit;
  2871. unsigned long offset = 0;
  2872. unsigned int rec_len, new_rec_len, free_space;
  2873. /*
  2874. * This calculates how many free bytes we'd have in block zero, should
  2875. * this function force expansion to an extent tree.
  2876. */
  2877. if (ocfs2_new_dir_wants_trailer(dir))
  2878. free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
  2879. else
  2880. free_space = dir->i_sb->s_blocksize - i_size_read(dir);
  2881. de_buf = di->id2.i_data.id_data;
  2882. limit = de_buf + i_size_read(dir);
  2883. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2884. while (de_buf < limit) {
  2885. de = (struct ocfs2_dir_entry *)de_buf;
  2886. if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  2887. ret = -ENOENT;
  2888. goto out;
  2889. }
  2890. if (ocfs2_match(namelen, name, de)) {
  2891. ret = -EEXIST;
  2892. goto out;
  2893. }
  2894. /*
  2895. * No need to check for a trailing dirent record here as
  2896. * they're not used for inline dirs.
  2897. */
  2898. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2899. /* Ok, we found a spot. Return this bh and let
  2900. * the caller actually fill it in. */
  2901. *ret_de_bh = di_bh;
  2902. get_bh(*ret_de_bh);
  2903. ret = 0;
  2904. goto out;
  2905. }
  2906. last_de = de;
  2907. de_buf += le16_to_cpu(de->rec_len);
  2908. offset += le16_to_cpu(de->rec_len);
  2909. }
  2910. /*
  2911. * We're going to require expansion of the directory - figure
  2912. * out how many blocks we'll need so that a place for the
  2913. * dirent can be found.
  2914. */
  2915. *blocks_wanted = 1;
  2916. new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
  2917. if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  2918. *blocks_wanted = 2;
  2919. ret = -ENOSPC;
  2920. out:
  2921. return ret;
  2922. }
  2923. static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  2924. int namelen, struct buffer_head **ret_de_bh)
  2925. {
  2926. unsigned long offset;
  2927. struct buffer_head *bh = NULL;
  2928. unsigned short rec_len;
  2929. struct ocfs2_dir_entry *de;
  2930. struct super_block *sb = dir->i_sb;
  2931. int status;
  2932. int blocksize = dir->i_sb->s_blocksize;
  2933. status = ocfs2_read_dir_block(dir, 0, &bh, 0);
  2934. if (status)
  2935. goto bail;
  2936. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2937. offset = 0;
  2938. de = (struct ocfs2_dir_entry *) bh->b_data;
  2939. while (1) {
  2940. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  2941. brelse(bh);
  2942. bh = NULL;
  2943. if (i_size_read(dir) <= offset) {
  2944. /*
  2945. * Caller will have to expand this
  2946. * directory.
  2947. */
  2948. status = -ENOSPC;
  2949. goto bail;
  2950. }
  2951. status = ocfs2_read_dir_block(dir,
  2952. offset >> sb->s_blocksize_bits,
  2953. &bh, 0);
  2954. if (status)
  2955. goto bail;
  2956. /* move to next block */
  2957. de = (struct ocfs2_dir_entry *) bh->b_data;
  2958. }
  2959. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  2960. status = -ENOENT;
  2961. goto bail;
  2962. }
  2963. if (ocfs2_match(namelen, name, de)) {
  2964. status = -EEXIST;
  2965. goto bail;
  2966. }
  2967. if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
  2968. blocksize))
  2969. goto next;
  2970. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2971. /* Ok, we found a spot. Return this bh and let
  2972. * the caller actually fill it in. */
  2973. *ret_de_bh = bh;
  2974. get_bh(*ret_de_bh);
  2975. status = 0;
  2976. goto bail;
  2977. }
  2978. next:
  2979. offset += le16_to_cpu(de->rec_len);
  2980. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  2981. }
  2982. bail:
  2983. brelse(bh);
  2984. if (status)
  2985. mlog_errno(status);
  2986. return status;
  2987. }
  2988. static int dx_leaf_sort_cmp(const void *a, const void *b)
  2989. {
  2990. const struct ocfs2_dx_entry *entry1 = a;
  2991. const struct ocfs2_dx_entry *entry2 = b;
  2992. u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
  2993. u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
  2994. u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
  2995. u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
  2996. if (major_hash1 > major_hash2)
  2997. return 1;
  2998. if (major_hash1 < major_hash2)
  2999. return -1;
  3000. /*
  3001. * It is not strictly necessary to sort by minor
  3002. */
  3003. if (minor_hash1 > minor_hash2)
  3004. return 1;
  3005. if (minor_hash1 < minor_hash2)
  3006. return -1;
  3007. return 0;
  3008. }
  3009. static void dx_leaf_sort_swap(void *a, void *b, int size)
  3010. {
  3011. struct ocfs2_dx_entry *entry1 = a;
  3012. struct ocfs2_dx_entry *entry2 = b;
  3013. BUG_ON(size != sizeof(*entry1));
  3014. swap(*entry1, *entry2);
  3015. }
  3016. static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  3017. {
  3018. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3019. int i, num = le16_to_cpu(dl_list->de_num_used);
  3020. for (i = 0; i < (num - 1); i++) {
  3021. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
  3022. le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
  3023. return 0;
  3024. }
  3025. return 1;
  3026. }
  3027. /*
  3028. * Find the optimal value to split this leaf on. This expects the leaf
  3029. * entries to be in sorted order.
  3030. *
  3031. * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
  3032. * the hash we want to insert.
  3033. *
  3034. * This function is only concerned with the major hash - that which
  3035. * determines which cluster an item belongs to.
  3036. */
  3037. static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
  3038. u32 leaf_cpos, u32 insert_hash,
  3039. u32 *split_hash)
  3040. {
  3041. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3042. int i, num_used = le16_to_cpu(dl_list->de_num_used);
  3043. int allsame;
  3044. /*
  3045. * There's a couple rare, but nasty corner cases we have to
  3046. * check for here. All of them involve a leaf where all value
  3047. * have the same hash, which is what we look for first.
  3048. *
  3049. * Most of the time, all of the above is false, and we simply
  3050. * pick the median value for a split.
  3051. */
  3052. allsame = ocfs2_dx_leaf_same_major(dx_leaf);
  3053. if (allsame) {
  3054. u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
  3055. if (val == insert_hash) {
  3056. /*
  3057. * No matter where we would choose to split,
  3058. * the new entry would want to occupy the same
  3059. * block as these. Since there's no space left
  3060. * in their existing block, we know there
  3061. * won't be space after the split.
  3062. */
  3063. return -ENOSPC;
  3064. }
  3065. if (val == leaf_cpos) {
  3066. /*
  3067. * Because val is the same as leaf_cpos (which
  3068. * is the smallest value this leaf can have),
  3069. * yet is not equal to insert_hash, then we
  3070. * know that insert_hash *must* be larger than
  3071. * val (and leaf_cpos). At least cpos+1 in value.
  3072. *
  3073. * We also know then, that there cannot be an
  3074. * adjacent extent (otherwise we'd be looking
  3075. * at it). Choosing this value gives us a
  3076. * chance to get some contiguousness.
  3077. */
  3078. *split_hash = leaf_cpos + 1;
  3079. return 0;
  3080. }
  3081. if (val > insert_hash) {
  3082. /*
  3083. * val can not be the same as insert hash, and
  3084. * also must be larger than leaf_cpos. Also,
  3085. * we know that there can't be a leaf between
  3086. * cpos and val, otherwise the entries with
  3087. * hash 'val' would be there.
  3088. */
  3089. *split_hash = val;
  3090. return 0;
  3091. }
  3092. *split_hash = insert_hash;
  3093. return 0;
  3094. }
  3095. /*
  3096. * Since the records are sorted and the checks above
  3097. * guaranteed that not all records in this block are the same,
  3098. * we simple travel forward, from the median, and pick the 1st
  3099. * record whose value is larger than leaf_cpos.
  3100. */
  3101. for (i = (num_used / 2); i < num_used; i++)
  3102. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
  3103. leaf_cpos)
  3104. break;
  3105. BUG_ON(i == num_used); /* Should be impossible */
  3106. *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
  3107. return 0;
  3108. }
  3109. /*
  3110. * Transfer all entries in orig_dx_leaves whose major hash is equal to or
  3111. * larger than split_hash into new_dx_leaves. We use a temporary
  3112. * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
  3113. *
  3114. * Since the block offset inside a leaf (cluster) is a constant mask
  3115. * of minor_hash, we can optimize - an item at block offset X within
  3116. * the original cluster, will be at offset X within the new cluster.
  3117. */
  3118. static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
  3119. handle_t *handle,
  3120. struct ocfs2_dx_leaf *tmp_dx_leaf,
  3121. struct buffer_head **orig_dx_leaves,
  3122. struct buffer_head **new_dx_leaves,
  3123. int num_dx_leaves)
  3124. {
  3125. int i, j, num_used;
  3126. u32 major_hash;
  3127. struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
  3128. struct ocfs2_dx_entry_list *orig_list, *tmp_list;
  3129. struct ocfs2_dx_entry *dx_entry;
  3130. tmp_list = &tmp_dx_leaf->dl_list;
  3131. for (i = 0; i < num_dx_leaves; i++) {
  3132. orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
  3133. orig_list = &orig_dx_leaf->dl_list;
  3134. new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
  3135. num_used = le16_to_cpu(orig_list->de_num_used);
  3136. memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
  3137. tmp_list->de_num_used = cpu_to_le16(0);
  3138. memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
  3139. for (j = 0; j < num_used; j++) {
  3140. dx_entry = &orig_list->de_entries[j];
  3141. major_hash = le32_to_cpu(dx_entry->dx_major_hash);
  3142. if (major_hash >= split_hash)
  3143. ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
  3144. dx_entry);
  3145. else
  3146. ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
  3147. dx_entry);
  3148. }
  3149. memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
  3150. ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
  3151. ocfs2_journal_dirty(handle, new_dx_leaves[i]);
  3152. }
  3153. }
  3154. static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
  3155. struct ocfs2_dx_root_block *dx_root)
  3156. {
  3157. int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
  3158. credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
  3159. credits += ocfs2_quota_trans_credits(osb->sb);
  3160. return credits;
  3161. }
  3162. /*
  3163. * Find the median value in dx_leaf_bh and allocate a new leaf to move
  3164. * half our entries into.
  3165. */
  3166. static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  3167. struct buffer_head *dx_root_bh,
  3168. struct buffer_head *dx_leaf_bh,
  3169. struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
  3170. u64 leaf_blkno)
  3171. {
  3172. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3173. int credits, ret, i, num_used, did_quota = 0;
  3174. u32 cpos, split_hash, insert_hash = hinfo->major_hash;
  3175. u64 orig_leaves_start;
  3176. int num_dx_leaves;
  3177. struct buffer_head **orig_dx_leaves = NULL;
  3178. struct buffer_head **new_dx_leaves = NULL;
  3179. struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
  3180. struct ocfs2_extent_tree et;
  3181. handle_t *handle = NULL;
  3182. struct ocfs2_dx_root_block *dx_root;
  3183. struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
  3184. trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
  3185. (unsigned long long)leaf_blkno,
  3186. insert_hash);
  3187. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3188. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3189. /*
  3190. * XXX: This is a rather large limit. We should use a more
  3191. * realistic value.
  3192. */
  3193. if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
  3194. return -ENOSPC;
  3195. num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  3196. if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3197. mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
  3198. "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3199. (unsigned long long)leaf_blkno, num_used);
  3200. ret = -EIO;
  3201. goto out;
  3202. }
  3203. orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3204. if (!orig_dx_leaves) {
  3205. ret = -ENOMEM;
  3206. mlog_errno(ret);
  3207. goto out;
  3208. }
  3209. new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
  3210. if (!new_dx_leaves) {
  3211. ret = -ENOMEM;
  3212. mlog_errno(ret);
  3213. goto out;
  3214. }
  3215. ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
  3216. if (ret) {
  3217. if (ret != -ENOSPC)
  3218. mlog_errno(ret);
  3219. goto out;
  3220. }
  3221. credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
  3222. handle = ocfs2_start_trans(osb, credits);
  3223. if (IS_ERR(handle)) {
  3224. ret = PTR_ERR(handle);
  3225. handle = NULL;
  3226. mlog_errno(ret);
  3227. goto out;
  3228. }
  3229. ret = dquot_alloc_space_nodirty(dir,
  3230. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3231. if (ret)
  3232. goto out_commit;
  3233. did_quota = 1;
  3234. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
  3235. OCFS2_JOURNAL_ACCESS_WRITE);
  3236. if (ret) {
  3237. mlog_errno(ret);
  3238. goto out_commit;
  3239. }
  3240. /*
  3241. * This block is changing anyway, so we can sort it in place.
  3242. */
  3243. sort(dx_leaf->dl_list.de_entries, num_used,
  3244. sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
  3245. dx_leaf_sort_swap);
  3246. ocfs2_journal_dirty(handle, dx_leaf_bh);
  3247. ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
  3248. &split_hash);
  3249. if (ret) {
  3250. mlog_errno(ret);
  3251. goto out_commit;
  3252. }
  3253. trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
  3254. /*
  3255. * We have to carefully order operations here. There are items
  3256. * which want to be in the new cluster before insert, but in
  3257. * order to put those items in the new cluster, we alter the
  3258. * old cluster. A failure to insert gets nasty.
  3259. *
  3260. * So, start by reserving writes to the old
  3261. * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
  3262. * the new cluster for us, before inserting it. The insert
  3263. * won't happen if there's an error before that. Once the
  3264. * insert is done then, we can transfer from one leaf into the
  3265. * other without fear of hitting any error.
  3266. */
  3267. /*
  3268. * The leaf transfer wants some scratch space so that we don't
  3269. * wind up doing a bunch of expensive memmove().
  3270. */
  3271. tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
  3272. if (!tmp_dx_leaf) {
  3273. ret = -ENOMEM;
  3274. mlog_errno(ret);
  3275. goto out_commit;
  3276. }
  3277. orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
  3278. ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
  3279. orig_dx_leaves);
  3280. if (ret) {
  3281. mlog_errno(ret);
  3282. goto out_commit;
  3283. }
  3284. cpos = split_hash;
  3285. ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
  3286. data_ac, meta_ac, new_dx_leaves,
  3287. num_dx_leaves);
  3288. if (ret) {
  3289. mlog_errno(ret);
  3290. goto out_commit;
  3291. }
  3292. for (i = 0; i < num_dx_leaves; i++) {
  3293. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  3294. orig_dx_leaves[i],
  3295. OCFS2_JOURNAL_ACCESS_WRITE);
  3296. if (ret) {
  3297. mlog_errno(ret);
  3298. goto out_commit;
  3299. }
  3300. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  3301. new_dx_leaves[i],
  3302. OCFS2_JOURNAL_ACCESS_WRITE);
  3303. if (ret) {
  3304. mlog_errno(ret);
  3305. goto out_commit;
  3306. }
  3307. }
  3308. ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
  3309. orig_dx_leaves, new_dx_leaves, num_dx_leaves);
  3310. out_commit:
  3311. if (ret < 0 && did_quota)
  3312. dquot_free_space_nodirty(dir,
  3313. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3314. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3315. ocfs2_commit_trans(osb, handle);
  3316. out:
  3317. if (orig_dx_leaves || new_dx_leaves) {
  3318. for (i = 0; i < num_dx_leaves; i++) {
  3319. if (orig_dx_leaves)
  3320. brelse(orig_dx_leaves[i]);
  3321. if (new_dx_leaves)
  3322. brelse(new_dx_leaves[i]);
  3323. }
  3324. kfree(orig_dx_leaves);
  3325. kfree(new_dx_leaves);
  3326. }
  3327. if (meta_ac)
  3328. ocfs2_free_alloc_context(meta_ac);
  3329. if (data_ac)
  3330. ocfs2_free_alloc_context(data_ac);
  3331. kfree(tmp_dx_leaf);
  3332. return ret;
  3333. }
  3334. static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
  3335. struct buffer_head *di_bh,
  3336. struct buffer_head *dx_root_bh,
  3337. const char *name, int namelen,
  3338. struct ocfs2_dir_lookup_result *lookup)
  3339. {
  3340. int ret, rebalanced = 0;
  3341. struct ocfs2_dx_root_block *dx_root;
  3342. struct buffer_head *dx_leaf_bh = NULL;
  3343. struct ocfs2_dx_leaf *dx_leaf;
  3344. u64 blkno;
  3345. u32 leaf_cpos;
  3346. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3347. restart_search:
  3348. ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
  3349. &leaf_cpos, &blkno);
  3350. if (ret) {
  3351. mlog_errno(ret);
  3352. goto out;
  3353. }
  3354. ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
  3355. if (ret) {
  3356. mlog_errno(ret);
  3357. goto out;
  3358. }
  3359. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3360. if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
  3361. le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3362. if (rebalanced) {
  3363. /*
  3364. * Rebalancing should have provided us with
  3365. * space in an appropriate leaf.
  3366. *
  3367. * XXX: Is this an abnormal condition then?
  3368. * Should we print a message here?
  3369. */
  3370. ret = -ENOSPC;
  3371. goto out;
  3372. }
  3373. ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
  3374. &lookup->dl_hinfo, leaf_cpos,
  3375. blkno);
  3376. if (ret) {
  3377. if (ret != -ENOSPC)
  3378. mlog_errno(ret);
  3379. goto out;
  3380. }
  3381. /*
  3382. * Restart the lookup. The rebalance might have
  3383. * changed which block our item fits into. Mark our
  3384. * progress, so we only execute this once.
  3385. */
  3386. brelse(dx_leaf_bh);
  3387. dx_leaf_bh = NULL;
  3388. rebalanced = 1;
  3389. goto restart_search;
  3390. }
  3391. lookup->dl_dx_leaf_bh = dx_leaf_bh;
  3392. dx_leaf_bh = NULL;
  3393. out:
  3394. brelse(dx_leaf_bh);
  3395. return ret;
  3396. }
  3397. static int ocfs2_search_dx_free_list(struct inode *dir,
  3398. struct buffer_head *dx_root_bh,
  3399. int namelen,
  3400. struct ocfs2_dir_lookup_result *lookup)
  3401. {
  3402. int ret = -ENOSPC;
  3403. struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
  3404. struct ocfs2_dir_block_trailer *db;
  3405. u64 next_block;
  3406. int rec_len = OCFS2_DIR_REC_LEN(namelen);
  3407. struct ocfs2_dx_root_block *dx_root;
  3408. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3409. next_block = le64_to_cpu(dx_root->dr_free_blk);
  3410. while (next_block) {
  3411. brelse(prev_leaf_bh);
  3412. prev_leaf_bh = leaf_bh;
  3413. leaf_bh = NULL;
  3414. ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
  3415. if (ret) {
  3416. mlog_errno(ret);
  3417. goto out;
  3418. }
  3419. db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  3420. if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
  3421. lookup->dl_leaf_bh = leaf_bh;
  3422. lookup->dl_prev_leaf_bh = prev_leaf_bh;
  3423. leaf_bh = NULL;
  3424. prev_leaf_bh = NULL;
  3425. break;
  3426. }
  3427. next_block = le64_to_cpu(db->db_free_next);
  3428. }
  3429. if (!next_block)
  3430. ret = -ENOSPC;
  3431. out:
  3432. brelse(leaf_bh);
  3433. brelse(prev_leaf_bh);
  3434. return ret;
  3435. }
  3436. static int ocfs2_expand_inline_dx_root(struct inode *dir,
  3437. struct buffer_head *dx_root_bh)
  3438. {
  3439. int ret, num_dx_leaves, i, j, did_quota = 0;
  3440. struct buffer_head **dx_leaves = NULL;
  3441. struct ocfs2_extent_tree et;
  3442. u64 insert_blkno;
  3443. struct ocfs2_alloc_context *data_ac = NULL;
  3444. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3445. handle_t *handle = NULL;
  3446. struct ocfs2_dx_root_block *dx_root;
  3447. struct ocfs2_dx_entry_list *entry_list;
  3448. struct ocfs2_dx_entry *dx_entry;
  3449. struct ocfs2_dx_leaf *target_leaf;
  3450. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  3451. if (ret) {
  3452. mlog_errno(ret);
  3453. goto out;
  3454. }
  3455. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3456. if (!dx_leaves) {
  3457. ret = -ENOMEM;
  3458. mlog_errno(ret);
  3459. goto out;
  3460. }
  3461. handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
  3462. if (IS_ERR(handle)) {
  3463. ret = PTR_ERR(handle);
  3464. mlog_errno(ret);
  3465. goto out;
  3466. }
  3467. ret = dquot_alloc_space_nodirty(dir,
  3468. ocfs2_clusters_to_bytes(osb->sb, 1));
  3469. if (ret)
  3470. goto out_commit;
  3471. did_quota = 1;
  3472. /*
  3473. * We do this up front, before the allocation, so that a
  3474. * failure to add the dx_root_bh to the journal won't result
  3475. * us losing clusters.
  3476. */
  3477. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  3478. OCFS2_JOURNAL_ACCESS_WRITE);
  3479. if (ret) {
  3480. mlog_errno(ret);
  3481. goto out_commit;
  3482. }
  3483. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
  3484. num_dx_leaves, &insert_blkno);
  3485. if (ret) {
  3486. mlog_errno(ret);
  3487. goto out_commit;
  3488. }
  3489. /*
  3490. * Transfer the entries from our dx_root into the appropriate
  3491. * block
  3492. */
  3493. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3494. entry_list = &dx_root->dr_entries;
  3495. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  3496. dx_entry = &entry_list->de_entries[i];
  3497. j = __ocfs2_dx_dir_hash_idx(osb,
  3498. le32_to_cpu(dx_entry->dx_minor_hash));
  3499. target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
  3500. ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
  3501. /* Each leaf has been passed to the journal already
  3502. * via __ocfs2_dx_dir_new_cluster() */
  3503. }
  3504. dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
  3505. memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
  3506. offsetof(struct ocfs2_dx_root_block, dr_list));
  3507. dx_root->dr_list.l_count =
  3508. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  3509. /* This should never fail considering we start with an empty
  3510. * dx_root. */
  3511. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3512. ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
  3513. if (ret)
  3514. mlog_errno(ret);
  3515. did_quota = 0;
  3516. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3517. ocfs2_journal_dirty(handle, dx_root_bh);
  3518. out_commit:
  3519. if (ret < 0 && did_quota)
  3520. dquot_free_space_nodirty(dir,
  3521. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3522. ocfs2_commit_trans(osb, handle);
  3523. out:
  3524. if (data_ac)
  3525. ocfs2_free_alloc_context(data_ac);
  3526. if (dx_leaves) {
  3527. for (i = 0; i < num_dx_leaves; i++)
  3528. brelse(dx_leaves[i]);
  3529. kfree(dx_leaves);
  3530. }
  3531. return ret;
  3532. }
  3533. static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
  3534. {
  3535. struct ocfs2_dx_root_block *dx_root;
  3536. struct ocfs2_dx_entry_list *entry_list;
  3537. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3538. entry_list = &dx_root->dr_entries;
  3539. if (le16_to_cpu(entry_list->de_num_used) >=
  3540. le16_to_cpu(entry_list->de_count))
  3541. return -ENOSPC;
  3542. return 0;
  3543. }
  3544. static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
  3545. struct buffer_head *di_bh,
  3546. const char *name,
  3547. int namelen,
  3548. struct ocfs2_dir_lookup_result *lookup)
  3549. {
  3550. int ret, free_dx_root = 1;
  3551. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3552. struct buffer_head *dx_root_bh = NULL;
  3553. struct buffer_head *leaf_bh = NULL;
  3554. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3555. struct ocfs2_dx_root_block *dx_root;
  3556. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3557. if (ret) {
  3558. mlog_errno(ret);
  3559. goto out;
  3560. }
  3561. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3562. if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
  3563. ret = -ENOSPC;
  3564. mlog_errno(ret);
  3565. goto out;
  3566. }
  3567. if (ocfs2_dx_root_inline(dx_root)) {
  3568. ret = ocfs2_inline_dx_has_space(dx_root_bh);
  3569. if (ret == 0)
  3570. goto search_el;
  3571. /*
  3572. * We ran out of room in the root block. Expand it to
  3573. * an extent, then allow ocfs2_find_dir_space_dx to do
  3574. * the rest.
  3575. */
  3576. ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
  3577. if (ret) {
  3578. mlog_errno(ret);
  3579. goto out;
  3580. }
  3581. }
  3582. /*
  3583. * Insert preparation for an indexed directory is split into two
  3584. * steps. The call to find_dir_space_dx reserves room in the index for
  3585. * an additional item. If we run out of space there, it's a real error
  3586. * we can't continue on.
  3587. */
  3588. ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
  3589. namelen, lookup);
  3590. if (ret) {
  3591. mlog_errno(ret);
  3592. goto out;
  3593. }
  3594. search_el:
  3595. /*
  3596. * Next, we need to find space in the unindexed tree. This call
  3597. * searches using the free space linked list. If the unindexed tree
  3598. * lacks sufficient space, we'll expand it below. The expansion code
  3599. * is smart enough to add any new blocks to the free space list.
  3600. */
  3601. ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
  3602. if (ret && ret != -ENOSPC) {
  3603. mlog_errno(ret);
  3604. goto out;
  3605. }
  3606. /* Do this up here - ocfs2_extend_dir might need the dx_root */
  3607. lookup->dl_dx_root_bh = dx_root_bh;
  3608. free_dx_root = 0;
  3609. if (ret == -ENOSPC) {
  3610. ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
  3611. if (ret) {
  3612. mlog_errno(ret);
  3613. goto out;
  3614. }
  3615. /*
  3616. * We make the assumption here that new leaf blocks are added
  3617. * to the front of our free list.
  3618. */
  3619. lookup->dl_prev_leaf_bh = NULL;
  3620. lookup->dl_leaf_bh = leaf_bh;
  3621. }
  3622. out:
  3623. if (free_dx_root)
  3624. brelse(dx_root_bh);
  3625. return ret;
  3626. }
  3627. /*
  3628. * Get a directory ready for insert. Any directory allocation required
  3629. * happens here. Success returns zero, and enough context in the dir
  3630. * lookup result that ocfs2_add_entry() will be able complete the task
  3631. * with minimal performance impact.
  3632. */
  3633. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  3634. struct inode *dir,
  3635. struct buffer_head *parent_fe_bh,
  3636. const char *name,
  3637. int namelen,
  3638. struct ocfs2_dir_lookup_result *lookup)
  3639. {
  3640. int ret;
  3641. unsigned int blocks_wanted = 1;
  3642. struct buffer_head *bh = NULL;
  3643. trace_ocfs2_prepare_dir_for_insert(
  3644. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
  3645. if (!namelen) {
  3646. ret = -EINVAL;
  3647. mlog_errno(ret);
  3648. goto out;
  3649. }
  3650. /*
  3651. * Do this up front to reduce confusion.
  3652. *
  3653. * The directory might start inline, then be turned into an
  3654. * indexed one, in which case we'd need to hash deep inside
  3655. * ocfs2_find_dir_space_id(). Since
  3656. * ocfs2_prepare_dx_dir_for_insert() also needs this hash
  3657. * done, there seems no point in spreading out the calls. We
  3658. * can optimize away the case where the file system doesn't
  3659. * support indexing.
  3660. */
  3661. if (ocfs2_supports_indexed_dirs(osb))
  3662. ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
  3663. if (ocfs2_dir_indexed(dir)) {
  3664. ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
  3665. name, namelen, lookup);
  3666. if (ret)
  3667. mlog_errno(ret);
  3668. goto out;
  3669. }
  3670. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  3671. ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  3672. namelen, &bh, &blocks_wanted);
  3673. } else
  3674. ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  3675. if (ret && ret != -ENOSPC) {
  3676. mlog_errno(ret);
  3677. goto out;
  3678. }
  3679. if (ret == -ENOSPC) {
  3680. /*
  3681. * We have to expand the directory to add this name.
  3682. */
  3683. BUG_ON(bh);
  3684. ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
  3685. lookup, &bh);
  3686. if (ret) {
  3687. if (ret != -ENOSPC)
  3688. mlog_errno(ret);
  3689. goto out;
  3690. }
  3691. BUG_ON(!bh);
  3692. }
  3693. lookup->dl_leaf_bh = bh;
  3694. bh = NULL;
  3695. out:
  3696. brelse(bh);
  3697. return ret;
  3698. }
  3699. static int ocfs2_dx_dir_remove_index(struct inode *dir,
  3700. struct buffer_head *di_bh,
  3701. struct buffer_head *dx_root_bh)
  3702. {
  3703. int ret;
  3704. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3705. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3706. struct ocfs2_dx_root_block *dx_root;
  3707. struct inode *dx_alloc_inode = NULL;
  3708. struct buffer_head *dx_alloc_bh = NULL;
  3709. handle_t *handle;
  3710. u64 blk;
  3711. u16 bit;
  3712. u64 bg_blkno;
  3713. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3714. dx_alloc_inode = ocfs2_get_system_file_inode(osb,
  3715. EXTENT_ALLOC_SYSTEM_INODE,
  3716. le16_to_cpu(dx_root->dr_suballoc_slot));
  3717. if (!dx_alloc_inode) {
  3718. ret = -ENOMEM;
  3719. mlog_errno(ret);
  3720. goto out;
  3721. }
  3722. inode_lock(dx_alloc_inode);
  3723. ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
  3724. if (ret) {
  3725. mlog_errno(ret);
  3726. goto out_mutex;
  3727. }
  3728. handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
  3729. if (IS_ERR(handle)) {
  3730. ret = PTR_ERR(handle);
  3731. mlog_errno(ret);
  3732. goto out_unlock;
  3733. }
  3734. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  3735. OCFS2_JOURNAL_ACCESS_WRITE);
  3736. if (ret) {
  3737. mlog_errno(ret);
  3738. goto out_commit;
  3739. }
  3740. spin_lock(&OCFS2_I(dir)->ip_lock);
  3741. OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
  3742. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  3743. spin_unlock(&OCFS2_I(dir)->ip_lock);
  3744. di->i_dx_root = cpu_to_le64(0ULL);
  3745. ocfs2_update_inode_fsync_trans(handle, dir, 1);
  3746. ocfs2_journal_dirty(handle, di_bh);
  3747. blk = le64_to_cpu(dx_root->dr_blkno);
  3748. bit = le16_to_cpu(dx_root->dr_suballoc_bit);
  3749. if (dx_root->dr_suballoc_loc)
  3750. bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
  3751. else
  3752. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  3753. ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
  3754. bit, bg_blkno, 1);
  3755. if (ret)
  3756. mlog_errno(ret);
  3757. out_commit:
  3758. ocfs2_commit_trans(osb, handle);
  3759. out_unlock:
  3760. ocfs2_inode_unlock(dx_alloc_inode, 1);
  3761. out_mutex:
  3762. inode_unlock(dx_alloc_inode);
  3763. brelse(dx_alloc_bh);
  3764. out:
  3765. iput(dx_alloc_inode);
  3766. return ret;
  3767. }
  3768. int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
  3769. {
  3770. int ret;
  3771. unsigned int clen;
  3772. u32 major_hash = UINT_MAX, p_cpos, cpos;
  3773. u64 blkno;
  3774. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3775. struct buffer_head *dx_root_bh = NULL;
  3776. struct ocfs2_dx_root_block *dx_root;
  3777. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3778. struct ocfs2_cached_dealloc_ctxt dealloc;
  3779. struct ocfs2_extent_tree et;
  3780. ocfs2_init_dealloc_ctxt(&dealloc);
  3781. if (!ocfs2_dir_indexed(dir))
  3782. return 0;
  3783. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3784. if (ret) {
  3785. mlog_errno(ret);
  3786. goto out;
  3787. }
  3788. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3789. if (ocfs2_dx_root_inline(dx_root))
  3790. goto remove_index;
  3791. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3792. /* XXX: What if dr_clusters is too large? */
  3793. while (le32_to_cpu(dx_root->dr_clusters)) {
  3794. ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
  3795. major_hash, &cpos, &blkno, &clen);
  3796. if (ret) {
  3797. mlog_errno(ret);
  3798. goto out;
  3799. }
  3800. p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
  3801. ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
  3802. &dealloc, 0, false);
  3803. if (ret) {
  3804. mlog_errno(ret);
  3805. goto out;
  3806. }
  3807. if (cpos == 0)
  3808. break;
  3809. major_hash = cpos - 1;
  3810. }
  3811. remove_index:
  3812. ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
  3813. if (ret) {
  3814. mlog_errno(ret);
  3815. goto out;
  3816. }
  3817. ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
  3818. out:
  3819. ocfs2_schedule_truncate_log_flush(osb, 1);
  3820. ocfs2_run_deallocs(osb, &dealloc);
  3821. brelse(dx_root_bh);
  3822. return ret;
  3823. }