namei.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/time.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/quotaops.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/bio.h>
  35. #include "ext4.h"
  36. #include "ext4_jbd2.h"
  37. #include "xattr.h"
  38. #include "acl.h"
  39. #include <trace/events/ext4.h>
  40. /*
  41. * define how far ahead to read directories while searching them.
  42. */
  43. #define NAMEI_RA_CHUNKS 2
  44. #define NAMEI_RA_BLOCKS 4
  45. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  46. static struct buffer_head *ext4_append(handle_t *handle,
  47. struct inode *inode,
  48. ext4_lblk_t *block)
  49. {
  50. struct buffer_head *bh;
  51. int err;
  52. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  53. ((inode->i_size >> 10) >=
  54. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  55. return ERR_PTR(-ENOSPC);
  56. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  57. bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
  58. if (IS_ERR(bh))
  59. return bh;
  60. inode->i_size += inode->i_sb->s_blocksize;
  61. EXT4_I(inode)->i_disksize = inode->i_size;
  62. BUFFER_TRACE(bh, "get_write_access");
  63. err = ext4_journal_get_write_access(handle, bh);
  64. if (err) {
  65. brelse(bh);
  66. ext4_std_error(inode->i_sb, err);
  67. return ERR_PTR(err);
  68. }
  69. return bh;
  70. }
  71. static int ext4_dx_csum_verify(struct inode *inode,
  72. struct ext4_dir_entry *dirent);
  73. typedef enum {
  74. EITHER, INDEX, DIRENT
  75. } dirblock_type_t;
  76. #define ext4_read_dirblock(inode, block, type) \
  77. __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
  78. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  79. ext4_lblk_t block,
  80. dirblock_type_t type,
  81. const char *func,
  82. unsigned int line)
  83. {
  84. struct buffer_head *bh;
  85. struct ext4_dir_entry *dirent;
  86. int is_dx_block = 0;
  87. bh = ext4_bread(NULL, inode, block, 0);
  88. if (IS_ERR(bh)) {
  89. __ext4_warning(inode->i_sb, func, line,
  90. "inode #%lu: lblock %lu: comm %s: "
  91. "error %ld reading directory block",
  92. inode->i_ino, (unsigned long)block,
  93. current->comm, PTR_ERR(bh));
  94. return bh;
  95. }
  96. if (!bh) {
  97. ext4_error_inode(inode, func, line, block,
  98. "Directory hole found");
  99. return ERR_PTR(-EFSCORRUPTED);
  100. }
  101. dirent = (struct ext4_dir_entry *) bh->b_data;
  102. /* Determine whether or not we have an index block */
  103. if (is_dx(inode)) {
  104. if (block == 0)
  105. is_dx_block = 1;
  106. else if (ext4_rec_len_from_disk(dirent->rec_len,
  107. inode->i_sb->s_blocksize) ==
  108. inode->i_sb->s_blocksize)
  109. is_dx_block = 1;
  110. }
  111. if (!is_dx_block && type == INDEX) {
  112. ext4_error_inode(inode, func, line, block,
  113. "directory leaf block found instead of index block");
  114. return ERR_PTR(-EFSCORRUPTED);
  115. }
  116. if (!ext4_has_metadata_csum(inode->i_sb) ||
  117. buffer_verified(bh))
  118. return bh;
  119. /*
  120. * An empty leaf block can get mistaken for a index block; for
  121. * this reason, we can only check the index checksum when the
  122. * caller is sure it should be an index block.
  123. */
  124. if (is_dx_block && type == INDEX) {
  125. if (ext4_dx_csum_verify(inode, dirent))
  126. set_buffer_verified(bh);
  127. else {
  128. ext4_error_inode(inode, func, line, block,
  129. "Directory index failed checksum");
  130. brelse(bh);
  131. return ERR_PTR(-EFSBADCRC);
  132. }
  133. }
  134. if (!is_dx_block) {
  135. if (ext4_dirent_csum_verify(inode, dirent))
  136. set_buffer_verified(bh);
  137. else {
  138. ext4_error_inode(inode, func, line, block,
  139. "Directory block failed checksum");
  140. brelse(bh);
  141. return ERR_PTR(-EFSBADCRC);
  142. }
  143. }
  144. return bh;
  145. }
  146. #ifndef assert
  147. #define assert(test) J_ASSERT(test)
  148. #endif
  149. #ifdef DX_DEBUG
  150. #define dxtrace(command) command
  151. #else
  152. #define dxtrace(command)
  153. #endif
  154. struct fake_dirent
  155. {
  156. __le32 inode;
  157. __le16 rec_len;
  158. u8 name_len;
  159. u8 file_type;
  160. };
  161. struct dx_countlimit
  162. {
  163. __le16 limit;
  164. __le16 count;
  165. };
  166. struct dx_entry
  167. {
  168. __le32 hash;
  169. __le32 block;
  170. };
  171. /*
  172. * dx_root_info is laid out so that if it should somehow get overlaid by a
  173. * dirent the two low bits of the hash version will be zero. Therefore, the
  174. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  175. */
  176. struct dx_root
  177. {
  178. struct fake_dirent dot;
  179. char dot_name[4];
  180. struct fake_dirent dotdot;
  181. char dotdot_name[4];
  182. struct dx_root_info
  183. {
  184. __le32 reserved_zero;
  185. u8 hash_version;
  186. u8 info_length; /* 8 */
  187. u8 indirect_levels;
  188. u8 unused_flags;
  189. }
  190. info;
  191. struct dx_entry entries[0];
  192. };
  193. struct dx_node
  194. {
  195. struct fake_dirent fake;
  196. struct dx_entry entries[0];
  197. };
  198. struct dx_frame
  199. {
  200. struct buffer_head *bh;
  201. struct dx_entry *entries;
  202. struct dx_entry *at;
  203. };
  204. struct dx_map_entry
  205. {
  206. u32 hash;
  207. u16 offs;
  208. u16 size;
  209. };
  210. /*
  211. * This goes at the end of each htree block.
  212. */
  213. struct dx_tail {
  214. u32 dt_reserved;
  215. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  216. };
  217. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  218. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  219. static inline unsigned dx_get_hash(struct dx_entry *entry);
  220. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  221. static unsigned dx_get_count(struct dx_entry *entries);
  222. static unsigned dx_get_limit(struct dx_entry *entries);
  223. static void dx_set_count(struct dx_entry *entries, unsigned value);
  224. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  225. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  226. static unsigned dx_node_limit(struct inode *dir);
  227. static struct dx_frame *dx_probe(struct ext4_filename *fname,
  228. struct inode *dir,
  229. struct dx_hash_info *hinfo,
  230. struct dx_frame *frame);
  231. static void dx_release(struct dx_frame *frames);
  232. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  233. unsigned blocksize, struct dx_hash_info *hinfo,
  234. struct dx_map_entry map[]);
  235. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  236. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  237. struct dx_map_entry *offsets, int count, unsigned blocksize);
  238. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
  239. static void dx_insert_block(struct dx_frame *frame,
  240. u32 hash, ext4_lblk_t block);
  241. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  242. struct dx_frame *frame,
  243. struct dx_frame *frames,
  244. __u32 *start_hash);
  245. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  246. struct ext4_filename *fname,
  247. struct ext4_dir_entry_2 **res_dir);
  248. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  249. struct inode *dir, struct inode *inode);
  250. /* checksumming functions */
  251. void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
  252. unsigned int blocksize)
  253. {
  254. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  255. t->det_rec_len = ext4_rec_len_to_disk(
  256. sizeof(struct ext4_dir_entry_tail), blocksize);
  257. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  258. }
  259. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  260. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  261. struct ext4_dir_entry *de)
  262. {
  263. struct ext4_dir_entry_tail *t;
  264. #ifdef PARANOID
  265. struct ext4_dir_entry *d, *top;
  266. d = de;
  267. top = (struct ext4_dir_entry *)(((void *)de) +
  268. (EXT4_BLOCK_SIZE(inode->i_sb) -
  269. sizeof(struct ext4_dir_entry_tail)));
  270. while (d < top && d->rec_len)
  271. d = (struct ext4_dir_entry *)(((void *)d) +
  272. le16_to_cpu(d->rec_len));
  273. if (d != top)
  274. return NULL;
  275. t = (struct ext4_dir_entry_tail *)d;
  276. #else
  277. t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
  278. #endif
  279. if (t->det_reserved_zero1 ||
  280. le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
  281. t->det_reserved_zero2 ||
  282. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  283. return NULL;
  284. return t;
  285. }
  286. static __le32 ext4_dirent_csum(struct inode *inode,
  287. struct ext4_dir_entry *dirent, int size)
  288. {
  289. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  290. struct ext4_inode_info *ei = EXT4_I(inode);
  291. __u32 csum;
  292. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  293. return cpu_to_le32(csum);
  294. }
  295. #define warn_no_space_for_csum(inode) \
  296. __warn_no_space_for_csum((inode), __func__, __LINE__)
  297. static void __warn_no_space_for_csum(struct inode *inode, const char *func,
  298. unsigned int line)
  299. {
  300. __ext4_warning_inode(inode, func, line,
  301. "No space for directory leaf checksum. Please run e2fsck -D.");
  302. }
  303. int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
  304. {
  305. struct ext4_dir_entry_tail *t;
  306. if (!ext4_has_metadata_csum(inode->i_sb))
  307. return 1;
  308. t = get_dirent_tail(inode, dirent);
  309. if (!t) {
  310. warn_no_space_for_csum(inode);
  311. return 0;
  312. }
  313. if (t->det_checksum != ext4_dirent_csum(inode, dirent,
  314. (void *)t - (void *)dirent))
  315. return 0;
  316. return 1;
  317. }
  318. static void ext4_dirent_csum_set(struct inode *inode,
  319. struct ext4_dir_entry *dirent)
  320. {
  321. struct ext4_dir_entry_tail *t;
  322. if (!ext4_has_metadata_csum(inode->i_sb))
  323. return;
  324. t = get_dirent_tail(inode, dirent);
  325. if (!t) {
  326. warn_no_space_for_csum(inode);
  327. return;
  328. }
  329. t->det_checksum = ext4_dirent_csum(inode, dirent,
  330. (void *)t - (void *)dirent);
  331. }
  332. int ext4_handle_dirty_dirent_node(handle_t *handle,
  333. struct inode *inode,
  334. struct buffer_head *bh)
  335. {
  336. ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  337. return ext4_handle_dirty_metadata(handle, inode, bh);
  338. }
  339. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  340. struct ext4_dir_entry *dirent,
  341. int *offset)
  342. {
  343. struct ext4_dir_entry *dp;
  344. struct dx_root_info *root;
  345. int count_offset;
  346. if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
  347. count_offset = 8;
  348. else if (le16_to_cpu(dirent->rec_len) == 12) {
  349. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  350. if (le16_to_cpu(dp->rec_len) !=
  351. EXT4_BLOCK_SIZE(inode->i_sb) - 12)
  352. return NULL;
  353. root = (struct dx_root_info *)(((void *)dp + 12));
  354. if (root->reserved_zero ||
  355. root->info_length != sizeof(struct dx_root_info))
  356. return NULL;
  357. count_offset = 32;
  358. } else
  359. return NULL;
  360. if (offset)
  361. *offset = count_offset;
  362. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  363. }
  364. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  365. int count_offset, int count, struct dx_tail *t)
  366. {
  367. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  368. struct ext4_inode_info *ei = EXT4_I(inode);
  369. __u32 csum;
  370. int size;
  371. __u32 dummy_csum = 0;
  372. int offset = offsetof(struct dx_tail, dt_checksum);
  373. size = count_offset + (count * sizeof(struct dx_entry));
  374. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
  375. csum = ext4_chksum(sbi, csum, (__u8 *)t, offset);
  376. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  377. return cpu_to_le32(csum);
  378. }
  379. static int ext4_dx_csum_verify(struct inode *inode,
  380. struct ext4_dir_entry *dirent)
  381. {
  382. struct dx_countlimit *c;
  383. struct dx_tail *t;
  384. int count_offset, limit, count;
  385. if (!ext4_has_metadata_csum(inode->i_sb))
  386. return 1;
  387. c = get_dx_countlimit(inode, dirent, &count_offset);
  388. if (!c) {
  389. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  390. return 0;
  391. }
  392. limit = le16_to_cpu(c->limit);
  393. count = le16_to_cpu(c->count);
  394. if (count_offset + (limit * sizeof(struct dx_entry)) >
  395. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  396. warn_no_space_for_csum(inode);
  397. return 0;
  398. }
  399. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  400. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  401. count, t))
  402. return 0;
  403. return 1;
  404. }
  405. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  406. {
  407. struct dx_countlimit *c;
  408. struct dx_tail *t;
  409. int count_offset, limit, count;
  410. if (!ext4_has_metadata_csum(inode->i_sb))
  411. return;
  412. c = get_dx_countlimit(inode, dirent, &count_offset);
  413. if (!c) {
  414. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  415. return;
  416. }
  417. limit = le16_to_cpu(c->limit);
  418. count = le16_to_cpu(c->count);
  419. if (count_offset + (limit * sizeof(struct dx_entry)) >
  420. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  421. warn_no_space_for_csum(inode);
  422. return;
  423. }
  424. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  425. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  426. }
  427. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  428. struct inode *inode,
  429. struct buffer_head *bh)
  430. {
  431. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  432. return ext4_handle_dirty_metadata(handle, inode, bh);
  433. }
  434. /*
  435. * p is at least 6 bytes before the end of page
  436. */
  437. static inline struct ext4_dir_entry_2 *
  438. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  439. {
  440. return (struct ext4_dir_entry_2 *)((char *)p +
  441. ext4_rec_len_from_disk(p->rec_len, blocksize));
  442. }
  443. /*
  444. * Future: use high four bits of block for coalesce-on-delete flags
  445. * Mask them off for now.
  446. */
  447. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  448. {
  449. return le32_to_cpu(entry->block) & 0x0fffffff;
  450. }
  451. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  452. {
  453. entry->block = cpu_to_le32(value);
  454. }
  455. static inline unsigned dx_get_hash(struct dx_entry *entry)
  456. {
  457. return le32_to_cpu(entry->hash);
  458. }
  459. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  460. {
  461. entry->hash = cpu_to_le32(value);
  462. }
  463. static inline unsigned dx_get_count(struct dx_entry *entries)
  464. {
  465. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  466. }
  467. static inline unsigned dx_get_limit(struct dx_entry *entries)
  468. {
  469. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  470. }
  471. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  472. {
  473. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  474. }
  475. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  476. {
  477. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  478. }
  479. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  480. {
  481. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  482. EXT4_DIR_REC_LEN(2) - infosize;
  483. if (ext4_has_metadata_csum(dir->i_sb))
  484. entry_space -= sizeof(struct dx_tail);
  485. return entry_space / sizeof(struct dx_entry);
  486. }
  487. static inline unsigned dx_node_limit(struct inode *dir)
  488. {
  489. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  490. if (ext4_has_metadata_csum(dir->i_sb))
  491. entry_space -= sizeof(struct dx_tail);
  492. return entry_space / sizeof(struct dx_entry);
  493. }
  494. /*
  495. * Debug
  496. */
  497. #ifdef DX_DEBUG
  498. static void dx_show_index(char * label, struct dx_entry *entries)
  499. {
  500. int i, n = dx_get_count (entries);
  501. printk(KERN_DEBUG "%s index", label);
  502. for (i = 0; i < n; i++) {
  503. printk(KERN_CONT " %x->%lu",
  504. i ? dx_get_hash(entries + i) : 0,
  505. (unsigned long)dx_get_block(entries + i));
  506. }
  507. printk(KERN_CONT "\n");
  508. }
  509. struct stats
  510. {
  511. unsigned names;
  512. unsigned space;
  513. unsigned bcount;
  514. };
  515. static struct stats dx_show_leaf(struct inode *dir,
  516. struct dx_hash_info *hinfo,
  517. struct ext4_dir_entry_2 *de,
  518. int size, int show_names)
  519. {
  520. unsigned names = 0, space = 0;
  521. char *base = (char *) de;
  522. struct dx_hash_info h = *hinfo;
  523. printk("names: ");
  524. while ((char *) de < base + size)
  525. {
  526. if (de->inode)
  527. {
  528. if (show_names)
  529. {
  530. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  531. int len;
  532. char *name;
  533. struct fscrypt_str fname_crypto_str =
  534. FSTR_INIT(NULL, 0);
  535. int res = 0;
  536. name = de->name;
  537. len = de->name_len;
  538. if (ext4_encrypted_inode(dir))
  539. res = fscrypt_get_encryption_info(dir);
  540. if (res) {
  541. printk(KERN_WARNING "Error setting up"
  542. " fname crypto: %d\n", res);
  543. }
  544. if (!fscrypt_has_encryption_key(dir)) {
  545. /* Directory is not encrypted */
  546. ext4fs_dirhash(de->name,
  547. de->name_len, &h);
  548. printk("%*.s:(U)%x.%u ", len,
  549. name, h.hash,
  550. (unsigned) ((char *) de
  551. - base));
  552. } else {
  553. struct fscrypt_str de_name =
  554. FSTR_INIT(name, len);
  555. /* Directory is encrypted */
  556. res = fscrypt_fname_alloc_buffer(
  557. dir, len,
  558. &fname_crypto_str);
  559. if (res)
  560. printk(KERN_WARNING "Error "
  561. "allocating crypto "
  562. "buffer--skipping "
  563. "crypto\n");
  564. res = fscrypt_fname_disk_to_usr(dir,
  565. 0, 0, &de_name,
  566. &fname_crypto_str);
  567. if (res) {
  568. printk(KERN_WARNING "Error "
  569. "converting filename "
  570. "from disk to usr"
  571. "\n");
  572. name = "??";
  573. len = 2;
  574. } else {
  575. name = fname_crypto_str.name;
  576. len = fname_crypto_str.len;
  577. }
  578. ext4fs_dirhash(de->name, de->name_len,
  579. &h);
  580. printk("%*.s:(E)%x.%u ", len, name,
  581. h.hash, (unsigned) ((char *) de
  582. - base));
  583. fscrypt_fname_free_buffer(
  584. &fname_crypto_str);
  585. }
  586. #else
  587. int len = de->name_len;
  588. char *name = de->name;
  589. ext4fs_dirhash(de->name, de->name_len, &h);
  590. printk("%*.s:%x.%u ", len, name, h.hash,
  591. (unsigned) ((char *) de - base));
  592. #endif
  593. }
  594. space += EXT4_DIR_REC_LEN(de->name_len);
  595. names++;
  596. }
  597. de = ext4_next_entry(de, size);
  598. }
  599. printk(KERN_CONT "(%i)\n", names);
  600. return (struct stats) { names, space, 1 };
  601. }
  602. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  603. struct dx_entry *entries, int levels)
  604. {
  605. unsigned blocksize = dir->i_sb->s_blocksize;
  606. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  607. unsigned bcount = 0;
  608. struct buffer_head *bh;
  609. printk("%i indexed blocks...\n", count);
  610. for (i = 0; i < count; i++, entries++)
  611. {
  612. ext4_lblk_t block = dx_get_block(entries);
  613. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  614. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  615. struct stats stats;
  616. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  617. bh = ext4_bread(NULL,dir, block, 0);
  618. if (!bh || IS_ERR(bh))
  619. continue;
  620. stats = levels?
  621. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  622. dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
  623. bh->b_data, blocksize, 0);
  624. names += stats.names;
  625. space += stats.space;
  626. bcount += stats.bcount;
  627. brelse(bh);
  628. }
  629. if (bcount)
  630. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  631. levels ? "" : " ", names, space/bcount,
  632. (space/bcount)*100/blocksize);
  633. return (struct stats) { names, space, bcount};
  634. }
  635. #endif /* DX_DEBUG */
  636. /*
  637. * Probe for a directory leaf block to search.
  638. *
  639. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  640. * error in the directory index, and the caller should fall back to
  641. * searching the directory normally. The callers of dx_probe **MUST**
  642. * check for this error code, and make sure it never gets reflected
  643. * back to userspace.
  644. */
  645. static struct dx_frame *
  646. dx_probe(struct ext4_filename *fname, struct inode *dir,
  647. struct dx_hash_info *hinfo, struct dx_frame *frame_in)
  648. {
  649. unsigned count, indirect;
  650. struct dx_entry *at, *entries, *p, *q, *m;
  651. struct dx_root *root;
  652. struct dx_frame *frame = frame_in;
  653. struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
  654. u32 hash;
  655. memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
  656. frame->bh = ext4_read_dirblock(dir, 0, INDEX);
  657. if (IS_ERR(frame->bh))
  658. return (struct dx_frame *) frame->bh;
  659. root = (struct dx_root *) frame->bh->b_data;
  660. if (root->info.hash_version != DX_HASH_TEA &&
  661. root->info.hash_version != DX_HASH_HALF_MD4 &&
  662. root->info.hash_version != DX_HASH_LEGACY) {
  663. ext4_warning_inode(dir, "Unrecognised inode hash code %u",
  664. root->info.hash_version);
  665. goto fail;
  666. }
  667. if (fname)
  668. hinfo = &fname->hinfo;
  669. hinfo->hash_version = root->info.hash_version;
  670. if (hinfo->hash_version <= DX_HASH_TEA)
  671. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  672. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  673. if (fname && fname_name(fname))
  674. ext4fs_dirhash(fname_name(fname), fname_len(fname), hinfo);
  675. hash = hinfo->hash;
  676. if (root->info.unused_flags & 1) {
  677. ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
  678. root->info.unused_flags);
  679. goto fail;
  680. }
  681. indirect = root->info.indirect_levels;
  682. if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
  683. ext4_warning(dir->i_sb,
  684. "Directory (ino: %lu) htree depth %#06x exceed"
  685. "supported value", dir->i_ino,
  686. ext4_dir_htree_level(dir->i_sb));
  687. if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
  688. ext4_warning(dir->i_sb, "Enable large directory "
  689. "feature to access it");
  690. }
  691. goto fail;
  692. }
  693. entries = (struct dx_entry *)(((char *)&root->info) +
  694. root->info.info_length);
  695. if (dx_get_limit(entries) != dx_root_limit(dir,
  696. root->info.info_length)) {
  697. ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
  698. dx_get_limit(entries),
  699. dx_root_limit(dir, root->info.info_length));
  700. goto fail;
  701. }
  702. dxtrace(printk("Look up %x", hash));
  703. while (1) {
  704. count = dx_get_count(entries);
  705. if (!count || count > dx_get_limit(entries)) {
  706. ext4_warning_inode(dir,
  707. "dx entry: count %u beyond limit %u",
  708. count, dx_get_limit(entries));
  709. goto fail;
  710. }
  711. p = entries + 1;
  712. q = entries + count - 1;
  713. while (p <= q) {
  714. m = p + (q - p) / 2;
  715. dxtrace(printk(KERN_CONT "."));
  716. if (dx_get_hash(m) > hash)
  717. q = m - 1;
  718. else
  719. p = m + 1;
  720. }
  721. if (0) { // linear search cross check
  722. unsigned n = count - 1;
  723. at = entries;
  724. while (n--)
  725. {
  726. dxtrace(printk(KERN_CONT ","));
  727. if (dx_get_hash(++at) > hash)
  728. {
  729. at--;
  730. break;
  731. }
  732. }
  733. assert (at == p - 1);
  734. }
  735. at = p - 1;
  736. dxtrace(printk(KERN_CONT " %x->%u\n",
  737. at == entries ? 0 : dx_get_hash(at),
  738. dx_get_block(at)));
  739. frame->entries = entries;
  740. frame->at = at;
  741. if (!indirect--)
  742. return frame;
  743. frame++;
  744. frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
  745. if (IS_ERR(frame->bh)) {
  746. ret_err = (struct dx_frame *) frame->bh;
  747. frame->bh = NULL;
  748. goto fail;
  749. }
  750. entries = ((struct dx_node *) frame->bh->b_data)->entries;
  751. if (dx_get_limit(entries) != dx_node_limit(dir)) {
  752. ext4_warning_inode(dir,
  753. "dx entry: limit %u != node limit %u",
  754. dx_get_limit(entries), dx_node_limit(dir));
  755. goto fail;
  756. }
  757. }
  758. fail:
  759. while (frame >= frame_in) {
  760. brelse(frame->bh);
  761. frame--;
  762. }
  763. if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
  764. ext4_warning_inode(dir,
  765. "Corrupt directory, running e2fsck is recommended");
  766. return ret_err;
  767. }
  768. static void dx_release(struct dx_frame *frames)
  769. {
  770. struct dx_root_info *info;
  771. int i;
  772. if (frames[0].bh == NULL)
  773. return;
  774. info = &((struct dx_root *)frames[0].bh->b_data)->info;
  775. for (i = 0; i <= info->indirect_levels; i++) {
  776. if (frames[i].bh == NULL)
  777. break;
  778. brelse(frames[i].bh);
  779. frames[i].bh = NULL;
  780. }
  781. }
  782. /*
  783. * This function increments the frame pointer to search the next leaf
  784. * block, and reads in the necessary intervening nodes if the search
  785. * should be necessary. Whether or not the search is necessary is
  786. * controlled by the hash parameter. If the hash value is even, then
  787. * the search is only continued if the next block starts with that
  788. * hash value. This is used if we are searching for a specific file.
  789. *
  790. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  791. *
  792. * This function returns 1 if the caller should continue to search,
  793. * or 0 if it should not. If there is an error reading one of the
  794. * index blocks, it will a negative error code.
  795. *
  796. * If start_hash is non-null, it will be filled in with the starting
  797. * hash of the next page.
  798. */
  799. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  800. struct dx_frame *frame,
  801. struct dx_frame *frames,
  802. __u32 *start_hash)
  803. {
  804. struct dx_frame *p;
  805. struct buffer_head *bh;
  806. int num_frames = 0;
  807. __u32 bhash;
  808. p = frame;
  809. /*
  810. * Find the next leaf page by incrementing the frame pointer.
  811. * If we run out of entries in the interior node, loop around and
  812. * increment pointer in the parent node. When we break out of
  813. * this loop, num_frames indicates the number of interior
  814. * nodes need to be read.
  815. */
  816. while (1) {
  817. if (++(p->at) < p->entries + dx_get_count(p->entries))
  818. break;
  819. if (p == frames)
  820. return 0;
  821. num_frames++;
  822. p--;
  823. }
  824. /*
  825. * If the hash is 1, then continue only if the next page has a
  826. * continuation hash of any value. This is used for readdir
  827. * handling. Otherwise, check to see if the hash matches the
  828. * desired contiuation hash. If it doesn't, return since
  829. * there's no point to read in the successive index pages.
  830. */
  831. bhash = dx_get_hash(p->at);
  832. if (start_hash)
  833. *start_hash = bhash;
  834. if ((hash & 1) == 0) {
  835. if ((bhash & ~1) != hash)
  836. return 0;
  837. }
  838. /*
  839. * If the hash is HASH_NB_ALWAYS, we always go to the next
  840. * block so no check is necessary
  841. */
  842. while (num_frames--) {
  843. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  844. if (IS_ERR(bh))
  845. return PTR_ERR(bh);
  846. p++;
  847. brelse(p->bh);
  848. p->bh = bh;
  849. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  850. }
  851. return 1;
  852. }
  853. /*
  854. * This function fills a red-black tree with information from a
  855. * directory block. It returns the number directory entries loaded
  856. * into the tree. If there is an error it is returned in err.
  857. */
  858. static int htree_dirblock_to_tree(struct file *dir_file,
  859. struct inode *dir, ext4_lblk_t block,
  860. struct dx_hash_info *hinfo,
  861. __u32 start_hash, __u32 start_minor_hash)
  862. {
  863. struct buffer_head *bh;
  864. struct ext4_dir_entry_2 *de, *top;
  865. int err = 0, count = 0;
  866. struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
  867. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  868. (unsigned long)block));
  869. bh = ext4_read_dirblock(dir, block, DIRENT);
  870. if (IS_ERR(bh))
  871. return PTR_ERR(bh);
  872. de = (struct ext4_dir_entry_2 *) bh->b_data;
  873. top = (struct ext4_dir_entry_2 *) ((char *) de +
  874. dir->i_sb->s_blocksize -
  875. EXT4_DIR_REC_LEN(0));
  876. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  877. /* Check if the directory is encrypted */
  878. if (ext4_encrypted_inode(dir)) {
  879. err = fscrypt_get_encryption_info(dir);
  880. if (err < 0) {
  881. brelse(bh);
  882. return err;
  883. }
  884. err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
  885. &fname_crypto_str);
  886. if (err < 0) {
  887. brelse(bh);
  888. return err;
  889. }
  890. }
  891. #endif
  892. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  893. if (ext4_check_dir_entry(dir, NULL, de, bh,
  894. bh->b_data, bh->b_size,
  895. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  896. + ((char *)de - bh->b_data))) {
  897. /* silently ignore the rest of the block */
  898. break;
  899. }
  900. ext4fs_dirhash(de->name, de->name_len, hinfo);
  901. if ((hinfo->hash < start_hash) ||
  902. ((hinfo->hash == start_hash) &&
  903. (hinfo->minor_hash < start_minor_hash)))
  904. continue;
  905. if (de->inode == 0)
  906. continue;
  907. if (!ext4_encrypted_inode(dir)) {
  908. tmp_str.name = de->name;
  909. tmp_str.len = de->name_len;
  910. err = ext4_htree_store_dirent(dir_file,
  911. hinfo->hash, hinfo->minor_hash, de,
  912. &tmp_str);
  913. } else {
  914. int save_len = fname_crypto_str.len;
  915. struct fscrypt_str de_name = FSTR_INIT(de->name,
  916. de->name_len);
  917. /* Directory is encrypted */
  918. err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
  919. hinfo->minor_hash, &de_name,
  920. &fname_crypto_str);
  921. if (err) {
  922. count = err;
  923. goto errout;
  924. }
  925. err = ext4_htree_store_dirent(dir_file,
  926. hinfo->hash, hinfo->minor_hash, de,
  927. &fname_crypto_str);
  928. fname_crypto_str.len = save_len;
  929. }
  930. if (err != 0) {
  931. count = err;
  932. goto errout;
  933. }
  934. count++;
  935. }
  936. errout:
  937. brelse(bh);
  938. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  939. fscrypt_fname_free_buffer(&fname_crypto_str);
  940. #endif
  941. return count;
  942. }
  943. /*
  944. * This function fills a red-black tree with information from a
  945. * directory. We start scanning the directory in hash order, starting
  946. * at start_hash and start_minor_hash.
  947. *
  948. * This function returns the number of entries inserted into the tree,
  949. * or a negative error code.
  950. */
  951. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  952. __u32 start_minor_hash, __u32 *next_hash)
  953. {
  954. struct dx_hash_info hinfo;
  955. struct ext4_dir_entry_2 *de;
  956. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  957. struct inode *dir;
  958. ext4_lblk_t block;
  959. int count = 0;
  960. int ret, err;
  961. __u32 hashval;
  962. struct fscrypt_str tmp_str;
  963. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  964. start_hash, start_minor_hash));
  965. dir = file_inode(dir_file);
  966. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  967. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  968. if (hinfo.hash_version <= DX_HASH_TEA)
  969. hinfo.hash_version +=
  970. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  971. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  972. if (ext4_has_inline_data(dir)) {
  973. int has_inline_data = 1;
  974. count = htree_inlinedir_to_tree(dir_file, dir, 0,
  975. &hinfo, start_hash,
  976. start_minor_hash,
  977. &has_inline_data);
  978. if (has_inline_data) {
  979. *next_hash = ~0;
  980. return count;
  981. }
  982. }
  983. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  984. start_hash, start_minor_hash);
  985. *next_hash = ~0;
  986. return count;
  987. }
  988. hinfo.hash = start_hash;
  989. hinfo.minor_hash = 0;
  990. frame = dx_probe(NULL, dir, &hinfo, frames);
  991. if (IS_ERR(frame))
  992. return PTR_ERR(frame);
  993. /* Add '.' and '..' from the htree header */
  994. if (!start_hash && !start_minor_hash) {
  995. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  996. tmp_str.name = de->name;
  997. tmp_str.len = de->name_len;
  998. err = ext4_htree_store_dirent(dir_file, 0, 0,
  999. de, &tmp_str);
  1000. if (err != 0)
  1001. goto errout;
  1002. count++;
  1003. }
  1004. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  1005. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  1006. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  1007. tmp_str.name = de->name;
  1008. tmp_str.len = de->name_len;
  1009. err = ext4_htree_store_dirent(dir_file, 2, 0,
  1010. de, &tmp_str);
  1011. if (err != 0)
  1012. goto errout;
  1013. count++;
  1014. }
  1015. while (1) {
  1016. if (fatal_signal_pending(current)) {
  1017. err = -ERESTARTSYS;
  1018. goto errout;
  1019. }
  1020. cond_resched();
  1021. block = dx_get_block(frame->at);
  1022. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  1023. start_hash, start_minor_hash);
  1024. if (ret < 0) {
  1025. err = ret;
  1026. goto errout;
  1027. }
  1028. count += ret;
  1029. hashval = ~0;
  1030. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  1031. frame, frames, &hashval);
  1032. *next_hash = hashval;
  1033. if (ret < 0) {
  1034. err = ret;
  1035. goto errout;
  1036. }
  1037. /*
  1038. * Stop if: (a) there are no more entries, or
  1039. * (b) we have inserted at least one entry and the
  1040. * next hash value is not a continuation
  1041. */
  1042. if ((ret == 0) ||
  1043. (count && ((hashval & 1) == 0)))
  1044. break;
  1045. }
  1046. dx_release(frames);
  1047. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  1048. "next hash: %x\n", count, *next_hash));
  1049. return count;
  1050. errout:
  1051. dx_release(frames);
  1052. return (err);
  1053. }
  1054. static inline int search_dirblock(struct buffer_head *bh,
  1055. struct inode *dir,
  1056. struct ext4_filename *fname,
  1057. unsigned int offset,
  1058. struct ext4_dir_entry_2 **res_dir)
  1059. {
  1060. return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  1061. fname, offset, res_dir);
  1062. }
  1063. /*
  1064. * Directory block splitting, compacting
  1065. */
  1066. /*
  1067. * Create map of hash values, offsets, and sizes, stored at end of block.
  1068. * Returns number of entries mapped.
  1069. */
  1070. static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
  1071. unsigned blocksize, struct dx_hash_info *hinfo,
  1072. struct dx_map_entry *map_tail)
  1073. {
  1074. int count = 0;
  1075. char *base = (char *) de;
  1076. struct dx_hash_info h = *hinfo;
  1077. while ((char *) de < base + blocksize) {
  1078. if (de->name_len && de->inode) {
  1079. ext4fs_dirhash(de->name, de->name_len, &h);
  1080. map_tail--;
  1081. map_tail->hash = h.hash;
  1082. map_tail->offs = ((char *) de - base)>>2;
  1083. map_tail->size = le16_to_cpu(de->rec_len);
  1084. count++;
  1085. cond_resched();
  1086. }
  1087. /* XXX: do we need to check rec_len == 0 case? -Chris */
  1088. de = ext4_next_entry(de, blocksize);
  1089. }
  1090. return count;
  1091. }
  1092. /* Sort map by hash value */
  1093. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  1094. {
  1095. struct dx_map_entry *p, *q, *top = map + count - 1;
  1096. int more;
  1097. /* Combsort until bubble sort doesn't suck */
  1098. while (count > 2) {
  1099. count = count*10/13;
  1100. if (count - 9 < 2) /* 9, 10 -> 11 */
  1101. count = 11;
  1102. for (p = top, q = p - count; q >= map; p--, q--)
  1103. if (p->hash < q->hash)
  1104. swap(*p, *q);
  1105. }
  1106. /* Garden variety bubble sort */
  1107. do {
  1108. more = 0;
  1109. q = top;
  1110. while (q-- > map) {
  1111. if (q[1].hash >= q[0].hash)
  1112. continue;
  1113. swap(*(q+1), *q);
  1114. more = 1;
  1115. }
  1116. } while(more);
  1117. }
  1118. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1119. {
  1120. struct dx_entry *entries = frame->entries;
  1121. struct dx_entry *old = frame->at, *new = old + 1;
  1122. int count = dx_get_count(entries);
  1123. assert(count < dx_get_limit(entries));
  1124. assert(old < entries + count);
  1125. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1126. dx_set_hash(new, hash);
  1127. dx_set_block(new, block);
  1128. dx_set_count(entries, count + 1);
  1129. }
  1130. /*
  1131. * Test whether a directory entry matches the filename being searched for.
  1132. *
  1133. * Return: %true if the directory entry matches, otherwise %false.
  1134. */
  1135. static inline bool ext4_match(const struct ext4_filename *fname,
  1136. const struct ext4_dir_entry_2 *de)
  1137. {
  1138. struct fscrypt_name f;
  1139. if (!de->inode)
  1140. return false;
  1141. f.usr_fname = fname->usr_fname;
  1142. f.disk_name = fname->disk_name;
  1143. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1144. f.crypto_buf = fname->crypto_buf;
  1145. #endif
  1146. return fscrypt_match_name(&f, de->name, de->name_len);
  1147. }
  1148. /*
  1149. * Returns 0 if not found, -1 on failure, and 1 on success
  1150. */
  1151. int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
  1152. struct inode *dir, struct ext4_filename *fname,
  1153. unsigned int offset, struct ext4_dir_entry_2 **res_dir)
  1154. {
  1155. struct ext4_dir_entry_2 * de;
  1156. char * dlimit;
  1157. int de_len;
  1158. de = (struct ext4_dir_entry_2 *)search_buf;
  1159. dlimit = search_buf + buf_size;
  1160. while ((char *) de < dlimit) {
  1161. /* this code is executed quadratically often */
  1162. /* do minimal checking `by hand' */
  1163. if ((char *) de + de->name_len <= dlimit &&
  1164. ext4_match(fname, de)) {
  1165. /* found a match - just to be sure, do
  1166. * a full check */
  1167. if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
  1168. bh->b_size, offset))
  1169. return -1;
  1170. *res_dir = de;
  1171. return 1;
  1172. }
  1173. /* prevent looping on a bad block */
  1174. de_len = ext4_rec_len_from_disk(de->rec_len,
  1175. dir->i_sb->s_blocksize);
  1176. if (de_len <= 0)
  1177. return -1;
  1178. offset += de_len;
  1179. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1180. }
  1181. return 0;
  1182. }
  1183. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1184. struct ext4_dir_entry *de)
  1185. {
  1186. struct super_block *sb = dir->i_sb;
  1187. if (!is_dx(dir))
  1188. return 0;
  1189. if (block == 0)
  1190. return 1;
  1191. if (de->inode == 0 &&
  1192. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1193. sb->s_blocksize)
  1194. return 1;
  1195. return 0;
  1196. }
  1197. /*
  1198. * ext4_find_entry()
  1199. *
  1200. * finds an entry in the specified directory with the wanted name. It
  1201. * returns the cache buffer in which the entry was found, and the entry
  1202. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1203. * entry - you'll have to do that yourself if you want to.
  1204. *
  1205. * The returned buffer_head has ->b_count elevated. The caller is expected
  1206. * to brelse() it when appropriate.
  1207. */
  1208. static struct buffer_head * ext4_find_entry (struct inode *dir,
  1209. const struct qstr *d_name,
  1210. struct ext4_dir_entry_2 **res_dir,
  1211. int *inlined)
  1212. {
  1213. struct super_block *sb;
  1214. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1215. struct buffer_head *bh, *ret = NULL;
  1216. ext4_lblk_t start, block;
  1217. const u8 *name = d_name->name;
  1218. size_t ra_max = 0; /* Number of bh's in the readahead
  1219. buffer, bh_use[] */
  1220. size_t ra_ptr = 0; /* Current index into readahead
  1221. buffer */
  1222. ext4_lblk_t nblocks;
  1223. int i, namelen, retval;
  1224. struct ext4_filename fname;
  1225. *res_dir = NULL;
  1226. sb = dir->i_sb;
  1227. namelen = d_name->len;
  1228. if (namelen > EXT4_NAME_LEN)
  1229. return NULL;
  1230. retval = ext4_fname_setup_filename(dir, d_name, 1, &fname);
  1231. if (retval == -ENOENT)
  1232. return NULL;
  1233. if (retval)
  1234. return ERR_PTR(retval);
  1235. if (ext4_has_inline_data(dir)) {
  1236. int has_inline_data = 1;
  1237. ret = ext4_find_inline_entry(dir, &fname, res_dir,
  1238. &has_inline_data);
  1239. if (has_inline_data) {
  1240. if (inlined)
  1241. *inlined = 1;
  1242. goto cleanup_and_exit;
  1243. }
  1244. }
  1245. if ((namelen <= 2) && (name[0] == '.') &&
  1246. (name[1] == '.' || name[1] == '\0')) {
  1247. /*
  1248. * "." or ".." will only be in the first block
  1249. * NFS may look up ".."; "." should be handled by the VFS
  1250. */
  1251. block = start = 0;
  1252. nblocks = 1;
  1253. goto restart;
  1254. }
  1255. if (is_dx(dir)) {
  1256. ret = ext4_dx_find_entry(dir, &fname, res_dir);
  1257. /*
  1258. * On success, or if the error was file not found,
  1259. * return. Otherwise, fall back to doing a search the
  1260. * old fashioned way.
  1261. */
  1262. if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR)
  1263. goto cleanup_and_exit;
  1264. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1265. "falling back\n"));
  1266. }
  1267. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1268. start = EXT4_I(dir)->i_dir_start_lookup;
  1269. if (start >= nblocks)
  1270. start = 0;
  1271. block = start;
  1272. restart:
  1273. do {
  1274. /*
  1275. * We deal with the read-ahead logic here.
  1276. */
  1277. if (ra_ptr >= ra_max) {
  1278. /* Refill the readahead buffer */
  1279. ra_ptr = 0;
  1280. if (block < start)
  1281. ra_max = start - block;
  1282. else
  1283. ra_max = nblocks - block;
  1284. ra_max = min(ra_max, ARRAY_SIZE(bh_use));
  1285. retval = ext4_bread_batch(dir, block, ra_max,
  1286. false /* wait */, bh_use);
  1287. if (retval) {
  1288. ret = ERR_PTR(retval);
  1289. ra_max = 0;
  1290. goto cleanup_and_exit;
  1291. }
  1292. }
  1293. if ((bh = bh_use[ra_ptr++]) == NULL)
  1294. goto next;
  1295. wait_on_buffer(bh);
  1296. if (!buffer_uptodate(bh)) {
  1297. EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
  1298. (unsigned long) block);
  1299. brelse(bh);
  1300. ret = ERR_PTR(-EIO);
  1301. goto cleanup_and_exit;
  1302. }
  1303. if (!buffer_verified(bh) &&
  1304. !is_dx_internal_node(dir, block,
  1305. (struct ext4_dir_entry *)bh->b_data) &&
  1306. !ext4_dirent_csum_verify(dir,
  1307. (struct ext4_dir_entry *)bh->b_data)) {
  1308. EXT4_ERROR_INODE(dir, "checksumming directory "
  1309. "block %lu", (unsigned long)block);
  1310. brelse(bh);
  1311. ret = ERR_PTR(-EFSBADCRC);
  1312. goto cleanup_and_exit;
  1313. }
  1314. set_buffer_verified(bh);
  1315. i = search_dirblock(bh, dir, &fname,
  1316. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  1317. if (i == 1) {
  1318. EXT4_I(dir)->i_dir_start_lookup = block;
  1319. ret = bh;
  1320. goto cleanup_and_exit;
  1321. } else {
  1322. brelse(bh);
  1323. if (i < 0)
  1324. goto cleanup_and_exit;
  1325. }
  1326. next:
  1327. if (++block >= nblocks)
  1328. block = 0;
  1329. } while (block != start);
  1330. /*
  1331. * If the directory has grown while we were searching, then
  1332. * search the last part of the directory before giving up.
  1333. */
  1334. block = nblocks;
  1335. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1336. if (block < nblocks) {
  1337. start = 0;
  1338. goto restart;
  1339. }
  1340. cleanup_and_exit:
  1341. /* Clean up the read-ahead blocks */
  1342. for (; ra_ptr < ra_max; ra_ptr++)
  1343. brelse(bh_use[ra_ptr]);
  1344. ext4_fname_free_filename(&fname);
  1345. return ret;
  1346. }
  1347. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  1348. struct ext4_filename *fname,
  1349. struct ext4_dir_entry_2 **res_dir)
  1350. {
  1351. struct super_block * sb = dir->i_sb;
  1352. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1353. struct buffer_head *bh;
  1354. ext4_lblk_t block;
  1355. int retval;
  1356. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1357. *res_dir = NULL;
  1358. #endif
  1359. frame = dx_probe(fname, dir, NULL, frames);
  1360. if (IS_ERR(frame))
  1361. return (struct buffer_head *) frame;
  1362. do {
  1363. block = dx_get_block(frame->at);
  1364. bh = ext4_read_dirblock(dir, block, DIRENT);
  1365. if (IS_ERR(bh))
  1366. goto errout;
  1367. retval = search_dirblock(bh, dir, fname,
  1368. block << EXT4_BLOCK_SIZE_BITS(sb),
  1369. res_dir);
  1370. if (retval == 1)
  1371. goto success;
  1372. brelse(bh);
  1373. if (retval == -1) {
  1374. bh = ERR_PTR(ERR_BAD_DX_DIR);
  1375. goto errout;
  1376. }
  1377. /* Check to see if we should continue to search */
  1378. retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
  1379. frames, NULL);
  1380. if (retval < 0) {
  1381. ext4_warning_inode(dir,
  1382. "error %d reading directory index block",
  1383. retval);
  1384. bh = ERR_PTR(retval);
  1385. goto errout;
  1386. }
  1387. } while (retval == 1);
  1388. bh = NULL;
  1389. errout:
  1390. dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
  1391. success:
  1392. dx_release(frames);
  1393. return bh;
  1394. }
  1395. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1396. {
  1397. struct inode *inode;
  1398. struct ext4_dir_entry_2 *de;
  1399. struct buffer_head *bh;
  1400. if (ext4_encrypted_inode(dir)) {
  1401. int res = fscrypt_get_encryption_info(dir);
  1402. /*
  1403. * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
  1404. * created while the directory was encrypted and we
  1405. * have access to the key.
  1406. */
  1407. if (fscrypt_has_encryption_key(dir))
  1408. fscrypt_set_encrypted_dentry(dentry);
  1409. fscrypt_set_d_op(dentry);
  1410. if (res && res != -ENOKEY)
  1411. return ERR_PTR(res);
  1412. }
  1413. if (dentry->d_name.len > EXT4_NAME_LEN)
  1414. return ERR_PTR(-ENAMETOOLONG);
  1415. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  1416. if (IS_ERR(bh))
  1417. return (struct dentry *) bh;
  1418. inode = NULL;
  1419. if (bh) {
  1420. __u32 ino = le32_to_cpu(de->inode);
  1421. brelse(bh);
  1422. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1423. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1424. return ERR_PTR(-EFSCORRUPTED);
  1425. }
  1426. if (unlikely(ino == dir->i_ino)) {
  1427. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1428. dentry);
  1429. return ERR_PTR(-EFSCORRUPTED);
  1430. }
  1431. inode = ext4_iget_normal(dir->i_sb, ino);
  1432. if (inode == ERR_PTR(-ESTALE)) {
  1433. EXT4_ERROR_INODE(dir,
  1434. "deleted inode referenced: %u",
  1435. ino);
  1436. return ERR_PTR(-EFSCORRUPTED);
  1437. }
  1438. if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
  1439. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  1440. !fscrypt_has_permitted_context(dir, inode)) {
  1441. ext4_warning(inode->i_sb,
  1442. "Inconsistent encryption contexts: %lu/%lu",
  1443. dir->i_ino, inode->i_ino);
  1444. iput(inode);
  1445. return ERR_PTR(-EPERM);
  1446. }
  1447. }
  1448. return d_splice_alias(inode, dentry);
  1449. }
  1450. struct dentry *ext4_get_parent(struct dentry *child)
  1451. {
  1452. __u32 ino;
  1453. static const struct qstr dotdot = QSTR_INIT("..", 2);
  1454. struct ext4_dir_entry_2 * de;
  1455. struct buffer_head *bh;
  1456. bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL);
  1457. if (IS_ERR(bh))
  1458. return (struct dentry *) bh;
  1459. if (!bh)
  1460. return ERR_PTR(-ENOENT);
  1461. ino = le32_to_cpu(de->inode);
  1462. brelse(bh);
  1463. if (!ext4_valid_inum(child->d_sb, ino)) {
  1464. EXT4_ERROR_INODE(d_inode(child),
  1465. "bad parent inode number: %u", ino);
  1466. return ERR_PTR(-EFSCORRUPTED);
  1467. }
  1468. return d_obtain_alias(ext4_iget_normal(child->d_sb, ino));
  1469. }
  1470. /*
  1471. * Move count entries from end of map between two memory locations.
  1472. * Returns pointer to last entry moved.
  1473. */
  1474. static struct ext4_dir_entry_2 *
  1475. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1476. unsigned blocksize)
  1477. {
  1478. unsigned rec_len = 0;
  1479. while (count--) {
  1480. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1481. (from + (map->offs<<2));
  1482. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1483. memcpy (to, de, rec_len);
  1484. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1485. ext4_rec_len_to_disk(rec_len, blocksize);
  1486. de->inode = 0;
  1487. map++;
  1488. to += rec_len;
  1489. }
  1490. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1491. }
  1492. /*
  1493. * Compact each dir entry in the range to the minimal rec_len.
  1494. * Returns pointer to last entry in range.
  1495. */
  1496. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1497. {
  1498. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1499. unsigned rec_len = 0;
  1500. prev = to = de;
  1501. while ((char*)de < base + blocksize) {
  1502. next = ext4_next_entry(de, blocksize);
  1503. if (de->inode && de->name_len) {
  1504. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1505. if (de > to)
  1506. memmove(to, de, rec_len);
  1507. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1508. prev = to;
  1509. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1510. }
  1511. de = next;
  1512. }
  1513. return prev;
  1514. }
  1515. /*
  1516. * Split a full leaf block to make room for a new dir entry.
  1517. * Allocate a new block, and move entries so that they are approx. equally full.
  1518. * Returns pointer to de in block into which the new entry will be inserted.
  1519. */
  1520. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1521. struct buffer_head **bh,struct dx_frame *frame,
  1522. struct dx_hash_info *hinfo)
  1523. {
  1524. unsigned blocksize = dir->i_sb->s_blocksize;
  1525. unsigned count, continued;
  1526. struct buffer_head *bh2;
  1527. ext4_lblk_t newblock;
  1528. u32 hash2;
  1529. struct dx_map_entry *map;
  1530. char *data1 = (*bh)->b_data, *data2;
  1531. unsigned split, move, size;
  1532. struct ext4_dir_entry_2 *de = NULL, *de2;
  1533. struct ext4_dir_entry_tail *t;
  1534. int csum_size = 0;
  1535. int err = 0, i;
  1536. if (ext4_has_metadata_csum(dir->i_sb))
  1537. csum_size = sizeof(struct ext4_dir_entry_tail);
  1538. bh2 = ext4_append(handle, dir, &newblock);
  1539. if (IS_ERR(bh2)) {
  1540. brelse(*bh);
  1541. *bh = NULL;
  1542. return (struct ext4_dir_entry_2 *) bh2;
  1543. }
  1544. BUFFER_TRACE(*bh, "get_write_access");
  1545. err = ext4_journal_get_write_access(handle, *bh);
  1546. if (err)
  1547. goto journal_error;
  1548. BUFFER_TRACE(frame->bh, "get_write_access");
  1549. err = ext4_journal_get_write_access(handle, frame->bh);
  1550. if (err)
  1551. goto journal_error;
  1552. data2 = bh2->b_data;
  1553. /* create map in the end of data2 block */
  1554. map = (struct dx_map_entry *) (data2 + blocksize);
  1555. count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1,
  1556. blocksize, hinfo, map);
  1557. map -= count;
  1558. dx_sort_map(map, count);
  1559. /* Split the existing block in the middle, size-wise */
  1560. size = 0;
  1561. move = 0;
  1562. for (i = count-1; i >= 0; i--) {
  1563. /* is more than half of this entry in 2nd half of the block? */
  1564. if (size + map[i].size/2 > blocksize/2)
  1565. break;
  1566. size += map[i].size;
  1567. move++;
  1568. }
  1569. /* map index at which we will split */
  1570. split = count - move;
  1571. hash2 = map[split].hash;
  1572. continued = hash2 == map[split - 1].hash;
  1573. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1574. (unsigned long)dx_get_block(frame->at),
  1575. hash2, split, count-split));
  1576. /* Fancy dance to stay within two buffers */
  1577. de2 = dx_move_dirents(data1, data2, map + split, count - split,
  1578. blocksize);
  1579. de = dx_pack_dirents(data1, blocksize);
  1580. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1581. (char *) de,
  1582. blocksize);
  1583. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1584. (char *) de2,
  1585. blocksize);
  1586. if (csum_size) {
  1587. t = EXT4_DIRENT_TAIL(data2, blocksize);
  1588. initialize_dirent_tail(t, blocksize);
  1589. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1590. initialize_dirent_tail(t, blocksize);
  1591. }
  1592. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
  1593. blocksize, 1));
  1594. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
  1595. blocksize, 1));
  1596. /* Which block gets the new entry? */
  1597. if (hinfo->hash >= hash2) {
  1598. swap(*bh, bh2);
  1599. de = de2;
  1600. }
  1601. dx_insert_block(frame, hash2 + continued, newblock);
  1602. err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1603. if (err)
  1604. goto journal_error;
  1605. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1606. if (err)
  1607. goto journal_error;
  1608. brelse(bh2);
  1609. dxtrace(dx_show_index("frame", frame->entries));
  1610. return de;
  1611. journal_error:
  1612. brelse(*bh);
  1613. brelse(bh2);
  1614. *bh = NULL;
  1615. ext4_std_error(dir->i_sb, err);
  1616. return ERR_PTR(err);
  1617. }
  1618. int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  1619. struct buffer_head *bh,
  1620. void *buf, int buf_size,
  1621. struct ext4_filename *fname,
  1622. struct ext4_dir_entry_2 **dest_de)
  1623. {
  1624. struct ext4_dir_entry_2 *de;
  1625. unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname));
  1626. int nlen, rlen;
  1627. unsigned int offset = 0;
  1628. char *top;
  1629. de = (struct ext4_dir_entry_2 *)buf;
  1630. top = buf + buf_size - reclen;
  1631. while ((char *) de <= top) {
  1632. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1633. buf, buf_size, offset))
  1634. return -EFSCORRUPTED;
  1635. if (ext4_match(fname, de))
  1636. return -EEXIST;
  1637. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1638. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1639. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1640. break;
  1641. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1642. offset += rlen;
  1643. }
  1644. if ((char *) de > top)
  1645. return -ENOSPC;
  1646. *dest_de = de;
  1647. return 0;
  1648. }
  1649. void ext4_insert_dentry(struct inode *inode,
  1650. struct ext4_dir_entry_2 *de,
  1651. int buf_size,
  1652. struct ext4_filename *fname)
  1653. {
  1654. int nlen, rlen;
  1655. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1656. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1657. if (de->inode) {
  1658. struct ext4_dir_entry_2 *de1 =
  1659. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1660. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1661. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1662. de = de1;
  1663. }
  1664. de->file_type = EXT4_FT_UNKNOWN;
  1665. de->inode = cpu_to_le32(inode->i_ino);
  1666. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1667. de->name_len = fname_len(fname);
  1668. memcpy(de->name, fname_name(fname), fname_len(fname));
  1669. }
  1670. /*
  1671. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1672. * it points to a directory entry which is guaranteed to be large
  1673. * enough for new directory entry. If de is NULL, then
  1674. * add_dirent_to_buf will attempt search the directory block for
  1675. * space. It will return -ENOSPC if no space is available, and -EIO
  1676. * and -EEXIST if directory entry already exists.
  1677. */
  1678. static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
  1679. struct inode *dir,
  1680. struct inode *inode, struct ext4_dir_entry_2 *de,
  1681. struct buffer_head *bh)
  1682. {
  1683. unsigned int blocksize = dir->i_sb->s_blocksize;
  1684. int csum_size = 0;
  1685. int err;
  1686. if (ext4_has_metadata_csum(inode->i_sb))
  1687. csum_size = sizeof(struct ext4_dir_entry_tail);
  1688. if (!de) {
  1689. err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
  1690. blocksize - csum_size, fname, &de);
  1691. if (err)
  1692. return err;
  1693. }
  1694. BUFFER_TRACE(bh, "get_write_access");
  1695. err = ext4_journal_get_write_access(handle, bh);
  1696. if (err) {
  1697. ext4_std_error(dir->i_sb, err);
  1698. return err;
  1699. }
  1700. /* By now the buffer is marked for journaling */
  1701. ext4_insert_dentry(inode, de, blocksize, fname);
  1702. /*
  1703. * XXX shouldn't update any times until successful
  1704. * completion of syscall, but too many callers depend
  1705. * on this.
  1706. *
  1707. * XXX similarly, too many callers depend on
  1708. * ext4_new_inode() setting the times, but error
  1709. * recovery deletes the inode, so the worst that can
  1710. * happen is that the times are slightly out of date
  1711. * and/or different from the directory change time.
  1712. */
  1713. dir->i_mtime = dir->i_ctime = current_time(dir);
  1714. ext4_update_dx_flag(dir);
  1715. inode_inc_iversion(dir);
  1716. ext4_mark_inode_dirty(handle, dir);
  1717. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1718. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  1719. if (err)
  1720. ext4_std_error(dir->i_sb, err);
  1721. return 0;
  1722. }
  1723. /*
  1724. * This converts a one block unindexed directory to a 3 block indexed
  1725. * directory, and adds the dentry to the indexed directory.
  1726. */
  1727. static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
  1728. struct inode *dir,
  1729. struct inode *inode, struct buffer_head *bh)
  1730. {
  1731. struct buffer_head *bh2;
  1732. struct dx_root *root;
  1733. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1734. struct dx_entry *entries;
  1735. struct ext4_dir_entry_2 *de, *de2;
  1736. struct ext4_dir_entry_tail *t;
  1737. char *data1, *top;
  1738. unsigned len;
  1739. int retval;
  1740. unsigned blocksize;
  1741. ext4_lblk_t block;
  1742. struct fake_dirent *fde;
  1743. int csum_size = 0;
  1744. if (ext4_has_metadata_csum(inode->i_sb))
  1745. csum_size = sizeof(struct ext4_dir_entry_tail);
  1746. blocksize = dir->i_sb->s_blocksize;
  1747. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1748. BUFFER_TRACE(bh, "get_write_access");
  1749. retval = ext4_journal_get_write_access(handle, bh);
  1750. if (retval) {
  1751. ext4_std_error(dir->i_sb, retval);
  1752. brelse(bh);
  1753. return retval;
  1754. }
  1755. root = (struct dx_root *) bh->b_data;
  1756. /* The 0th block becomes the root, move the dirents out */
  1757. fde = &root->dotdot;
  1758. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1759. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1760. if ((char *) de >= (((char *) root) + blocksize)) {
  1761. EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
  1762. brelse(bh);
  1763. return -EFSCORRUPTED;
  1764. }
  1765. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  1766. /* Allocate new block for the 0th block's dirents */
  1767. bh2 = ext4_append(handle, dir, &block);
  1768. if (IS_ERR(bh2)) {
  1769. brelse(bh);
  1770. return PTR_ERR(bh2);
  1771. }
  1772. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  1773. data1 = bh2->b_data;
  1774. memcpy (data1, de, len);
  1775. de = (struct ext4_dir_entry_2 *) data1;
  1776. top = data1 + len;
  1777. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1778. de = de2;
  1779. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1780. (char *) de,
  1781. blocksize);
  1782. if (csum_size) {
  1783. t = EXT4_DIRENT_TAIL(data1, blocksize);
  1784. initialize_dirent_tail(t, blocksize);
  1785. }
  1786. /* Initialize the root; the dot dirents already exist */
  1787. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1788. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1789. blocksize);
  1790. memset (&root->info, 0, sizeof(root->info));
  1791. root->info.info_length = sizeof(root->info);
  1792. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1793. entries = root->entries;
  1794. dx_set_block(entries, 1);
  1795. dx_set_count(entries, 1);
  1796. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1797. /* Initialize as for dx_probe */
  1798. fname->hinfo.hash_version = root->info.hash_version;
  1799. if (fname->hinfo.hash_version <= DX_HASH_TEA)
  1800. fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1801. fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1802. ext4fs_dirhash(fname_name(fname), fname_len(fname), &fname->hinfo);
  1803. memset(frames, 0, sizeof(frames));
  1804. frame = frames;
  1805. frame->entries = entries;
  1806. frame->at = entries;
  1807. frame->bh = bh;
  1808. retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1809. if (retval)
  1810. goto out_frames;
  1811. retval = ext4_handle_dirty_dirent_node(handle, dir, bh2);
  1812. if (retval)
  1813. goto out_frames;
  1814. de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
  1815. if (IS_ERR(de)) {
  1816. retval = PTR_ERR(de);
  1817. goto out_frames;
  1818. }
  1819. retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
  1820. out_frames:
  1821. /*
  1822. * Even if the block split failed, we have to properly write
  1823. * out all the changes we did so far. Otherwise we can end up
  1824. * with corrupted filesystem.
  1825. */
  1826. if (retval)
  1827. ext4_mark_inode_dirty(handle, dir);
  1828. dx_release(frames);
  1829. brelse(bh2);
  1830. return retval;
  1831. }
  1832. /*
  1833. * ext4_add_entry()
  1834. *
  1835. * adds a file entry to the specified directory, using the same
  1836. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1837. *
  1838. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1839. * may not sleep between calling this and putting something into
  1840. * the entry, as someone else might have used it while you slept.
  1841. */
  1842. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1843. struct inode *inode)
  1844. {
  1845. struct inode *dir = d_inode(dentry->d_parent);
  1846. struct buffer_head *bh = NULL;
  1847. struct ext4_dir_entry_2 *de;
  1848. struct ext4_dir_entry_tail *t;
  1849. struct super_block *sb;
  1850. struct ext4_filename fname;
  1851. int retval;
  1852. int dx_fallback=0;
  1853. unsigned blocksize;
  1854. ext4_lblk_t block, blocks;
  1855. int csum_size = 0;
  1856. if (ext4_has_metadata_csum(inode->i_sb))
  1857. csum_size = sizeof(struct ext4_dir_entry_tail);
  1858. sb = dir->i_sb;
  1859. blocksize = sb->s_blocksize;
  1860. if (!dentry->d_name.len)
  1861. return -EINVAL;
  1862. retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
  1863. if (retval)
  1864. return retval;
  1865. if (ext4_has_inline_data(dir)) {
  1866. retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
  1867. if (retval < 0)
  1868. goto out;
  1869. if (retval == 1) {
  1870. retval = 0;
  1871. goto out;
  1872. }
  1873. }
  1874. if (is_dx(dir)) {
  1875. retval = ext4_dx_add_entry(handle, &fname, dir, inode);
  1876. if (!retval || (retval != ERR_BAD_DX_DIR))
  1877. goto out;
  1878. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  1879. dx_fallback++;
  1880. ext4_mark_inode_dirty(handle, dir);
  1881. }
  1882. blocks = dir->i_size >> sb->s_blocksize_bits;
  1883. for (block = 0; block < blocks; block++) {
  1884. bh = ext4_read_dirblock(dir, block, DIRENT);
  1885. if (IS_ERR(bh)) {
  1886. retval = PTR_ERR(bh);
  1887. bh = NULL;
  1888. goto out;
  1889. }
  1890. retval = add_dirent_to_buf(handle, &fname, dir, inode,
  1891. NULL, bh);
  1892. if (retval != -ENOSPC)
  1893. goto out;
  1894. if (blocks == 1 && !dx_fallback &&
  1895. ext4_has_feature_dir_index(sb)) {
  1896. retval = make_indexed_dir(handle, &fname, dir,
  1897. inode, bh);
  1898. bh = NULL; /* make_indexed_dir releases bh */
  1899. goto out;
  1900. }
  1901. brelse(bh);
  1902. }
  1903. bh = ext4_append(handle, dir, &block);
  1904. if (IS_ERR(bh)) {
  1905. retval = PTR_ERR(bh);
  1906. bh = NULL;
  1907. goto out;
  1908. }
  1909. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1910. de->inode = 0;
  1911. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  1912. if (csum_size) {
  1913. t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  1914. initialize_dirent_tail(t, blocksize);
  1915. }
  1916. retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
  1917. out:
  1918. ext4_fname_free_filename(&fname);
  1919. brelse(bh);
  1920. if (retval == 0)
  1921. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  1922. return retval;
  1923. }
  1924. /*
  1925. * Returns 0 for success, or a negative error value
  1926. */
  1927. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  1928. struct inode *dir, struct inode *inode)
  1929. {
  1930. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1931. struct dx_entry *entries, *at;
  1932. struct buffer_head *bh;
  1933. struct super_block *sb = dir->i_sb;
  1934. struct ext4_dir_entry_2 *de;
  1935. int restart;
  1936. int err;
  1937. again:
  1938. restart = 0;
  1939. frame = dx_probe(fname, dir, NULL, frames);
  1940. if (IS_ERR(frame))
  1941. return PTR_ERR(frame);
  1942. entries = frame->entries;
  1943. at = frame->at;
  1944. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
  1945. if (IS_ERR(bh)) {
  1946. err = PTR_ERR(bh);
  1947. bh = NULL;
  1948. goto cleanup;
  1949. }
  1950. BUFFER_TRACE(bh, "get_write_access");
  1951. err = ext4_journal_get_write_access(handle, bh);
  1952. if (err)
  1953. goto journal_error;
  1954. err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
  1955. if (err != -ENOSPC)
  1956. goto cleanup;
  1957. err = 0;
  1958. /* Block full, should compress but for now just split */
  1959. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1960. dx_get_count(entries), dx_get_limit(entries)));
  1961. /* Need to split index? */
  1962. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1963. ext4_lblk_t newblock;
  1964. int levels = frame - frames + 1;
  1965. unsigned int icount;
  1966. int add_level = 1;
  1967. struct dx_entry *entries2;
  1968. struct dx_node *node2;
  1969. struct buffer_head *bh2;
  1970. while (frame > frames) {
  1971. if (dx_get_count((frame - 1)->entries) <
  1972. dx_get_limit((frame - 1)->entries)) {
  1973. add_level = 0;
  1974. break;
  1975. }
  1976. frame--; /* split higher index block */
  1977. at = frame->at;
  1978. entries = frame->entries;
  1979. restart = 1;
  1980. }
  1981. if (add_level && levels == ext4_dir_htree_level(sb)) {
  1982. ext4_warning(sb, "Directory (ino: %lu) index full, "
  1983. "reach max htree level :%d",
  1984. dir->i_ino, levels);
  1985. if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
  1986. ext4_warning(sb, "Large directory feature is "
  1987. "not enabled on this "
  1988. "filesystem");
  1989. }
  1990. err = -ENOSPC;
  1991. goto cleanup;
  1992. }
  1993. icount = dx_get_count(entries);
  1994. bh2 = ext4_append(handle, dir, &newblock);
  1995. if (IS_ERR(bh2)) {
  1996. err = PTR_ERR(bh2);
  1997. goto cleanup;
  1998. }
  1999. node2 = (struct dx_node *)(bh2->b_data);
  2000. entries2 = node2->entries;
  2001. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  2002. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  2003. sb->s_blocksize);
  2004. BUFFER_TRACE(frame->bh, "get_write_access");
  2005. err = ext4_journal_get_write_access(handle, frame->bh);
  2006. if (err)
  2007. goto journal_error;
  2008. if (!add_level) {
  2009. unsigned icount1 = icount/2, icount2 = icount - icount1;
  2010. unsigned hash2 = dx_get_hash(entries + icount1);
  2011. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  2012. icount1, icount2));
  2013. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  2014. err = ext4_journal_get_write_access(handle,
  2015. (frame - 1)->bh);
  2016. if (err)
  2017. goto journal_error;
  2018. memcpy((char *) entries2, (char *) (entries + icount1),
  2019. icount2 * sizeof(struct dx_entry));
  2020. dx_set_count(entries, icount1);
  2021. dx_set_count(entries2, icount2);
  2022. dx_set_limit(entries2, dx_node_limit(dir));
  2023. /* Which index block gets the new entry? */
  2024. if (at - entries >= icount1) {
  2025. frame->at = at = at - entries - icount1 + entries2;
  2026. frame->entries = entries = entries2;
  2027. swap(frame->bh, bh2);
  2028. }
  2029. dx_insert_block((frame - 1), hash2, newblock);
  2030. dxtrace(dx_show_index("node", frame->entries));
  2031. dxtrace(dx_show_index("node",
  2032. ((struct dx_node *) bh2->b_data)->entries));
  2033. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2034. if (err)
  2035. goto journal_error;
  2036. brelse (bh2);
  2037. err = ext4_handle_dirty_dx_node(handle, dir,
  2038. (frame - 1)->bh);
  2039. if (err)
  2040. goto journal_error;
  2041. if (restart) {
  2042. err = ext4_handle_dirty_dx_node(handle, dir,
  2043. frame->bh);
  2044. goto journal_error;
  2045. }
  2046. } else {
  2047. struct dx_root *dxroot;
  2048. memcpy((char *) entries2, (char *) entries,
  2049. icount * sizeof(struct dx_entry));
  2050. dx_set_limit(entries2, dx_node_limit(dir));
  2051. /* Set up root */
  2052. dx_set_count(entries, 1);
  2053. dx_set_block(entries + 0, newblock);
  2054. dxroot = (struct dx_root *)frames[0].bh->b_data;
  2055. dxroot->info.indirect_levels += 1;
  2056. dxtrace(printk(KERN_DEBUG
  2057. "Creating %d level index...\n",
  2058. info->indirect_levels));
  2059. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  2060. if (err)
  2061. goto journal_error;
  2062. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2063. brelse(bh2);
  2064. restart = 1;
  2065. goto journal_error;
  2066. }
  2067. }
  2068. de = do_split(handle, dir, &bh, frame, &fname->hinfo);
  2069. if (IS_ERR(de)) {
  2070. err = PTR_ERR(de);
  2071. goto cleanup;
  2072. }
  2073. err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  2074. goto cleanup;
  2075. journal_error:
  2076. ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
  2077. cleanup:
  2078. brelse(bh);
  2079. dx_release(frames);
  2080. /* @restart is true means htree-path has been changed, we need to
  2081. * repeat dx_probe() to find out valid htree-path
  2082. */
  2083. if (restart && err == 0)
  2084. goto again;
  2085. return err;
  2086. }
  2087. /*
  2088. * ext4_generic_delete_entry deletes a directory entry by merging it
  2089. * with the previous entry
  2090. */
  2091. int ext4_generic_delete_entry(handle_t *handle,
  2092. struct inode *dir,
  2093. struct ext4_dir_entry_2 *de_del,
  2094. struct buffer_head *bh,
  2095. void *entry_buf,
  2096. int buf_size,
  2097. int csum_size)
  2098. {
  2099. struct ext4_dir_entry_2 *de, *pde;
  2100. unsigned int blocksize = dir->i_sb->s_blocksize;
  2101. int i;
  2102. i = 0;
  2103. pde = NULL;
  2104. de = (struct ext4_dir_entry_2 *)entry_buf;
  2105. while (i < buf_size - csum_size) {
  2106. if (ext4_check_dir_entry(dir, NULL, de, bh,
  2107. bh->b_data, bh->b_size, i))
  2108. return -EFSCORRUPTED;
  2109. if (de == de_del) {
  2110. if (pde)
  2111. pde->rec_len = ext4_rec_len_to_disk(
  2112. ext4_rec_len_from_disk(pde->rec_len,
  2113. blocksize) +
  2114. ext4_rec_len_from_disk(de->rec_len,
  2115. blocksize),
  2116. blocksize);
  2117. else
  2118. de->inode = 0;
  2119. inode_inc_iversion(dir);
  2120. return 0;
  2121. }
  2122. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  2123. pde = de;
  2124. de = ext4_next_entry(de, blocksize);
  2125. }
  2126. return -ENOENT;
  2127. }
  2128. static int ext4_delete_entry(handle_t *handle,
  2129. struct inode *dir,
  2130. struct ext4_dir_entry_2 *de_del,
  2131. struct buffer_head *bh)
  2132. {
  2133. int err, csum_size = 0;
  2134. if (ext4_has_inline_data(dir)) {
  2135. int has_inline_data = 1;
  2136. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  2137. &has_inline_data);
  2138. if (has_inline_data)
  2139. return err;
  2140. }
  2141. if (ext4_has_metadata_csum(dir->i_sb))
  2142. csum_size = sizeof(struct ext4_dir_entry_tail);
  2143. BUFFER_TRACE(bh, "get_write_access");
  2144. err = ext4_journal_get_write_access(handle, bh);
  2145. if (unlikely(err))
  2146. goto out;
  2147. err = ext4_generic_delete_entry(handle, dir, de_del,
  2148. bh, bh->b_data,
  2149. dir->i_sb->s_blocksize, csum_size);
  2150. if (err)
  2151. goto out;
  2152. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  2153. err = ext4_handle_dirty_dirent_node(handle, dir, bh);
  2154. if (unlikely(err))
  2155. goto out;
  2156. return 0;
  2157. out:
  2158. if (err != -ENOENT)
  2159. ext4_std_error(dir->i_sb, err);
  2160. return err;
  2161. }
  2162. /*
  2163. * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
  2164. * since this indicates that nlinks count was previously 1 to avoid overflowing
  2165. * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
  2166. * that subdirectory link counts are not being maintained accurately.
  2167. *
  2168. * The caller has already checked for i_nlink overflow in case the DIR_LINK
  2169. * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
  2170. * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
  2171. * on regular files) and to avoid creating huge/slow non-HTREE directories.
  2172. */
  2173. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  2174. {
  2175. inc_nlink(inode);
  2176. if (is_dx(inode) &&
  2177. (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
  2178. set_nlink(inode, 1);
  2179. }
  2180. /*
  2181. * If a directory had nlink == 1, then we should let it be 1. This indicates
  2182. * directory has >EXT4_LINK_MAX subdirs.
  2183. */
  2184. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  2185. {
  2186. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  2187. drop_nlink(inode);
  2188. }
  2189. static int ext4_add_nondir(handle_t *handle,
  2190. struct dentry *dentry, struct inode *inode)
  2191. {
  2192. int err = ext4_add_entry(handle, dentry, inode);
  2193. if (!err) {
  2194. ext4_mark_inode_dirty(handle, inode);
  2195. unlock_new_inode(inode);
  2196. d_instantiate(dentry, inode);
  2197. return 0;
  2198. }
  2199. drop_nlink(inode);
  2200. unlock_new_inode(inode);
  2201. iput(inode);
  2202. return err;
  2203. }
  2204. /*
  2205. * By the time this is called, we already have created
  2206. * the directory cache entry for the new file, but it
  2207. * is so far negative - it has no inode.
  2208. *
  2209. * If the create succeeds, we fill in the inode information
  2210. * with d_instantiate().
  2211. */
  2212. static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2213. bool excl)
  2214. {
  2215. handle_t *handle;
  2216. struct inode *inode;
  2217. int err, credits, retries = 0;
  2218. err = dquot_initialize(dir);
  2219. if (err)
  2220. return err;
  2221. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2222. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2223. retry:
  2224. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2225. NULL, EXT4_HT_DIR, credits);
  2226. handle = ext4_journal_current_handle();
  2227. err = PTR_ERR(inode);
  2228. if (!IS_ERR(inode)) {
  2229. inode->i_op = &ext4_file_inode_operations;
  2230. inode->i_fop = &ext4_file_operations;
  2231. ext4_set_aops(inode);
  2232. err = ext4_add_nondir(handle, dentry, inode);
  2233. if (!err && IS_DIRSYNC(dir))
  2234. ext4_handle_sync(handle);
  2235. }
  2236. if (handle)
  2237. ext4_journal_stop(handle);
  2238. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2239. goto retry;
  2240. return err;
  2241. }
  2242. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  2243. umode_t mode, dev_t rdev)
  2244. {
  2245. handle_t *handle;
  2246. struct inode *inode;
  2247. int err, credits, retries = 0;
  2248. err = dquot_initialize(dir);
  2249. if (err)
  2250. return err;
  2251. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2252. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2253. retry:
  2254. inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0,
  2255. NULL, EXT4_HT_DIR, credits);
  2256. handle = ext4_journal_current_handle();
  2257. err = PTR_ERR(inode);
  2258. if (!IS_ERR(inode)) {
  2259. init_special_inode(inode, inode->i_mode, rdev);
  2260. inode->i_op = &ext4_special_inode_operations;
  2261. err = ext4_add_nondir(handle, dentry, inode);
  2262. if (!err && IS_DIRSYNC(dir))
  2263. ext4_handle_sync(handle);
  2264. }
  2265. if (handle)
  2266. ext4_journal_stop(handle);
  2267. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2268. goto retry;
  2269. return err;
  2270. }
  2271. static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2272. {
  2273. handle_t *handle;
  2274. struct inode *inode;
  2275. int err, retries = 0;
  2276. err = dquot_initialize(dir);
  2277. if (err)
  2278. return err;
  2279. retry:
  2280. inode = ext4_new_inode_start_handle(dir, mode,
  2281. NULL, 0, NULL,
  2282. EXT4_HT_DIR,
  2283. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2284. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2285. handle = ext4_journal_current_handle();
  2286. err = PTR_ERR(inode);
  2287. if (!IS_ERR(inode)) {
  2288. inode->i_op = &ext4_file_inode_operations;
  2289. inode->i_fop = &ext4_file_operations;
  2290. ext4_set_aops(inode);
  2291. d_tmpfile(dentry, inode);
  2292. err = ext4_orphan_add(handle, inode);
  2293. if (err)
  2294. goto err_unlock_inode;
  2295. mark_inode_dirty(inode);
  2296. unlock_new_inode(inode);
  2297. }
  2298. if (handle)
  2299. ext4_journal_stop(handle);
  2300. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2301. goto retry;
  2302. return err;
  2303. err_unlock_inode:
  2304. ext4_journal_stop(handle);
  2305. unlock_new_inode(inode);
  2306. return err;
  2307. }
  2308. struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  2309. struct ext4_dir_entry_2 *de,
  2310. int blocksize, int csum_size,
  2311. unsigned int parent_ino, int dotdot_real_len)
  2312. {
  2313. de->inode = cpu_to_le32(inode->i_ino);
  2314. de->name_len = 1;
  2315. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  2316. blocksize);
  2317. strcpy(de->name, ".");
  2318. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2319. de = ext4_next_entry(de, blocksize);
  2320. de->inode = cpu_to_le32(parent_ino);
  2321. de->name_len = 2;
  2322. if (!dotdot_real_len)
  2323. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2324. (csum_size + EXT4_DIR_REC_LEN(1)),
  2325. blocksize);
  2326. else
  2327. de->rec_len = ext4_rec_len_to_disk(
  2328. EXT4_DIR_REC_LEN(de->name_len), blocksize);
  2329. strcpy(de->name, "..");
  2330. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2331. return ext4_next_entry(de, blocksize);
  2332. }
  2333. static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2334. struct inode *inode)
  2335. {
  2336. struct buffer_head *dir_block = NULL;
  2337. struct ext4_dir_entry_2 *de;
  2338. struct ext4_dir_entry_tail *t;
  2339. ext4_lblk_t block = 0;
  2340. unsigned int blocksize = dir->i_sb->s_blocksize;
  2341. int csum_size = 0;
  2342. int err;
  2343. if (ext4_has_metadata_csum(dir->i_sb))
  2344. csum_size = sizeof(struct ext4_dir_entry_tail);
  2345. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2346. err = ext4_try_create_inline_dir(handle, dir, inode);
  2347. if (err < 0 && err != -ENOSPC)
  2348. goto out;
  2349. if (!err)
  2350. goto out;
  2351. }
  2352. inode->i_size = 0;
  2353. dir_block = ext4_append(handle, inode, &block);
  2354. if (IS_ERR(dir_block))
  2355. return PTR_ERR(dir_block);
  2356. de = (struct ext4_dir_entry_2 *)dir_block->b_data;
  2357. ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
  2358. set_nlink(inode, 2);
  2359. if (csum_size) {
  2360. t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
  2361. initialize_dirent_tail(t, blocksize);
  2362. }
  2363. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2364. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  2365. if (err)
  2366. goto out;
  2367. set_buffer_verified(dir_block);
  2368. out:
  2369. brelse(dir_block);
  2370. return err;
  2371. }
  2372. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2373. {
  2374. handle_t *handle;
  2375. struct inode *inode;
  2376. int err, credits, retries = 0;
  2377. if (EXT4_DIR_LINK_MAX(dir))
  2378. return -EMLINK;
  2379. err = dquot_initialize(dir);
  2380. if (err)
  2381. return err;
  2382. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2383. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2384. retry:
  2385. inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode,
  2386. &dentry->d_name,
  2387. 0, NULL, EXT4_HT_DIR, credits);
  2388. handle = ext4_journal_current_handle();
  2389. err = PTR_ERR(inode);
  2390. if (IS_ERR(inode))
  2391. goto out_stop;
  2392. inode->i_op = &ext4_dir_inode_operations;
  2393. inode->i_fop = &ext4_dir_operations;
  2394. err = ext4_init_new_dir(handle, dir, inode);
  2395. if (err)
  2396. goto out_clear_inode;
  2397. err = ext4_mark_inode_dirty(handle, inode);
  2398. if (!err)
  2399. err = ext4_add_entry(handle, dentry, inode);
  2400. if (err) {
  2401. out_clear_inode:
  2402. clear_nlink(inode);
  2403. unlock_new_inode(inode);
  2404. ext4_mark_inode_dirty(handle, inode);
  2405. iput(inode);
  2406. goto out_stop;
  2407. }
  2408. ext4_inc_count(handle, dir);
  2409. ext4_update_dx_flag(dir);
  2410. err = ext4_mark_inode_dirty(handle, dir);
  2411. if (err)
  2412. goto out_clear_inode;
  2413. unlock_new_inode(inode);
  2414. d_instantiate(dentry, inode);
  2415. if (IS_DIRSYNC(dir))
  2416. ext4_handle_sync(handle);
  2417. out_stop:
  2418. if (handle)
  2419. ext4_journal_stop(handle);
  2420. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2421. goto retry;
  2422. return err;
  2423. }
  2424. /*
  2425. * routine to check that the specified directory is empty (for rmdir)
  2426. */
  2427. bool ext4_empty_dir(struct inode *inode)
  2428. {
  2429. unsigned int offset;
  2430. struct buffer_head *bh;
  2431. struct ext4_dir_entry_2 *de, *de1;
  2432. struct super_block *sb;
  2433. if (ext4_has_inline_data(inode)) {
  2434. int has_inline_data = 1;
  2435. int ret;
  2436. ret = empty_inline_dir(inode, &has_inline_data);
  2437. if (has_inline_data)
  2438. return ret;
  2439. }
  2440. sb = inode->i_sb;
  2441. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
  2442. EXT4_ERROR_INODE(inode, "invalid size");
  2443. return true;
  2444. }
  2445. bh = ext4_read_dirblock(inode, 0, EITHER);
  2446. if (IS_ERR(bh))
  2447. return true;
  2448. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2449. de1 = ext4_next_entry(de, sb->s_blocksize);
  2450. if (le32_to_cpu(de->inode) != inode->i_ino ||
  2451. le32_to_cpu(de1->inode) == 0 ||
  2452. strcmp(".", de->name) || strcmp("..", de1->name)) {
  2453. ext4_warning_inode(inode, "directory missing '.' and/or '..'");
  2454. brelse(bh);
  2455. return true;
  2456. }
  2457. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  2458. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  2459. de = ext4_next_entry(de1, sb->s_blocksize);
  2460. while (offset < inode->i_size) {
  2461. if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  2462. unsigned int lblock;
  2463. brelse(bh);
  2464. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2465. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2466. if (IS_ERR(bh))
  2467. return true;
  2468. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2469. }
  2470. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2471. bh->b_data, bh->b_size, offset)) {
  2472. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  2473. sb->s_blocksize);
  2474. offset = (offset | (sb->s_blocksize - 1)) + 1;
  2475. continue;
  2476. }
  2477. if (le32_to_cpu(de->inode)) {
  2478. brelse(bh);
  2479. return false;
  2480. }
  2481. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2482. de = ext4_next_entry(de, sb->s_blocksize);
  2483. }
  2484. brelse(bh);
  2485. return true;
  2486. }
  2487. /*
  2488. * ext4_orphan_add() links an unlinked or truncated inode into a list of
  2489. * such inodes, starting at the superblock, in case we crash before the
  2490. * file is closed/deleted, or in case the inode truncate spans multiple
  2491. * transactions and the last transaction is not recovered after a crash.
  2492. *
  2493. * At filesystem recovery time, we walk this list deleting unlinked
  2494. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  2495. *
  2496. * Orphan list manipulation functions must be called under i_mutex unless
  2497. * we are just creating the inode or deleting it.
  2498. */
  2499. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  2500. {
  2501. struct super_block *sb = inode->i_sb;
  2502. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2503. struct ext4_iloc iloc;
  2504. int err = 0, rc;
  2505. bool dirty = false;
  2506. if (!sbi->s_journal || is_bad_inode(inode))
  2507. return 0;
  2508. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2509. !inode_is_locked(inode));
  2510. /*
  2511. * Exit early if inode already is on orphan list. This is a big speedup
  2512. * since we don't have to contend on the global s_orphan_lock.
  2513. */
  2514. if (!list_empty(&EXT4_I(inode)->i_orphan))
  2515. return 0;
  2516. /*
  2517. * Orphan handling is only valid for files with data blocks
  2518. * being truncated, or files being unlinked. Note that we either
  2519. * hold i_mutex, or the inode can not be referenced from outside,
  2520. * so i_nlink should not be bumped due to race
  2521. */
  2522. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  2523. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  2524. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2525. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2526. if (err)
  2527. goto out;
  2528. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2529. if (err)
  2530. goto out;
  2531. mutex_lock(&sbi->s_orphan_lock);
  2532. /*
  2533. * Due to previous errors inode may be already a part of on-disk
  2534. * orphan list. If so skip on-disk list modification.
  2535. */
  2536. if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
  2537. (le32_to_cpu(sbi->s_es->s_inodes_count))) {
  2538. /* Insert this inode at the head of the on-disk orphan list */
  2539. NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
  2540. sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  2541. dirty = true;
  2542. }
  2543. list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
  2544. mutex_unlock(&sbi->s_orphan_lock);
  2545. if (dirty) {
  2546. err = ext4_handle_dirty_super(handle, sb);
  2547. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2548. if (!err)
  2549. err = rc;
  2550. if (err) {
  2551. /*
  2552. * We have to remove inode from in-memory list if
  2553. * addition to on disk orphan list failed. Stray orphan
  2554. * list entries can cause panics at unmount time.
  2555. */
  2556. mutex_lock(&sbi->s_orphan_lock);
  2557. list_del_init(&EXT4_I(inode)->i_orphan);
  2558. mutex_unlock(&sbi->s_orphan_lock);
  2559. }
  2560. }
  2561. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  2562. jbd_debug(4, "orphan inode %lu will point to %d\n",
  2563. inode->i_ino, NEXT_ORPHAN(inode));
  2564. out:
  2565. ext4_std_error(sb, err);
  2566. return err;
  2567. }
  2568. /*
  2569. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  2570. * of such inodes stored on disk, because it is finally being cleaned up.
  2571. */
  2572. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  2573. {
  2574. struct list_head *prev;
  2575. struct ext4_inode_info *ei = EXT4_I(inode);
  2576. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2577. __u32 ino_next;
  2578. struct ext4_iloc iloc;
  2579. int err = 0;
  2580. if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
  2581. return 0;
  2582. WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
  2583. !inode_is_locked(inode));
  2584. /* Do this quick check before taking global s_orphan_lock. */
  2585. if (list_empty(&ei->i_orphan))
  2586. return 0;
  2587. if (handle) {
  2588. /* Grab inode buffer early before taking global s_orphan_lock */
  2589. err = ext4_reserve_inode_write(handle, inode, &iloc);
  2590. }
  2591. mutex_lock(&sbi->s_orphan_lock);
  2592. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  2593. prev = ei->i_orphan.prev;
  2594. list_del_init(&ei->i_orphan);
  2595. /* If we're on an error path, we may not have a valid
  2596. * transaction handle with which to update the orphan list on
  2597. * disk, but we still need to remove the inode from the linked
  2598. * list in memory. */
  2599. if (!handle || err) {
  2600. mutex_unlock(&sbi->s_orphan_lock);
  2601. goto out_err;
  2602. }
  2603. ino_next = NEXT_ORPHAN(inode);
  2604. if (prev == &sbi->s_orphan) {
  2605. jbd_debug(4, "superblock will point to %u\n", ino_next);
  2606. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  2607. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  2608. if (err) {
  2609. mutex_unlock(&sbi->s_orphan_lock);
  2610. goto out_brelse;
  2611. }
  2612. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  2613. mutex_unlock(&sbi->s_orphan_lock);
  2614. err = ext4_handle_dirty_super(handle, inode->i_sb);
  2615. } else {
  2616. struct ext4_iloc iloc2;
  2617. struct inode *i_prev =
  2618. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  2619. jbd_debug(4, "orphan inode %lu will point to %u\n",
  2620. i_prev->i_ino, ino_next);
  2621. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  2622. if (err) {
  2623. mutex_unlock(&sbi->s_orphan_lock);
  2624. goto out_brelse;
  2625. }
  2626. NEXT_ORPHAN(i_prev) = ino_next;
  2627. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  2628. mutex_unlock(&sbi->s_orphan_lock);
  2629. }
  2630. if (err)
  2631. goto out_brelse;
  2632. NEXT_ORPHAN(inode) = 0;
  2633. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  2634. out_err:
  2635. ext4_std_error(inode->i_sb, err);
  2636. return err;
  2637. out_brelse:
  2638. brelse(iloc.bh);
  2639. goto out_err;
  2640. }
  2641. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2642. {
  2643. int retval;
  2644. struct inode *inode;
  2645. struct buffer_head *bh;
  2646. struct ext4_dir_entry_2 *de;
  2647. handle_t *handle = NULL;
  2648. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2649. return -EIO;
  2650. /* Initialize quotas before so that eventual writes go in
  2651. * separate transaction */
  2652. retval = dquot_initialize(dir);
  2653. if (retval)
  2654. return retval;
  2655. retval = dquot_initialize(d_inode(dentry));
  2656. if (retval)
  2657. return retval;
  2658. retval = -ENOENT;
  2659. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2660. if (IS_ERR(bh))
  2661. return PTR_ERR(bh);
  2662. if (!bh)
  2663. goto end_rmdir;
  2664. inode = d_inode(dentry);
  2665. retval = -EFSCORRUPTED;
  2666. if (le32_to_cpu(de->inode) != inode->i_ino)
  2667. goto end_rmdir;
  2668. retval = -ENOTEMPTY;
  2669. if (!ext4_empty_dir(inode))
  2670. goto end_rmdir;
  2671. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2672. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2673. if (IS_ERR(handle)) {
  2674. retval = PTR_ERR(handle);
  2675. handle = NULL;
  2676. goto end_rmdir;
  2677. }
  2678. if (IS_DIRSYNC(dir))
  2679. ext4_handle_sync(handle);
  2680. retval = ext4_delete_entry(handle, dir, de, bh);
  2681. if (retval)
  2682. goto end_rmdir;
  2683. if (!EXT4_DIR_LINK_EMPTY(inode))
  2684. ext4_warning_inode(inode,
  2685. "empty directory '%.*s' has too many links (%u)",
  2686. dentry->d_name.len, dentry->d_name.name,
  2687. inode->i_nlink);
  2688. inode->i_version++;
  2689. clear_nlink(inode);
  2690. /* There's no need to set i_disksize: the fact that i_nlink is
  2691. * zero will ensure that the right thing happens during any
  2692. * recovery. */
  2693. inode->i_size = 0;
  2694. ext4_orphan_add(handle, inode);
  2695. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  2696. ext4_mark_inode_dirty(handle, inode);
  2697. ext4_dec_count(handle, dir);
  2698. ext4_update_dx_flag(dir);
  2699. ext4_mark_inode_dirty(handle, dir);
  2700. end_rmdir:
  2701. brelse(bh);
  2702. if (handle)
  2703. ext4_journal_stop(handle);
  2704. return retval;
  2705. }
  2706. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2707. {
  2708. int retval;
  2709. struct inode *inode;
  2710. struct buffer_head *bh;
  2711. struct ext4_dir_entry_2 *de;
  2712. handle_t *handle = NULL;
  2713. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2714. return -EIO;
  2715. trace_ext4_unlink_enter(dir, dentry);
  2716. /* Initialize quotas before so that eventual writes go
  2717. * in separate transaction */
  2718. retval = dquot_initialize(dir);
  2719. if (retval)
  2720. return retval;
  2721. retval = dquot_initialize(d_inode(dentry));
  2722. if (retval)
  2723. return retval;
  2724. retval = -ENOENT;
  2725. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2726. if (IS_ERR(bh))
  2727. return PTR_ERR(bh);
  2728. if (!bh)
  2729. goto end_unlink;
  2730. inode = d_inode(dentry);
  2731. retval = -EFSCORRUPTED;
  2732. if (le32_to_cpu(de->inode) != inode->i_ino)
  2733. goto end_unlink;
  2734. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2735. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2736. if (IS_ERR(handle)) {
  2737. retval = PTR_ERR(handle);
  2738. handle = NULL;
  2739. goto end_unlink;
  2740. }
  2741. if (IS_DIRSYNC(dir))
  2742. ext4_handle_sync(handle);
  2743. if (inode->i_nlink == 0) {
  2744. ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
  2745. dentry->d_name.len, dentry->d_name.name);
  2746. set_nlink(inode, 1);
  2747. }
  2748. retval = ext4_delete_entry(handle, dir, de, bh);
  2749. if (retval)
  2750. goto end_unlink;
  2751. dir->i_ctime = dir->i_mtime = current_time(dir);
  2752. ext4_update_dx_flag(dir);
  2753. ext4_mark_inode_dirty(handle, dir);
  2754. drop_nlink(inode);
  2755. if (!inode->i_nlink)
  2756. ext4_orphan_add(handle, inode);
  2757. inode->i_ctime = current_time(inode);
  2758. ext4_mark_inode_dirty(handle, inode);
  2759. end_unlink:
  2760. brelse(bh);
  2761. if (handle)
  2762. ext4_journal_stop(handle);
  2763. trace_ext4_unlink_exit(dentry, retval);
  2764. return retval;
  2765. }
  2766. static int ext4_symlink(struct inode *dir,
  2767. struct dentry *dentry, const char *symname)
  2768. {
  2769. handle_t *handle;
  2770. struct inode *inode;
  2771. int err, len = strlen(symname);
  2772. int credits;
  2773. bool encryption_required;
  2774. struct fscrypt_str disk_link;
  2775. struct fscrypt_symlink_data *sd = NULL;
  2776. if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
  2777. return -EIO;
  2778. disk_link.len = len + 1;
  2779. disk_link.name = (char *) symname;
  2780. encryption_required = (ext4_encrypted_inode(dir) ||
  2781. DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
  2782. if (encryption_required) {
  2783. err = fscrypt_get_encryption_info(dir);
  2784. if (err)
  2785. return err;
  2786. if (!fscrypt_has_encryption_key(dir))
  2787. return -ENOKEY;
  2788. disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
  2789. sizeof(struct fscrypt_symlink_data));
  2790. sd = kzalloc(disk_link.len, GFP_KERNEL);
  2791. if (!sd)
  2792. return -ENOMEM;
  2793. }
  2794. if (disk_link.len > dir->i_sb->s_blocksize) {
  2795. err = -ENAMETOOLONG;
  2796. goto err_free_sd;
  2797. }
  2798. err = dquot_initialize(dir);
  2799. if (err)
  2800. goto err_free_sd;
  2801. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2802. /*
  2803. * For non-fast symlinks, we just allocate inode and put it on
  2804. * orphan list in the first transaction => we need bitmap,
  2805. * group descriptor, sb, inode block, quota blocks, and
  2806. * possibly selinux xattr blocks.
  2807. */
  2808. credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
  2809. EXT4_XATTR_TRANS_BLOCKS;
  2810. } else {
  2811. /*
  2812. * Fast symlink. We have to add entry to directory
  2813. * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
  2814. * allocate new inode (bitmap, group descriptor, inode block,
  2815. * quota blocks, sb is already counted in previous macros).
  2816. */
  2817. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2818. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  2819. }
  2820. inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO,
  2821. &dentry->d_name, 0, NULL,
  2822. EXT4_HT_DIR, credits);
  2823. handle = ext4_journal_current_handle();
  2824. if (IS_ERR(inode)) {
  2825. if (handle)
  2826. ext4_journal_stop(handle);
  2827. err = PTR_ERR(inode);
  2828. goto err_free_sd;
  2829. }
  2830. if (encryption_required) {
  2831. struct qstr istr;
  2832. struct fscrypt_str ostr =
  2833. FSTR_INIT(sd->encrypted_path, disk_link.len);
  2834. istr.name = (const unsigned char *) symname;
  2835. istr.len = len;
  2836. err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
  2837. if (err)
  2838. goto err_drop_inode;
  2839. sd->len = cpu_to_le16(ostr.len);
  2840. disk_link.name = (char *) sd;
  2841. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  2842. }
  2843. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  2844. if (!encryption_required)
  2845. inode->i_op = &ext4_symlink_inode_operations;
  2846. inode_nohighmem(inode);
  2847. ext4_set_aops(inode);
  2848. /*
  2849. * We cannot call page_symlink() with transaction started
  2850. * because it calls into ext4_write_begin() which can wait
  2851. * for transaction commit if we are running out of space
  2852. * and thus we deadlock. So we have to stop transaction now
  2853. * and restart it when symlink contents is written.
  2854. *
  2855. * To keep fs consistent in case of crash, we have to put inode
  2856. * to orphan list in the mean time.
  2857. */
  2858. drop_nlink(inode);
  2859. err = ext4_orphan_add(handle, inode);
  2860. ext4_journal_stop(handle);
  2861. handle = NULL;
  2862. if (err)
  2863. goto err_drop_inode;
  2864. err = __page_symlink(inode, disk_link.name, disk_link.len, 1);
  2865. if (err)
  2866. goto err_drop_inode;
  2867. /*
  2868. * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
  2869. * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
  2870. */
  2871. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2872. EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2873. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
  2874. if (IS_ERR(handle)) {
  2875. err = PTR_ERR(handle);
  2876. handle = NULL;
  2877. goto err_drop_inode;
  2878. }
  2879. set_nlink(inode, 1);
  2880. err = ext4_orphan_del(handle, inode);
  2881. if (err)
  2882. goto err_drop_inode;
  2883. } else {
  2884. /* clear the extent format for fast symlink */
  2885. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  2886. if (!encryption_required) {
  2887. inode->i_op = &ext4_fast_symlink_inode_operations;
  2888. inode->i_link = (char *)&EXT4_I(inode)->i_data;
  2889. }
  2890. memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
  2891. disk_link.len);
  2892. inode->i_size = disk_link.len - 1;
  2893. }
  2894. EXT4_I(inode)->i_disksize = inode->i_size;
  2895. err = ext4_add_nondir(handle, dentry, inode);
  2896. if (!err && IS_DIRSYNC(dir))
  2897. ext4_handle_sync(handle);
  2898. if (handle)
  2899. ext4_journal_stop(handle);
  2900. kfree(sd);
  2901. return err;
  2902. err_drop_inode:
  2903. if (handle)
  2904. ext4_journal_stop(handle);
  2905. clear_nlink(inode);
  2906. unlock_new_inode(inode);
  2907. iput(inode);
  2908. err_free_sd:
  2909. kfree(sd);
  2910. return err;
  2911. }
  2912. static int ext4_link(struct dentry *old_dentry,
  2913. struct inode *dir, struct dentry *dentry)
  2914. {
  2915. handle_t *handle;
  2916. struct inode *inode = d_inode(old_dentry);
  2917. int err, retries = 0;
  2918. if (inode->i_nlink >= EXT4_LINK_MAX)
  2919. return -EMLINK;
  2920. if (ext4_encrypted_inode(dir) &&
  2921. !fscrypt_has_permitted_context(dir, inode))
  2922. return -EPERM;
  2923. if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
  2924. (!projid_eq(EXT4_I(dir)->i_projid,
  2925. EXT4_I(old_dentry->d_inode)->i_projid)))
  2926. return -EXDEV;
  2927. err = dquot_initialize(dir);
  2928. if (err)
  2929. return err;
  2930. retry:
  2931. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2932. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2933. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  2934. if (IS_ERR(handle))
  2935. return PTR_ERR(handle);
  2936. if (IS_DIRSYNC(dir))
  2937. ext4_handle_sync(handle);
  2938. inode->i_ctime = current_time(inode);
  2939. ext4_inc_count(handle, inode);
  2940. ihold(inode);
  2941. err = ext4_add_entry(handle, dentry, inode);
  2942. if (!err) {
  2943. ext4_mark_inode_dirty(handle, inode);
  2944. /* this can happen only for tmpfile being
  2945. * linked the first time
  2946. */
  2947. if (inode->i_nlink == 1)
  2948. ext4_orphan_del(handle, inode);
  2949. d_instantiate(dentry, inode);
  2950. } else {
  2951. drop_nlink(inode);
  2952. iput(inode);
  2953. }
  2954. ext4_journal_stop(handle);
  2955. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2956. goto retry;
  2957. return err;
  2958. }
  2959. /*
  2960. * Try to find buffer head where contains the parent block.
  2961. * It should be the inode block if it is inlined or the 1st block
  2962. * if it is a normal dir.
  2963. */
  2964. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  2965. struct inode *inode,
  2966. int *retval,
  2967. struct ext4_dir_entry_2 **parent_de,
  2968. int *inlined)
  2969. {
  2970. struct buffer_head *bh;
  2971. if (!ext4_has_inline_data(inode)) {
  2972. bh = ext4_read_dirblock(inode, 0, EITHER);
  2973. if (IS_ERR(bh)) {
  2974. *retval = PTR_ERR(bh);
  2975. return NULL;
  2976. }
  2977. *parent_de = ext4_next_entry(
  2978. (struct ext4_dir_entry_2 *)bh->b_data,
  2979. inode->i_sb->s_blocksize);
  2980. return bh;
  2981. }
  2982. *inlined = 1;
  2983. return ext4_get_first_inline_block(inode, parent_de, retval);
  2984. }
  2985. struct ext4_renament {
  2986. struct inode *dir;
  2987. struct dentry *dentry;
  2988. struct inode *inode;
  2989. bool is_dir;
  2990. int dir_nlink_delta;
  2991. /* entry for "dentry" */
  2992. struct buffer_head *bh;
  2993. struct ext4_dir_entry_2 *de;
  2994. int inlined;
  2995. /* entry for ".." in inode if it's a directory */
  2996. struct buffer_head *dir_bh;
  2997. struct ext4_dir_entry_2 *parent_de;
  2998. int dir_inlined;
  2999. };
  3000. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent)
  3001. {
  3002. int retval;
  3003. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  3004. &retval, &ent->parent_de,
  3005. &ent->dir_inlined);
  3006. if (!ent->dir_bh)
  3007. return retval;
  3008. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  3009. return -EFSCORRUPTED;
  3010. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  3011. return ext4_journal_get_write_access(handle, ent->dir_bh);
  3012. }
  3013. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  3014. unsigned dir_ino)
  3015. {
  3016. int retval;
  3017. ent->parent_de->inode = cpu_to_le32(dir_ino);
  3018. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  3019. if (!ent->dir_inlined) {
  3020. if (is_dx(ent->inode)) {
  3021. retval = ext4_handle_dirty_dx_node(handle,
  3022. ent->inode,
  3023. ent->dir_bh);
  3024. } else {
  3025. retval = ext4_handle_dirty_dirent_node(handle,
  3026. ent->inode,
  3027. ent->dir_bh);
  3028. }
  3029. } else {
  3030. retval = ext4_mark_inode_dirty(handle, ent->inode);
  3031. }
  3032. if (retval) {
  3033. ext4_std_error(ent->dir->i_sb, retval);
  3034. return retval;
  3035. }
  3036. return 0;
  3037. }
  3038. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  3039. unsigned ino, unsigned file_type)
  3040. {
  3041. int retval;
  3042. BUFFER_TRACE(ent->bh, "get write access");
  3043. retval = ext4_journal_get_write_access(handle, ent->bh);
  3044. if (retval)
  3045. return retval;
  3046. ent->de->inode = cpu_to_le32(ino);
  3047. if (ext4_has_feature_filetype(ent->dir->i_sb))
  3048. ent->de->file_type = file_type;
  3049. ent->dir->i_version++;
  3050. ent->dir->i_ctime = ent->dir->i_mtime =
  3051. current_time(ent->dir);
  3052. ext4_mark_inode_dirty(handle, ent->dir);
  3053. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  3054. if (!ent->inlined) {
  3055. retval = ext4_handle_dirty_dirent_node(handle,
  3056. ent->dir, ent->bh);
  3057. if (unlikely(retval)) {
  3058. ext4_std_error(ent->dir->i_sb, retval);
  3059. return retval;
  3060. }
  3061. }
  3062. brelse(ent->bh);
  3063. ent->bh = NULL;
  3064. return 0;
  3065. }
  3066. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  3067. const struct qstr *d_name)
  3068. {
  3069. int retval = -ENOENT;
  3070. struct buffer_head *bh;
  3071. struct ext4_dir_entry_2 *de;
  3072. bh = ext4_find_entry(dir, d_name, &de, NULL);
  3073. if (IS_ERR(bh))
  3074. return PTR_ERR(bh);
  3075. if (bh) {
  3076. retval = ext4_delete_entry(handle, dir, de, bh);
  3077. brelse(bh);
  3078. }
  3079. return retval;
  3080. }
  3081. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
  3082. int force_reread)
  3083. {
  3084. int retval;
  3085. /*
  3086. * ent->de could have moved from under us during htree split, so make
  3087. * sure that we are deleting the right entry. We might also be pointing
  3088. * to a stale entry in the unused part of ent->bh so just checking inum
  3089. * and the name isn't enough.
  3090. */
  3091. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  3092. ent->de->name_len != ent->dentry->d_name.len ||
  3093. strncmp(ent->de->name, ent->dentry->d_name.name,
  3094. ent->de->name_len) ||
  3095. force_reread) {
  3096. retval = ext4_find_delete_entry(handle, ent->dir,
  3097. &ent->dentry->d_name);
  3098. } else {
  3099. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  3100. if (retval == -ENOENT) {
  3101. retval = ext4_find_delete_entry(handle, ent->dir,
  3102. &ent->dentry->d_name);
  3103. }
  3104. }
  3105. if (retval) {
  3106. ext4_warning_inode(ent->dir,
  3107. "Deleting old file: nlink %d, error=%d",
  3108. ent->dir->i_nlink, retval);
  3109. }
  3110. }
  3111. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  3112. {
  3113. if (ent->dir_nlink_delta) {
  3114. if (ent->dir_nlink_delta == -1)
  3115. ext4_dec_count(handle, ent->dir);
  3116. else
  3117. ext4_inc_count(handle, ent->dir);
  3118. ext4_mark_inode_dirty(handle, ent->dir);
  3119. }
  3120. }
  3121. static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent,
  3122. int credits, handle_t **h)
  3123. {
  3124. struct inode *wh;
  3125. handle_t *handle;
  3126. int retries = 0;
  3127. /*
  3128. * for inode block, sb block, group summaries,
  3129. * and inode bitmap
  3130. */
  3131. credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
  3132. EXT4_XATTR_TRANS_BLOCKS + 4);
  3133. retry:
  3134. wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE,
  3135. &ent->dentry->d_name, 0, NULL,
  3136. EXT4_HT_DIR, credits);
  3137. handle = ext4_journal_current_handle();
  3138. if (IS_ERR(wh)) {
  3139. if (handle)
  3140. ext4_journal_stop(handle);
  3141. if (PTR_ERR(wh) == -ENOSPC &&
  3142. ext4_should_retry_alloc(ent->dir->i_sb, &retries))
  3143. goto retry;
  3144. } else {
  3145. *h = handle;
  3146. init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
  3147. wh->i_op = &ext4_special_inode_operations;
  3148. }
  3149. return wh;
  3150. }
  3151. /*
  3152. * Anybody can rename anything with this: the permission checks are left to the
  3153. * higher-level routines.
  3154. *
  3155. * n.b. old_{dentry,inode) refers to the source dentry/inode
  3156. * while new_{dentry,inode) refers to the destination dentry/inode
  3157. * This comes from rename(const char *oldpath, const char *newpath)
  3158. */
  3159. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  3160. struct inode *new_dir, struct dentry *new_dentry,
  3161. unsigned int flags)
  3162. {
  3163. handle_t *handle = NULL;
  3164. struct ext4_renament old = {
  3165. .dir = old_dir,
  3166. .dentry = old_dentry,
  3167. .inode = d_inode(old_dentry),
  3168. };
  3169. struct ext4_renament new = {
  3170. .dir = new_dir,
  3171. .dentry = new_dentry,
  3172. .inode = d_inode(new_dentry),
  3173. };
  3174. int force_reread;
  3175. int retval;
  3176. struct inode *whiteout = NULL;
  3177. int credits;
  3178. u8 old_file_type;
  3179. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
  3180. (!projid_eq(EXT4_I(new_dir)->i_projid,
  3181. EXT4_I(old_dentry->d_inode)->i_projid)))
  3182. return -EXDEV;
  3183. if ((ext4_encrypted_inode(old_dir) &&
  3184. !fscrypt_has_encryption_key(old_dir)) ||
  3185. (ext4_encrypted_inode(new_dir) &&
  3186. !fscrypt_has_encryption_key(new_dir)))
  3187. return -ENOKEY;
  3188. retval = dquot_initialize(old.dir);
  3189. if (retval)
  3190. return retval;
  3191. retval = dquot_initialize(new.dir);
  3192. if (retval)
  3193. return retval;
  3194. /* Initialize quotas before so that eventual writes go
  3195. * in separate transaction */
  3196. if (new.inode) {
  3197. retval = dquot_initialize(new.inode);
  3198. if (retval)
  3199. return retval;
  3200. }
  3201. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL);
  3202. if (IS_ERR(old.bh))
  3203. return PTR_ERR(old.bh);
  3204. /*
  3205. * Check for inode number is _not_ due to possible IO errors.
  3206. * We might rmdir the source, keep it as pwd of some process
  3207. * and merrily kill the link to whatever was created under the
  3208. * same name. Goodbye sticky bit ;-<
  3209. */
  3210. retval = -ENOENT;
  3211. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3212. goto end_rename;
  3213. if ((old.dir != new.dir) &&
  3214. ext4_encrypted_inode(new.dir) &&
  3215. !fscrypt_has_permitted_context(new.dir, old.inode)) {
  3216. retval = -EPERM;
  3217. goto end_rename;
  3218. }
  3219. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3220. &new.de, &new.inlined);
  3221. if (IS_ERR(new.bh)) {
  3222. retval = PTR_ERR(new.bh);
  3223. new.bh = NULL;
  3224. goto end_rename;
  3225. }
  3226. if (new.bh) {
  3227. if (!new.inode) {
  3228. brelse(new.bh);
  3229. new.bh = NULL;
  3230. }
  3231. }
  3232. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  3233. ext4_alloc_da_blocks(old.inode);
  3234. credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3235. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  3236. if (!(flags & RENAME_WHITEOUT)) {
  3237. handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
  3238. if (IS_ERR(handle)) {
  3239. retval = PTR_ERR(handle);
  3240. handle = NULL;
  3241. goto end_rename;
  3242. }
  3243. } else {
  3244. whiteout = ext4_whiteout_for_rename(&old, credits, &handle);
  3245. if (IS_ERR(whiteout)) {
  3246. retval = PTR_ERR(whiteout);
  3247. whiteout = NULL;
  3248. goto end_rename;
  3249. }
  3250. }
  3251. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3252. ext4_handle_sync(handle);
  3253. if (S_ISDIR(old.inode->i_mode)) {
  3254. if (new.inode) {
  3255. retval = -ENOTEMPTY;
  3256. if (!ext4_empty_dir(new.inode))
  3257. goto end_rename;
  3258. } else {
  3259. retval = -EMLINK;
  3260. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  3261. goto end_rename;
  3262. }
  3263. retval = ext4_rename_dir_prepare(handle, &old);
  3264. if (retval)
  3265. goto end_rename;
  3266. }
  3267. /*
  3268. * If we're renaming a file within an inline_data dir and adding or
  3269. * setting the new dirent causes a conversion from inline_data to
  3270. * extents/blockmap, we need to force the dirent delete code to
  3271. * re-read the directory, or else we end up trying to delete a dirent
  3272. * from what is now the extent tree root (or a block map).
  3273. */
  3274. force_reread = (new.dir->i_ino == old.dir->i_ino &&
  3275. ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
  3276. old_file_type = old.de->file_type;
  3277. if (whiteout) {
  3278. /*
  3279. * Do this before adding a new entry, so the old entry is sure
  3280. * to be still pointing to the valid old entry.
  3281. */
  3282. retval = ext4_setent(handle, &old, whiteout->i_ino,
  3283. EXT4_FT_CHRDEV);
  3284. if (retval)
  3285. goto end_rename;
  3286. ext4_mark_inode_dirty(handle, whiteout);
  3287. }
  3288. if (!new.bh) {
  3289. retval = ext4_add_entry(handle, new.dentry, old.inode);
  3290. if (retval)
  3291. goto end_rename;
  3292. } else {
  3293. retval = ext4_setent(handle, &new,
  3294. old.inode->i_ino, old_file_type);
  3295. if (retval)
  3296. goto end_rename;
  3297. }
  3298. if (force_reread)
  3299. force_reread = !ext4_test_inode_flag(new.dir,
  3300. EXT4_INODE_INLINE_DATA);
  3301. /*
  3302. * Like most other Unix systems, set the ctime for inodes on a
  3303. * rename.
  3304. */
  3305. old.inode->i_ctime = current_time(old.inode);
  3306. ext4_mark_inode_dirty(handle, old.inode);
  3307. if (!whiteout) {
  3308. /*
  3309. * ok, that's it
  3310. */
  3311. ext4_rename_delete(handle, &old, force_reread);
  3312. }
  3313. if (new.inode) {
  3314. ext4_dec_count(handle, new.inode);
  3315. new.inode->i_ctime = current_time(new.inode);
  3316. }
  3317. old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir);
  3318. ext4_update_dx_flag(old.dir);
  3319. if (old.dir_bh) {
  3320. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3321. if (retval)
  3322. goto end_rename;
  3323. ext4_dec_count(handle, old.dir);
  3324. if (new.inode) {
  3325. /* checked ext4_empty_dir above, can't have another
  3326. * parent, ext4_dec_count() won't work for many-linked
  3327. * dirs */
  3328. clear_nlink(new.inode);
  3329. } else {
  3330. ext4_inc_count(handle, new.dir);
  3331. ext4_update_dx_flag(new.dir);
  3332. ext4_mark_inode_dirty(handle, new.dir);
  3333. }
  3334. }
  3335. ext4_mark_inode_dirty(handle, old.dir);
  3336. if (new.inode) {
  3337. ext4_mark_inode_dirty(handle, new.inode);
  3338. if (!new.inode->i_nlink)
  3339. ext4_orphan_add(handle, new.inode);
  3340. }
  3341. retval = 0;
  3342. end_rename:
  3343. brelse(old.dir_bh);
  3344. brelse(old.bh);
  3345. brelse(new.bh);
  3346. if (whiteout) {
  3347. if (retval)
  3348. drop_nlink(whiteout);
  3349. unlock_new_inode(whiteout);
  3350. iput(whiteout);
  3351. }
  3352. if (handle)
  3353. ext4_journal_stop(handle);
  3354. return retval;
  3355. }
  3356. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  3357. struct inode *new_dir, struct dentry *new_dentry)
  3358. {
  3359. handle_t *handle = NULL;
  3360. struct ext4_renament old = {
  3361. .dir = old_dir,
  3362. .dentry = old_dentry,
  3363. .inode = d_inode(old_dentry),
  3364. };
  3365. struct ext4_renament new = {
  3366. .dir = new_dir,
  3367. .dentry = new_dentry,
  3368. .inode = d_inode(new_dentry),
  3369. };
  3370. u8 new_file_type;
  3371. int retval;
  3372. struct timespec ctime;
  3373. if ((ext4_encrypted_inode(old_dir) &&
  3374. !fscrypt_has_encryption_key(old_dir)) ||
  3375. (ext4_encrypted_inode(new_dir) &&
  3376. !fscrypt_has_encryption_key(new_dir)))
  3377. return -ENOKEY;
  3378. if ((ext4_encrypted_inode(old_dir) ||
  3379. ext4_encrypted_inode(new_dir)) &&
  3380. (old_dir != new_dir) &&
  3381. (!fscrypt_has_permitted_context(new_dir, old.inode) ||
  3382. !fscrypt_has_permitted_context(old_dir, new.inode)))
  3383. return -EPERM;
  3384. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
  3385. !projid_eq(EXT4_I(new_dir)->i_projid,
  3386. EXT4_I(old_dentry->d_inode)->i_projid)) ||
  3387. (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
  3388. !projid_eq(EXT4_I(old_dir)->i_projid,
  3389. EXT4_I(new_dentry->d_inode)->i_projid)))
  3390. return -EXDEV;
  3391. retval = dquot_initialize(old.dir);
  3392. if (retval)
  3393. return retval;
  3394. retval = dquot_initialize(new.dir);
  3395. if (retval)
  3396. return retval;
  3397. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  3398. &old.de, &old.inlined);
  3399. if (IS_ERR(old.bh))
  3400. return PTR_ERR(old.bh);
  3401. /*
  3402. * Check for inode number is _not_ due to possible IO errors.
  3403. * We might rmdir the source, keep it as pwd of some process
  3404. * and merrily kill the link to whatever was created under the
  3405. * same name. Goodbye sticky bit ;-<
  3406. */
  3407. retval = -ENOENT;
  3408. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3409. goto end_rename;
  3410. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3411. &new.de, &new.inlined);
  3412. if (IS_ERR(new.bh)) {
  3413. retval = PTR_ERR(new.bh);
  3414. new.bh = NULL;
  3415. goto end_rename;
  3416. }
  3417. /* RENAME_EXCHANGE case: old *and* new must both exist */
  3418. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  3419. goto end_rename;
  3420. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3421. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3422. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3423. if (IS_ERR(handle)) {
  3424. retval = PTR_ERR(handle);
  3425. handle = NULL;
  3426. goto end_rename;
  3427. }
  3428. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3429. ext4_handle_sync(handle);
  3430. if (S_ISDIR(old.inode->i_mode)) {
  3431. old.is_dir = true;
  3432. retval = ext4_rename_dir_prepare(handle, &old);
  3433. if (retval)
  3434. goto end_rename;
  3435. }
  3436. if (S_ISDIR(new.inode->i_mode)) {
  3437. new.is_dir = true;
  3438. retval = ext4_rename_dir_prepare(handle, &new);
  3439. if (retval)
  3440. goto end_rename;
  3441. }
  3442. /*
  3443. * Other than the special case of overwriting a directory, parents'
  3444. * nlink only needs to be modified if this is a cross directory rename.
  3445. */
  3446. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3447. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3448. new.dir_nlink_delta = -old.dir_nlink_delta;
  3449. retval = -EMLINK;
  3450. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3451. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3452. goto end_rename;
  3453. }
  3454. new_file_type = new.de->file_type;
  3455. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3456. if (retval)
  3457. goto end_rename;
  3458. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3459. if (retval)
  3460. goto end_rename;
  3461. /*
  3462. * Like most other Unix systems, set the ctime for inodes on a
  3463. * rename.
  3464. */
  3465. ctime = current_time(old.inode);
  3466. old.inode->i_ctime = ctime;
  3467. new.inode->i_ctime = ctime;
  3468. ext4_mark_inode_dirty(handle, old.inode);
  3469. ext4_mark_inode_dirty(handle, new.inode);
  3470. if (old.dir_bh) {
  3471. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3472. if (retval)
  3473. goto end_rename;
  3474. }
  3475. if (new.dir_bh) {
  3476. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3477. if (retval)
  3478. goto end_rename;
  3479. }
  3480. ext4_update_dir_count(handle, &old);
  3481. ext4_update_dir_count(handle, &new);
  3482. retval = 0;
  3483. end_rename:
  3484. brelse(old.dir_bh);
  3485. brelse(new.dir_bh);
  3486. brelse(old.bh);
  3487. brelse(new.bh);
  3488. if (handle)
  3489. ext4_journal_stop(handle);
  3490. return retval;
  3491. }
  3492. static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
  3493. struct inode *new_dir, struct dentry *new_dentry,
  3494. unsigned int flags)
  3495. {
  3496. if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb))))
  3497. return -EIO;
  3498. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3499. return -EINVAL;
  3500. if (flags & RENAME_EXCHANGE) {
  3501. return ext4_cross_rename(old_dir, old_dentry,
  3502. new_dir, new_dentry);
  3503. }
  3504. return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  3505. }
  3506. /*
  3507. * directories can handle most operations...
  3508. */
  3509. const struct inode_operations ext4_dir_inode_operations = {
  3510. .create = ext4_create,
  3511. .lookup = ext4_lookup,
  3512. .link = ext4_link,
  3513. .unlink = ext4_unlink,
  3514. .symlink = ext4_symlink,
  3515. .mkdir = ext4_mkdir,
  3516. .rmdir = ext4_rmdir,
  3517. .mknod = ext4_mknod,
  3518. .tmpfile = ext4_tmpfile,
  3519. .rename = ext4_rename2,
  3520. .setattr = ext4_setattr,
  3521. .getattr = ext4_getattr,
  3522. .listxattr = ext4_listxattr,
  3523. .get_acl = ext4_get_acl,
  3524. .set_acl = ext4_set_acl,
  3525. .fiemap = ext4_fiemap,
  3526. };
  3527. const struct inode_operations ext4_special_inode_operations = {
  3528. .setattr = ext4_setattr,
  3529. .getattr = ext4_getattr,
  3530. .listxattr = ext4_listxattr,
  3531. .get_acl = ext4_get_acl,
  3532. .set_acl = ext4_set_acl,
  3533. };