namei.c 65 KB

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