namei.c 66 KB

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