dir.c 115 KB

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