namei.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  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 "namei.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. /*
  42. * define how far ahead to read directories while searching them.
  43. */
  44. #define NAMEI_RA_CHUNKS 2
  45. #define NAMEI_RA_BLOCKS 4
  46. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  47. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block, int *err)
  51. {
  52. struct buffer_head *bh;
  53. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  54. bh = ext4_bread(handle, inode, *block, 1, err);
  55. if (bh) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT4_I(inode)->i_disksize = inode->i_size;
  58. *err = ext4_journal_get_write_access(handle, bh);
  59. if (*err) {
  60. brelse(bh);
  61. bh = NULL;
  62. }
  63. }
  64. return bh;
  65. }
  66. #ifndef assert
  67. #define assert(test) J_ASSERT(test)
  68. #endif
  69. #ifdef DX_DEBUG
  70. #define dxtrace(command) command
  71. #else
  72. #define dxtrace(command)
  73. #endif
  74. struct fake_dirent
  75. {
  76. __le32 inode;
  77. __le16 rec_len;
  78. u8 name_len;
  79. u8 file_type;
  80. };
  81. struct dx_countlimit
  82. {
  83. __le16 limit;
  84. __le16 count;
  85. };
  86. struct dx_entry
  87. {
  88. __le32 hash;
  89. __le32 block;
  90. };
  91. /*
  92. * dx_root_info is laid out so that if it should somehow get overlaid by a
  93. * dirent the two low bits of the hash version will be zero. Therefore, the
  94. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  95. */
  96. struct dx_root
  97. {
  98. struct fake_dirent dot;
  99. char dot_name[4];
  100. struct fake_dirent dotdot;
  101. char dotdot_name[4];
  102. struct dx_root_info
  103. {
  104. __le32 reserved_zero;
  105. u8 hash_version;
  106. u8 info_length; /* 8 */
  107. u8 indirect_levels;
  108. u8 unused_flags;
  109. }
  110. info;
  111. struct dx_entry entries[0];
  112. };
  113. struct dx_node
  114. {
  115. struct fake_dirent fake;
  116. struct dx_entry entries[0];
  117. };
  118. struct dx_frame
  119. {
  120. struct buffer_head *bh;
  121. struct dx_entry *entries;
  122. struct dx_entry *at;
  123. };
  124. struct dx_map_entry
  125. {
  126. u32 hash;
  127. u16 offs;
  128. u16 size;
  129. };
  130. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  131. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  132. static inline unsigned dx_get_hash(struct dx_entry *entry);
  133. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  134. static unsigned dx_get_count(struct dx_entry *entries);
  135. static unsigned dx_get_limit(struct dx_entry *entries);
  136. static void dx_set_count(struct dx_entry *entries, unsigned value);
  137. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  138. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  139. static unsigned dx_node_limit(struct inode *dir);
  140. static struct dx_frame *dx_probe(const struct qstr *d_name,
  141. struct inode *dir,
  142. struct dx_hash_info *hinfo,
  143. struct dx_frame *frame,
  144. int *err);
  145. static void dx_release(struct dx_frame *frames);
  146. static int dx_make_map(struct ext4_dir_entry_2 *de, int size,
  147. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  148. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  149. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  150. struct dx_map_entry *offsets, int count);
  151. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size);
  152. static void dx_insert_block(struct dx_frame *frame,
  153. u32 hash, ext4_lblk_t block);
  154. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  155. struct dx_frame *frame,
  156. struct dx_frame *frames,
  157. __u32 *start_hash);
  158. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  159. const struct qstr *d_name,
  160. struct ext4_dir_entry_2 **res_dir,
  161. int *err);
  162. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  163. struct inode *inode);
  164. /*
  165. * p is at least 6 bytes before the end of page
  166. */
  167. static inline struct ext4_dir_entry_2 *
  168. ext4_next_entry(struct ext4_dir_entry_2 *p)
  169. {
  170. return (struct ext4_dir_entry_2 *)((char *)p +
  171. ext4_rec_len_from_disk(p->rec_len));
  172. }
  173. /*
  174. * Future: use high four bits of block for coalesce-on-delete flags
  175. * Mask them off for now.
  176. */
  177. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  178. {
  179. return le32_to_cpu(entry->block) & 0x00ffffff;
  180. }
  181. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  182. {
  183. entry->block = cpu_to_le32(value);
  184. }
  185. static inline unsigned dx_get_hash(struct dx_entry *entry)
  186. {
  187. return le32_to_cpu(entry->hash);
  188. }
  189. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  190. {
  191. entry->hash = cpu_to_le32(value);
  192. }
  193. static inline unsigned dx_get_count(struct dx_entry *entries)
  194. {
  195. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  196. }
  197. static inline unsigned dx_get_limit(struct dx_entry *entries)
  198. {
  199. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  200. }
  201. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  202. {
  203. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  204. }
  205. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  206. {
  207. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  208. }
  209. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  210. {
  211. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  212. EXT4_DIR_REC_LEN(2) - infosize;
  213. return entry_space / sizeof(struct dx_entry);
  214. }
  215. static inline unsigned dx_node_limit(struct inode *dir)
  216. {
  217. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  218. return entry_space / sizeof(struct dx_entry);
  219. }
  220. /*
  221. * Debug
  222. */
  223. #ifdef DX_DEBUG
  224. static void dx_show_index(char * label, struct dx_entry *entries)
  225. {
  226. int i, n = dx_get_count (entries);
  227. printk(KERN_DEBUG "%s index ", label);
  228. for (i = 0; i < n; i++) {
  229. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  230. 0, (unsigned long)dx_get_block(entries + i));
  231. }
  232. printk("\n");
  233. }
  234. struct stats
  235. {
  236. unsigned names;
  237. unsigned space;
  238. unsigned bcount;
  239. };
  240. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  241. int size, int show_names)
  242. {
  243. unsigned names = 0, space = 0;
  244. char *base = (char *) de;
  245. struct dx_hash_info h = *hinfo;
  246. printk("names: ");
  247. while ((char *) de < base + size)
  248. {
  249. if (de->inode)
  250. {
  251. if (show_names)
  252. {
  253. int len = de->name_len;
  254. char *name = de->name;
  255. while (len--) printk("%c", *name++);
  256. ext4fs_dirhash(de->name, de->name_len, &h);
  257. printk(":%x.%u ", h.hash,
  258. ((char *) de - base));
  259. }
  260. space += EXT4_DIR_REC_LEN(de->name_len);
  261. names++;
  262. }
  263. de = ext4_next_entry(de);
  264. }
  265. printk("(%i)\n", names);
  266. return (struct stats) { names, space, 1 };
  267. }
  268. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  269. struct dx_entry *entries, int levels)
  270. {
  271. unsigned blocksize = dir->i_sb->s_blocksize;
  272. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  273. unsigned bcount = 0;
  274. struct buffer_head *bh;
  275. int err;
  276. printk("%i indexed blocks...\n", count);
  277. for (i = 0; i < count; i++, entries++)
  278. {
  279. ext4_lblk_t block = dx_get_block(entries);
  280. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  281. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  282. struct stats stats;
  283. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  284. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  285. stats = levels?
  286. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  287. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  288. names += stats.names;
  289. space += stats.space;
  290. bcount += stats.bcount;
  291. brelse(bh);
  292. }
  293. if (bcount)
  294. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  295. levels ? "" : " ", names, space/bcount,
  296. (space/bcount)*100/blocksize);
  297. return (struct stats) { names, space, bcount};
  298. }
  299. #endif /* DX_DEBUG */
  300. /*
  301. * Probe for a directory leaf block to search.
  302. *
  303. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  304. * error in the directory index, and the caller should fall back to
  305. * searching the directory normally. The callers of dx_probe **MUST**
  306. * check for this error code, and make sure it never gets reflected
  307. * back to userspace.
  308. */
  309. static struct dx_frame *
  310. dx_probe(const struct qstr *d_name, struct inode *dir,
  311. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  312. {
  313. unsigned count, indirect;
  314. struct dx_entry *at, *entries, *p, *q, *m;
  315. struct dx_root *root;
  316. struct buffer_head *bh;
  317. struct dx_frame *frame = frame_in;
  318. u32 hash;
  319. frame->bh = NULL;
  320. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  321. goto fail;
  322. root = (struct dx_root *) bh->b_data;
  323. if (root->info.hash_version != DX_HASH_TEA &&
  324. root->info.hash_version != DX_HASH_HALF_MD4 &&
  325. root->info.hash_version != DX_HASH_LEGACY) {
  326. ext4_warning(dir->i_sb, __func__,
  327. "Unrecognised inode hash code %d",
  328. root->info.hash_version);
  329. brelse(bh);
  330. *err = ERR_BAD_DX_DIR;
  331. goto fail;
  332. }
  333. hinfo->hash_version = root->info.hash_version;
  334. if (hinfo->hash_version <= DX_HASH_TEA)
  335. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  336. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  337. if (d_name)
  338. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  339. hash = hinfo->hash;
  340. if (root->info.unused_flags & 1) {
  341. ext4_warning(dir->i_sb, __func__,
  342. "Unimplemented inode hash flags: %#06x",
  343. root->info.unused_flags);
  344. brelse(bh);
  345. *err = ERR_BAD_DX_DIR;
  346. goto fail;
  347. }
  348. if ((indirect = root->info.indirect_levels) > 1) {
  349. ext4_warning(dir->i_sb, __func__,
  350. "Unimplemented inode hash depth: %#06x",
  351. root->info.indirect_levels);
  352. brelse(bh);
  353. *err = ERR_BAD_DX_DIR;
  354. goto fail;
  355. }
  356. entries = (struct dx_entry *) (((char *)&root->info) +
  357. root->info.info_length);
  358. if (dx_get_limit(entries) != dx_root_limit(dir,
  359. root->info.info_length)) {
  360. ext4_warning(dir->i_sb, __func__,
  361. "dx entry: limit != root limit");
  362. brelse(bh);
  363. *err = ERR_BAD_DX_DIR;
  364. goto fail;
  365. }
  366. dxtrace(printk("Look up %x", hash));
  367. while (1)
  368. {
  369. count = dx_get_count(entries);
  370. if (!count || count > dx_get_limit(entries)) {
  371. ext4_warning(dir->i_sb, __func__,
  372. "dx entry: no count or count > limit");
  373. brelse(bh);
  374. *err = ERR_BAD_DX_DIR;
  375. goto fail2;
  376. }
  377. p = entries + 1;
  378. q = entries + count - 1;
  379. while (p <= q)
  380. {
  381. m = p + (q - p)/2;
  382. dxtrace(printk("."));
  383. if (dx_get_hash(m) > hash)
  384. q = m - 1;
  385. else
  386. p = m + 1;
  387. }
  388. if (0) // linear search cross check
  389. {
  390. unsigned n = count - 1;
  391. at = entries;
  392. while (n--)
  393. {
  394. dxtrace(printk(","));
  395. if (dx_get_hash(++at) > hash)
  396. {
  397. at--;
  398. break;
  399. }
  400. }
  401. assert (at == p - 1);
  402. }
  403. at = p - 1;
  404. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  405. frame->bh = bh;
  406. frame->entries = entries;
  407. frame->at = at;
  408. if (!indirect--) return frame;
  409. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  410. goto fail2;
  411. at = entries = ((struct dx_node *) bh->b_data)->entries;
  412. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  413. ext4_warning(dir->i_sb, __func__,
  414. "dx entry: limit != node limit");
  415. brelse(bh);
  416. *err = ERR_BAD_DX_DIR;
  417. goto fail2;
  418. }
  419. frame++;
  420. frame->bh = NULL;
  421. }
  422. fail2:
  423. while (frame >= frame_in) {
  424. brelse(frame->bh);
  425. frame--;
  426. }
  427. fail:
  428. if (*err == ERR_BAD_DX_DIR)
  429. ext4_warning(dir->i_sb, __func__,
  430. "Corrupt dir inode %ld, running e2fsck is "
  431. "recommended.", dir->i_ino);
  432. return NULL;
  433. }
  434. static void dx_release (struct dx_frame *frames)
  435. {
  436. if (frames[0].bh == NULL)
  437. return;
  438. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  439. brelse(frames[1].bh);
  440. brelse(frames[0].bh);
  441. }
  442. /*
  443. * This function increments the frame pointer to search the next leaf
  444. * block, and reads in the necessary intervening nodes if the search
  445. * should be necessary. Whether or not the search is necessary is
  446. * controlled by the hash parameter. If the hash value is even, then
  447. * the search is only continued if the next block starts with that
  448. * hash value. This is used if we are searching for a specific file.
  449. *
  450. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  451. *
  452. * This function returns 1 if the caller should continue to search,
  453. * or 0 if it should not. If there is an error reading one of the
  454. * index blocks, it will a negative error code.
  455. *
  456. * If start_hash is non-null, it will be filled in with the starting
  457. * hash of the next page.
  458. */
  459. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  460. struct dx_frame *frame,
  461. struct dx_frame *frames,
  462. __u32 *start_hash)
  463. {
  464. struct dx_frame *p;
  465. struct buffer_head *bh;
  466. int err, num_frames = 0;
  467. __u32 bhash;
  468. p = frame;
  469. /*
  470. * Find the next leaf page by incrementing the frame pointer.
  471. * If we run out of entries in the interior node, loop around and
  472. * increment pointer in the parent node. When we break out of
  473. * this loop, num_frames indicates the number of interior
  474. * nodes need to be read.
  475. */
  476. while (1) {
  477. if (++(p->at) < p->entries + dx_get_count(p->entries))
  478. break;
  479. if (p == frames)
  480. return 0;
  481. num_frames++;
  482. p--;
  483. }
  484. /*
  485. * If the hash is 1, then continue only if the next page has a
  486. * continuation hash of any value. This is used for readdir
  487. * handling. Otherwise, check to see if the hash matches the
  488. * desired contiuation hash. If it doesn't, return since
  489. * there's no point to read in the successive index pages.
  490. */
  491. bhash = dx_get_hash(p->at);
  492. if (start_hash)
  493. *start_hash = bhash;
  494. if ((hash & 1) == 0) {
  495. if ((bhash & ~1) != hash)
  496. return 0;
  497. }
  498. /*
  499. * If the hash is HASH_NB_ALWAYS, we always go to the next
  500. * block so no check is necessary
  501. */
  502. while (num_frames--) {
  503. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  504. 0, &err)))
  505. return err; /* Failure */
  506. p++;
  507. brelse(p->bh);
  508. p->bh = bh;
  509. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  510. }
  511. return 1;
  512. }
  513. /*
  514. * This function fills a red-black tree with information from a
  515. * directory block. It returns the number directory entries loaded
  516. * into the tree. If there is an error it is returned in err.
  517. */
  518. static int htree_dirblock_to_tree(struct file *dir_file,
  519. struct inode *dir, ext4_lblk_t block,
  520. struct dx_hash_info *hinfo,
  521. __u32 start_hash, __u32 start_minor_hash)
  522. {
  523. struct buffer_head *bh;
  524. struct ext4_dir_entry_2 *de, *top;
  525. int err, count = 0;
  526. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  527. (unsigned long)block));
  528. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  529. return err;
  530. de = (struct ext4_dir_entry_2 *) bh->b_data;
  531. top = (struct ext4_dir_entry_2 *) ((char *) de +
  532. dir->i_sb->s_blocksize -
  533. EXT4_DIR_REC_LEN(0));
  534. for (; de < top; de = ext4_next_entry(de)) {
  535. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  536. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  537. +((char *)de - bh->b_data))) {
  538. /* On error, skip the f_pos to the next block. */
  539. dir_file->f_pos = (dir_file->f_pos |
  540. (dir->i_sb->s_blocksize - 1)) + 1;
  541. brelse(bh);
  542. return count;
  543. }
  544. ext4fs_dirhash(de->name, de->name_len, hinfo);
  545. if ((hinfo->hash < start_hash) ||
  546. ((hinfo->hash == start_hash) &&
  547. (hinfo->minor_hash < start_minor_hash)))
  548. continue;
  549. if (de->inode == 0)
  550. continue;
  551. if ((err = ext4_htree_store_dirent(dir_file,
  552. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  553. brelse(bh);
  554. return err;
  555. }
  556. count++;
  557. }
  558. brelse(bh);
  559. return count;
  560. }
  561. /*
  562. * This function fills a red-black tree with information from a
  563. * directory. We start scanning the directory in hash order, starting
  564. * at start_hash and start_minor_hash.
  565. *
  566. * This function returns the number of entries inserted into the tree,
  567. * or a negative error code.
  568. */
  569. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  570. __u32 start_minor_hash, __u32 *next_hash)
  571. {
  572. struct dx_hash_info hinfo;
  573. struct ext4_dir_entry_2 *de;
  574. struct dx_frame frames[2], *frame;
  575. struct inode *dir;
  576. ext4_lblk_t block;
  577. int count = 0;
  578. int ret, err;
  579. __u32 hashval;
  580. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  581. start_hash, start_minor_hash));
  582. dir = dir_file->f_path.dentry->d_inode;
  583. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  584. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  585. if (hinfo.hash_version <= DX_HASH_TEA)
  586. hinfo.hash_version +=
  587. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  588. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  589. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  590. start_hash, start_minor_hash);
  591. *next_hash = ~0;
  592. return count;
  593. }
  594. hinfo.hash = start_hash;
  595. hinfo.minor_hash = 0;
  596. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  597. if (!frame)
  598. return err;
  599. /* Add '.' and '..' from the htree header */
  600. if (!start_hash && !start_minor_hash) {
  601. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  602. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  603. goto errout;
  604. count++;
  605. }
  606. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  607. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  608. de = ext4_next_entry(de);
  609. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  610. goto errout;
  611. count++;
  612. }
  613. while (1) {
  614. block = dx_get_block(frame->at);
  615. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  616. start_hash, start_minor_hash);
  617. if (ret < 0) {
  618. err = ret;
  619. goto errout;
  620. }
  621. count += ret;
  622. hashval = ~0;
  623. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  624. frame, frames, &hashval);
  625. *next_hash = hashval;
  626. if (ret < 0) {
  627. err = ret;
  628. goto errout;
  629. }
  630. /*
  631. * Stop if: (a) there are no more entries, or
  632. * (b) we have inserted at least one entry and the
  633. * next hash value is not a continuation
  634. */
  635. if ((ret == 0) ||
  636. (count && ((hashval & 1) == 0)))
  637. break;
  638. }
  639. dx_release(frames);
  640. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  641. "next hash: %x\n", count, *next_hash));
  642. return count;
  643. errout:
  644. dx_release(frames);
  645. return (err);
  646. }
  647. /*
  648. * Directory block splitting, compacting
  649. */
  650. /*
  651. * Create map of hash values, offsets, and sizes, stored at end of block.
  652. * Returns number of entries mapped.
  653. */
  654. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  655. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  656. {
  657. int count = 0;
  658. char *base = (char *) de;
  659. struct dx_hash_info h = *hinfo;
  660. while ((char *) de < base + size)
  661. {
  662. if (de->name_len && de->inode) {
  663. ext4fs_dirhash(de->name, de->name_len, &h);
  664. map_tail--;
  665. map_tail->hash = h.hash;
  666. map_tail->offs = (u16) ((char *) de - base);
  667. map_tail->size = le16_to_cpu(de->rec_len);
  668. count++;
  669. cond_resched();
  670. }
  671. /* XXX: do we need to check rec_len == 0 case? -Chris */
  672. de = ext4_next_entry(de);
  673. }
  674. return count;
  675. }
  676. /* Sort map by hash value */
  677. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  678. {
  679. struct dx_map_entry *p, *q, *top = map + count - 1;
  680. int more;
  681. /* Combsort until bubble sort doesn't suck */
  682. while (count > 2) {
  683. count = count*10/13;
  684. if (count - 9 < 2) /* 9, 10 -> 11 */
  685. count = 11;
  686. for (p = top, q = p - count; q >= map; p--, q--)
  687. if (p->hash < q->hash)
  688. swap(*p, *q);
  689. }
  690. /* Garden variety bubble sort */
  691. do {
  692. more = 0;
  693. q = top;
  694. while (q-- > map) {
  695. if (q[1].hash >= q[0].hash)
  696. continue;
  697. swap(*(q+1), *q);
  698. more = 1;
  699. }
  700. } while(more);
  701. }
  702. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  703. {
  704. struct dx_entry *entries = frame->entries;
  705. struct dx_entry *old = frame->at, *new = old + 1;
  706. int count = dx_get_count(entries);
  707. assert(count < dx_get_limit(entries));
  708. assert(old < entries + count);
  709. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  710. dx_set_hash(new, hash);
  711. dx_set_block(new, block);
  712. dx_set_count(entries, count + 1);
  713. }
  714. static void ext4_update_dx_flag(struct inode *inode)
  715. {
  716. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  717. EXT4_FEATURE_COMPAT_DIR_INDEX))
  718. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  719. }
  720. /*
  721. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  722. *
  723. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  724. * `de != NULL' is guaranteed by caller.
  725. */
  726. static inline int ext4_match (int len, const char * const name,
  727. struct ext4_dir_entry_2 * de)
  728. {
  729. if (len != de->name_len)
  730. return 0;
  731. if (!de->inode)
  732. return 0;
  733. return !memcmp(name, de->name, len);
  734. }
  735. /*
  736. * Returns 0 if not found, -1 on failure, and 1 on success
  737. */
  738. static inline int search_dirblock(struct buffer_head *bh,
  739. struct inode *dir,
  740. const struct qstr *d_name,
  741. unsigned int offset,
  742. struct ext4_dir_entry_2 ** res_dir)
  743. {
  744. struct ext4_dir_entry_2 * de;
  745. char * dlimit;
  746. int de_len;
  747. const char *name = d_name->name;
  748. int namelen = d_name->len;
  749. de = (struct ext4_dir_entry_2 *) bh->b_data;
  750. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  751. while ((char *) de < dlimit) {
  752. /* this code is executed quadratically often */
  753. /* do minimal checking `by hand' */
  754. if ((char *) de + namelen <= dlimit &&
  755. ext4_match (namelen, name, de)) {
  756. /* found a match - just to be sure, do a full check */
  757. if (!ext4_check_dir_entry("ext4_find_entry",
  758. dir, de, bh, offset))
  759. return -1;
  760. *res_dir = de;
  761. return 1;
  762. }
  763. /* prevent looping on a bad block */
  764. de_len = ext4_rec_len_from_disk(de->rec_len);
  765. if (de_len <= 0)
  766. return -1;
  767. offset += de_len;
  768. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  769. }
  770. return 0;
  771. }
  772. /*
  773. * ext4_find_entry()
  774. *
  775. * finds an entry in the specified directory with the wanted name. It
  776. * returns the cache buffer in which the entry was found, and the entry
  777. * itself (as a parameter - res_dir). It does NOT read the inode of the
  778. * entry - you'll have to do that yourself if you want to.
  779. *
  780. * The returned buffer_head has ->b_count elevated. The caller is expected
  781. * to brelse() it when appropriate.
  782. */
  783. static struct buffer_head * ext4_find_entry (struct inode *dir,
  784. const struct qstr *d_name,
  785. struct ext4_dir_entry_2 ** res_dir)
  786. {
  787. struct super_block *sb;
  788. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  789. struct buffer_head *bh, *ret = NULL;
  790. ext4_lblk_t start, block, b;
  791. int ra_max = 0; /* Number of bh's in the readahead
  792. buffer, bh_use[] */
  793. int ra_ptr = 0; /* Current index into readahead
  794. buffer */
  795. int num = 0;
  796. ext4_lblk_t nblocks;
  797. int i, err;
  798. int namelen;
  799. *res_dir = NULL;
  800. sb = dir->i_sb;
  801. namelen = d_name->len;
  802. if (namelen > EXT4_NAME_LEN)
  803. return NULL;
  804. if (is_dx(dir)) {
  805. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  806. /*
  807. * On success, or if the error was file not found,
  808. * return. Otherwise, fall back to doing a search the
  809. * old fashioned way.
  810. */
  811. if (bh || (err != ERR_BAD_DX_DIR))
  812. return bh;
  813. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  814. "falling back\n"));
  815. }
  816. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  817. start = EXT4_I(dir)->i_dir_start_lookup;
  818. if (start >= nblocks)
  819. start = 0;
  820. block = start;
  821. restart:
  822. do {
  823. /*
  824. * We deal with the read-ahead logic here.
  825. */
  826. if (ra_ptr >= ra_max) {
  827. /* Refill the readahead buffer */
  828. ra_ptr = 0;
  829. b = block;
  830. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  831. /*
  832. * Terminate if we reach the end of the
  833. * directory and must wrap, or if our
  834. * search has finished at this block.
  835. */
  836. if (b >= nblocks || (num && block == start)) {
  837. bh_use[ra_max] = NULL;
  838. break;
  839. }
  840. num++;
  841. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  842. bh_use[ra_max] = bh;
  843. if (bh)
  844. ll_rw_block(READ_META, 1, &bh);
  845. }
  846. }
  847. if ((bh = bh_use[ra_ptr++]) == NULL)
  848. goto next;
  849. wait_on_buffer(bh);
  850. if (!buffer_uptodate(bh)) {
  851. /* read error, skip block & hope for the best */
  852. ext4_error(sb, __func__, "reading directory #%lu "
  853. "offset %lu", dir->i_ino,
  854. (unsigned long)block);
  855. brelse(bh);
  856. goto next;
  857. }
  858. i = search_dirblock(bh, dir, d_name,
  859. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  860. if (i == 1) {
  861. EXT4_I(dir)->i_dir_start_lookup = block;
  862. ret = bh;
  863. goto cleanup_and_exit;
  864. } else {
  865. brelse(bh);
  866. if (i < 0)
  867. goto cleanup_and_exit;
  868. }
  869. next:
  870. if (++block >= nblocks)
  871. block = 0;
  872. } while (block != start);
  873. /*
  874. * If the directory has grown while we were searching, then
  875. * search the last part of the directory before giving up.
  876. */
  877. block = nblocks;
  878. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  879. if (block < nblocks) {
  880. start = 0;
  881. goto restart;
  882. }
  883. cleanup_and_exit:
  884. /* Clean up the read-ahead blocks */
  885. for (; ra_ptr < ra_max; ra_ptr++)
  886. brelse(bh_use[ra_ptr]);
  887. return ret;
  888. }
  889. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  890. struct ext4_dir_entry_2 **res_dir, int *err)
  891. {
  892. struct super_block * sb;
  893. struct dx_hash_info hinfo;
  894. u32 hash;
  895. struct dx_frame frames[2], *frame;
  896. struct ext4_dir_entry_2 *de, *top;
  897. struct buffer_head *bh;
  898. ext4_lblk_t block;
  899. int retval;
  900. int namelen = d_name->len;
  901. const u8 *name = d_name->name;
  902. sb = dir->i_sb;
  903. /* NFS may look up ".." - look at dx_root directory block */
  904. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  905. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  906. return NULL;
  907. } else {
  908. frame = frames;
  909. frame->bh = NULL; /* for dx_release() */
  910. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  911. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  912. }
  913. hash = hinfo.hash;
  914. do {
  915. block = dx_get_block(frame->at);
  916. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  917. goto errout;
  918. de = (struct ext4_dir_entry_2 *) bh->b_data;
  919. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  920. EXT4_DIR_REC_LEN(0));
  921. for (; de < top; de = ext4_next_entry(de)) {
  922. int off = (block << EXT4_BLOCK_SIZE_BITS(sb))
  923. + ((char *) de - bh->b_data);
  924. if (!ext4_check_dir_entry(__func__, dir, de, bh, off)) {
  925. brelse(bh);
  926. *err = ERR_BAD_DX_DIR;
  927. goto errout;
  928. }
  929. if (ext4_match(namelen, name, de)) {
  930. *res_dir = de;
  931. dx_release(frames);
  932. return bh;
  933. }
  934. }
  935. brelse(bh);
  936. /* Check to see if we should continue to search */
  937. retval = ext4_htree_next_block(dir, hash, frame,
  938. frames, NULL);
  939. if (retval < 0) {
  940. ext4_warning(sb, __func__,
  941. "error reading index page in directory #%lu",
  942. dir->i_ino);
  943. *err = retval;
  944. goto errout;
  945. }
  946. } while (retval == 1);
  947. *err = -ENOENT;
  948. errout:
  949. dxtrace(printk(KERN_DEBUG "%s not found\n", name));
  950. dx_release (frames);
  951. return NULL;
  952. }
  953. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  954. {
  955. struct inode *inode;
  956. struct ext4_dir_entry_2 *de;
  957. struct buffer_head *bh;
  958. if (dentry->d_name.len > EXT4_NAME_LEN)
  959. return ERR_PTR(-ENAMETOOLONG);
  960. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  961. inode = NULL;
  962. if (bh) {
  963. __u32 ino = le32_to_cpu(de->inode);
  964. brelse(bh);
  965. if (!ext4_valid_inum(dir->i_sb, ino)) {
  966. ext4_error(dir->i_sb, "ext4_lookup",
  967. "bad inode number: %u", ino);
  968. return ERR_PTR(-EIO);
  969. }
  970. inode = ext4_iget(dir->i_sb, ino);
  971. if (IS_ERR(inode))
  972. return ERR_CAST(inode);
  973. }
  974. return d_splice_alias(inode, dentry);
  975. }
  976. struct dentry *ext4_get_parent(struct dentry *child)
  977. {
  978. __u32 ino;
  979. struct inode *inode;
  980. static const struct qstr dotdot = {
  981. .name = "..",
  982. .len = 2,
  983. };
  984. struct ext4_dir_entry_2 * de;
  985. struct buffer_head *bh;
  986. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  987. inode = NULL;
  988. if (!bh)
  989. return ERR_PTR(-ENOENT);
  990. ino = le32_to_cpu(de->inode);
  991. brelse(bh);
  992. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  993. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  994. "bad inode number: %u", ino);
  995. return ERR_PTR(-EIO);
  996. }
  997. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  998. }
  999. #define S_SHIFT 12
  1000. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  1001. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  1002. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  1003. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  1004. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  1005. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  1006. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  1007. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  1008. };
  1009. static inline void ext4_set_de_type(struct super_block *sb,
  1010. struct ext4_dir_entry_2 *de,
  1011. umode_t mode) {
  1012. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1013. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1014. }
  1015. /*
  1016. * Move count entries from end of map between two memory locations.
  1017. * Returns pointer to last entry moved.
  1018. */
  1019. static struct ext4_dir_entry_2 *
  1020. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  1021. {
  1022. unsigned rec_len = 0;
  1023. while (count--) {
  1024. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
  1025. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1026. memcpy (to, de, rec_len);
  1027. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1028. ext4_rec_len_to_disk(rec_len);
  1029. de->inode = 0;
  1030. map++;
  1031. to += rec_len;
  1032. }
  1033. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1034. }
  1035. /*
  1036. * Compact each dir entry in the range to the minimal rec_len.
  1037. * Returns pointer to last entry in range.
  1038. */
  1039. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
  1040. {
  1041. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1042. unsigned rec_len = 0;
  1043. prev = to = de;
  1044. while ((char*)de < base + size) {
  1045. next = ext4_next_entry(de);
  1046. if (de->inode && de->name_len) {
  1047. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1048. if (de > to)
  1049. memmove(to, de, rec_len);
  1050. to->rec_len = ext4_rec_len_to_disk(rec_len);
  1051. prev = to;
  1052. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1053. }
  1054. de = next;
  1055. }
  1056. return prev;
  1057. }
  1058. /*
  1059. * Split a full leaf block to make room for a new dir entry.
  1060. * Allocate a new block, and move entries so that they are approx. equally full.
  1061. * Returns pointer to de in block into which the new entry will be inserted.
  1062. */
  1063. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1064. struct buffer_head **bh,struct dx_frame *frame,
  1065. struct dx_hash_info *hinfo, int *error)
  1066. {
  1067. unsigned blocksize = dir->i_sb->s_blocksize;
  1068. unsigned count, continued;
  1069. struct buffer_head *bh2;
  1070. ext4_lblk_t newblock;
  1071. u32 hash2;
  1072. struct dx_map_entry *map;
  1073. char *data1 = (*bh)->b_data, *data2;
  1074. unsigned split, move, size;
  1075. struct ext4_dir_entry_2 *de = NULL, *de2;
  1076. int err = 0, i;
  1077. bh2 = ext4_append (handle, dir, &newblock, &err);
  1078. if (!(bh2)) {
  1079. brelse(*bh);
  1080. *bh = NULL;
  1081. goto errout;
  1082. }
  1083. BUFFER_TRACE(*bh, "get_write_access");
  1084. err = ext4_journal_get_write_access(handle, *bh);
  1085. if (err)
  1086. goto journal_error;
  1087. BUFFER_TRACE(frame->bh, "get_write_access");
  1088. err = ext4_journal_get_write_access(handle, frame->bh);
  1089. if (err)
  1090. goto journal_error;
  1091. data2 = bh2->b_data;
  1092. /* create map in the end of data2 block */
  1093. map = (struct dx_map_entry *) (data2 + blocksize);
  1094. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1095. blocksize, hinfo, map);
  1096. map -= count;
  1097. dx_sort_map(map, count);
  1098. /* Split the existing block in the middle, size-wise */
  1099. size = 0;
  1100. move = 0;
  1101. for (i = count-1; i >= 0; i--) {
  1102. /* is more than half of this entry in 2nd half of the block? */
  1103. if (size + map[i].size/2 > blocksize/2)
  1104. break;
  1105. size += map[i].size;
  1106. move++;
  1107. }
  1108. /* map index at which we will split */
  1109. split = count - move;
  1110. hash2 = map[split].hash;
  1111. continued = hash2 == map[split - 1].hash;
  1112. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1113. (unsigned long)dx_get_block(frame->at),
  1114. hash2, split, count-split));
  1115. /* Fancy dance to stay within two buffers */
  1116. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1117. de = dx_pack_dirents(data1, blocksize);
  1118. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1119. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
  1120. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1121. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1122. /* Which block gets the new entry? */
  1123. if (hinfo->hash >= hash2)
  1124. {
  1125. swap(*bh, bh2);
  1126. de = de2;
  1127. }
  1128. dx_insert_block(frame, hash2 + continued, newblock);
  1129. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1130. if (err)
  1131. goto journal_error;
  1132. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1133. if (err)
  1134. goto journal_error;
  1135. brelse(bh2);
  1136. dxtrace(dx_show_index("frame", frame->entries));
  1137. return de;
  1138. journal_error:
  1139. brelse(*bh);
  1140. brelse(bh2);
  1141. *bh = NULL;
  1142. ext4_std_error(dir->i_sb, err);
  1143. errout:
  1144. *error = err;
  1145. return NULL;
  1146. }
  1147. /*
  1148. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1149. * it points to a directory entry which is guaranteed to be large
  1150. * enough for new directory entry. If de is NULL, then
  1151. * add_dirent_to_buf will attempt search the directory block for
  1152. * space. It will return -ENOSPC if no space is available, and -EIO
  1153. * and -EEXIST if directory entry already exists.
  1154. *
  1155. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1156. * all other cases bh is released.
  1157. */
  1158. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1159. struct inode *inode, struct ext4_dir_entry_2 *de,
  1160. struct buffer_head *bh)
  1161. {
  1162. struct inode *dir = dentry->d_parent->d_inode;
  1163. const char *name = dentry->d_name.name;
  1164. int namelen = dentry->d_name.len;
  1165. unsigned int offset = 0;
  1166. unsigned short reclen;
  1167. int nlen, rlen, err;
  1168. char *top;
  1169. reclen = EXT4_DIR_REC_LEN(namelen);
  1170. if (!de) {
  1171. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1172. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1173. while ((char *) de <= top) {
  1174. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1175. bh, offset)) {
  1176. brelse(bh);
  1177. return -EIO;
  1178. }
  1179. if (ext4_match(namelen, name, de)) {
  1180. brelse(bh);
  1181. return -EEXIST;
  1182. }
  1183. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1184. rlen = ext4_rec_len_from_disk(de->rec_len);
  1185. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1186. break;
  1187. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1188. offset += rlen;
  1189. }
  1190. if ((char *) de > top)
  1191. return -ENOSPC;
  1192. }
  1193. BUFFER_TRACE(bh, "get_write_access");
  1194. err = ext4_journal_get_write_access(handle, bh);
  1195. if (err) {
  1196. ext4_std_error(dir->i_sb, err);
  1197. brelse(bh);
  1198. return err;
  1199. }
  1200. /* By now the buffer is marked for journaling */
  1201. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1202. rlen = ext4_rec_len_from_disk(de->rec_len);
  1203. if (de->inode) {
  1204. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1205. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
  1206. de->rec_len = ext4_rec_len_to_disk(nlen);
  1207. de = de1;
  1208. }
  1209. de->file_type = EXT4_FT_UNKNOWN;
  1210. if (inode) {
  1211. de->inode = cpu_to_le32(inode->i_ino);
  1212. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1213. } else
  1214. de->inode = 0;
  1215. de->name_len = namelen;
  1216. memcpy(de->name, name, namelen);
  1217. /*
  1218. * XXX shouldn't update any times until successful
  1219. * completion of syscall, but too many callers depend
  1220. * on this.
  1221. *
  1222. * XXX similarly, too many callers depend on
  1223. * ext4_new_inode() setting the times, but error
  1224. * recovery deletes the inode, so the worst that can
  1225. * happen is that the times are slightly out of date
  1226. * and/or different from the directory change time.
  1227. */
  1228. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1229. ext4_update_dx_flag(dir);
  1230. dir->i_version++;
  1231. ext4_mark_inode_dirty(handle, dir);
  1232. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1233. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1234. if (err)
  1235. ext4_std_error(dir->i_sb, err);
  1236. brelse(bh);
  1237. return 0;
  1238. }
  1239. /*
  1240. * This converts a one block unindexed directory to a 3 block indexed
  1241. * directory, and adds the dentry to the indexed directory.
  1242. */
  1243. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1244. struct inode *inode, struct buffer_head *bh)
  1245. {
  1246. struct inode *dir = dentry->d_parent->d_inode;
  1247. const char *name = dentry->d_name.name;
  1248. int namelen = dentry->d_name.len;
  1249. struct buffer_head *bh2;
  1250. struct dx_root *root;
  1251. struct dx_frame frames[2], *frame;
  1252. struct dx_entry *entries;
  1253. struct ext4_dir_entry_2 *de, *de2;
  1254. char *data1, *top;
  1255. unsigned len;
  1256. int retval;
  1257. unsigned blocksize;
  1258. struct dx_hash_info hinfo;
  1259. ext4_lblk_t block;
  1260. struct fake_dirent *fde;
  1261. blocksize = dir->i_sb->s_blocksize;
  1262. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1263. retval = ext4_journal_get_write_access(handle, bh);
  1264. if (retval) {
  1265. ext4_std_error(dir->i_sb, retval);
  1266. brelse(bh);
  1267. return retval;
  1268. }
  1269. root = (struct dx_root *) bh->b_data;
  1270. /* The 0th block becomes the root, move the dirents out */
  1271. fde = &root->dotdot;
  1272. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1273. ext4_rec_len_from_disk(fde->rec_len));
  1274. if ((char *) de >= (((char *) root) + blocksize)) {
  1275. ext4_error(dir->i_sb, __func__,
  1276. "invalid rec_len for '..' in inode %lu",
  1277. dir->i_ino);
  1278. brelse(bh);
  1279. return -EIO;
  1280. }
  1281. len = ((char *) root) + blocksize - (char *) de;
  1282. /* Allocate new block for the 0th block's dirents */
  1283. bh2 = ext4_append(handle, dir, &block, &retval);
  1284. if (!(bh2)) {
  1285. brelse(bh);
  1286. return retval;
  1287. }
  1288. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1289. data1 = bh2->b_data;
  1290. memcpy (data1, de, len);
  1291. de = (struct ext4_dir_entry_2 *) data1;
  1292. top = data1 + len;
  1293. while ((char *)(de2 = ext4_next_entry(de)) < top)
  1294. de = de2;
  1295. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1296. /* Initialize the root; the dot dirents already exist */
  1297. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1298. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
  1299. memset (&root->info, 0, sizeof(root->info));
  1300. root->info.info_length = sizeof(root->info);
  1301. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1302. entries = root->entries;
  1303. dx_set_block(entries, 1);
  1304. dx_set_count(entries, 1);
  1305. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1306. /* Initialize as for dx_probe */
  1307. hinfo.hash_version = root->info.hash_version;
  1308. if (hinfo.hash_version <= DX_HASH_TEA)
  1309. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1310. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1311. ext4fs_dirhash(name, namelen, &hinfo);
  1312. frame = frames;
  1313. frame->entries = entries;
  1314. frame->at = entries;
  1315. frame->bh = bh;
  1316. bh = bh2;
  1317. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1318. dx_release (frames);
  1319. if (!(de))
  1320. return retval;
  1321. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1322. }
  1323. /*
  1324. * ext4_add_entry()
  1325. *
  1326. * adds a file entry to the specified directory, using the same
  1327. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1328. *
  1329. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1330. * may not sleep between calling this and putting something into
  1331. * the entry, as someone else might have used it while you slept.
  1332. */
  1333. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1334. struct inode *inode)
  1335. {
  1336. struct inode *dir = dentry->d_parent->d_inode;
  1337. struct buffer_head *bh;
  1338. struct ext4_dir_entry_2 *de;
  1339. struct super_block *sb;
  1340. int retval;
  1341. int dx_fallback=0;
  1342. unsigned blocksize;
  1343. ext4_lblk_t block, blocks;
  1344. sb = dir->i_sb;
  1345. blocksize = sb->s_blocksize;
  1346. if (!dentry->d_name.len)
  1347. return -EINVAL;
  1348. if (is_dx(dir)) {
  1349. retval = ext4_dx_add_entry(handle, dentry, inode);
  1350. if (!retval || (retval != ERR_BAD_DX_DIR))
  1351. return retval;
  1352. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1353. dx_fallback++;
  1354. ext4_mark_inode_dirty(handle, dir);
  1355. }
  1356. blocks = dir->i_size >> sb->s_blocksize_bits;
  1357. for (block = 0; block < blocks; block++) {
  1358. bh = ext4_bread(handle, dir, block, 0, &retval);
  1359. if(!bh)
  1360. return retval;
  1361. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1362. if (retval != -ENOSPC)
  1363. return retval;
  1364. if (blocks == 1 && !dx_fallback &&
  1365. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1366. return make_indexed_dir(handle, dentry, inode, bh);
  1367. brelse(bh);
  1368. }
  1369. bh = ext4_append(handle, dir, &block, &retval);
  1370. if (!bh)
  1371. return retval;
  1372. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1373. de->inode = 0;
  1374. de->rec_len = ext4_rec_len_to_disk(blocksize);
  1375. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1376. }
  1377. /*
  1378. * Returns 0 for success, or a negative error value
  1379. */
  1380. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1381. struct inode *inode)
  1382. {
  1383. struct dx_frame frames[2], *frame;
  1384. struct dx_entry *entries, *at;
  1385. struct dx_hash_info hinfo;
  1386. struct buffer_head *bh;
  1387. struct inode *dir = dentry->d_parent->d_inode;
  1388. struct super_block *sb = dir->i_sb;
  1389. struct ext4_dir_entry_2 *de;
  1390. int err;
  1391. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1392. if (!frame)
  1393. return err;
  1394. entries = frame->entries;
  1395. at = frame->at;
  1396. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1397. goto cleanup;
  1398. BUFFER_TRACE(bh, "get_write_access");
  1399. err = ext4_journal_get_write_access(handle, bh);
  1400. if (err)
  1401. goto journal_error;
  1402. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1403. if (err != -ENOSPC) {
  1404. bh = NULL;
  1405. goto cleanup;
  1406. }
  1407. /* Block full, should compress but for now just split */
  1408. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1409. dx_get_count(entries), dx_get_limit(entries)));
  1410. /* Need to split index? */
  1411. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1412. ext4_lblk_t newblock;
  1413. unsigned icount = dx_get_count(entries);
  1414. int levels = frame - frames;
  1415. struct dx_entry *entries2;
  1416. struct dx_node *node2;
  1417. struct buffer_head *bh2;
  1418. if (levels && (dx_get_count(frames->entries) ==
  1419. dx_get_limit(frames->entries))) {
  1420. ext4_warning(sb, __func__,
  1421. "Directory index full!");
  1422. err = -ENOSPC;
  1423. goto cleanup;
  1424. }
  1425. bh2 = ext4_append (handle, dir, &newblock, &err);
  1426. if (!(bh2))
  1427. goto cleanup;
  1428. node2 = (struct dx_node *)(bh2->b_data);
  1429. entries2 = node2->entries;
  1430. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
  1431. node2->fake.inode = 0;
  1432. BUFFER_TRACE(frame->bh, "get_write_access");
  1433. err = ext4_journal_get_write_access(handle, frame->bh);
  1434. if (err)
  1435. goto journal_error;
  1436. if (levels) {
  1437. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1438. unsigned hash2 = dx_get_hash(entries + icount1);
  1439. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1440. icount1, icount2));
  1441. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1442. err = ext4_journal_get_write_access(handle,
  1443. frames[0].bh);
  1444. if (err)
  1445. goto journal_error;
  1446. memcpy((char *) entries2, (char *) (entries + icount1),
  1447. icount2 * sizeof(struct dx_entry));
  1448. dx_set_count(entries, icount1);
  1449. dx_set_count(entries2, icount2);
  1450. dx_set_limit(entries2, dx_node_limit(dir));
  1451. /* Which index block gets the new entry? */
  1452. if (at - entries >= icount1) {
  1453. frame->at = at = at - entries - icount1 + entries2;
  1454. frame->entries = entries = entries2;
  1455. swap(frame->bh, bh2);
  1456. }
  1457. dx_insert_block(frames + 0, hash2, newblock);
  1458. dxtrace(dx_show_index("node", frames[1].entries));
  1459. dxtrace(dx_show_index("node",
  1460. ((struct dx_node *) bh2->b_data)->entries));
  1461. err = ext4_handle_dirty_metadata(handle, inode, bh2);
  1462. if (err)
  1463. goto journal_error;
  1464. brelse (bh2);
  1465. } else {
  1466. dxtrace(printk(KERN_DEBUG
  1467. "Creating second level index...\n"));
  1468. memcpy((char *) entries2, (char *) entries,
  1469. icount * sizeof(struct dx_entry));
  1470. dx_set_limit(entries2, dx_node_limit(dir));
  1471. /* Set up root */
  1472. dx_set_count(entries, 1);
  1473. dx_set_block(entries + 0, newblock);
  1474. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1475. /* Add new access path frame */
  1476. frame = frames + 1;
  1477. frame->at = at = at - entries + entries2;
  1478. frame->entries = entries = entries2;
  1479. frame->bh = bh2;
  1480. err = ext4_journal_get_write_access(handle,
  1481. frame->bh);
  1482. if (err)
  1483. goto journal_error;
  1484. }
  1485. ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
  1486. }
  1487. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1488. if (!de)
  1489. goto cleanup;
  1490. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1491. bh = NULL;
  1492. goto cleanup;
  1493. journal_error:
  1494. ext4_std_error(dir->i_sb, err);
  1495. cleanup:
  1496. if (bh)
  1497. brelse(bh);
  1498. dx_release(frames);
  1499. return err;
  1500. }
  1501. /*
  1502. * ext4_delete_entry deletes a directory entry by merging it with the
  1503. * previous entry
  1504. */
  1505. static int ext4_delete_entry(handle_t *handle,
  1506. struct inode *dir,
  1507. struct ext4_dir_entry_2 *de_del,
  1508. struct buffer_head *bh)
  1509. {
  1510. struct ext4_dir_entry_2 *de, *pde;
  1511. int i;
  1512. i = 0;
  1513. pde = NULL;
  1514. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1515. while (i < bh->b_size) {
  1516. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1517. return -EIO;
  1518. if (de == de_del) {
  1519. BUFFER_TRACE(bh, "get_write_access");
  1520. ext4_journal_get_write_access(handle, bh);
  1521. if (pde)
  1522. pde->rec_len = ext4_rec_len_to_disk(
  1523. ext4_rec_len_from_disk(pde->rec_len) +
  1524. ext4_rec_len_from_disk(de->rec_len));
  1525. else
  1526. de->inode = 0;
  1527. dir->i_version++;
  1528. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1529. ext4_handle_dirty_metadata(handle, dir, bh);
  1530. return 0;
  1531. }
  1532. i += ext4_rec_len_from_disk(de->rec_len);
  1533. pde = de;
  1534. de = ext4_next_entry(de);
  1535. }
  1536. return -ENOENT;
  1537. }
  1538. /*
  1539. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1540. * since this indicates that nlinks count was previously 1.
  1541. */
  1542. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1543. {
  1544. inc_nlink(inode);
  1545. if (is_dx(inode) && inode->i_nlink > 1) {
  1546. /* limit is 16-bit i_links_count */
  1547. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1548. inode->i_nlink = 1;
  1549. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1550. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1551. }
  1552. }
  1553. }
  1554. /*
  1555. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1556. * directory has >EXT4_LINK_MAX subdirs.
  1557. */
  1558. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1559. {
  1560. drop_nlink(inode);
  1561. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1562. inc_nlink(inode);
  1563. }
  1564. static int ext4_add_nondir(handle_t *handle,
  1565. struct dentry *dentry, struct inode *inode)
  1566. {
  1567. int err = ext4_add_entry(handle, dentry, inode);
  1568. if (!err) {
  1569. ext4_mark_inode_dirty(handle, inode);
  1570. d_instantiate(dentry, inode);
  1571. unlock_new_inode(inode);
  1572. return 0;
  1573. }
  1574. drop_nlink(inode);
  1575. unlock_new_inode(inode);
  1576. iput(inode);
  1577. return err;
  1578. }
  1579. /*
  1580. * By the time this is called, we already have created
  1581. * the directory cache entry for the new file, but it
  1582. * is so far negative - it has no inode.
  1583. *
  1584. * If the create succeeds, we fill in the inode information
  1585. * with d_instantiate().
  1586. */
  1587. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1588. struct nameidata *nd)
  1589. {
  1590. handle_t *handle;
  1591. struct inode *inode;
  1592. int err, retries = 0;
  1593. retry:
  1594. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1595. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1596. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1597. if (IS_ERR(handle))
  1598. return PTR_ERR(handle);
  1599. if (IS_DIRSYNC(dir))
  1600. ext4_handle_sync(handle);
  1601. inode = ext4_new_inode (handle, dir, mode);
  1602. err = PTR_ERR(inode);
  1603. if (!IS_ERR(inode)) {
  1604. inode->i_op = &ext4_file_inode_operations;
  1605. inode->i_fop = &ext4_file_operations;
  1606. ext4_set_aops(inode);
  1607. err = ext4_add_nondir(handle, dentry, inode);
  1608. }
  1609. ext4_journal_stop(handle);
  1610. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1611. goto retry;
  1612. return err;
  1613. }
  1614. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1615. int mode, dev_t rdev)
  1616. {
  1617. handle_t *handle;
  1618. struct inode *inode;
  1619. int err, retries = 0;
  1620. if (!new_valid_dev(rdev))
  1621. return -EINVAL;
  1622. retry:
  1623. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1624. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1625. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1626. if (IS_ERR(handle))
  1627. return PTR_ERR(handle);
  1628. if (IS_DIRSYNC(dir))
  1629. ext4_handle_sync(handle);
  1630. inode = ext4_new_inode(handle, dir, mode);
  1631. err = PTR_ERR(inode);
  1632. if (!IS_ERR(inode)) {
  1633. init_special_inode(inode, inode->i_mode, rdev);
  1634. #ifdef CONFIG_EXT4_FS_XATTR
  1635. inode->i_op = &ext4_special_inode_operations;
  1636. #endif
  1637. err = ext4_add_nondir(handle, dentry, inode);
  1638. }
  1639. ext4_journal_stop(handle);
  1640. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1641. goto retry;
  1642. return err;
  1643. }
  1644. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1645. {
  1646. handle_t *handle;
  1647. struct inode *inode;
  1648. struct buffer_head *dir_block;
  1649. struct ext4_dir_entry_2 *de;
  1650. int err, retries = 0;
  1651. if (EXT4_DIR_LINK_MAX(dir))
  1652. return -EMLINK;
  1653. retry:
  1654. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1655. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1656. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1657. if (IS_ERR(handle))
  1658. return PTR_ERR(handle);
  1659. if (IS_DIRSYNC(dir))
  1660. ext4_handle_sync(handle);
  1661. inode = ext4_new_inode(handle, dir, S_IFDIR | mode);
  1662. err = PTR_ERR(inode);
  1663. if (IS_ERR(inode))
  1664. goto out_stop;
  1665. inode->i_op = &ext4_dir_inode_operations;
  1666. inode->i_fop = &ext4_dir_operations;
  1667. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1668. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1669. if (!dir_block)
  1670. goto out_clear_inode;
  1671. BUFFER_TRACE(dir_block, "get_write_access");
  1672. ext4_journal_get_write_access(handle, dir_block);
  1673. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1674. de->inode = cpu_to_le32(inode->i_ino);
  1675. de->name_len = 1;
  1676. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
  1677. strcpy(de->name, ".");
  1678. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1679. de = ext4_next_entry(de);
  1680. de->inode = cpu_to_le32(dir->i_ino);
  1681. de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
  1682. EXT4_DIR_REC_LEN(1));
  1683. de->name_len = 2;
  1684. strcpy(de->name, "..");
  1685. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1686. inode->i_nlink = 2;
  1687. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1688. ext4_handle_dirty_metadata(handle, dir, dir_block);
  1689. brelse(dir_block);
  1690. ext4_mark_inode_dirty(handle, inode);
  1691. err = ext4_add_entry(handle, dentry, inode);
  1692. if (err) {
  1693. out_clear_inode:
  1694. clear_nlink(inode);
  1695. unlock_new_inode(inode);
  1696. ext4_mark_inode_dirty(handle, inode);
  1697. iput(inode);
  1698. goto out_stop;
  1699. }
  1700. ext4_inc_count(handle, dir);
  1701. ext4_update_dx_flag(dir);
  1702. ext4_mark_inode_dirty(handle, dir);
  1703. d_instantiate(dentry, inode);
  1704. unlock_new_inode(inode);
  1705. out_stop:
  1706. ext4_journal_stop(handle);
  1707. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1708. goto retry;
  1709. return err;
  1710. }
  1711. /*
  1712. * routine to check that the specified directory is empty (for rmdir)
  1713. */
  1714. static int empty_dir(struct inode *inode)
  1715. {
  1716. unsigned int offset;
  1717. struct buffer_head *bh;
  1718. struct ext4_dir_entry_2 *de, *de1;
  1719. struct super_block *sb;
  1720. int err = 0;
  1721. sb = inode->i_sb;
  1722. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1723. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1724. if (err)
  1725. ext4_error(inode->i_sb, __func__,
  1726. "error %d reading directory #%lu offset 0",
  1727. err, inode->i_ino);
  1728. else
  1729. ext4_warning(inode->i_sb, __func__,
  1730. "bad directory (dir #%lu) - no data block",
  1731. inode->i_ino);
  1732. return 1;
  1733. }
  1734. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1735. de1 = ext4_next_entry(de);
  1736. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1737. !le32_to_cpu(de1->inode) ||
  1738. strcmp(".", de->name) ||
  1739. strcmp("..", de1->name)) {
  1740. ext4_warning(inode->i_sb, "empty_dir",
  1741. "bad directory (dir #%lu) - no `.' or `..'",
  1742. inode->i_ino);
  1743. brelse(bh);
  1744. return 1;
  1745. }
  1746. offset = ext4_rec_len_from_disk(de->rec_len) +
  1747. ext4_rec_len_from_disk(de1->rec_len);
  1748. de = ext4_next_entry(de1);
  1749. while (offset < inode->i_size) {
  1750. if (!bh ||
  1751. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1752. err = 0;
  1753. brelse(bh);
  1754. bh = ext4_bread(NULL, inode,
  1755. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1756. if (!bh) {
  1757. if (err)
  1758. ext4_error(sb, __func__,
  1759. "error %d reading directory"
  1760. " #%lu offset %u",
  1761. err, inode->i_ino, offset);
  1762. offset += sb->s_blocksize;
  1763. continue;
  1764. }
  1765. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1766. }
  1767. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1768. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1769. sb->s_blocksize);
  1770. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1771. continue;
  1772. }
  1773. if (le32_to_cpu(de->inode)) {
  1774. brelse(bh);
  1775. return 0;
  1776. }
  1777. offset += ext4_rec_len_from_disk(de->rec_len);
  1778. de = ext4_next_entry(de);
  1779. }
  1780. brelse(bh);
  1781. return 1;
  1782. }
  1783. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1784. * such inodes, starting at the superblock, in case we crash before the
  1785. * file is closed/deleted, or in case the inode truncate spans multiple
  1786. * transactions and the last transaction is not recovered after a crash.
  1787. *
  1788. * At filesystem recovery time, we walk this list deleting unlinked
  1789. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1790. */
  1791. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1792. {
  1793. struct super_block *sb = inode->i_sb;
  1794. struct ext4_iloc iloc;
  1795. int err = 0, rc;
  1796. if (!ext4_handle_valid(handle))
  1797. return 0;
  1798. lock_super(sb);
  1799. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1800. goto out_unlock;
  1801. /* Orphan handling is only valid for files with data blocks
  1802. * being truncated, or files being unlinked. */
  1803. /* @@@ FIXME: Observation from aviro:
  1804. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1805. * here (on lock_super()), so race with ext4_link() which might bump
  1806. * ->i_nlink. For, say it, character device. Not a regular file,
  1807. * not a directory, not a symlink and ->i_nlink > 0.
  1808. */
  1809. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1810. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1811. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1812. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1813. if (err)
  1814. goto out_unlock;
  1815. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1816. if (err)
  1817. goto out_unlock;
  1818. /* Insert this inode at the head of the on-disk orphan list... */
  1819. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1820. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1821. err = ext4_handle_dirty_metadata(handle, inode, EXT4_SB(sb)->s_sbh);
  1822. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1823. if (!err)
  1824. err = rc;
  1825. /* Only add to the head of the in-memory list if all the
  1826. * previous operations succeeded. If the orphan_add is going to
  1827. * fail (possibly taking the journal offline), we can't risk
  1828. * leaving the inode on the orphan list: stray orphan-list
  1829. * entries can cause panics at unmount time.
  1830. *
  1831. * This is safe: on error we're going to ignore the orphan list
  1832. * anyway on the next recovery. */
  1833. if (!err)
  1834. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1835. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1836. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1837. inode->i_ino, NEXT_ORPHAN(inode));
  1838. out_unlock:
  1839. unlock_super(sb);
  1840. ext4_std_error(inode->i_sb, err);
  1841. return err;
  1842. }
  1843. /*
  1844. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1845. * of such inodes stored on disk, because it is finally being cleaned up.
  1846. */
  1847. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1848. {
  1849. struct list_head *prev;
  1850. struct ext4_inode_info *ei = EXT4_I(inode);
  1851. struct ext4_sb_info *sbi;
  1852. __u32 ino_next;
  1853. struct ext4_iloc iloc;
  1854. int err = 0;
  1855. if (!ext4_handle_valid(handle))
  1856. return 0;
  1857. lock_super(inode->i_sb);
  1858. if (list_empty(&ei->i_orphan)) {
  1859. unlock_super(inode->i_sb);
  1860. return 0;
  1861. }
  1862. ino_next = NEXT_ORPHAN(inode);
  1863. prev = ei->i_orphan.prev;
  1864. sbi = EXT4_SB(inode->i_sb);
  1865. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1866. list_del_init(&ei->i_orphan);
  1867. /* If we're on an error path, we may not have a valid
  1868. * transaction handle with which to update the orphan list on
  1869. * disk, but we still need to remove the inode from the linked
  1870. * list in memory. */
  1871. if (sbi->s_journal && !handle)
  1872. goto out;
  1873. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1874. if (err)
  1875. goto out_err;
  1876. if (prev == &sbi->s_orphan) {
  1877. jbd_debug(4, "superblock will point to %u\n", ino_next);
  1878. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1879. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1880. if (err)
  1881. goto out_brelse;
  1882. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1883. err = ext4_handle_dirty_metadata(handle, inode, sbi->s_sbh);
  1884. } else {
  1885. struct ext4_iloc iloc2;
  1886. struct inode *i_prev =
  1887. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1888. jbd_debug(4, "orphan inode %lu will point to %u\n",
  1889. i_prev->i_ino, ino_next);
  1890. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1891. if (err)
  1892. goto out_brelse;
  1893. NEXT_ORPHAN(i_prev) = ino_next;
  1894. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1895. }
  1896. if (err)
  1897. goto out_brelse;
  1898. NEXT_ORPHAN(inode) = 0;
  1899. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1900. out_err:
  1901. ext4_std_error(inode->i_sb, err);
  1902. out:
  1903. unlock_super(inode->i_sb);
  1904. return err;
  1905. out_brelse:
  1906. brelse(iloc.bh);
  1907. goto out_err;
  1908. }
  1909. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1910. {
  1911. int retval;
  1912. struct inode *inode;
  1913. struct buffer_head *bh;
  1914. struct ext4_dir_entry_2 *de;
  1915. handle_t *handle;
  1916. /* Initialize quotas before so that eventual writes go in
  1917. * separate transaction */
  1918. vfs_dq_init(dentry->d_inode);
  1919. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1920. if (IS_ERR(handle))
  1921. return PTR_ERR(handle);
  1922. retval = -ENOENT;
  1923. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1924. if (!bh)
  1925. goto end_rmdir;
  1926. if (IS_DIRSYNC(dir))
  1927. ext4_handle_sync(handle);
  1928. inode = dentry->d_inode;
  1929. retval = -EIO;
  1930. if (le32_to_cpu(de->inode) != inode->i_ino)
  1931. goto end_rmdir;
  1932. retval = -ENOTEMPTY;
  1933. if (!empty_dir(inode))
  1934. goto end_rmdir;
  1935. retval = ext4_delete_entry(handle, dir, de, bh);
  1936. if (retval)
  1937. goto end_rmdir;
  1938. if (!EXT4_DIR_LINK_EMPTY(inode))
  1939. ext4_warning(inode->i_sb, "ext4_rmdir",
  1940. "empty directory has too many links (%d)",
  1941. inode->i_nlink);
  1942. inode->i_version++;
  1943. clear_nlink(inode);
  1944. /* There's no need to set i_disksize: the fact that i_nlink is
  1945. * zero will ensure that the right thing happens during any
  1946. * recovery. */
  1947. inode->i_size = 0;
  1948. ext4_orphan_add(handle, inode);
  1949. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1950. ext4_mark_inode_dirty(handle, inode);
  1951. ext4_dec_count(handle, dir);
  1952. ext4_update_dx_flag(dir);
  1953. ext4_mark_inode_dirty(handle, dir);
  1954. end_rmdir:
  1955. ext4_journal_stop(handle);
  1956. brelse(bh);
  1957. return retval;
  1958. }
  1959. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1960. {
  1961. int retval;
  1962. struct inode *inode;
  1963. struct buffer_head *bh;
  1964. struct ext4_dir_entry_2 *de;
  1965. handle_t *handle;
  1966. /* Initialize quotas before so that eventual writes go
  1967. * in separate transaction */
  1968. vfs_dq_init(dentry->d_inode);
  1969. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1970. if (IS_ERR(handle))
  1971. return PTR_ERR(handle);
  1972. if (IS_DIRSYNC(dir))
  1973. ext4_handle_sync(handle);
  1974. retval = -ENOENT;
  1975. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1976. if (!bh)
  1977. goto end_unlink;
  1978. inode = dentry->d_inode;
  1979. retval = -EIO;
  1980. if (le32_to_cpu(de->inode) != inode->i_ino)
  1981. goto end_unlink;
  1982. if (!inode->i_nlink) {
  1983. ext4_warning(inode->i_sb, "ext4_unlink",
  1984. "Deleting nonexistent file (%lu), %d",
  1985. inode->i_ino, inode->i_nlink);
  1986. inode->i_nlink = 1;
  1987. }
  1988. retval = ext4_delete_entry(handle, dir, de, bh);
  1989. if (retval)
  1990. goto end_unlink;
  1991. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  1992. ext4_update_dx_flag(dir);
  1993. ext4_mark_inode_dirty(handle, dir);
  1994. drop_nlink(inode);
  1995. if (!inode->i_nlink)
  1996. ext4_orphan_add(handle, inode);
  1997. inode->i_ctime = ext4_current_time(inode);
  1998. ext4_mark_inode_dirty(handle, inode);
  1999. retval = 0;
  2000. end_unlink:
  2001. ext4_journal_stop(handle);
  2002. brelse(bh);
  2003. return retval;
  2004. }
  2005. static int ext4_symlink(struct inode *dir,
  2006. struct dentry *dentry, const char *symname)
  2007. {
  2008. handle_t *handle;
  2009. struct inode *inode;
  2010. int l, err, retries = 0;
  2011. l = strlen(symname)+1;
  2012. if (l > dir->i_sb->s_blocksize)
  2013. return -ENAMETOOLONG;
  2014. retry:
  2015. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2016. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  2017. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  2018. if (IS_ERR(handle))
  2019. return PTR_ERR(handle);
  2020. if (IS_DIRSYNC(dir))
  2021. ext4_handle_sync(handle);
  2022. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO);
  2023. err = PTR_ERR(inode);
  2024. if (IS_ERR(inode))
  2025. goto out_stop;
  2026. if (l > sizeof(EXT4_I(inode)->i_data)) {
  2027. inode->i_op = &ext4_symlink_inode_operations;
  2028. ext4_set_aops(inode);
  2029. /*
  2030. * page_symlink() calls into ext4_prepare/commit_write.
  2031. * We have a transaction open. All is sweetness. It also sets
  2032. * i_size in generic_commit_write().
  2033. */
  2034. err = __page_symlink(inode, symname, l, 1);
  2035. if (err) {
  2036. clear_nlink(inode);
  2037. unlock_new_inode(inode);
  2038. ext4_mark_inode_dirty(handle, inode);
  2039. iput(inode);
  2040. goto out_stop;
  2041. }
  2042. } else {
  2043. /* clear the extent format for fast symlink */
  2044. EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
  2045. inode->i_op = &ext4_fast_symlink_inode_operations;
  2046. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2047. inode->i_size = l-1;
  2048. }
  2049. EXT4_I(inode)->i_disksize = inode->i_size;
  2050. err = ext4_add_nondir(handle, dentry, inode);
  2051. out_stop:
  2052. ext4_journal_stop(handle);
  2053. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2054. goto retry;
  2055. return err;
  2056. }
  2057. static int ext4_link(struct dentry *old_dentry,
  2058. struct inode *dir, struct dentry *dentry)
  2059. {
  2060. handle_t *handle;
  2061. struct inode *inode = old_dentry->d_inode;
  2062. int err, retries = 0;
  2063. if (EXT4_DIR_LINK_MAX(inode))
  2064. return -EMLINK;
  2065. /*
  2066. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  2067. * otherwise has the potential to corrupt the orphan inode list.
  2068. */
  2069. if (inode->i_nlink == 0)
  2070. return -ENOENT;
  2071. retry:
  2072. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2073. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2074. if (IS_ERR(handle))
  2075. return PTR_ERR(handle);
  2076. if (IS_DIRSYNC(dir))
  2077. ext4_handle_sync(handle);
  2078. inode->i_ctime = ext4_current_time(inode);
  2079. ext4_inc_count(handle, inode);
  2080. atomic_inc(&inode->i_count);
  2081. err = ext4_add_entry(handle, dentry, inode);
  2082. if (!err) {
  2083. ext4_mark_inode_dirty(handle, inode);
  2084. d_instantiate(dentry, inode);
  2085. } else {
  2086. drop_nlink(inode);
  2087. iput(inode);
  2088. }
  2089. ext4_journal_stop(handle);
  2090. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2091. goto retry;
  2092. return err;
  2093. }
  2094. #define PARENT_INO(buffer) \
  2095. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
  2096. /*
  2097. * Anybody can rename anything with this: the permission checks are left to the
  2098. * higher-level routines.
  2099. */
  2100. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2101. struct inode *new_dir, struct dentry *new_dentry)
  2102. {
  2103. handle_t *handle;
  2104. struct inode *old_inode, *new_inode;
  2105. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2106. struct ext4_dir_entry_2 *old_de, *new_de;
  2107. int retval;
  2108. old_bh = new_bh = dir_bh = NULL;
  2109. /* Initialize quotas before so that eventual writes go
  2110. * in separate transaction */
  2111. if (new_dentry->d_inode)
  2112. vfs_dq_init(new_dentry->d_inode);
  2113. handle = ext4_journal_start(old_dir, 2 *
  2114. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2115. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2116. if (IS_ERR(handle))
  2117. return PTR_ERR(handle);
  2118. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2119. ext4_handle_sync(handle);
  2120. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2121. /*
  2122. * Check for inode number is _not_ due to possible IO errors.
  2123. * We might rmdir the source, keep it as pwd of some process
  2124. * and merrily kill the link to whatever was created under the
  2125. * same name. Goodbye sticky bit ;-<
  2126. */
  2127. old_inode = old_dentry->d_inode;
  2128. retval = -ENOENT;
  2129. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2130. goto end_rename;
  2131. new_inode = new_dentry->d_inode;
  2132. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2133. if (new_bh) {
  2134. if (!new_inode) {
  2135. brelse(new_bh);
  2136. new_bh = NULL;
  2137. }
  2138. }
  2139. if (S_ISDIR(old_inode->i_mode)) {
  2140. if (new_inode) {
  2141. retval = -ENOTEMPTY;
  2142. if (!empty_dir(new_inode))
  2143. goto end_rename;
  2144. }
  2145. retval = -EIO;
  2146. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2147. if (!dir_bh)
  2148. goto end_rename;
  2149. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2150. goto end_rename;
  2151. retval = -EMLINK;
  2152. if (!new_inode && new_dir != old_dir &&
  2153. new_dir->i_nlink >= EXT4_LINK_MAX)
  2154. goto end_rename;
  2155. }
  2156. if (!new_bh) {
  2157. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2158. if (retval)
  2159. goto end_rename;
  2160. } else {
  2161. BUFFER_TRACE(new_bh, "get write access");
  2162. ext4_journal_get_write_access(handle, new_bh);
  2163. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2164. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2165. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2166. new_de->file_type = old_de->file_type;
  2167. new_dir->i_version++;
  2168. new_dir->i_ctime = new_dir->i_mtime =
  2169. ext4_current_time(new_dir);
  2170. ext4_mark_inode_dirty(handle, new_dir);
  2171. BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
  2172. ext4_handle_dirty_metadata(handle, new_dir, new_bh);
  2173. brelse(new_bh);
  2174. new_bh = NULL;
  2175. }
  2176. /*
  2177. * Like most other Unix systems, set the ctime for inodes on a
  2178. * rename.
  2179. */
  2180. old_inode->i_ctime = ext4_current_time(old_inode);
  2181. ext4_mark_inode_dirty(handle, old_inode);
  2182. /*
  2183. * ok, that's it
  2184. */
  2185. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2186. old_de->name_len != old_dentry->d_name.len ||
  2187. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2188. (retval = ext4_delete_entry(handle, old_dir,
  2189. old_de, old_bh)) == -ENOENT) {
  2190. /* old_de could have moved from under us during htree split, so
  2191. * make sure that we are deleting the right entry. We might
  2192. * also be pointing to a stale entry in the unused part of
  2193. * old_bh so just checking inum and the name isn't enough. */
  2194. struct buffer_head *old_bh2;
  2195. struct ext4_dir_entry_2 *old_de2;
  2196. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2197. if (old_bh2) {
  2198. retval = ext4_delete_entry(handle, old_dir,
  2199. old_de2, old_bh2);
  2200. brelse(old_bh2);
  2201. }
  2202. }
  2203. if (retval) {
  2204. ext4_warning(old_dir->i_sb, "ext4_rename",
  2205. "Deleting old file (%lu), %d, error=%d",
  2206. old_dir->i_ino, old_dir->i_nlink, retval);
  2207. }
  2208. if (new_inode) {
  2209. ext4_dec_count(handle, new_inode);
  2210. new_inode->i_ctime = ext4_current_time(new_inode);
  2211. }
  2212. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2213. ext4_update_dx_flag(old_dir);
  2214. if (dir_bh) {
  2215. BUFFER_TRACE(dir_bh, "get_write_access");
  2216. ext4_journal_get_write_access(handle, dir_bh);
  2217. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2218. BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
  2219. ext4_handle_dirty_metadata(handle, old_dir, dir_bh);
  2220. ext4_dec_count(handle, old_dir);
  2221. if (new_inode) {
  2222. /* checked empty_dir above, can't have another parent,
  2223. * ext4_dec_count() won't work for many-linked dirs */
  2224. new_inode->i_nlink = 0;
  2225. } else {
  2226. ext4_inc_count(handle, new_dir);
  2227. ext4_update_dx_flag(new_dir);
  2228. ext4_mark_inode_dirty(handle, new_dir);
  2229. }
  2230. }
  2231. ext4_mark_inode_dirty(handle, old_dir);
  2232. if (new_inode) {
  2233. ext4_mark_inode_dirty(handle, new_inode);
  2234. if (!new_inode->i_nlink)
  2235. ext4_orphan_add(handle, new_inode);
  2236. }
  2237. retval = 0;
  2238. end_rename:
  2239. brelse(dir_bh);
  2240. brelse(old_bh);
  2241. brelse(new_bh);
  2242. ext4_journal_stop(handle);
  2243. return retval;
  2244. }
  2245. /*
  2246. * directories can handle most operations...
  2247. */
  2248. const struct inode_operations ext4_dir_inode_operations = {
  2249. .create = ext4_create,
  2250. .lookup = ext4_lookup,
  2251. .link = ext4_link,
  2252. .unlink = ext4_unlink,
  2253. .symlink = ext4_symlink,
  2254. .mkdir = ext4_mkdir,
  2255. .rmdir = ext4_rmdir,
  2256. .mknod = ext4_mknod,
  2257. .rename = ext4_rename,
  2258. .setattr = ext4_setattr,
  2259. #ifdef CONFIG_EXT4_FS_XATTR
  2260. .setxattr = generic_setxattr,
  2261. .getxattr = generic_getxattr,
  2262. .listxattr = ext4_listxattr,
  2263. .removexattr = generic_removexattr,
  2264. #endif
  2265. .permission = ext4_permission,
  2266. };
  2267. const struct inode_operations ext4_special_inode_operations = {
  2268. .setattr = ext4_setattr,
  2269. #ifdef CONFIG_EXT4_FS_XATTR
  2270. .setxattr = generic_setxattr,
  2271. .getxattr = generic_getxattr,
  2272. .listxattr = ext4_listxattr,
  2273. .removexattr = generic_removexattr,
  2274. #endif
  2275. .permission = ext4_permission,
  2276. };