namei.c 102 KB

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