namei.c 86 KB

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