namei.c 104 KB

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