namei.c 64 KB

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