namei.c 103 KB

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