namei.c 86 KB

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