namei.c 86 KB

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