namei.c 84 KB

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