dir.c 116 KB

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