namei.c 68 KB

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