namei.c 94 KB

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