namei.c 102 KB

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