namei.c 86 KB

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